How to copy v8::FunctionCallbackInfo array from one isolate to another?
Clash Royale CLAN TAG#URR8PPP
How to copy v8::FunctionCallbackInfo<v8::Value> array from one isolate to another?
In my project I've multiple threads which have their own V8 isolates. So currently I don't need any v8::Lockers anywhere. But now I want to implement a function "execute" which asynchronously can execute other scripts:
Thread1: execute("script1", "param1"); execute("script2", param1:
"param1", param2: 5);
Thread2: executes script1
Thread3: executes script2
So far so good... But now I want to pass V8 parameters across isolates. So is there any way to pass a v8::FunctionCallbackInfo argument array from one isolate to another?
With my current architecture I don't need any v8:Lockers, so a solution without having to use them across my whole code base would be preferred.
1 Answer
1
V8 developer here. v8::Values are generally tied to one isolate. The only way to use them in another isolate is to create a corresponding value there. Depending on your requirements, you can either copy them directly (iterating over the object, property by property, and creating a matching object in the second isolate), or use a serialization format in between (JSON, or StructuredClone, or something you define yourself).
The technical background is that each isolate has a garbage-collected heap, and those values are stored on that heap. One isolate can't access another isolate's heap, so it needs its own copy of any objects it wants to work with. They're called "isolates" because they're isolated from each other ;-)
JSON is the easiest option. You could implement some other serialization format if you wanted to. When I said you could "copy directly, iterating", what I mean is that once you have extracted a property's value (say, an int, or a string) from an object, you don't have to convert it to text first, you can store that int or string directly on the target object. But you do have to do this "extraction" manually; there is no shortcut to copy an entire v8::Value to another isolate.
– jmrk
Aug 14 at 16:39
Thanks! For now I'll stick with my JSON conversion, because I always got some wired exceptions when trying to extract V8 values from one isolate and creating new V8 values from that in another isolate.
– VitaminCpp
Aug 16 at 7:03
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
OK I see, but how do I copy v8::Values from one isolate to another? Currently I'm struggling with accessing the v8::FunctionCallbackInfo<v8::Value> array from isolate A in isolate B. Is the slow JSON serialization/deserialization my only option?
– VitaminCpp
Aug 13 at 12:16