Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share Value between contexts #46

Open
AaronWright3 opened this issue Feb 2, 2022 · 1 comment
Open

Share Value between contexts #46

AaronWright3 opened this issue Feb 2, 2022 · 1 comment

Comments

@AaronWright3
Copy link

AaronWright3 commented Feb 2, 2022

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?

@ftk
Copy link
Owner

ftk commented Feb 2, 2022

When I was writing quickjspp I was not expecting for values to be shared between contexts. The assertion might not be needed indeed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants