emscripten-core / emscripten-core/emscripten

Can't expand macros inside EM_ASM and EM_JS

Open
#23,690 6 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

**Version of emscripten/emsdk:**

```
$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.3-git (a9651ff57165f5710b
b09a5fe52590fd6ddb72df)
clang version 21.0.0git
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: C:/msys64/clang64/opt/emscripten-llvm/bin
```

**Failing command line in full:**

```
$ cat asm-preprocessor.c
#include

int main(void)
{
#define VAL 'hi'
EM_ASM(console.log(VAL));
return 0;
}
$ emcc asm-preprocessor.c && node a.out.js
D:\Random\C\a.out.js:844
65536: () => { console.log(VAL) }
^

ReferenceError: VAL is not defined
at 65536 (D:\Random\C\a.out.js:844:30)
at runEmAsmFunction (D:\Random\C\a.out.js:970:30)
at _emscripten_asm_const_int (D:\Random\C\a.out.js:973:14)
at wasm://wasm/afa51dde:wasm-function[2]:0x232
at wasm://wasm/afa51dde:wasm-function[3]:0x258
at D:\Random\C\a.out.js:688:12
at callMain (D:\Random\C\a.out.js:1352:15)
at doRun (D:\Random\C\a.out.js:1404:24)
at run (D:\Random\C\a.out.js:1417:5)
at removeRunDependency (D:\Random\C\a.out.js:624:7)

Node.js v23.5.0
```

this is the workaround:

```
$ cat asm-preprocessor-good.c
#include

int main(void)
{
#define VAL 'hi'
#define EM_ASM2(...) EM_ASM(__VA_ARGS__)
EM_ASM2(console.log(VAL));
return 0;
}
$ emcc asm-preprocessor-good.c && node a.out.js
hi
```

I believe users would be surprised if their JavaScript code suddenly started expanding macros after a "fix". hence, I believe it is more useful if every macro of EM_ASM family and EM_JS gets a second version that performs macro expansion before stringification. otherwise, at least document the current behavior in documentation, providing rationale and the workaround.

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.