diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-08-21 16:43:38 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-08-23 00:01:46 +0100 |
commit | 4dffa40a8dc00549a7c47dc5a335a8fbc7832de1 (patch) | |
tree | 81294d2102e9383f479b93eb8a95c43bbe9051ac /Userland | |
parent | 98d8a858cddb3b7c5f37a0044b5df4654374cfa6 (diff) | |
download | serenity-4dffa40a8dc00549a7c47dc5a335a8fbc7832de1.zip |
LibJS: Implement Promise.any on the Promise constructor
Diffstat (limited to 'Userland')
4 files changed, 243 insertions, 2 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp b/Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp index 5f19389bc2..8dc8b6c9b5 100644 --- a/Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp @@ -7,6 +7,7 @@ #include <AK/Function.h> #include <LibJS/Interpreter.h> #include <LibJS/Runtime/AbstractOperations.h> +#include <LibJS/Runtime/AggregateError.h> #include <LibJS/Runtime/Array.h> #include <LibJS/Runtime/Error.h> #include <LibJS/Runtime/FunctionObject.h> @@ -150,6 +151,30 @@ static Value perform_promise_all(GlobalObject& global_object, Object& iterator_r }); } +// 27.2.4.3.1 PerformPromiseAny ( iteratorRecord, constructor, resultCapability, promiseResolve ), https://tc39.es/ecma262/#sec-performpromiseany +static Value perform_promise_any(GlobalObject& global_object, Object& iterator_record, Value constructor, PromiseCapability result_capability, Value promise_resolve) +{ + auto& vm = global_object.vm(); + + return perform_promise_common( + global_object, iterator_record, constructor, result_capability, promise_resolve, + [&](PromiseValueList& errors) -> Value { + auto errors_array = Array::create_from(global_object, errors.values); + + auto* error = AggregateError::create(global_object); + error->define_property_or_throw(vm.names.errors, { .value = errors_array, .writable = true, .enumerable = false, .configurable = true }); + + vm.throw_exception(global_object, error); + return {}; + }, + [&](PromiseValueList& errors, RemainingElements& remaining_elements_count, Value next_promise, size_t index) { + auto* on_rejected = PromiseAnyRejectElementFunction::create(global_object, index, errors, result_capability, remaining_elements_count); + on_rejected->define_direct_property(vm.names.name, js_string(vm, String::empty()), Attribute::Configurable); + + (void)next_promise.invoke(global_object, vm.names.then, result_capability.resolve, on_rejected); + }); +} + PromiseConstructor::PromiseConstructor(GlobalObject& global_object) : NativeFunction(vm().names.Promise.as_string(), *global_object.function_prototype()) { @@ -165,9 +190,9 @@ void PromiseConstructor::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.all, all, 1, attr); + define_native_function(vm.names.any, any, 1, attr); // TODO: Implement these functions below and uncomment this. // define_native_function(vm.names.allSettled, all_settled, 1, attr); - // define_native_function(vm.names.any, any, 1, attr); // define_native_function(vm.names.race, race, 1, attr); define_native_function(vm.names.reject, reject, 1, attr); define_native_function(vm.names.resolve, resolve, 1, attr); @@ -252,7 +277,32 @@ JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::all_settled) // 27.2.4.3 Promise.any ( iterable ), https://tc39.es/ecma262/#sec-promise.any JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::any) { - TODO(); + auto* constructor = vm.this_value(global_object).to_object(global_object); + if (!constructor) + return {}; + + auto promise_capability = new_promise_capability(global_object, constructor); + if (vm.exception()) + return {}; + + auto promise_resolve = get_promise_resolve(global_object, constructor); + if (auto abrupt = if_abrupt_reject_promise(global_object, promise_resolve, promise_capability); abrupt.has_value()) + return abrupt.value(); + + auto iterator_record = get_iterator(global_object, vm.argument(0)); + if (auto abrupt = if_abrupt_reject_promise(global_object, iterator_record, promise_capability); abrupt.has_value()) + return abrupt.value(); + + auto result = perform_promise_any(global_object, *iterator_record, constructor, promise_capability, promise_resolve); + if (vm.exception()) { + if (!iterator_record_is_complete(global_object, *iterator_record)) + iterator_close(*iterator_record); + + auto abrupt = if_abrupt_reject_promise(global_object, result, promise_capability); + return abrupt.value(); + } + + return result; } // 27.2.4.5 Promise.race ( iterable ), https://tc39.es/ecma262/#sec-promise.race diff --git a/Userland/Libraries/LibJS/Runtime/PromiseResolvingElementFunctions.cpp b/Userland/Libraries/LibJS/Runtime/PromiseResolvingElementFunctions.cpp index a3f3269d73..396c9c16f7 100644 --- a/Userland/Libraries/LibJS/Runtime/PromiseResolvingElementFunctions.cpp +++ b/Userland/Libraries/LibJS/Runtime/PromiseResolvingElementFunctions.cpp @@ -5,6 +5,7 @@ */ #include <LibJS/Runtime/AbstractOperations.h> +#include <LibJS/Runtime/AggregateError.h> #include <LibJS/Runtime/Array.h> #include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/PromiseReaction.h> @@ -72,4 +73,33 @@ Value PromiseAllResolveElementFunction::resolve_element() return js_undefined(); } +PromiseAnyRejectElementFunction* PromiseAnyRejectElementFunction::create(GlobalObject& global_object, size_t index, PromiseValueList& errors, PromiseCapability capability, RemainingElements& remaining_elements) +{ + return global_object.heap().allocate<PromiseAnyRejectElementFunction>(global_object, index, errors, capability, remaining_elements, *global_object.function_prototype()); +} + +PromiseAnyRejectElementFunction::PromiseAnyRejectElementFunction(size_t index, PromiseValueList& errors, PromiseCapability capability, RemainingElements& remaining_elements, Object& prototype) + : PromiseResolvingElementFunction(index, errors, move(capability), remaining_elements, prototype) +{ +} + +Value PromiseAnyRejectElementFunction::resolve_element() +{ + auto& vm = this->vm(); + auto& global_object = this->global_object(); + + m_values.values[m_index] = vm.argument(0); + + if (--m_remaining_elements.value == 0) { + auto errors_array = Array::create_from(global_object, m_values.values); + + auto* error = AggregateError::create(global_object); + error->define_property_or_throw(vm.names.errors, { .value = errors_array, .writable = true, .enumerable = false, .configurable = true }); + + return vm.call(*m_capability.reject, js_undefined(), error); + } + + return js_undefined(); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/PromiseResolvingElementFunctions.h b/Userland/Libraries/LibJS/Runtime/PromiseResolvingElementFunctions.h index 9bb8b07232..b8d589e0e3 100644 --- a/Userland/Libraries/LibJS/Runtime/PromiseResolvingElementFunctions.h +++ b/Userland/Libraries/LibJS/Runtime/PromiseResolvingElementFunctions.h @@ -74,4 +74,18 @@ private: virtual Value resolve_element() override; }; +// 27.2.4.3.2 Promise.any Reject Element Functions, https://tc39.es/ecma262/#sec-promise.any-reject-element-functions +class PromiseAnyRejectElementFunction final : public PromiseResolvingElementFunction { + JS_OBJECT(PromiseResolvingFunction, NativeFunction); + +public: + static PromiseAnyRejectElementFunction* create(GlobalObject&, size_t, PromiseValueList&, PromiseCapability, RemainingElements&); + + explicit PromiseAnyRejectElementFunction(size_t, PromiseValueList&, PromiseCapability, RemainingElements&, Object& prototype); + virtual ~PromiseAnyRejectElementFunction() override = default; + +private: + virtual Value resolve_element() override; +}; + } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Promise/Promise.any.js b/Userland/Libraries/LibJS/Tests/builtins/Promise/Promise.any.js new file mode 100644 index 0000000000..904d2d7852 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Promise/Promise.any.js @@ -0,0 +1,147 @@ +test("length is 1", () => { + expect(Promise.any).toHaveLength(1); +}); + +describe("normal behavior", () => { + test("returns a Promise", () => { + const promise = Promise.any(); + expect(promise).toBeInstanceOf(Promise); + }); + + test("all resolve", () => { + const promise1 = Promise.resolve(3); + const promise2 = 42; + const promise3 = new Promise((resolve, reject) => { + resolve("foo"); + }); + + let resolvedValues = null; + let wasRejected = false; + + Promise.any([promise1, promise2, promise3]).then( + values => { + resolvedValues = values; + }, + () => { + wasRejected = true; + } + ); + + runQueuedPromiseJobs(); + expect(resolvedValues).toBe(3); + expect(wasRejected).toBeFalse(); + }); + + test("last resolve", () => { + const promise1 = Promise.reject(3); + const promise2 = new Promise((resolve, reject) => { + resolve("foo"); + }); + + let resolvedValues = null; + let wasRejected = false; + + Promise.any([promise1, promise2]).then( + values => { + resolvedValues = values; + }, + () => { + wasRejected = true; + } + ); + + runQueuedPromiseJobs(); + expect(resolvedValues).toBe("foo"); + expect(wasRejected).toBeFalse(); + }); + + test("reject", () => { + const promise1 = Promise.reject(3); + const promise2 = new Promise((resolve, reject) => { + reject("foo"); + }); + + let rejectionReason = null; + let wasResolved = false; + + Promise.any([promise1, promise2]).then( + () => { + wasResolved = true; + }, + reason => { + rejectionReason = reason; + } + ); + + runQueuedPromiseJobs(); + expect(rejectionReason).toBeInstanceOf(AggregateError); + expect(wasResolved).toBeFalse(); + }); +}); + +describe("exceptional behavior", () => { + test("cannot invoke capabilities executor twice", () => { + function fn() {} + + expect(() => { + function promise(executor) { + executor(fn, fn); + executor(fn, fn); + } + + Promise.any.call(promise, []); + }).toThrow(TypeError); + + expect(() => { + function promise(executor) { + executor(fn, undefined); + executor(fn, fn); + } + + Promise.any.call(promise, []); + }).toThrow(TypeError); + + expect(() => { + function promise(executor) { + executor(undefined, fn); + executor(fn, fn); + } + + Promise.any.call(promise, []); + }).toThrow(TypeError); + }); + + test("promise without resolve method", () => { + expect(() => { + function promise(executor) {} + Promise.any.call(promise, []); + }).toThrow(TypeError); + }); + + test("no parameters", () => { + let rejectionReason = null; + Promise.any().catch(reason => { + rejectionReason = reason; + }); + runQueuedPromiseJobs(); + expect(rejectionReason).toBeInstanceOf(TypeError); + }); + + test("non-iterable", () => { + let rejectionReason = null; + Promise.any(1).catch(reason => { + rejectionReason = reason; + }); + runQueuedPromiseJobs(); + expect(rejectionReason).toBeInstanceOf(TypeError); + }); + + test("empty list", () => { + let rejectionReason = null; + Promise.any([]).catch(reason => { + rejectionReason = reason; + }); + runQueuedPromiseJobs(); + expect(rejectionReason).toBeInstanceOf(AggregateError); + }); +}); |