emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.76k stars 3.3k forks source link

Regression "ReferenceError: global is not defined" #22412

Open juj opened 2 months ago

juj commented 2 months ago

User reported a regression in my wasm_webgpu bindings when using newer Emscripten version: https://github.com/juj/wasm_webgpu/issues/44

There I had the following code pattern:

lib_a.js

#if global.MY_PREPROCESSING_DIRECTIVE
#error "MY_PREPROCESSING_DIRECTIVE seen ok"
#else
#error "MY_PREPROCESSING_DIRECTIVE not seen"
#endif
emcc test/hello_world.c --js-library lib_a.js

(to be used with emcc test/hello_world.c --js-library lib_a.js -jsDMY_PREPROCESSING_DIRECTIVE occassionally)

which after PR https://github.com/emscripten-core/emscripten/pull/21542 landed began to error out with:

C:\emsdk\emscripten\main>emcc test\hello_world.c -o a.js --js-library lib_a.js
cache:INFO: generating system asset: symbol_lists/8caa41f7e27a07b1dc6e8a19db04181ecc6e43bb.json... (this will be cached in "C:\emsdk\emscripten\main\cache\symbol_lists\8caa41f7e27a07b1dc6e8a19db04181ecc6e43bb.json" for subsequent builds)
error: C:\emsdk\emscripten\main\lib_a.js: failure to execute js library "C:\emsdk\emscripten\main\lib_a.js":
error: C:\emsdk\emscripten\main\lib_a.js: use -sVERBOSE to see more details
Internal compiler error in src/compiler.js!
Please create a bug report at https://github.com/emscripten-core/emscripten/issues/
with a log of the build and the input files used to run. Exception message: "C:\emsdk\emscripten\main\lib_a.js:1
 global.MY_PREPROCESSING_DIRECTIVE
 ^

ReferenceError: global is not defined
    at C:\emsdk\emscripten\main\lib_a.js:1:5
    at Script.runInContext (node:vm:133:12)
    at Script.runInNewContext (node:vm:138:17)
    at Module.runInNewContext (node:vm:290:38)
    at runInMacroContext (file:///C:/emsdk/emscripten/main/src/utility.mjs:333:13)
    at preprocess (file:///C:/emsdk/emscripten/main/src/parseTools.mjs:115:28)
    at Object.load (file:///C:/emsdk/emscripten/main/src/modules.mjs:248:35)
    at Module.runJSify (file:///C:/emsdk/emscripten/main/src/jsifier.mjs:169:18)
    at file:///C:/emsdk/emscripten/main/src/compiler.mjs:89:11
emcc: error: 'C:/emsdk/node/18.20.3_64bit/bin/node.exe C:\emsdk\emscripten\main\src\compiler.mjs C:\Users\clb\AppData\Local\Temp\tmp14rs5xx8.json --symbols-only' failed (returned 1)

It looks like I can fix this by changing the code to use globalThis instead of global:

lib_a.js

#if globalThis.MY_PREPROCESSING_DIRECTIVE
#error "MY_PREPROCESSING_DIRECTIVE seen ok"
#else
#error "MY_PREPROCESSING_DIRECTIVE not seen"
#endif

and that fixes the impact of the regression in my codebase. However, that makes me think there is a relevant test case of functionality missing since this wasn't caught?

sbc100 commented 2 months ago

We can/should probably fix this. I believe its likely fallout from #21542

(edit: I see you already bisected to that PR)

sbc100 commented 2 months ago

Would it be nicer perhaps if you could just do #if MY_PREPROCESSING_DIRECTIVE. I can't remember if there was a reason why -jsD didn't just work like that?

sbc100 commented 2 months ago

I need it looks like we only test for #if MY_PREPROCESSING_DIRECTIVE: https://github.com/emscripten-core/emscripten/blob/a94a40eb3116aebfdd92227ddff16260bd6aebe1/test/core/test_custom_js_settings.js#L3-L7