mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:57:35 +00:00
LibJS: Update NumberPrototype's this_number_value() to take a Value
This is now about as close to the spec as it gets - instead of querying the |this| value inside of the function, we now pass it in from the outside. Also get rid of the oddly specific error messages, they're nice but pretty inconsistent with most others. Let's prefer consistency and simplicity for now. Other than that, no functionality change.
This commit is contained in:
parent
3cfd9f51f7
commit
d255e6892b
4 changed files with 11 additions and 13 deletions
|
@ -54,7 +54,6 @@
|
||||||
M(NotASymbol, "{} is not a symbol") \
|
M(NotASymbol, "{} is not a symbol") \
|
||||||
M(NotIterable, "{} is not iterable") \
|
M(NotIterable, "{} is not iterable") \
|
||||||
M(NotObjectCoercible, "{} cannot be converted to an object") \
|
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(ObjectDefinePropertyReturnedFalse, "Object's [[DefineProperty]] method returned false") \
|
||||||
M(ObjectFreezeFailed, "Could not freeze object") \
|
M(ObjectFreezeFailed, "Could not freeze object") \
|
||||||
M(ObjectSealFailed, "Could not seal object") \
|
M(ObjectSealFailed, "Could not seal object") \
|
||||||
|
|
|
@ -42,22 +42,21 @@ NumberPrototype::~NumberPrototype()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://tc39.es/ecma262/#thisnumbervalue
|
// thisNumberValue, https://tc39.es/ecma262/#thisnumbervalue
|
||||||
static Value this_number_value(GlobalObject& global_object, StringView const& name)
|
static Value this_number_value(GlobalObject& global_object, Value value)
|
||||||
{
|
{
|
||||||
|
if (value.is_number())
|
||||||
|
return value;
|
||||||
|
if (value.is_object() && is<NumberObject>(value.as_object()))
|
||||||
|
return static_cast<NumberObject&>(value.as_object()).value_of();
|
||||||
auto& vm = global_object.vm();
|
auto& vm = global_object.vm();
|
||||||
auto this_value = vm.this_value(global_object);
|
vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Number");
|
||||||
if (this_value.is_number())
|
|
||||||
return this_value;
|
|
||||||
if (this_value.is_object() && is<NumberObject>(this_value.as_object()))
|
|
||||||
return static_cast<NumberObject&>(this_value.as_object()).value_of();
|
|
||||||
vm.throw_exception<TypeError>(global_object, ErrorType::NumberIncompatibleThis, name);
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string)
|
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())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
|
@ -134,7 +133,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string)
|
||||||
|
|
||||||
JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::value_of)
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,7 +77,7 @@ test("errors", () => {
|
||||||
[true, [], {}, Symbol("foo"), "bar", 1n].forEach(value => {
|
[true, [], {}, Symbol("foo"), "bar", 1n].forEach(value => {
|
||||||
expect(() => Number.prototype.toString.call(value)).toThrowWithMessage(
|
expect(() => Number.prototype.toString.call(value)).toThrowWithMessage(
|
||||||
TypeError,
|
TypeError,
|
||||||
"Number.prototype.toString() called with incompatible this target"
|
"Not a Number object"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -14,7 +14,7 @@ test("errors", () => {
|
||||||
[true, [], {}, Symbol("foo"), "bar", 1n].forEach(value => {
|
[true, [], {}, Symbol("foo"), "bar", 1n].forEach(value => {
|
||||||
expect(() => Number.prototype.valueOf.call(value)).toThrowWithMessage(
|
expect(() => Number.prototype.valueOf.call(value)).toThrowWithMessage(
|
||||||
TypeError,
|
TypeError,
|
||||||
"Number.prototype.valueOf() called with incompatible this target"
|
"Not a Number object"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue