Skip to content

Commit 3051725

Browse files
authored
Allow mutation of EXPORTED_RUNTIME_METHODS in JS libraries (#26389)
This used to work in the past but regressed at some point. Fixes: #23057
1 parent 56d5b48 commit 3051725

File tree

6 files changed

+34
-12
lines changed

6 files changed

+34
-12
lines changed

src/preamble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ function getWasmImports() {
768768
updateGOT(origExports);
769769
#endif
770770

771-
#if EXPORTED_RUNTIME_METHODS.includes('wasmExports')
771+
#if EXPORTED_RUNTIME_METHODS.has('wasmExports')
772772
Module['wasmExports'] = wasmExports;
773773
#endif
774774

src/runtime_common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ var runtimeExited = false;
126126
let shouldExport = false;
127127
if (MODULARIZE && EXPORT_ALL) {
128128
shouldExport = true;
129-
} else if (EXPORTED_RUNTIME_METHODS.includes(x)) {
129+
} else if (EXPORTED_RUNTIME_METHODS.has(x)) {
130130
shouldExport = true;
131131
}
132132
return shouldExport;

src/utility.mjs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,27 @@ export function addToCompileTimeContext(object) {
331331
Object.assign(compileTimeContext, object);
332332
}
333333

334+
const setLikeSettings = [
335+
'EXPORTED_FUNCTIONS',
336+
'WASM_EXPORTS',
337+
'SIDE_MODULE_EXPORTS',
338+
'INCOMING_MODULE_JS_API',
339+
'ALL_INCOMING_MODULE_JS_API',
340+
'EXPORTED_RUNTIME_METHODS',
341+
'WEAK_IMPORTS'
342+
];
343+
334344
export function applySettings(obj) {
345+
// Certain settings are read in as lists, but we convert them to Set
346+
// within the compiler, for efficiency.
347+
for (const key of setLikeSettings) {
348+
if (typeof obj[key] !== 'undefined') {
349+
obj[key] = new Set(obj[key]);
350+
}
351+
}
352+
335353
// Make settings available both in the current / global context
336-
// and also in the macro execution contexted.
354+
// and also in the macro execution context.
337355
Object.assign(globalThis, obj);
338356
addToCompileTimeContext(obj);
339357
}

test/jslib/test_export.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
EXPORTED_RUNTIME_METHODS.add("myFunc");
2+
3+
addToLibrary({
4+
$myFunc__postset: 'console.log("myFunc included")',
5+
$myFunc: () => {
6+
console.log("myFunc called");
7+
},
8+
})

test/test_jslib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,3 +757,7 @@ class ParentClass {}
757757
})
758758
def test_multiline_string(self, args):
759759
self.do_run_in_out_file_test('jslib/test_multiline_string.c', cflags=['--js-library', test_file('jslib/test_multiline_string.js')] + args)
760+
761+
def test_export(self):
762+
create_file('post.js', 'Module.myFunc();')
763+
self.do_runf('hello_world.c', 'myFunc included\nmyFunc called\n', cflags=['--js-library', test_file('jslib/test_export.js'), '--extern-post-js=post.js'])

tools/compiler.mjs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,9 @@ process.env['EMCC_BUILD_DIR'] = process.cwd();
6060

6161
// In case compiler.mjs is run directly (as in gen_sig_info)
6262
// ALL_INCOMING_MODULE_JS_API might not be populated yet.
63-
if (!ALL_INCOMING_MODULE_JS_API.length) {
63+
if (!ALL_INCOMING_MODULE_JS_API.size) {
6464
ALL_INCOMING_MODULE_JS_API = INCOMING_MODULE_JS_API;
6565
}
66-
67-
EXPORTED_FUNCTIONS = new Set(EXPORTED_FUNCTIONS);
68-
WASM_EXPORTS = new Set(WASM_EXPORTS);
69-
SIDE_MODULE_EXPORTS = new Set(SIDE_MODULE_EXPORTS);
70-
INCOMING_MODULE_JS_API = new Set(INCOMING_MODULE_JS_API);
71-
ALL_INCOMING_MODULE_JS_API = new Set(ALL_INCOMING_MODULE_JS_API);
72-
EXPORTED_RUNTIME_METHODS = new Set(EXPORTED_RUNTIME_METHODS);
73-
WEAK_IMPORTS = new Set(WEAK_IMPORTS);
7466
if (symbolsOnly) {
7567
INCLUDE_FULL_LIBRARY = 1;
7668
}

0 commit comments

Comments
 (0)