Skip to content

Commit 6c822be

Browse files
author
Bartek Wrona
committed
Replace require() with library symbols in EXPORT_ES6 library files
Library functions run in synchronous context where await is unavailable. Define top-level library symbols that use await import() at module init time, then reference them via __deps from synchronous functions. - Add libnode_imports.js with shared $nodeOs symbol, register in modules.mjs when EXPORT_ES6 is enabled. - libatomic.js, libwasm_worker.js: Use $nodeOs for os.cpus().length instead of require('node:os'). - libwasi.js: Define $nodeCrypto for crypto.randomFillSync in $initRandomFill. Combine conditional __deps to avoid override. - libcore.js: Define $nodeChildProcess for _emscripten_system. - libnodepath.js: Switch $nodePath initializer to await import(). - libsockfs.js: Define $nodeWs ((await import('ws')).default) for WebSocket constructor in connect() and Server in listen().
1 parent ddadaf2 commit 6c822be

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

src/lib/libatomic.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,12 @@ addToLibrary({
159159

160160
emscripten_has_threading_support: () => !!globalThis.SharedArrayBuffer,
161161

162+
#if ENVIRONMENT_MAY_BE_NODE
163+
emscripten_num_logical_cores__deps: ['$nodeOs'],
164+
#endif
162165
emscripten_num_logical_cores: () =>
163166
#if ENVIRONMENT_MAY_BE_NODE
164-
ENVIRONMENT_IS_NODE ? require('node:os').cpus().length :
167+
ENVIRONMENT_IS_NODE ? nodeOs.cpus().length :
165168
#endif
166169
navigator['hardwareConcurrency'],
167170
});

src/lib/libcore.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,11 @@ addToLibrary({
343343
},
344344
#endif
345345

346+
#if ENVIRONMENT_MAY_BE_NODE
347+
$nodeOs: "ENVIRONMENT_IS_NODE ? {{{ makeNodeImport('node:os') }}} : undefined",
348+
$nodeChildProcess: "ENVIRONMENT_IS_NODE ? {{{ makeNodeImport('node:child_process') }}} : undefined",
349+
_emscripten_system__deps: ['$nodeChildProcess'],
350+
#endif
346351
_emscripten_system: (command) => {
347352
#if ENVIRONMENT_MAY_BE_NODE
348353
if (ENVIRONMENT_IS_NODE) {

src/lib/libnodepath.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// operations. Hence, using `nodePath` should be safe here.
1313

1414
addToLibrary({
15-
$nodePath: "require('node:path')",
15+
$nodePath: "{{{ makeNodeImport('node:path') }}}",
1616
$PATH__deps: ['$nodePath'],
1717
$PATH: `{
1818
isAbs: nodePath.isAbsolute,

src/lib/libsockfs.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@
55
*/
66

77
addToLibrary({
8+
#if ENVIRONMENT_MAY_BE_NODE
9+
#if EXPORT_ES6
10+
$nodeWs: "ENVIRONMENT_IS_NODE ? ({{{ makeNodeImport('ws') }}}).default : undefined",
11+
#else
12+
$nodeWs: "ENVIRONMENT_IS_NODE ? {{{ makeNodeImport('ws') }}} : undefined",
13+
#endif
14+
$SOCKFS__deps: ['$FS', '$nodeWs'],
15+
#else
16+
$SOCKFS__deps: ['$FS'],
17+
#endif
818
$SOCKFS__postset: () => {
919
addAtInit('SOCKFS.root = FS.mount(SOCKFS, {}, null);');
1020
},
11-
$SOCKFS__deps: ['$FS'],
1221
$SOCKFS: {
1322
#if expectToReceiveOnModule('websocket')
1423
websocketArgs: {},
@@ -216,7 +225,7 @@ addToLibrary({
216225
var WebSocketConstructor;
217226
#if ENVIRONMENT_MAY_BE_NODE
218227
if (ENVIRONMENT_IS_NODE) {
219-
WebSocketConstructor = /** @type{(typeof WebSocket)} */(require('ws'));
228+
WebSocketConstructor = /** @type{(typeof WebSocket)} */(nodeWs);
220229
} else
221230
#endif // ENVIRONMENT_MAY_BE_NODE
222231
{
@@ -522,7 +531,7 @@ addToLibrary({
522531
if (sock.server) {
523532
throw new FS.ErrnoError({{{ cDefs.EINVAL }}}); // already listening
524533
}
525-
var WebSocketServer = require('ws').Server;
534+
var WebSocketServer = nodeWs.Server;
526535
var host = sock.saddr;
527536
#if SOCKET_DEBUG
528537
dbg(`websocket: listen: ${host}:${sock.sport}`);

src/lib/libwasi.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,14 +569,21 @@ var WasiLibrary = {
569569

570570
// random.h
571571

572-
#if ENVIRONMENT_MAY_BE_SHELL
572+
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
573+
$nodeCrypto: "ENVIRONMENT_IS_NODE ? {{{ makeNodeImport('node:crypto') }}} : undefined",
574+
#endif
575+
576+
#if ENVIRONMENT_MAY_BE_SHELL && ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
577+
$initRandomFill__deps: ['$base64Decode', '$nodeCrypto'],
578+
#elif ENVIRONMENT_MAY_BE_SHELL
573579
$initRandomFill__deps: ['$base64Decode'],
580+
#elif ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
581+
$initRandomFill__deps: ['$nodeCrypto'],
574582
#endif
575583
$initRandomFill: () => {
576584
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
577585
// This block is not needed on v19+ since crypto.getRandomValues is builtin
578586
if (ENVIRONMENT_IS_NODE) {
579-
var nodeCrypto = require('node:crypto');
580587
return (view) => nodeCrypto.randomFillSync(view);
581588
}
582589
#endif // ENVIRONMENT_MAY_BE_NODE

src/lib/libwasm_worker.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,12 @@ if (ENVIRONMENT_IS_WASM_WORKER
289289
_wasmWorkers[id].postMessage({'_wsc': funcPtr, 'x': readEmAsmArgs(sigPtr, varargs) });
290290
},
291291

292+
#if ENVIRONMENT_MAY_BE_NODE
293+
emscripten_navigator_hardware_concurrency__deps: ['$nodeOs'],
294+
#endif
292295
emscripten_navigator_hardware_concurrency: () => {
293296
#if ENVIRONMENT_MAY_BE_NODE
294-
if (ENVIRONMENT_IS_NODE) return require('node:os').cpus().length;
297+
if (ENVIRONMENT_IS_NODE) return nodeOs.cpus().length;
295298
#endif
296299
return navigator['hardwareConcurrency'];
297300
},

0 commit comments

Comments
 (0)