diff --git a/Userland/Libraries/LibJS/Runtime/PromisePrototype.cpp b/Userland/Libraries/LibJS/Runtime/PromisePrototype.cpp index 3ff8a67bd0..f232d8c021 100644 --- a/Userland/Libraries/LibJS/Runtime/PromisePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/PromisePrototype.cpp @@ -73,12 +73,14 @@ JS_DEFINE_NATIVE_FUNCTION(PromisePrototype::finally) auto on_finally = vm.argument(0); // 1. Let promise be the this value. + auto promise = vm.this_value(global_object); + // 2. If Type(promise) is not Object, throw a TypeError exception. - // FIXME: Don't coerce to object! - auto* promise = TRY(vm.this_value(global_object).to_object(global_object)); + if (!promise.is_object()) + return vm.throw_completion(global_object, ErrorType::NotAnObject, promise.to_string_without_side_effects()); // 3. Let C be ? SpeciesConstructor(promise, %Promise%). - auto* constructor = TRY(species_constructor(global_object, *promise, *global_object.promise_constructor())); + auto* constructor = TRY(species_constructor(global_object, promise.as_object(), *global_object.promise_constructor())); // 4. Assert: IsConstructor(C) is true. VERIFY(constructor); @@ -150,7 +152,7 @@ JS_DEFINE_NATIVE_FUNCTION(PromisePrototype::finally) } // 7. Return ? Invoke(promise, "then", « thenFinally, catchFinally »). - return TRY(Value(promise).invoke(global_object, vm.names.then, then_finally, catch_finally)); + return TRY(promise.invoke(global_object, vm.names.then, then_finally, catch_finally)); } } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Promise/Promise.prototype.finally.js b/Userland/Libraries/LibJS/Tests/builtins/Promise/Promise.prototype.finally.js index cdb7a09029..a062b44590 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Promise/Promise.prototype.finally.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Promise/Promise.prototype.finally.js @@ -52,3 +52,11 @@ describe("normal behavior", () => { expect(thenFinallyArg).not.toBe(catchFinallyArg); }); }); + +describe("errors", () => { + test("this value must be an object", () => { + expect(() => { + Promise.prototype.finally.call("foo"); + }).toThrowWithMessage(TypeError, "foo is not an object"); + }); +});