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.
On Friday, February 14, 2025 at 9:23:38 AM UTC [email protected] wrote:

> On Thu, Feb 13, 2025 at 2:23 AM Charles Lew <[email protected]> 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
>> [email protected]
>> 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 [email protected].
>> To view this discussion visit 
>> https://groups.google.com/d/msgid/v8-users/1fb15c01-198a-4ef2-8705-a78265eb0691n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/v8-users/1fb15c01-198a-4ef2-8705-a78265eb0691n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
-- 
v8-users mailing list
[email protected]
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 [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/v8-users/7bd6a1c3-98a6-48fe-a4e2-e658e8882b68n%40googlegroups.com.

Reply via email to