Trying to share the JS Realm between v8 contexts

47 views
Skip to first unread message

Charles Lew

unread,
Feb 13, 2025, 5:23:25 AMFeb 13
to v8-users
I'm seeking for a way to share the same JSRealm with different v8 contexts, so they share the same `Object` values in memory, but with different global values.

Currently when evaluating `a instanceof Object` with `a` value defined from another context, this evaluates to false, which is not what i want.

What i've tried:
I've tried overwriting one context's global `Object` with value from another context, which seems ok at first, but it's flawed in corner cases, that literal object values still uses the V8::Context's original Object prototype.

Is there a way to accomplish what i want?

Ben Noordhuis

unread,
Feb 13, 2025, 8:57:13 AMFeb 13
to v8-u...@googlegroups.com
No. It's the most asked question about node's vm module (which is also
based around contexts) by a mile but there's no good way to do it.

J Decker

unread,
Feb 14, 2025, 4:23:38 AMFeb 14
to v8-u...@googlegroups.com
On Thu, Feb 13, 2025 at 2:23 AM Charles Lew <crlf...@gmail.com> wrote:
I'm seeking for a way to share the same JSRealm with different v8 contexts, so they share the same `Object` values in memory, but with different global values.

Currently when evaluating `a instanceof Object` with `a` value defined from another context, this evaluates to false, which is not what i want.


this can work as a workaround ... if( obj.constructor.name === "Object" )    
but probably the better answer is 'don't do that'.
 
What i've tried:
I've tried overwriting one context's global `Object` with value from another context, which seems ok at first, but it's flawed in corner cases, that literal object values still uses the V8::Context's original Object prototype.

Is there a way to accomplish what i want?

--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/v8-users/1fb15c01-198a-4ef2-8705-a78265eb0691n%40googlegroups.com.

guest271314

unread,
Feb 15, 2025, 10:41:21 AMFeb 15
to v8-users
In the browser there are WHATWG Transferable Streams which provides a means to read and write to different context using WHATWG Streams, for example using the readable and writable pairs of a TransformStream. Effectively meaning it's possible to communicate between an iframe and a window or a window and a window, or a Worklet and a Worker, etc. 

There is also WebAssembly.Memory, which provides a means to read and write to the same linear memory object between different contexts. 

In Node.js there is --import flag to preload modules; in Bun there is the -r option to preload modules, which effectively provides a means to set objects and definitions of objects globally before the main module loads. For example, let's say we wanted to define Node.js' fs.readSync function onto Node.js' process.stdin, in preprocess-module.js 

import process from "node:process";
import { readSync } from "node:fs";
process.stdin.readSync = readSync;
export {};

node --import preprocess.module module.ts

Re

> I've tried overwriting one context's global `Object` 

That can be potentially disasterous since Object itself is writable, we can do

Object = 1;

in one context and Object.assign() would throw in the nth context.
Reply all
Reply to author
Forward
0 new messages