emscripten-core / emscripten-core/emscripten

MAIN_THREAD_EM_ASM compiles with BUILD_AS_WORKER, fails at runtime

Open
#15,829 0 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

Minimal example:
main.cpp
```
#include

int main() {
int w1 = emscripten_create_worker("worker.js");

emscripten_call_worker(w1, "worker_main", nullptr, 0, nullptr, nullptr);
}
```

worker.cpp
```
#include

extern "C" EMSCRIPTEN_KEEPALIVE void worker_main(char *data, int size) {
MAIN_THREAD_EM_ASM({
console.log(window.location);
});
}

```

This compiles and then fails at runtime with `Uncaught ReferenceError: window is not defined`.
Intuitively (to me) this shouldn't compile. I'd appreciate pointers on how to go about fixing this.

**Version of emscripten/emsdk:**
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.0.0 (3fd52e107187b8a169bb04a02b9f982c8a075205)
clang version 14.0.0 (https://github.com/llvm/llvm-project 4348cd42c385e71b63e5da7e492172cff6a79d7b)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Volumes/SSD/git/emsdk/upstream/bin
```

**Failing command line in full:**
It doesn't fail, that's the problem.
```
emcc -sBUILD_AS_WORKER worker.cpp -o worker.js
```

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.