소스 검색

Update test262 suite and implement Promise.try (#1895)

Marko Lahma 1 년 전
부모
커밋
15a1c210d8
3개의 변경된 파일30개의 추가작업 그리고 2개의 파일을 삭제
  1. 1 1
      Jint.Tests.Test262/Test262Harness.settings.json
  2. 28 1
      Jint/Native/Promise/PromiseConstructor.cs
  3. 1 0
      README.md

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

@@ -1,5 +1,5 @@
 {
-  "SuiteGitSha": "c2ae5ed5e90d86e17730730b003e9b6fb050693e",
+  "SuiteGitSha": "9dec509c93fa9b19d46330d90a43f871bb1aaf84",
   //"SuiteDirectory": "//mnt/c/work/test262",
   "TargetPath": "./Generated",
   "Namespace": "Jint.Tests.Test262",

+ 28 - 1
Jint/Native/Promise/PromiseConstructor.cs

@@ -40,7 +40,7 @@ namespace Jint.Native.Promise
         {
             const PropertyFlag PropertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
             const PropertyFlag LengthFlags = PropertyFlag.Configurable;
-            var properties = new PropertyDictionary(6, checkExistingKeys: false)
+            var properties = new PropertyDictionary(8, checkExistingKeys: false)
             {
                 ["all"] = new(new PropertyDescriptor(new ClrFunction(Engine, "all", All, 1, LengthFlags), PropertyFlags)),
                 ["allSettled"] = new(new PropertyDescriptor(new ClrFunction(Engine, "allSettled", AllSettled, 1, LengthFlags), PropertyFlags)),
@@ -48,6 +48,7 @@ namespace Jint.Native.Promise
                 ["race"] = new(new PropertyDescriptor(new ClrFunction(Engine, "race", Race, 1, LengthFlags), PropertyFlags)),
                 ["reject"] = new(new PropertyDescriptor(new ClrFunction(Engine, "reject", Reject, 1, LengthFlags), PropertyFlags)),
                 ["resolve"] = new(new PropertyDescriptor(new ClrFunction(Engine, "resolve", Resolve, 1, LengthFlags), PropertyFlags)),
+                ["try"] = new(new PropertyDescriptor(new ClrFunction(Engine, "try", Try, 1, LengthFlags), PropertyFlags)),
                 ["withResolvers"] = new(new PropertyDescriptor(new ClrFunction(Engine, "withResolvers", WithResolvers , 0, LengthFlags), PropertyFlags)),
             };
             SetProperties(properties);
@@ -169,6 +170,32 @@ namespace Jint.Native.Promise
             return instance;
         }
 
+        /// <summary>
+        /// https://tc39.es/proposal-promise-try/
+        /// </summary>
+        private JsValue Try(JsValue thisObject, JsValue[] arguments)
+        {
+            if (!thisObject.IsObject())
+            {
+                ExceptionHelper.ThrowTypeError(_realm, "Promise.try called on non-object");
+            }
+
+            var callbackfn = arguments.At(0);
+            var promiseCapability = NewPromiseCapability(_engine, thisObject);
+
+            try
+            {
+                var status = callbackfn.Call(Undefined, arguments.Skip(1));
+                promiseCapability.Resolve.Call(Undefined, new[] { status });
+            }
+            catch (JavaScriptException e)
+            {
+                promiseCapability.Reject.Call(Undefined, new[] { e.Error });
+            }
+
+            return promiseCapability.PromiseInstance;
+        }
+
         // This helper methods executes the first 6 steps in the specs belonging to static Promise methods like all, any etc.
         // If it returns false, that means it has an error and it is already rejected
         // If it returns true, the logic specific to the calling function should continue executing

+ 1 - 0
README.md

@@ -125,6 +125,7 @@ and many more.
 - ✔ Float16Array (Jint v4, requires NET 6 target or higher)
 - ✔ Import attributes
 - ✔ JSON modules
+- ✔ `Promise.try` (Jint v4)
 - ✔ `Promise.withResolvers`
 - ✔ Resizable and growable ArrayBuffers
 - ✔ Set methods (`intersection`, `union`, `difference`, `symmetricDifference`, `isSubsetOf`, `isSupersetOf`, `isDisjointFrom`)