emscripten-core / emscripten-core/emscripten

`embind` callback is not accesible in thread

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

Description

👋 Apologies if Im overlooking something. Im trying my best to understand emscripten but im still a rookie 🙏

Im trying to call a callback function within C code in a thread however Im not able to access to it. I find this feature useful for native code that tends to block the UI.

```cpp
void add(const emscripten::val& vec, int id, emscripten::val cb) {
std::vector data = input_vec(vec);

std::thread t([this, data, id, cb](){
// Intensive operation that blocks the main thread
cb(id);
});
t.detach();
}
```

the error

```js
nn.js:8 Uncaught ErrorEvent {isTrusted: true, message: "Uncaught TypeError: Cannot read properties of undefined (reading 'value')", filename: 'http://localhost:8082/wann.worker.js', lineno: 1, colno: 3548, …}
worker.onerror @ wann.js:8
error (async)
(anonymous) @ wann.js:8
loadWasmModuleToWorker @ wann.js:8
getNewWorker @ wann.js:8
spawnThread @ wann.js:8
___pthread_create_js @ wann.js:8
imports. @ wann.js:8
$func218 @ wann.wasm:0x25858
$dynCall_iiiii @ wann.wasm:0x99a21
ret. @ wann.js:8
(anonymous) @ wann.js:8
invoke_iiiii @ wann.js:8
imports. @ wann.js:8
$func119 @ wann.wasm:0xcc59
$dynCall_iii @ wann.wasm:0x9985c
ret. @ wann.js:8
(anonymous) @ wann.js:8
invoke_iii @ wann.js:8
imports. @ wann.js:8
$func111 @ wann.wasm:0x8b8d
$dynCall_viiii @ wann.wasm:0x99799
ret. @ wann.js:8
(anonymous) @ wann.js:8
invoke_viiii @ wann.js:8
imports. @ wann.js:8
$func112 @ wann.wasm:0x8fc8
$dynCall_viiiii @ wann.wasm:0x99d91
ret. @ wann.js:8
(anonymous) @ wann.js:8
dynCallLegacy @ wann.js:8
dynCall @ wann.js:8
(anonymous) @ wann.js:8
AnnIndex$add @ VM432:11
main @ (index):44
await in main (async)
(anonymous) @ (index):61
wann.worker.js:1 Uncaught TypeError: Cannot read properties of undefined (reading 'value')
at Object.toValue (wann.js:8:140424)
at __emval_call (wann.js:8:153028)
at imports. (wann.js:8:129792)
at wann.wasm:0x99a21
at ret. (wann.js:8:130385)
at wann.js:8:19235
at invoke_iiiii (wann.js:8:184393)
at imports. (wann.js:8:129792)
at wann.wasm:0x2004c
at wann.wasm:0x99603
t

```

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.