Skip to content

Commit 604fed6

Browse files
committed
Revert "In MODULARIZE mode avoid modifying the incoming moduleArg. NFC (emscripten-core#21775)"
This change effectively reverts 4dac6d6. It turns out that there is at least one use case where accessing the partially constructed module prior to the promise resolution is necessary. Specifically when using `--preload-files` the file packager output expects to be able to use the `Module.FS` functions before the module is loaded. This PR adds a test for this scenario.
1 parent 995df59 commit 604fed6

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

src/shell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// before the code. Then that object will be used in the code, and you
2222
// can continue to use Module afterwards as well.
2323
#if MODULARIZE
24-
var Module = Object.assign({}, moduleArg);
24+
var Module = moduleArg;
2525
#elif USE_CLOSURE_COMPILER
2626
// if (!Module)` is crucial for Closure Compiler here as it will otherwise replace every `Module` occurrence with a string
2727
var /** @type {{

test/test_other.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3585,6 +3585,31 @@ def test_file_packager_depfile(self):
35853585
self.assertTrue('./subdir \\' in after)
35863586
self.assertTrue('./subdir/data2.txt \\' in after)
35873587

3588+
def test_file_packager_modularize(self):
3589+
create_file('somefile.txt', 'hello world')
3590+
self.run_process([FILE_PACKAGER, 'test.data', '--js-output=embed.js', '--preload', 'somefile.txt'])
3591+
3592+
create_file('main.c', r'''
3593+
#include <assert.h>
3594+
#include <stdio.h>
3595+
int main() {
3596+
FILE *f = fopen("somefile.txt", "r");
3597+
assert(f);
3598+
char buf[20] = { 0 };
3599+
int rtn = fread(buf, 1, 20, f);
3600+
fclose(f);
3601+
printf("|%s|\n", buf);
3602+
return 0;
3603+
}
3604+
''')
3605+
3606+
create_file('post.js', 'MyModule(Module).then(() => console.log("done"));')
3607+
3608+
self.run_process([EMCC, 'main.c', '--extern-pre-js=embed.js', '--extern-post-js=post.js', '-sMODULARIZE', '-sEXPORT_NAME=MyModule', '-sFORCE_FILESYSTEM'])
3609+
3610+
result = self.run_js('a.out.js')
3611+
self.assertContained('|hello world|', result)
3612+
35883613
def test_sdl_headless(self):
35893614
shutil.copyfile(test_file('screenshot.png'), 'example.png')
35903615
self.do_other_test('test_sdl_headless.c', emcc_args=['-sHEADLESS'])
@@ -6495,19 +6520,6 @@ def test_modularize_sync_compilation(self):
64956520
after
64966521
''', self.run_js('a.out.js'))
64976522

6498-
def test_modularize_argument_misuse(self):
6499-
create_file('test.c', '''
6500-
#include <emscripten.h>
6501-
EMSCRIPTEN_KEEPALIVE int foo() { return 42; }''')
6502-
6503-
create_file('post.js', r'''
6504-
var arg = { bar: 1 };
6505-
var promise = Module(arg);
6506-
arg._foo();''')
6507-
6508-
expected = "Aborted(Access to module property ('_foo') is no longer possible via the module constructor argument; Instead, use the result of the module constructor"
6509-
self.do_runf('test.c', expected, assert_returncode=NON_ZERO, emcc_args=['--no-entry', '-sMODULARIZE', '--extern-post-js=post.js'])
6510-
65116523
def test_export_all_3142(self):
65126524
create_file('src.cpp', r'''
65136525
typedef unsigned int Bit32u;

0 commit comments

Comments
 (0)