-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic pthreads support for Node.js Workers (#9745)
This ended up tricky because of various node/web differences, like the global scope, the event loop, APIs, etc. With help on #6567 I think this PR finally manages to make it work, at least for an initial "hello world" type test. Thanks to everyone on that issue and in particular @addaleax! This moves some code from shell.js into side .js files, so that we can use it in multiple places. Fixes #6567 but as we add more tests I'm sure we'll find more issues, this just shows basic functionality so far. We may not be able to easily reuse the existing browser tests as they have browser-specific things in them, but we can try.
- Loading branch information
Showing
11 changed files
with
313 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
read_ = function shell_read(filename, binary) { | ||
var ret; | ||
#if SUPPORT_BASE64_EMBEDDING | ||
ret = tryParseAsDataURI(filename); | ||
if (!ret) { | ||
#endif | ||
if (!nodeFS) nodeFS = require('fs'); | ||
if (!nodePath) nodePath = require('path'); | ||
filename = nodePath['normalize'](filename); | ||
ret = nodeFS['readFileSync'](filename); | ||
#if SUPPORT_BASE64_EMBEDDING | ||
} | ||
#endif | ||
return binary ? ret : ret.toString(); | ||
}; | ||
|
||
readBinary = function readBinary(filename) { | ||
var ret = read_(filename, true); | ||
if (!ret.buffer) { | ||
ret = new Uint8Array(ret); | ||
} | ||
assert(ret.buffer); | ||
return ret; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
read_ = function shell_read(url) { | ||
#if SUPPORT_BASE64_EMBEDDING | ||
try { | ||
#endif | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', url, false); | ||
xhr.send(null); | ||
return xhr.responseText; | ||
#if SUPPORT_BASE64_EMBEDDING | ||
} catch (err) { | ||
var data = tryParseAsDataURI(url); | ||
if (data) { | ||
return intArrayToString(data); | ||
} | ||
throw err; | ||
} | ||
#endif | ||
}; | ||
|
||
if (ENVIRONMENT_IS_WORKER) { | ||
readBinary = function readBinary(url) { | ||
#if SUPPORT_BASE64_EMBEDDING | ||
try { | ||
#endif | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', url, false); | ||
xhr.responseType = 'arraybuffer'; | ||
xhr.send(null); | ||
return new Uint8Array(xhr.response); | ||
#if SUPPORT_BASE64_EMBEDDING | ||
} catch (err) { | ||
var data = tryParseAsDataURI(url); | ||
if (data) { | ||
return data; | ||
} | ||
throw err; | ||
} | ||
#endif | ||
}; | ||
} | ||
|
||
readAsync = function readAsync(url, onload, onerror) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', url, true); | ||
xhr.responseType = 'arraybuffer'; | ||
xhr.onload = function xhr_onload() { | ||
if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0 | ||
onload(xhr.response); | ||
return; | ||
} | ||
#if SUPPORT_BASE64_EMBEDDING | ||
var data = tryParseAsDataURI(url); | ||
if (data) { | ||
onload(data.buffer); | ||
return; | ||
} | ||
#endif | ||
onerror(); | ||
}; | ||
xhr.onerror = onerror; | ||
xhr.send(null); | ||
}; | ||
|
Oops, something went wrong.