diff --git a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h index db65922626..af2b0a9d79 100644 --- a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h +++ b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h @@ -54,7 +54,6 @@ M(NotASymbol, "{} is not a symbol") \ M(NotIterable, "{} is not iterable") \ M(NotObjectCoercible, "{} cannot be converted to an object") \ - M(NumberIncompatibleThis, "Number.prototype.{}() called with incompatible this target") \ M(ObjectDefinePropertyReturnedFalse, "Object's [[DefineProperty]] method returned false") \ M(ObjectFreezeFailed, "Could not freeze object") \ M(ObjectSealFailed, "Could not seal object") \ diff --git a/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp b/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp index 568275dee4..fcf42d8721 100644 --- a/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp @@ -42,22 +42,21 @@ NumberPrototype::~NumberPrototype() { } -// https://tc39.es/ecma262/#thisnumbervalue -static Value this_number_value(GlobalObject& global_object, StringView const& name) +// thisNumberValue, https://tc39.es/ecma262/#thisnumbervalue +static Value this_number_value(GlobalObject& global_object, Value value) { + if (value.is_number()) + return value; + if (value.is_object() && is(value.as_object())) + return static_cast(value.as_object()).value_of(); auto& vm = global_object.vm(); - auto this_value = vm.this_value(global_object); - if (this_value.is_number()) - return this_value; - if (this_value.is_object() && is(this_value.as_object())) - return static_cast(this_value.as_object()).value_of(); - vm.throw_exception(global_object, ErrorType::NumberIncompatibleThis, name); + vm.throw_exception(global_object, ErrorType::NotA, "Number"); return {}; } JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string) { - auto number_value = this_number_value(global_object, "toString"); + auto number_value = this_number_value(global_object, vm.this_value(global_object)); if (vm.exception()) return {}; @@ -134,7 +133,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string) JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::value_of) { - return this_number_value(global_object, "valueOf"); + return this_number_value(global_object, vm.this_value(global_object)); } } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toString.js b/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toString.js index da478dcf08..7ebc4fb748 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toString.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toString.js @@ -77,7 +77,7 @@ test("errors", () => { [true, [], {}, Symbol("foo"), "bar", 1n].forEach(value => { expect(() => Number.prototype.toString.call(value)).toThrowWithMessage( TypeError, - "Number.prototype.toString() called with incompatible this target" + "Not a Number object" ); }); }); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.valueOf.js b/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.valueOf.js index eb9cae2c59..7871a1ed43 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.valueOf.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.valueOf.js @@ -14,7 +14,7 @@ test("errors", () => { [true, [], {}, Symbol("foo"), "bar", 1n].forEach(value => { expect(() => Number.prototype.valueOf.call(value)).toThrowWithMessage( TypeError, - "Number.prototype.valueOf() called with incompatible this target" + "Not a Number object" ); }); });