Skip to content

Commit 0d0e9c9

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 bca78f5 commit 0d0e9c9

File tree

2 files changed

+19
-38
lines changed

2 files changed

+19
-38
lines changed

src/library_wasi.js

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,7 @@ var WasiLibrary = {
229229
}
230230
return ret;
231231
},
232-
#endif
233-
#if !SYSCALLS_REQUIRE_FILESYSTEM || WASM_WORKERS
232+
#else
234233
// MEMFS filesystem disabled lite handling of stdout and stderr:
235234
$printCharBuffers: [null, [], []], // 1 => stdout, 2 => stderr
236235
$printCharBuffers__internal: true,
@@ -248,7 +247,11 @@ var WasiLibrary = {
248247
buffer.push(curr);
249248
}
250249
},
251-
#if !MINIMAL_RUNTIME || EXIT_RUNTIME
250+
#endif // SYSCALLS_REQUIRE_FILESYSTEM
251+
252+
#if SYSCALLS_REQUIRE_FILESYSTEM
253+
fd_write__deps: ['$doWritev'],
254+
#elif (!MINIMAL_RUNTIME || EXIT_RUNTIME)
252255
$flush_NO_FILESYSTEM__deps: ['$printChar', '$printCharBuffers'],
253256
$flush_NO_FILESYSTEM: () => {
254257
// flush anything remaining in the buffers during shutdown
@@ -258,13 +261,17 @@ var WasiLibrary = {
258261
if (printCharBuffers[1].length) printChar(1, {{{ charCode("\n") }}});
259262
if (printCharBuffers[2].length) printChar(2, {{{ charCode("\n") }}});
260263
},
261-
$fd_write_nofs__postset: () => addAtExit('flush_NO_FILESYSTEM()'),
262-
$fd_write_nofs__deps: ['$printChar', '$flush_NO_FILESYSTEM'],
264+
fd_write__deps: ['$flush_NO_FILESYSTEM', '$printChar'],
265+
fd_write__postset: () => addAtExit('flush_NO_FILESYSTEM()'),
263266
#else
264-
$fd_write_nofs__deps: ['$printChar'],
267+
fd_write__deps: ['$printChar'],
265268
#endif
266-
$fd_write_nofs__sig: 'iippp',
267-
$fd_write_nofs: (fd, iov, iovcnt, pnum) => {
269+
fd_write: (fd, iov, iovcnt, pnum) => {
270+
#if SYSCALLS_REQUIRE_FILESYSTEM
271+
var stream = SYSCALLS.getStreamFromFD(fd);
272+
var num = doWritev(stream, iov, iovcnt);
273+
#else
274+
// hack to support printf in SYSCALLS_REQUIRE_FILESYSTEM=0
268275
var num = 0;
269276
for (var i = 0; i < iovcnt; i++) {
270277
var ptr = {{{ makeGetValue('iov', C_STRUCTS.iovec.iov_base, '*') }}};
@@ -275,36 +282,10 @@ var WasiLibrary = {
275282
}
276283
num += len;
277284
}
285+
#endif // SYSCALLS_REQUIRE_FILESYSTEM
278286
{{{ makeSetValue('pnum', 0, 'num', SIZE_TYPE) }}};
279287
return 0;
280288
},
281-
#endif
282-
283-
#if SYSCALLS_REQUIRE_FILESYSTEM
284-
fd_write__deps: [
285-
'$doWritev',
286-
#if WASM_WORKERS
287-
'$fd_write_nofs',
288-
#endif
289-
],
290-
fd_write: (fd, iov, iovcnt, pnum) => {
291-
#if WASM_WORKERS
292-
if (ENVIRONMENT_IS_WASM_WORKER) {
293-
return fd_write_nofs(fd, iov, iovcnt, pnum);
294-
}
295-
#endif // WASM_WORKERS
296-
var stream = SYSCALLS.getStreamFromFD(fd);
297-
var num = doWritev(stream, iov, iovcnt);
298-
{{{ makeSetValue('pnum', 0, 'num', SIZE_TYPE) }}};
299-
return 0;
300-
},
301-
#else
302-
fd_write__deps: ['$fd_write_nofs'],
303-
fd_write: (fd, iov, iovcnt, pnum) => {
304-
// May be wrapped by wrapSyscallFunction
305-
return fd_write_nofs(fd, iov, iovcnt, pnum);
306-
},
307-
#endif
308289

309290
#if SYSCALLS_REQUIRE_FILESYSTEM
310291
fd_pwrite__deps: ['$doWritev'],

test/wasm_worker/hello_wasm_worker.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
#include <assert.h>
1+
#include <emscripten.h>
22
#include <emscripten/wasm_worker.h>
3-
#include <stdio.h>
3+
#include <assert.h>
44

55
// This is the code example in site/source/docs/api_reference/wasm_workers.rst
66

77
void run_in_worker()
88
{
9-
printf("Hello from wasm worker!\n");
9+
emscripten_console_log("Hello from wasm worker!\n");
1010
#ifdef REPORT_RESULT
1111
REPORT_RESULT(0);
1212
#endif

0 commit comments

Comments
 (0)