Closed cutsoy closed 1 year ago
In general I don't think it's safe to use something created with one env, in another env.
@cutsoy I agree with @mhdawson. It would be safer for modules foo
and bar
to coordinate via JS. For example, module bar
can expose a function that receives as its sole parameter the desired object. Then, the function can be passed into module foo
, which will call it with the object. Module bar
can then create a second reference to the same object. So, roughly,
foo.sendObject(bar.receiveObject);
where sendObject
is implemented as
static napi_value SendObject(napi_env env, napi_callback_info info) {
napi_value cb, the_object;
size_t argc = 1;
napi_get_cb_info(env, info, &argc, &cb, NULL, NULL);
napi_get_reference_value(env, the_object_ref, &the_object);
napi_call_function(env, the_object, cb, 1, &the_object, NULL);
}
@cutsoy I hope this answers your question. If you have any further questions, please feel free to re-open this issue or open a new one.
Not sure if this is the right repo to ask this, but I was wondering if references to objects (i.e. created with
napi_create_reference
) can be shared between different native modules and/ornapi_env
s (assuming of course it runs in the same instance of Node).For example:
foo
that receives an object from JS and creates a reference to keep it around a bit longer;foo
makes a native C call to native modulebar
and passes the reference tobar
;bar
andbar
callsnapi_get_reference_value
(with a differentnapi_env
).