emscripten-core / emscripten-core/emscripten

Regression "ReferenceError: global is not defined"

Open
#22,412 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

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`

```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`

```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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.