Skip to content

Commit

Permalink
Revert "In MODULARIZE mode avoid modifying the incoming moduleArg. NFC (
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sbc100 committed May 23, 2024
1 parent 995df59 commit 604fed6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {{
Expand Down
38 changes: 25 additions & 13 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -3585,6 +3585,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 <assert.h>
#include <stdio.h>
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'])
Expand Down Expand Up @@ -6495,19 +6520,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.h>
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;
Expand Down

0 comments on commit 604fed6

Please sign in to comment.