-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support TextDecoder in pthreads mode #14399
Changes from all commits
f82d090
7fe8c59
8e5fc8c
7b99cae
0f76328
ca3c156
b4c9645
6e80c1e
78424fb
3fe0d97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,34 @@ | |
// Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the given array that contains uint8 values, returns | ||
// a copy of that string as a Javascript String object. | ||
|
||
#if USE_PTHREADS && TEXTDECODER | ||
// UTF8Decoder.decode may not work with a view of a SharedArrayBuffer, see | ||
// https://github.com/whatwg/encoding/issues/172 | ||
// To avoid that, we wrap around it and add a copy into a normal ArrayBuffer, | ||
// which can still be much faster than creating a string character by | ||
// character. | ||
function TextDecoderWrapper(encoding) { | ||
var textDecoder = new TextDecoder(encoding); | ||
this.decode = function(data) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that ideally we should use functions on the prototype instead of creating one per instance, but it's only possible with ES6 classes and I guess we currently expect output to be ES5-compatible? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. Also, we just create up to 2 instances of this. |
||
#if ASSERTIONS | ||
assert(data instanceof Uint8Array); | ||
#endif | ||
// While we compile with pthreads, this method can be called on side buffers | ||
// as well, such as the stdout buffer in the filesystem code. Only copy when | ||
// we have to. | ||
if (data.buffer instanceof SharedArrayBuffer) { | ||
RReverser marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, don't we know statically that this wrapper is only used on shared memory? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, you may be right... I thought there was some possibility, but I think it was some mode we removed in the past. Changed to assert on it, good idea. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, this can be called on side buffers too, like filesystem buffers. Reverted the assertion and added a comment. |
||
data = new Uint8Array(data); | ||
} | ||
return textDecoder.decode.call(textDecoder, data); | ||
}; | ||
} | ||
#endif | ||
|
||
#if TEXTDECODER == 2 | ||
var UTF8Decoder = new TextDecoder('utf8'); | ||
var UTF8Decoder = new TextDecoder{{{ USE_PTHREADS ? 'Wrapper' : ''}}}('utf8'); | ||
#else // TEXTDECODER == 2 | ||
#if TEXTDECODER | ||
var UTF8Decoder = typeof TextDecoder !== 'undefined' ? new TextDecoder('utf8') : undefined; | ||
var UTF8Decoder = typeof TextDecoder !== 'undefined' ? new TextDecoder{{{ USE_PTHREADS ? 'Wrapper' : ''}}}('utf8') : undefined; | ||
#endif // TEXTDECODER | ||
#endif // TEXTDECODER == 2 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really grok this.. but not a blocker on landing :)