فهرست منبع

Cleanup and expose shadow realm API (#1362)

Marko Lahma 2 سال پیش
والد
کامیت
18089a7154

+ 48 - 0
Jint.Tests.PublicInterface/ShadowRealmTests.cs

@@ -0,0 +1,48 @@
+using Jint.Native.Object;
+
+namespace Jint.Tests.PublicInterface;
+
+public class ShadowRealmTests
+{
+    [Fact]
+    public void CanUseViaEngineMethods()
+    {
+        var engine = new Engine(options => options.EnableModules(GetBasePath()));
+        var shadowRealm = engine.Realm.Intrinsics.ShadowRealm.Construct();
+
+        // lexically scoped (let/const) are visible during single call
+        Assert.Equal(123, shadowRealm.Evaluate("const s = 123; const f = () => s; f();"));
+        Assert.Equal(true, shadowRealm.Evaluate("typeof f === 'undefined'"));
+
+        // vars hold longer
+        Assert.Equal(456, shadowRealm.Evaluate("function foo() { return 456; }; foo();"));
+        Assert.Equal(456, shadowRealm.Evaluate("foo();"));
+
+        // not visible in global engine though
+        Assert.Equal(true, engine.Evaluate("typeof foo === 'undefined'"));
+
+        // modules
+        var importValue = shadowRealm.ImportValue("./modules/format-name.js", "formatName");
+        var formatName = (ObjectInstance) importValue.UnwrapIfPromise();
+        var result = engine.Invoke(formatName, "John", "Doe").AsString();
+        Assert.Equal("John Doe", result);
+    }
+
+    private static string GetBasePath()
+    {
+        var assemblyDirectory = new DirectoryInfo(AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory);
+
+        var current = assemblyDirectory;
+        while (current is not null && current.GetDirectories().All(x => x.Name != "Jint.Tests"))
+        {
+            current = current.Parent;
+        }
+
+        if (current is null)
+        {
+            throw new NullReferenceException($"Could not find tests base path, assemblyPath: {assemblyDirectory}");
+        }
+
+        return Path.Combine(current.FullName, "Jint.Tests", "Runtime", "Scripts");
+    }
+}

+ 0 - 30
Jint.Tests/Runtime/ShadowRealmTests.cs

@@ -1,30 +0,0 @@
-using Jint.Native.Object;
-
-namespace Jint.Tests.Runtime;
-
-public class ShadowRealmTests
-{
-    [Fact]
-    public void CanUseViaEngineMethods()
-    {
-        var engine = new Engine(options => options.EnableModules(ModuleTests.GetBasePath()));
-        var shadowRealm1 = engine.Realm.Intrinsics.ShadowRealm.Construct();
-
-        // lexically scoped (let/const) are visible during single call
-        Assert.Equal(123, shadowRealm1.Evaluate("const s = 123; const f = () => s; f();"));
-        Assert.Equal(true, shadowRealm1.Evaluate("typeof f === 'undefined'"));
-
-        // vars hold longer
-        Assert.Equal(456, shadowRealm1.Evaluate("function foo() { return 456; }; foo();"));
-        Assert.Equal(456, shadowRealm1.Evaluate("foo();"));
-
-        // not visible in global engine though
-        Assert.Equal(true, engine.Evaluate("typeof foo === 'undefined'"));
-
-        // modules
-        var importValue = shadowRealm1.ImportValue("./modules/format-name.js", "formatName");
-        var formatName = (ObjectInstance) importValue.UnwrapIfPromise();
-        var result = engine.Invoke(formatName, "John", "Doe").AsString();
-        Assert.Equal("John Doe", result);
-    }
-}

+ 5 - 5
Jint/Native/ShadowRealm/ShadowRealmInstance.cs → Jint/Native/ShadowRealm/ShadowRealm.cs

@@ -15,13 +15,13 @@ namespace Jint.Native.ShadowRealm;
 /// <summary>
 /// https://tc39.es/proposal-shadowrealm/#sec-properties-of-shadowrealm-instances
 /// </summary>
-internal sealed class ShadowRealmInstance : ObjectInstance
+public sealed class ShadowRealm : ObjectInstance
 {
     private readonly JavaScriptParser _parser = new(new ParserOptions { Tolerant = false });
     internal readonly Realm _shadowRealm;
-    internal readonly ExecutionContext _executionContext;
+    private readonly ExecutionContext _executionContext;
 
-    internal ShadowRealmInstance(Engine engine, ExecutionContext executionContext, Realm shadowRealm) : base(engine)
+    internal ShadowRealm(Engine engine, ExecutionContext executionContext, Realm shadowRealm) : base(engine)
     {
         _executionContext = executionContext;
         _shadowRealm = shadowRealm;
@@ -242,9 +242,9 @@ internal sealed class ShadowRealmInstance : ObjectInstance
         }
     }
 
-    private static ShadowRealmInstance ValidateShadowRealmObject(Realm callerRealm, JsValue thisObj)
+    private static ShadowRealm ValidateShadowRealmObject(Realm callerRealm, JsValue thisObj)
     {
-        var instance = thisObj as ShadowRealmInstance;
+        var instance = thisObj as ShadowRealm;
         if (instance is null)
         {
             ExceptionHelper.ThrowTypeError(callerRealm, "object must be a ShadowRealm");

+ 5 - 5
Jint/Native/ShadowRealm/ShadowRealmConstructor.cs

@@ -9,9 +9,9 @@ namespace Jint.Native.ShadowRealm;
 /// <summary>
 /// https://tc39.es/proposal-shadowrealm/#sec-properties-of-the-shadowRealm-constructor
 /// </summary>
-internal sealed class ShadowRealmConstructor : FunctionInstance, IConstructor
+public sealed class ShadowRealmConstructor : FunctionInstance, IConstructor
 {
-    private static readonly JsString _functionName = new JsString("ShadowRealm");
+    private static readonly JsString _functionName = new("ShadowRealm");
 
     internal ShadowRealmConstructor(
         Engine engine,
@@ -34,12 +34,12 @@ internal sealed class ShadowRealmConstructor : FunctionInstance, IConstructor
         return null;
     }
 
-    public ShadowRealmInstance Construct()
+    public ShadowRealm Construct()
     {
         return Construct(PrototypeObject);
     }
 
-    private ShadowRealmInstance Construct(JsValue newTarget)
+    private ShadowRealm Construct(JsValue newTarget)
     {
         var realmRec = _engine._host.CreateRealm();
 
@@ -56,7 +56,7 @@ internal sealed class ShadowRealmConstructor : FunctionInstance, IConstructor
                     realm: realmRec,
                     function: null);
 
-                return new ShadowRealmInstance(engine, context, realmRec);
+                return new ShadowRealm(engine, context, realmRec);
             },
             realmRec);
 

+ 2 - 2
Jint/Native/ShadowRealm/ShadowRealmPrototype.cs

@@ -80,9 +80,9 @@ internal sealed class ShadowRealmPrototype : Prototype
         return O.ShadowRealmImportValue(specifierString.ToString(), exportName.ToString(), callerRealm);
     }
 
-    private ShadowRealmInstance ValidateShadowRealmObject(JsValue thisObj)
+    private ShadowRealm ValidateShadowRealmObject(JsValue thisObj)
     {
-        var instance = thisObj as ShadowRealmInstance;
+        var instance = thisObj as ShadowRealm;
         if (instance is null)
         {
             ExceptionHelper.ThrowTypeError(_realm, "object must be a ShadowRealm");

+ 1 - 1
Jint/Runtime/Intrinsics.cs

@@ -241,7 +241,7 @@ namespace Jint.Runtime
         internal SymbolConstructor Symbol =>
             _symbol ??= new SymbolConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
 
-        internal ShadowRealmConstructor ShadowRealm =>
+        public ShadowRealmConstructor ShadowRealm =>
             _shadowRealm ??= new ShadowRealmConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
 
         internal EvalFunctionInstance Eval =>