Skip to content

Commit bbf542b

Browse files
committed
Fix the WASM Worker cannot start in node.js environment
`emscripten_malloc_wasm_worker` and `emscripten_create_wasm_worker` functions adapted to the nodejs environment and modified the core related tests.
1 parent 0566a76 commit bbf542b

File tree

8 files changed

+62
-16
lines changed

8 files changed

+62
-16
lines changed

src/library_wasm_worker.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ addToLibrary({
4747
$_wasmWorkerRunPostMessage: (e) => {
4848
// '_wsc' is short for 'wasm call', trying to use an identifier name that
4949
// will never conflict with user code
50-
let data = e.data, wasmCall = data['_wsc'];
50+
#if ENVIRONMENT_MAY_BE_NODE
51+
const data = ENVIRONMENT_IS_NODE ? e : e.data;
52+
#else
53+
const data = e.data;
54+
#endif
55+
const wasmCall = data['_wsc'];
5156
wasmCall && getWasmTableEntry(wasmCall)(...data['x']);
5257
},
5358

src/wasm_worker.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
11
// N.B. The contents of this file are duplicated in src/library_wasm_worker.js
22
// in variable "_wasmWorkerBlobUrl" (where the contents are pre-minified) If
33
// doing any changes to this file, be sure to update the contents there too.
4-
onmessage = function(d) {
4+
5+
'use strict';
6+
7+
#if ENVIRONMENT_MAY_BE_NODE
8+
// Node.js support
9+
var ENVIRONMENT_IS_NODE = typeof process == 'object' && typeof process.versions == 'object' && typeof process.versions.node == 'string';
10+
if (ENVIRONMENT_IS_NODE) {
11+
// Create as web-worker-like an environment as we can.
12+
13+
var nodeWorkerThreads = require('worker_threads');
14+
15+
var parentPort = nodeWorkerThreads.parentPort;
16+
17+
parentPort.on('message', (data) => typeof onmessage === "function" && onmessage({ data: data }));
18+
19+
var fs = require('fs');
20+
21+
Object.assign(global, {
22+
self: global,
23+
require,
24+
location: {
25+
href: __filename
26+
},
27+
Worker: nodeWorkerThreads.Worker,
28+
importScripts: (f) => (0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f),
29+
postMessage: (msg) => parentPort.postMessage(msg),
30+
performance: global.performance || { now: Date.now },
31+
addEventListener: (name, handler) => parentPort.on(name, handler),
32+
removeEventListener: (name, handler) => parentPort.off(name, handler),
33+
});
34+
}
35+
#endif // ENVIRONMENT_MAY_BE_NODE
36+
37+
self.onmessage = function(d) {
538
// The first message sent to the Worker is always the bootstrap message.
639
// Drop this message listener, it served its purpose of bootstrapping
740
// the Wasm Module load, and is no longer needed. Let user code register
841
// any desired message handlers from now on.
9-
onmessage = null;
42+
self.onmessage = null;
1043
d = d.data;
1144
#if !MODULARIZE
1245
self.{{{ EXPORT_NAME }}} = d;

test/code_size/hello_wasm_worker_wasm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var b = Module, c = b.$ww, e = b.mem || new WebAssembly.Memory({
44
shared: !0
55
}), f = e.buffer, g = [], h, k = a => {
66
a = a.data;
7-
let d = a._wsc;
7+
const d = a._wsc;
88
d && h.get(d)(...a.x);
99
}, l = a => {
1010
g.push(a);
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 737,
33
"a.html.gz": 433,
4-
"a.js": 667,
5-
"a.js.gz": 458,
4+
"a.js": 669,
5+
"a.js.gz": 460,
66
"a.wasm": 1862,
77
"a.wasm.gz": 1040,
8-
"total": 3266,
9-
"total_gz": 1931
8+
"total": 3268,
9+
"total_gz": 1933
1010
}

test/common.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,8 @@ def cleanup(line):
10641064
def run_js(self, filename, engine=None, args=None,
10651065
output_nicerizer=None,
10661066
assert_returncode=0,
1067-
interleaved_output=True):
1067+
interleaved_output=True,
1068+
timeout=None, timeout_as_error=True):
10681069
# use files, as PIPE can get too full and hang us
10691070
stdout_file = self.in_dir('stdout')
10701071
stderr_file = None
@@ -1086,9 +1087,11 @@ def run_js(self, filename, engine=None, args=None,
10861087
jsrun.run_js(filename, engine, args,
10871088
stdout=stdout,
10881089
stderr=stderr,
1089-
assert_returncode=assert_returncode)
1090+
assert_returncode=assert_returncode,
1091+
timeout=timeout)
10901092
except subprocess.TimeoutExpired as e:
1091-
timeout_error = e
1093+
if timeout_as_error:
1094+
timeout_error = e
10921095
except subprocess.CalledProcessError as e:
10931096
error = e
10941097
finally:
@@ -1506,7 +1509,8 @@ def _build_and_run(self, filename, expected_output, args=None, output_nicerizer=
15061509
check_for_error=True, force_c=False, emcc_args=None,
15071510
interleaved_output=True,
15081511
regex=False,
1509-
output_basename=None):
1512+
output_basename=None,
1513+
timeout=None, timeout_as_error=True):
15101514
logger.debug(f'_build_and_run: {filename}')
15111515

15121516
if no_build:
@@ -1533,7 +1537,9 @@ def _build_and_run(self, filename, expected_output, args=None, output_nicerizer=
15331537
js_output = self.run_js(js_file, engine, args,
15341538
output_nicerizer=output_nicerizer,
15351539
assert_returncode=assert_returncode,
1536-
interleaved_output=interleaved_output)
1540+
interleaved_output=interleaved_output,
1541+
timeout=timeout,
1542+
timeout_as_error=timeout_as_error)
15371543
js_output = js_output.replace('\r\n', '\n')
15381544
if expected_output:
15391545
if type(expected_output) not in [list, tuple]:

test/test_core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9795,15 +9795,15 @@ def test_emscripten_async_load_script(self):
97959795

97969796
@node_pthreads
97979797
def test_wasm_worker_hello(self):
9798-
self.do_runf(test_file('wasm_worker/hello_wasm_worker.c'), emcc_args=['-sWASM_WORKERS'])
9798+
self.do_run_in_out_file_test(test_file('wasm_worker/hello_wasm_worker.c'), emcc_args=['-sWASM_WORKERS'], timeout=3, timeout_as_error=False)
97999799

98009800
@node_pthreads
98019801
def test_wasm_worker_malloc(self):
9802-
self.do_runf(test_file('wasm_worker/malloc_wasm_worker.c'), emcc_args=['-sWASM_WORKERS'])
9802+
self.do_run_in_out_file_test(test_file('wasm_worker/malloc_wasm_worker.c'), emcc_args=['-sWASM_WORKERS'], timeout=3, timeout_as_error=False)
98039803

98049804
@node_pthreads
98059805
def test_wasm_worker_wait_async(self):
9806-
self.do_runf(test_file('wasm_worker/wait_async.c'), emcc_args=['-sWASM_WORKERS'])
9806+
self.do_runf(test_file('wasm_worker/wait_async.c'), emcc_args=['-sWASM_WORKERS'], timeout=3, timeout_as_error=False)
98079807

98089808

98099809
# Generate tests for everything
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello from wasm worker!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello from wasm worker!

0 commit comments

Comments
 (0)