浏览代码

Implement Error.isError (#2030)

Marko Lahma 6 月之前
父节点
当前提交
a3c7444c97
共有 3 个文件被更改,包括 25 次插入6 次删除
  1. 0 1
      Jint.Tests.Test262/Test262Harness.settings.json
  2. 20 1
      Jint/Native/Error/ErrorConstructor.cs
  3. 5 4
      README.md

+ 0 - 1
Jint.Tests.Test262/Test262Harness.settings.json

@@ -9,7 +9,6 @@
     "async-iteration",
     "async-iteration",
     "Atomics",
     "Atomics",
     "decorators",
     "decorators",
-    "Error.isError",
     "explicit-resource-management",
     "explicit-resource-management",
     "import-defer",
     "import-defer",
     "iterator-helpers",
     "iterator-helpers",

+ 20 - 1
Jint/Native/Error/ErrorConstructor.cs

@@ -1,6 +1,8 @@
+using Jint.Collections;
 using Jint.Native.Object;
 using Jint.Native.Object;
 using Jint.Runtime;
 using Jint.Runtime;
 using Jint.Runtime.Descriptors;
 using Jint.Runtime.Descriptors;
+using Jint.Runtime.Interop;
 
 
 namespace Jint.Native.Error;
 namespace Jint.Native.Error;
 
 
@@ -25,6 +27,15 @@ public sealed class ErrorConstructor : Constructor
 
 
     internal ErrorPrototype PrototypeObject { get; }
     internal ErrorPrototype PrototypeObject { get; }
 
 
+    protected override void Initialize()
+    {
+        var properties = new PropertyDictionary(3, checkExistingKeys: false)
+        {
+            ["isError"] = new PropertyDescriptor(new PropertyDescriptor(new ClrFunction(Engine, "isError", IsError, 1), PropertyFlag.NonEnumerable)),
+        };
+        SetProperties(properties);
+    }
+
     protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments)
     protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments)
     {
     {
         return Construct(arguments, this);
         return Construct(arguments, this);
@@ -32,7 +43,7 @@ public sealed class ErrorConstructor : Constructor
 
 
     public ObjectInstance Construct(string? message = null)
     public ObjectInstance Construct(string? message = null)
     {
     {
-        return Construct(message != null ? new JsValue[]{ message } : System.Array.Empty<JsValue>(), this);
+        return Construct(message != null ? new JsValue[] { message } : System.Array.Empty<JsValue>(), this);
     }
     }
 
 
     /// <summary>
     /// <summary>
@@ -83,4 +94,12 @@ public sealed class ErrorConstructor : Constructor
             return callStack.BuildCallStackString(_engine, lastSyntaxNode.Location, currentFunction == this ? 1 : 0);
             return callStack.BuildCallStackString(_engine, lastSyntaxNode.Location, currentFunction == this ? 1 : 0);
         }
         }
     }
     }
+
+    /// <summary>
+    /// https://tc39.es/proposal-is-error/
+    /// </summary>
+    private static JsValue IsError(JsValue? thisObj, JsValue[] arguments)
+    {
+        return arguments.At(0) is JsError;
+    }
 }
 }

+ 5 - 4
README.md

@@ -130,14 +130,15 @@ and many more.
 
 
 #### ECMAScript Stage 3 (no version yet)
 #### ECMAScript Stage 3 (no version yet)
 
 
-- ✔ Float16Array (Requires NET 6 or higher)
+- ✔ `Error.isError`
+- ✔ `Float16Array` (Requires NET 6 or higher)
 - ✔ Import attributes
 - ✔ Import attributes
 - ✔ JSON modules
 - ✔ JSON modules
-- ✔ Math.sumPrecise
+- ✔ `Math.sumPrecise`
 - ✔ `Promise.try`
 - ✔ `Promise.try`
 - ✔ Set methods (`intersection`, `union`, `difference`, `symmetricDifference`, `isSubsetOf`, `isSupersetOf`, `isDisjointFrom`)
 - ✔ Set methods (`intersection`, `union`, `difference`, `symmetricDifference`, `isSubsetOf`, `isSupersetOf`, `isDisjointFrom`)
-- ✔ ShadowRealm
-- ✔ Uint8Array to/from base64
+- ✔ `ShadowRealm`
+- ✔ `Uint8Array` to/from base64
 
 
 #### Other
 #### Other