A realm has its own globals.
dotnet add package RecursiveAI.OneEcmaRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); var first = engine.MainRealm; var second = engine.CreateRealm(); Console.WriteLine(first.Evaluate( "var label = 'first'; label").AsString); Console.WriteLine(second.Evaluate( "var label = 'second'; label").AsString);
first second
Unzip the example, then dotnet run.
Full program
This example checks separate array constructors, passes a function between realms and tests an array created in the other realm. It reads no files.
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); var first = engine.MainRealm; var second = engine.CreateRealm(); first.Evaluate("globalThis.label = 'first'"); second.Evaluate("globalThis.label = 'second'"); Console.WriteLine(first.Evaluate("label").AsString); Console.WriteLine(second.Evaluate("label").AsString); var firstArray = first.Get(first.Global, "Array"); var secondArray = second.Get(second.Global, "Array"); Console.WriteLine($"same Array constructor: {ReferenceEquals(firstArray.AsObject, secondArray.AsObject)}"); second.DefineData(second.Global, "readFirst", first.Evaluate("(() => label)")); Console.WriteLine(second.Evaluate("label + ' / ' + readFirst()").AsString); second.DefineData(second.Global, "foreign", first.Evaluate("[1, 2]")); Console.WriteLine(second.Evaluate("Array.isArray(foreign) + ' / ' + (foreign instanceof Array)").AsString);
dotnet run prints:
first second same Array constructor: False second / first true / false
A realm is not an OS security boundary: the docs state, “Limits and the job queue are the engine’s.”
Variations
Share an object explicitly; changing it in the second realm is visible in the first:
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); var first = engine.MainRealm; var second = engine.CreateRealm(); second.DefineData(second.Global, "shared", first.Evaluate("globalThis.shared = { total: 1 }")); second.Evaluate("shared.total = 7"); Console.WriteLine(first.Evaluate("shared.total").AsNumber);
7Related recipes
The short answer
Use engine.MainRealm for the original JavaScript environment and engine.CreateRealm() for another. Call each realm’s Evaluate method to run code against its own global object. The snippet at the top sets the same global name twice and reads two different strings: the assignments belong to different environments.
This separates global names and built-ins without creating a second engine. It does not duplicate every value that crosses the boundary. Pass values deliberately through the public realm API, and remember that the engine owns the resource limits and job queue for all its realms.
What is a realm in JavaScript?
In OneEcma, a realm has a global object and its own built-ins, including Object and Array. The pinned host API docs describe exactly that scope. The full program retrieves Array from each global and compares the constructor objects in C#. They are different objects, so the printed comparison is False.
The practical use is running scripts whose global settings should not overwrite each other. A label, configuration object or helper installed in the first global does not become a global property in the second merely because both realms belong to one engine. Run setup code through the intended realm, not through the main engine’s evaluation entry point by accident.
A realm is also not a separate scheduling or resource-budget unit here. Limits and jobs belong to the containing engine. If your application needs independent engine limits, separate realm creation does not supply them. Review the resource-limit API when choosing the unit of work your application will host.
How do you create separate JavaScript global environments in C#?
Create the engine once, retain the realm references and call Evaluate on the reference that owns the script. The initial assignments use globalThis so the example is explicitly about global-object properties rather than local function variables. No wrapper function or generated source string is needed to emulate separate environments.
Use DefineData on the receiving realm’s global object to expose a value from another realm. The full program installs readFirst in the second realm, but creates the function in the first. Calling it from the second still reads the first realm’s label. The docs specify that each function runs in the realm where it was created; moving the value does not rebind that function’s environment.
The variation demonstrates the other important consequence: passing an object shares that object rather than copying its properties into an independent object. The assignment in the second realm changes the total later read in the first. Only pass a shared mutable value when both scripts are meant to observe those changes. Keep all engine and realm calls serialized; the hosting API is not thread-safe.
What is instanceof in JavaScript?
instanceof tests a relationship to a particular constructor’s prototype, not simply whether a value has the broad shape of an array. The MDN explanation calls out the cross-realm consequence: different realms have different constructors and prototypes.
The full program gives the second realm an array created in the first. Array.isArray(foreign) returns true, but foreign instanceof Array returns false because the second realm’s Array is not the constructor associated with that array’s prototype chain. Neither result means the value was lost or serialized. It remains an array object from its original realm.
Use Array.isArray when the question is whether a received value is an array. Reserve instanceof for cases where the relationship to that specific constructor is what your code needs to test. This distinction matters most once a host intentionally permits values to move across realm boundaries.