quickjspp
quickjspp copied to clipboard
Share Value between contexts
Hi,
I have two files, test_a.js and test_b.js. Here they are:
test_a.js
var test_object = {
foo: "test_a.js",
}
function func_a(obj) {
if ("foo" in obj)
{
System.print(obj.foo);
}
}
test_b.js
var test_object = {
foo: "test_b.js",
}
I load test_a.js along with native C++ function print
(imported as module System
) into context context
, and test_b.js into context context2
. Both belong to the same runtime.
My goal is to call func_a
from test_a.js in context
with the object test_object
from test_b.js from context2
as an argument.
However, trying this (ignoring my contexts and methods which are actually from my own wrapper class):
qjs::Value test_obj_b = context2.get()->eval("test_object");
auto func_a = context.get()->eval("func_a").as<std::function<void(const qjs::Value&)>>();
func_a(test_obj_b);
results in the following:
Assertion failed: ctx == v.ctx, file ../include/quickjspp/quickjspp.hpp, line 1807
Apparently when wrapping a JSValue into a Value, a check is performed to make sure the value is from the same context as the context it's being used in. However, I was under the impression from quickjs's documentation that sharing objects between contexts was possible ("There can be several JSContexts per JSRuntime and they can share objects[...]"), so I'm not sure if I'm doing something wrong with quickjspp, if this is intended behavior, or if I'm just missing some crucial piece of information in my basic idea.
EDIT: if I remove the assertion from quickjspp.hpp, things behave the way I expect, and "test_b.js" is output to the console. I imagine this might have consequences, though?
When I was writing quickjspp I was not expecting for values to be shared between contexts. The assertion might not be needed indeed