From a751c39f092be5ef5340e84f7bf9e7729fcc4e82 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Fri, 1 Nov 2024 11:37:45 -0700 Subject: [PATCH] Reapply "In MODULARIZE mode avoid modifying the incoming moduleArg. NFC (#21775)" This change was reverted in #21994 because the file_packager generated code expected to be able to use propertied on the Module object that was passed to the constructor/factor function. However that issue was fixed in #22086 so we can try to land this once again. --- src/shell.js | 2 +- test/test_other.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/shell.js b/src/shell.js index 2d0c32426d78e..36f5c27c0c6f1 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 = moduleArg; +var Module = Object.assign({}, 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 c5a19fb2b2ad3..4196e58719b80 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -6770,6 +6770,19 @@ 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;