Skip to content

Commit cb99414

Browse files
authored
Revert "In MODULARIZE mode avoid modifying the incoming moduleArg. NFC (#21775)" (#21994)
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 27b3050 commit cb99414

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
@@ -3587,6 +3587,31 @@ def test_file_packager_depfile(self):
35873587
self.assertTrue('./subdir \\' in after)
35883588
self.assertTrue('./subdir/data2.txt \\' in after)
35893589

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

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

0 commit comments

Comments
 (0)