From 439d89440b463bd02017344c209d62a8525e3d20 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 29 May 2024 10:21:51 -0700 Subject: [PATCH] Revert "In MODULARIZE mode avoid modifying the incoming moduleArg. NFC (#21775)" (#21994) This change effectively reverts 4dac6d6d76. 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. --- src/shell.js | 2 +- test/test_other.py | 38 +++++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/shell.js b/src/shell.js index 21e84a4ae96a9..add93992b4960 100644 --- a/src/shell.js +++ b/src/shell.js @@ -21,7 +21,7 @@ // before the code. Then that object will be used in the code, and you // can continue to use Module afterwards as well. #if MODULARIZE -var Module = Object.assign({}, moduleArg); +var Module = moduleArg; #elif USE_CLOSURE_COMPILER // if (!Module)` is crucial for Closure Compiler here as it will otherwise replace every `Module` occurrence with a string var /** @type {{ diff --git a/test/test_other.py b/test/test_other.py index 477f93c6df3f9..242ce132b3fe4 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -3587,6 +3587,31 @@ def test_file_packager_depfile(self): self.assertTrue('./subdir \\' in after) self.assertTrue('./subdir/data2.txt \\' in after) + def test_file_packager_modularize(self): + create_file('somefile.txt', 'hello world') + self.run_process([FILE_PACKAGER, 'test.data', '--js-output=embed.js', '--preload', 'somefile.txt']) + + create_file('main.c', r''' + #include + #include + int main() { + FILE *f = fopen("somefile.txt", "r"); + assert(f); + char buf[20] = { 0 }; + int rtn = fread(buf, 1, 20, f); + fclose(f); + printf("|%s|\n", buf); + return 0; + } + ''') + + create_file('post.js', 'MyModule(Module).then(() => console.log("done"));') + + self.run_process([EMCC, 'main.c', '--extern-pre-js=embed.js', '--extern-post-js=post.js', '-sMODULARIZE', '-sEXPORT_NAME=MyModule', '-sFORCE_FILESYSTEM']) + + result = self.run_js('a.out.js') + self.assertContained('|hello world|', result) + def test_sdl_headless(self): shutil.copyfile(test_file('screenshot.png'), 'example.png') self.do_other_test('test_sdl_headless.c', emcc_args=['-sHEADLESS']) @@ -6497,19 +6522,6 @@ def test_modularize_sync_compilation(self): after ''', self.run_js('a.out.js')) - def test_modularize_argument_misuse(self): - create_file('test.c', ''' - #include - EMSCRIPTEN_KEEPALIVE int foo() { return 42; }''') - - create_file('post.js', r''' - var arg = { bar: 1 }; - var promise = Module(arg); - arg._foo();''') - - expected = "Aborted(Access to module property ('_foo') is no longer possible via the module constructor argument; Instead, use the result of the module constructor" - self.do_runf('test.c', expected, assert_returncode=NON_ZERO, emcc_args=['--no-entry', '-sMODULARIZE', '--extern-post-js=post.js']) - def test_export_all_3142(self): create_file('src.cpp', r''' typedef unsigned int Bit32u;