Skip to content

Commit 3461f0a

Browse files
committed
use process.builtinModules when available
1 parent 8839d07 commit 3461f0a

14 files changed

Lines changed: 81 additions & 38 deletions

src/lib/libatomic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ addToLibrary({
172172

173173
emscripten_num_logical_cores: () =>
174174
#if ENVIRONMENT_MAY_BE_NODE
175-
ENVIRONMENT_IS_NODE ? require('node:os').cpus().length :
175+
ENVIRONMENT_IS_NODE ? getBuiltinModule('os').cpus().length :
176176
#endif
177177
navigator['hardwareConcurrency'],
178178

src/lib/libcore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ addToLibrary({
377377
var cmdstr = UTF8ToString(command);
378378
if (!cmdstr.length) return 0; // this is what glibc seems to do (shell works test?)
379379

380-
var cp = require('node:child_process');
380+
var cp = getBuiltinModule('child_process');
381381
var ret = cp.spawnSync(cmdstr, [], {shell:true, stdio:'inherit'});
382382

383383
var _W_EXITCODE = (ret, sig) => ((ret) << 8 | (sig));

src/lib/libembind_gen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ var LibraryEmbind = {
924924
const printer = new TsPrinter(moduleDefinitions);
925925
#endif
926926
const output = printer.print();
927-
var fs = require('node:fs');
927+
var fs = getBuiltinModule('fs');
928928
fs.writeFileSync(process.argv[2], output + '\n');
929929
},
930930

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: "getBuiltinModule('path')",
1616
$PATH__deps: ['$nodePath'],
1717
$PATH: `{
1818
isAbs: nodePath.isAbsolute,

src/lib/libnoderawfs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ addToLibrary({
1010
if (!ENVIRONMENT_IS_NODE) {
1111
throw new Error("NODERAWFS is currently only supported on Node.js environment.")
1212
}
13-
var nodeTTY = require('node:tty');
13+
var nodeTTY = getBuiltinModule('tty');
1414
function _wrapNodeError(func) {
1515
return (...args) => {
1616
try {

src/lib/libsockfs.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,28 @@
55
*/
66

77
addToLibrary({
8+
#if ENVIRONMENT_MAY_BE_NODE
9+
// Memoized `require()` for the non-builtin `ws` module (getBuiltinModule only
10+
// handles builtins).
11+
$req: `(() => {
12+
var req;
13+
return (id) => (req ??= getBuiltinModule('module').createRequire(
14+
#if EXPORT_ES6
15+
import.meta.url
16+
#else
17+
__filename
18+
#endif
19+
))(id);
20+
})()`,
21+
#endif
822
$SOCKFS__postset: () => {
923
addAtInit('SOCKFS.root = FS.mount(SOCKFS, {}, null);');
1024
},
25+
#if ENVIRONMENT_MAY_BE_NODE
26+
$SOCKFS__deps: ['$FS', '$req'],
27+
#else
1128
$SOCKFS__deps: ['$FS'],
29+
#endif
1230
$SOCKFS: {
1331
#if expectToReceiveOnModule('websocket')
1432
websocketArgs: {},
@@ -216,7 +234,7 @@ addToLibrary({
216234
var WebSocketConstructor;
217235
#if ENVIRONMENT_MAY_BE_NODE
218236
if (ENVIRONMENT_IS_NODE) {
219-
WebSocketConstructor = /** @type{(typeof WebSocket)} */(require('ws'));
237+
WebSocketConstructor = /** @type{(typeof WebSocket)} */(req('ws'));
220238
} else
221239
#endif // ENVIRONMENT_MAY_BE_NODE
222240
{
@@ -518,7 +536,7 @@ addToLibrary({
518536
if (sock.server) {
519537
throw new FS.ErrnoError({{{ cDefs.EINVAL }}}); // already listening
520538
}
521-
var WebSocketServer = require('ws').Server;
539+
var WebSocketServer = req('ws').Server;
522540
var host = sock.saddr;
523541
#if SOCKET_DEBUG
524542
dbg(`websocket: listen: ${host}:${sock.sport}`);

src/lib/libwasi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ var WasiLibrary = {
570570
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
571571
// This block is not needed on v19+ since crypto.getRandomValues is builtin
572572
if (ENVIRONMENT_IS_NODE) {
573-
var nodeCrypto = require('node:crypto');
573+
var nodeCrypto = getBuiltinModule('crypto');
574574
return (view) => nodeCrypto.randomFillSync(view);
575575
}
576576
#endif // ENVIRONMENT_MAY_BE_NODE

src/lib/libwasm_worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ if (ENVIRONMENT_IS_WASM_WORKER
295295

296296
emscripten_navigator_hardware_concurrency: () => {
297297
#if ENVIRONMENT_MAY_BE_NODE
298-
if (ENVIRONMENT_IS_NODE) return require('node:os').cpus().length;
298+
if (ENVIRONMENT_IS_NODE) return getBuiltinModule('os').cpus().length;
299299
#endif
300300
return navigator['hardwareConcurrency'];
301301
},

src/preamble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ function instantiateSync(file, info) {
553553
var binary = getBinarySync(file);
554554
#if NODE_CODE_CACHING
555555
if (ENVIRONMENT_IS_NODE) {
556-
var v8 = require('node:v8');
556+
var v8 = getBuiltinModule('v8');
557557
// Include the V8 version in the cache name, so that we don't try to
558558
// load cached code from another version, which fails silently (it seems
559559
// to load ok, but we do actually recompile the binary every time).

src/runtime_debug.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function dbg(...args) {
1515
// See https://github.com/emscripten-core/emscripten/issues/14804
1616
if (ENVIRONMENT_IS_NODE) {
1717
// TODO(sbc): Unify with err/out implementation in shell.sh.
18-
var fs = require('node:fs');
19-
var utils = require('node:util');
18+
var fs = getBuiltinModule('fs');
19+
var utils = getBuiltinModule('util');
2020
function stringify(a) {
2121
switch (typeof a) {
2222
case 'object': return utils.inspect(a);

0 commit comments

Comments
 (0)