Skip to content

Commit acb0442

Browse files
authored
Emit EM_JS/EM_ASM code after JS library code (#23878)
This change fixes an issue with trailing code in EM_JS macros. See #23875 and #23884 #23875 The other advantage of this ordering is that it removes the needs for the magic macro here.
1 parent 1d35dfb commit acb0442

10 files changed

+33
-14
lines changed

src/jsifier.mjs

+2
Original file line numberDiff line numberDiff line change
@@ -767,9 +767,11 @@ function(${args}) {
767767
const preFile = MINIMAL_RUNTIME ? 'preamble_minimal.js' : 'preamble.js';
768768
includeSystemFile(preFile);
769769

770+
writeOutput('// Begin JS library code\n');
770771
for (const item of libraryItems.concat(postSets)) {
771772
writeOutput(indentify(item || '', 2));
772773
}
774+
writeOutput('// End JS library code\n');
773775

774776
if (PTHREADS) {
775777
writeOutput(`

src/preamble.js

-2
Original file line numberDiff line numberDiff line change
@@ -1084,5 +1084,3 @@ function getCompilerSetting(name) {
10841084
// dynamic linker as symbols are loaded.
10851085
var asyncifyStubs = {};
10861086
#endif
1087-
1088-
<<< EM_JS_CODE >>>

src/preamble_minimal.js

-2
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,3 @@ var runtimeExited = false;
5454
#if IMPORTED_MEMORY
5555
#include "runtime_init_memory.js"
5656
#endif // IMPORTED_MEMORY
57-
58-
<<< EM_JS_CODE >>>
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4137
1+
4133

test/code_size/embind_hello_wasm.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 552,
33
"a.html.gz": 380,
4-
"a.js": 9746,
4+
"a.js": 9750,
55
"a.js.gz": 4262,
66
"a.wasm": 7348,
77
"a.wasm.gz": 3375,
8-
"total": 17646,
8+
"total": 17650,
99
"total_gz": 8017
1010
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
52624
1+
52673
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
27506
1+
27555
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
50912
1+
50961

test/test_other.py

+23
Original file line numberDiff line numberDiff line change
@@ -13505,6 +13505,29 @@ def test_em_js_external_usage(self):
1350513505
self.run_process([EMCC, 'em_js.c', '-c'])
1350613506
self.do_runf('main.c', 'js_func called\n', emcc_args=['em_js.o'])
1350713507

13508+
def test_em_js_top_level(self):
13509+
# It turns out that EM_JS can be used to inject top level JS code.
13510+
# This test verifies that it works, despite it not being an officially
13511+
# supported feature.
13512+
# See https://github.com/emscripten-core/emscripten/issues/23884
13513+
create_file('main.c', r'''
13514+
#include <emscripten/em_js.h>
13515+
13516+
EM_JS_DEPS(deps, "$addOnPreRun");
13517+
13518+
EM_JS(void, js_func, (), {
13519+
out('js_func called');
13520+
}
13521+
console.log("Top level code");
13522+
addOnPreRun(() => console.log("hello from pre-run"));
13523+
);
13524+
13525+
int main() {
13526+
js_func();
13527+
}
13528+
''')
13529+
self.do_runf('main.c', 'Top level code\nhello from pre-run\njs_func called\n')
13530+
1350813531
# On Windows maximum command line length is 32767 characters. Create such a long build line by linking together
1350913532
# several .o files to test that emcc internally uses response files properly when calling llvm-nm and wasm-ld.
1351013533
@is_slow_test

tools/emscripten.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,10 @@ def emscript(in_wasm, out_wasm, outfile_js, js_syms, finalize=True, base_metadat
414414
pre = apply_static_code_hooks(forwarded_json, pre)
415415

416416
asm_const_pairs = ['%s: %s' % (key, value) for key, value in asm_consts]
417-
em_js_code = ''
418417
if asm_const_pairs or settings.MAIN_MODULE:
419-
em_js_code += 'var ASM_CONSTS = {\n ' + ', \n '.join(asm_const_pairs) + '\n};\n'
418+
pre += 'var ASM_CONSTS = {\n ' + ', \n '.join(asm_const_pairs) + '\n};\n'
420419
if em_js_funcs:
421-
em_js_code += '\n'.join(em_js_funcs) + '\n'
422-
pre = shared.do_replace(pre, '<<< EM_JS_CODE >>>', em_js_code)
420+
pre += '\n'.join(em_js_funcs) + '\n'
423421

424422
if base_metadata:
425423
function_exports = base_metadata.function_exports

0 commit comments

Comments
 (0)