Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: 'await' is only allowed within async functions and at the top levels of modules. #23687

Open
jrouwe opened this issue Feb 16, 2025 · 0 comments · May be fixed by #23704
Open

Error: 'await' is only allowed within async functions and at the top levels of modules. #23687

jrouwe opened this issue Feb 16, 2025 · 0 comments · May be fixed by #23704
Assignees

Comments

@jrouwe
Copy link
Contributor

jrouwe commented Feb 16, 2025

Hello,

Between version 3.1.74 and 4.0.0 the debug build of JoltPhysics.js broke with error:

SyntaxError: /tmp/emscripten_temp_562xmtnb/jolt-physics.js: 'await' is only allowed within async functions and at the top levels of modules. (1002:18)

  1000 |
  1001 | };
> 1002 | var wasmExports = await createWasm();
       |                   ^

I think it's because of this change: #23157

I've created a minimal repro here (just run ./build.sh to trigger the error):

Repro.zip

In JoltPhysics.js you would reproduce it by running ./build.sh Debug.

I can reproduce almost the same error by downloading and building Ammo.js.

It seems to be the combination of -s MODULARIZE=1 and -s LEGACY_VM_SUPPORT=1 that causes the issue.

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.3 (a9651ff57165f5710bb09a5fe52590fd6ddb72df)

Failing command line in full:

emcc glue.o -s MODULARIZE=1 -s LEGACY_VM_SUPPORT=1 -o /mnt/c/Users/jrouw/Documents/Code/JoltPhysics.js/jolt-physics.js
SyntaxError: /tmp/emscripten_temp_562xmtnb/jolt-physics.js: 'await' is only allowed within async functions and at the top levels of modules. (1002:18)

  1000 |
  1001 | };
> 1002 | var wasmExports = await createWasm();
       |                   ^
  1003 | var ___wasm_call_ctors = createExportWrapper('__wasm_call_ctors', 0);
  1004 | var _fflush = createExportWrapper('fflush', 1);
  1005 | var _emscripten_stack_init = wasmExports['emscripten_stack_init']
    at constructor (/home/jorrit/emsdk/upstream/emscripten/node_modules/@babel/parser/lib/index.js:360:19)
    at Parser.raise (/home/jorrit/emsdk/upstream/emscripten/node_modules/@babel/parser/lib/index.js:3327:19)
    at Parser.raiseOverwrite (/home/jorrit/emsdk/upstream/emscripten/node_modules/@babel/parser/lib/index.js:3343:17)
    at Parser.parseMaybeUnary (/home/jorrit/emsdk/upstream/emscripten/node_modules/@babel/parser/lib/index.js:10720:14)
    at Parser.parseMaybeUnaryOrPrivate (/home/jorrit/emsdk/upstream/emscripten/node_modules/@babel/parser/lib/index.js:10566:61)
    at Parser.parseExprOps (/home/jorrit/emsdk/upstream/emscripten/node_modules/@babel/parser/lib/index.js:10571:23)
    at Parser.parseMaybeConditional (/home/jorrit/emsdk/upstream/emscripten/node_modules/@babel/parser/lib/index.js:10548:23)
    at Parser.parseMaybeAssign (/home/jorrit/emsdk/upstream/emscripten/node_modules/@babel/parser/lib/index.js:10511:21)
    at /home/jorrit/emsdk/upstream/emscripten/node_modules/@babel/parser/lib/index.js:10481:39
    at Parser.allowInAnd (/home/jorrit/emsdk/upstream/emscripten/node_modules/@babel/parser/lib/index.js:12085:16) {
  code: 'BABEL_PARSE_ERROR',
  reasonCode: 'AwaitNotInAsyncContext',
  loc: Position { line: 1002, column: 18, index: 36412 },
  pos: 36412,
  syntaxPlugin: undefined
}
emcc: error: '/home/jorrit/emsdk/node/20.18.0_64bit/bin/node /home/jorrit/emsdk/upstream/emscripten/node_modules/.bin/babel /tmp/emscripten_temp_562xmtnb/jolt-physics.js -o /tmp/tmpvbgwc158babel.js --presets @babel/preset-env --config-file /tmp/tmpnezi8zirbabel_config.json' failed (returned 1)

Full link command and output with -v appended:

