Is it possible to compile script in one realm and execute it in another?


I am currently upgrading from Spidermonkey 1.8.5 to 115.
A pretty big change, and until I reached this issue everything was fine.

A simple example of what I am trying to achieve:
1. Create global object “globalA”
2. Enter realm with JS::AutoRealm arA(cx, globalA)
3. Compile JSScript “testScript” with JS::Compile(cx, options, code)
4. Store “testScript” in JS::Heap
>>> program executes for some time until “testScript” is needed
5. Create global object “globalB” which has “globalA” as its prototype
6. Enter realm with JS::AutoRealm arB(cx, globalB)
7. Execute “testScript” with JS_ExecuteScript(cx, testScript, &retval)

The result of running the example above is segmentation fault with crash reason “Realm mismatch”.

I am wondering if anyone has encountered similar issue or know a way for this to work?

If needed, I can share implementation example and a crash backtrace.

Each script belongs to a single realm and can’t be executed in another realm.

One option is to compile the JS source code to a Stencil with CompileGlobalScriptToStencil. Then you can instantiate that Stencil in both realms with InstantiateGlobalStencil to get a JSScript* for that realm.

1 Like