You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After reading through #789 I've been trying to set up a shadow realm to use as an isolated context. It looks like it could work, but I don't see an easy way to dynamically add objects from C#.
// manually writing the object works as expectedvarengine=newEngine();varsr=engine.Realm.Intrinsics.ShadowRealm.Construct();sr.Evaluate("var obj = { prop: 100 };");sr.Evaluate("obj.prop + 100");// output: 200 - expectedengine.Evaluate("obj.prop + 100");// error: obj is not defined - expected
What I've tried:
// JsObject.ToString() doesn't serialize the object to JSON.sr.Evaluate($"var obj = {JsObject.FromObject(sr.Engine,new{prop=100})};");// error: unexpected token
// value passing between realms must be callable or primitivevarfunc=sr.Evaluate("(obj) => obj.prop + 100");Engine.Invoke(func,new{Prop=1});// error: result is not callable - sort of expected
// this just sets a prop on the ShadowRealm object itselfsr.Set("obj",JsValue.FromObject(sr.Engine,new{prop=100}));sr.Evaluate("obj.prop + 100");// error: obj is not defined - expectedengine.Evaluate("obj.prop + 100");// error: obj is not defined - expected
// both of these set the value in the global realm not the shadow realmsr.Engine.SetValue("obj",new{prop=100});sr.Engine.Realm.GlobalObject.Set("obj",JsValue.FromObject(sr.Engine,new{prop=100}));sr.Evaluate("obj.prop + 100");// Cross-Realm Error: obj is not definedengine.Evaluate("obj.prop + 100");// output: 200
A work around is either manually serializing the object with Json.NET, System.Text.Json, etc. -- or reflecting the internal realm from the ShadowRealm and setting the global state of that:
varrealm=(Realm)sr.GetType().GetField("_shadowRealm",BindingFlags.NonPublic|BindingFlags.Instance)!.GetValue(sr);realm.GlobalObject.Set("obj",JsValue.FromObject(sr.Engine,new{prop=100}));sr.Evaluate("obj.prop + 100");// output: 200 - worksengine.Evaluate("obj.prop + 100");// error: obj is not defined - expected
Question
My question is,from the C# side, am I missing a way to add non-primitive values to a ShadowRealm before or after creating it?
Request
If I'm not, my request is to add a SetValue method to ShadowRealm. That would help make it more usable for non-primitive values. I understand that within JavaScript non-callable, non-primitive values can't cross realms, but between JavaScript and C# we should be able to inject dynamic objects when creating a ShadowRealm. Otherwise re-looking at #789 would be nice.
The text was updated successfully, but these errors were encountered:
Repository owner
locked and limited conversation to collaborators
Aug 1, 2023
After reading through #789 I've been trying to set up a shadow realm to use as an isolated context. It looks like it could work, but I don't see an easy way to dynamically add objects from C#.
What I've tried:
A work around is either manually serializing the object with Json.NET, System.Text.Json, etc. -- or reflecting the internal realm from the ShadowRealm and setting the global state of that:
Question
My question is,from the C# side, am I missing a way to add non-primitive values to a ShadowRealm before or after creating it?
Request
If I'm not, my request is to add a SetValue method to ShadowRealm. That would help make it more usable for non-primitive values. I understand that within JavaScript non-callable, non-primitive values can't cross realms, but between JavaScript and C# we should be able to inject dynamic objects when creating a ShadowRealm. Otherwise re-looking at #789 would be nice.
The text was updated successfully, but these errors were encountered: