From 15a1c210d8facaefd2d0bf2efc79252b5b13dbaa Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Sun, 16 Jun 2024 10:01:00 +0300 Subject: [PATCH] Update test262 suite and implement Promise.try (#1895) --- .../Test262Harness.settings.json | 2 +- Jint/Native/Promise/PromiseConstructor.cs | 29 ++++++++++++++++++- README.md | 1 + 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Jint.Tests.Test262/Test262Harness.settings.json b/Jint.Tests.Test262/Test262Harness.settings.json index a4593036e8..9b8743aee2 100644 --- a/Jint.Tests.Test262/Test262Harness.settings.json +++ b/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", diff --git a/Jint/Native/Promise/PromiseConstructor.cs b/Jint/Native/Promise/PromiseConstructor.cs index 99820f5960..219cd59925 100644 --- a/Jint/Native/Promise/PromiseConstructor.cs +++ b/Jint/Native/Promise/PromiseConstructor.cs @@ -40,7 +40,7 @@ protected override void Initialize() { 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 @@ protected override void Initialize() ["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 @@ private JsValue Reject(JsValue thisObject, JsValue[] arguments) return instance; } + /// + /// https://tc39.es/proposal-promise-try/ + /// + 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 diff --git a/README.md b/README.md index 2b003dd43a..e2f66f7ec7 100644 --- a/README.md +++ b/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`)