Skip to content

Commit a381606

Browse files
authored
Fix for STRICT_JS + pthreads (#20899)
Switch from using `eval` to `vm.runInThisContext` when emulating `importScripts` under node. Using `eval` is generally no a good idea especially since its behaviour changes according the `use strict`.
1 parent 7ef0c59 commit a381606

File tree

5 files changed

+10
-6
lines changed

5 files changed

+10
-6
lines changed

src/worker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ if (ENVIRONMENT_IS_NODE) {
2525
parentPort.on('message', (data) => onmessage({ data: data }));
2626

2727
var fs = require('fs');
28+
var vm = require('vm');
2829

2930
Object.assign(global, {
3031
self: global,
@@ -34,7 +35,7 @@ if (ENVIRONMENT_IS_NODE) {
3435
href: __filename
3536
},
3637
Worker: nodeWorkerThreads.Worker,
37-
importScripts: (f) => (0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f),
38+
importScripts: (f) => vm.runInThisContext(fs.readFileSync(f, 'utf8'), {filename: f}),
3839
postMessage: (msg) => parentPort.postMessage(msg),
3940
performance: global.performance || { now: Date.now },
4041
});

test/runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
'corez',
5959
'core_2gb',
6060
'strict',
61+
'strict_js',
6162
'wasm2js0',
6263
'wasm2js1',
6364
'wasm2js2',

test/test_core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9299,6 +9299,7 @@ def test_pthread_create(self):
92999299
# test that the node environment can be specified by itself, and that still
93009300
# works with pthreads (even though we did not specify 'node,worker')
93019301
self.set_setting('ENVIRONMENT', 'node')
9302+
self.set_setting('STRICT_JS')
93029303
self.do_run_in_out_file_test('core/pthread/create.cpp')
93039304

93049305
@node_pthreads
@@ -9977,6 +9978,7 @@ def setUp(self):
99779978

99789979
# Add DEFAULT_TO_CXX=0
99799980
strict = make_run('strict', emcc_args=[], settings={'STRICT': 1})
9981+
strict_js = make_run('strict_js', emcc_args=[], settings={'STRICT_JS': 1})
99809982

99819983
ubsan = make_run('ubsan', emcc_args=['-fsanitize=undefined', '--profiling'])
99829984
lsan = make_run('lsan', emcc_args=['-fsanitize=leak', '--profiling'], settings={'ALLOW_MEMORY_GROWTH': 1})

test/unistd/access.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ int main() {
2020
FS.mount(NODEFS, { root: '.' }, 'working');
2121
#endif
2222
FS.chdir('working');
23-
FS.writeFile('forbidden', ""); FS.chmod('forbidden', 0000);
24-
FS.writeFile('readable', ""); FS.chmod('readable', 0444);
25-
FS.writeFile('writeable', ""); FS.chmod('writeable', 0222);
26-
FS.writeFile('allaccess', ""); FS.chmod('allaccess', 0777);
23+
FS.writeFile('forbidden', ""); FS.chmod('forbidden', 0o000);
24+
FS.writeFile('readable', ""); FS.chmod('readable', 0o444);
25+
FS.writeFile('writeable', ""); FS.chmod('writeable', 0o222);
26+
FS.writeFile('allaccess', ""); FS.chmod('allaccess', 0o777);
2727
FS.writeFile('fchmodtest', "");
2828
);
2929

test/unistd/truncate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void setup() {
2525
FS.chdir('working');
2626
FS.writeFile('towrite', 'abcdef');
2727
FS.writeFile('toread', 'abcdef');
28-
FS.chmod('toread', 0444);
28+
FS.chmod('toread', 0o444);
2929
);
3030
#else
3131
FILE* f = fopen("towrite", "w");

0 commit comments

Comments
 (0)