Calling derived JavaScript object inside another thread causes binding error
I want to achieve following goals
- Define class
Band abstract classAin C++, while JavaScript will provide an implementation forA - Call method of
Ain C++ - (2) should happen in another thread.
I'm leveraging embind, it works well for (1) and (2), however hits error when integrating multi thread.
Uncaught (in promise) abort() at Error at jsStackTrace (http://127.0.0.1:8887/wasm.js:1042:13) at stackTrace (http://127.0.0.1:8887/wasm.js:1059:12) at Object.abort (http://127.0.0.1:8887/wasm.js:9854:44) at _abort (http://127.0.0.1:8887/wasm.js:7221:22) at wasm-function[465]:63 at wasm-function[463]:410 at wasm-function[487]:39 at wasm-function[486]:116 at wasm-function[447]:27 at wasm-function[85]:60
pthread-main.js onmessage() captured an uncaught exception: BindingError: parameter 0 has unknown type v
BindingError: parameter 0 has unknown type v at BindingError.
(http://127.0.0.1:8887/wasm.js:5500:24) at new BindingError (eval at createNamedFunction (http://127.0.0.1:8887/wasm.js:5441:14), :4:34) at throwBindingError (http://127.0.0.1:8887/wasm.js:5578:13) at requireRegisteredType (http://127.0.0.1:8887/wasm.js:5615:11) at __emval_lookupTypes (http://127.0.0.1:8887/wasm.js:7163:18) at __emval_get_method_caller (http://127.0.0.1:8887/wasm.js:7169:19) at wasm-function[178]:48 at wasm-function[175]:53 at wasm-function[174]:38 at wasm-function[173]:53
BindingError {name: "BindingError", message: "parameter 0 has unknown type v", stack: "BindingError: parameter 0 has unknown type v↵ a…asm-function[174]:38↵ at wasm-function[173]:53"}
My code:
index.cpp
#include <emscripten/bind.h>
#include <thread>
using namespace emscripten;
class A
{
public:
virtual ~A(){};
virtual void hello() = 0;
};
class AWrapper : public wrapper<A>
{
public:
EMSCRIPTEN_WRAPPER(AWrapper);
void hello() override
{
return call<void>("hello");
}
};
class B
{
public:
void initialize(A *a);
void hello();
private:
A *m_a;
};
void B::initialize(A *a)
{
m_a = a;
}
void B::hello()
{
// it works well if I don't wrap the call inside thread
std::thread t([=] {
m_a->hello();
});
}
EMSCRIPTEN_BINDINGS(A)
{
class_<A>("A")
.function("hello", &A::hello, pure_virtual())
.allow_subclass<AWrapper>("AWrapper");
}
EMSCRIPTEN_BINDINGS(B)
{
class_<B>("B")
.constructor<>()
.function("initialize", &B::initialize, allow_raw_pointers())
.function("hello", &B::hello);
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script src="wasm.js"></script>
<script src="index.js"></script>
</body>
</html>
index.js
Module.onRuntimeInitialized = () => {
const A = Module.A.extend("A", {
hello: () => {
console.log("hello from index.js");
}
});
const a = new A();
const b = new Module.B();
b.initialize(a);
b.hello();
};
build command:
emcc --bind -o wasm.js index.cpp -s USE_PTHREADS=1
emscripten sdk version: 1.38.21
At present I believe embind has no awareness of threads, and you're likely to have trouble doing anything outside the main thread.
In particular I'm not really sure how it could deal with transferring a reference to a JavaScript object across threads, since the main page context and the Worker contexts do not share JavaScript worlds. This may make what you're trying to do impossible...
What you could do I think is keep the object reference around in something that runs in a worker thread, then call back into the main thread to manipulate the object. However it'll be up to you to enforce which thread has access to the object.
Alternately it may be possible to do the JS extension for the class in the worker thread with code that's in your Worker's context, in which case you may be able to call into it on the worker thread... but I have no idea if that will work in practice right now.
Thanks for the reply! @brion , @kripken (Sorry but not to offend) so do we have any example to make my MCV (Minimal, Complete and Verifiable) code work as expected? Also besides embind, do we have any other mature options to make that work? (I tried webidl which seems not working well either).
I thought this would be frequently-asked feature as many isolated C++ SDK would require up layer to provide an implementation, while the SDK itself may have its own thread model...
Due to the way Worker threads work in browser JavaScript, what you want is not possible -- JavaScript objects in the main thread aren't accessible from the worker thread, and vice versa.
As already mentioned, it might work to extend the object on the worker thread instead of on the main thread, but:
- I don't know if type registrations will be visible on the worker thread (it looks like they are not from your original error messages)
- If you start a second worker thread, it won't have your JS objects either. You would need to be careful to use the right threads consistently.
In general I would recommend using embind etc to communicate with JavaScript only on the main thread.
Thanks, seems type registrations are not available on worker thread, as when I debug into pthread-main.js, there is no A type on Module which means I can't extend from it.
I tried the pure C way which seems to work, do you know why?
#include <pthread.h>
#include <iostream>
extern "C"
{
extern int js_func();
void* test(void*)
{
std::cout << "Background thread" << std::endl;
return NULL;
}
void cpp_func()
{
pthread_t t;
pthread_create(&t, NULL, &test, NULL);
std::cout << "Main thread" << std::endl;
}
}
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.