emcc -v glue.o -s MODULARIZE=1 -s LEGACY_VM_SUPPORT=1 -o /mnt/c/Users/jrouw/Documents/Code/JoltPhysics.js/jolt-physics.js
 /home/jorrit/emsdk/upstream/bin/clang --version
 /home/jorrit/emsdk/upstream/bin/wasm-ld -o /mnt/c/Users/jrouw/Documents/Code/JoltPhysics.js/jolt-physics.wasm glue.o -L/home/jorrit/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten -L/home/jorrit/emsdk/upstream/emscripten/src/lib -lGL-getprocaddr -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc-debug -lcompiler_rt -lc++-noexcept -lc++abi-debug-noexcept -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr /tmp/tmp6f5_zltilibemscripten_js_symbols.so --strip-debug --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_init --export=_emscripten_stack_alloc --export=__get_temp_ret --export=__set_temp_ret --export=__wasm_call_ctors --export=_emscripten_stack_restore --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=main --export-if-defined=__main_argc_argv --export-if-defined=fflush --export-table -z stack-size=65536 --no-growable-memory --initial-heap=16777216 --no-entry --stack-first --table-base=1
 /home/jorrit/emsdk/upstream/bin/llvm-objcopy /mnt/c/Users/jrouw/Documents/Code/JoltPhysics.js/jolt-physics.wasm /mnt/c/Users/jrouw/Documents/Code/JoltPhysics.js/jolt-physics.wasm --remove-section=.debug* --remove-section=producers --remove-section=name
 /home/jorrit/emsdk/upstream/bin/wasm-emscripten-finalize --dyncalls-i64 --pass-arg=legalize-js-interface-exported-helpers /mnt/c/Users/jrouw/Documents/Code/JoltPhysics.js/jolt-physics.wasm -o /mnt/c/Users/jrouw/Documents/Code/JoltPhysics.js/jolt-physics.wasm --detect-features
 /home/jorrit/emsdk/upstream/bin/wasm-emscripten-finalize --dyncalls-i64 --pass-arg=legalize-js-interface-exported-helpers /mnt/c/Users/jrouw/Documents/Code/JoltPhysics.js/jolt-physics.wasm -o /mnt/c/Users/jrouw/Documents/Code/JoltPhysics.js/jolt-physics.wasm --detect-features
 /home/jorrit/emsdk/node/20.18.0_64bit/bin/node /home/jorrit/emsdk/upstream/emscripten/src/compiler.mjs /tmp/tmpi00fn030.json
 /home/jorrit/emsdk/upstream/bin/wasm-opt --signext-lowering --llvm-nontrapping-fptoint-lowering --llvm-memory-copy-fill-lowering /mnt/c/Users/jrouw/Documents/Code/JoltPhysics.js/jolt-physics.wasm -o /mnt/c/Users/jrouw/Documents/Code/JoltPhysics.js/jolt-physics.wasm --mvp-features --enable-bulk-memory --enable-bulk-memory-opt --enable-call-indirect-overlong --enable-multivalue --enable-mutable-globals --enable-nontrapping-float-to-int --enable-reference-types --enable-sign-ext
 /home/jorrit/emsdk/upstream/bin/wasm-opt --signext-lowering --llvm-nontrapping-fptoint-lowering --llvm-memory-copy-fill-lowering /mnt/c/Users/jrouw/Documents/Code/JoltPhysics.js/jolt-physics.wasm -o /mnt/c/Users/jrouw/Documents/Code/JoltPhysics.js/jolt-physics.wasm --mvp-features --enable-bulk-memory --enable-bulk-memory-opt --enable-call-indirect-overlong --enable-multivalue --enable-mutable-globals --enable-nontrapping-float-to-int --enable-reference-types --enable-sign-ext
 /home/jorrit/emsdk/node/20.18.0_64bit/bin/node /home/jorrit/emsdk/upstream/emscripten/node_modules/.bin/babel /tmp/emscripten_temp_x5ddoj62/jolt-physics.js -o /tmp/tmp43mtqh9xbabel.js --presets @babel/preset-env --config-file /tmp/tmpcu9h_z5rbabel_config.json
SyntaxError: /tmp/emscripten_temp_x5ddoj62/jolt-physics.js: 'await' is only allowed within async functions and at the top levels of modules. (1002:18)
sbc100 added a commit to sbc100/emscripten that referenced this issue Feb 18, 2025
When we run babel in `-sMODULARIZE` mode the input to babel contains
`await` calls.  This code appears to be top-level code at the point
at which we run babel since we have yet to perform the modularization
process which wraps the output in a factory function.

Fixes: emscripten-core#23687
sbc100 added a commit to sbc100/emscripten that referenced this issue Feb 19, 2025
When we run babel in `-sMODULARIZE` mode the input to babel contains
`await` calls.  This code appears to be top-level code at the point
at which we run babel since we have yet to perform the modularization
process which wraps the output in a factory function.

Fixes: emscripten-core#23687
sbc100 added a commit to sbc100/emscripten that referenced this issue Feb 19, 2025
I really which we had a better solution here, but this makes the use
of async/await independent of closure usage.

This is a workaround for emscripten-core#23158, but it also happens to fix emscripten-core#23687
which stems from the same issue.

Fixes: emscripten-core#23687
sbc100 added a commit to sbc100/emscripten that referenced this issue Feb 19, 2025
I really which we had a better solution here, but this makes the use
of async/await independent of closure usage.

This is a workaround for emscripten-core#23158, but it also happens to fix emscripten-core#23687
which stems from the same issue.

Fixes: emscripten-core#23687
sbc100 added a commit to sbc100/emscripten that referenced this issue Feb 19, 2025
I really which we had a better solution here, but this makes the use
of async/await independent of closure usage.

This is a workaround for emscripten-core#23158, but it also happens to fix emscripten-core#23687
which stems from the same issue.

Fixes: emscripten-core#23687
sbc100 added a commit to sbc100/emscripten that referenced this issue Feb 19, 2025
I really which we had a better solution here, but this makes the use
of async/await independent of closure usage.

This is a workaround for emscripten-core#23158, but it also happens to fix emscripten-core#23687
which stems from the same issue.

Fixes: emscripten-core#23687
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants