emscripten-core / emscripten-core/emscripten

Calling JS callback from C++ thread

Open
#19,292 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

C++: Here, the callback function in the struct I created is equal to the pointer address of the function that I will add as a parameter on the JS side.
```
extern "C" EMSCRIPTEN KEEPALIVE void InitializeLoggingWeb(void (*cbLogging)(void*, void*)) {
Debug::debugOpts->Logging = cbLogging;
}
```

JS: My Callback function
```
function Logging(t, b) {
if (b === 1) {
log.append(UTF8ToString(t) + "
");
}
}
let pointerLogging = Module.addFunction(Logging, "vii");
Module.ccall('InitializeLoggingWeb', 'number', ['number'], [pointerLogging]);
```

JS: Then I init my project and hope the callback function works where necessary. However, I am getting the following error.

`Module.Initialize(options);`

```
Uncaught RuntimeError: table index is out of bounds
at XX-XXX_Library.wasm:0x4672
at XX-XXX_Library.wasm:0x3ff9
at XX-XXX_Library.wasm:0x2478
at Object.invokeEntryPoint (XX-XXX_Library.js:3966:37)
at handleMessage (XX-XXX_Library.worker.js:164:35)
```

#18390 I'm having almost the same problem as the one here. However, I couldn't figure out how to solve it.
NOTE: The project works fine without using std::thread. I will be glad if you help me.

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.