mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:47:44 +00:00
LibJS: Implement Number.prototype.valueOf()
This commit is contained in:
parent
f09216ac42
commit
adc3de4480
5 changed files with 51 additions and 13 deletions
|
@ -53,7 +53,7 @@
|
|||
M(NotASymbol, "{} is not a symbol") \
|
||||
M(NotIterable, "{} is not iterable") \
|
||||
M(NonExtensibleDefine, "Cannot define property {} on non-extensible object") \
|
||||
M(NumberIncompatibleThis, "Number.prototype.{} method called with incompatible this target") \
|
||||
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") \
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -32,26 +33,33 @@ void NumberPrototype::initialize(GlobalObject& object)
|
|||
{
|
||||
auto& vm = this->vm();
|
||||
Object::initialize(object);
|
||||
define_native_function(vm.names.toString, to_string, 1, Attribute::Configurable | Attribute::Writable);
|
||||
u8 attr = Attribute::Configurable | Attribute::Writable;
|
||||
define_native_function(vm.names.toString, to_string, 1, attr);
|
||||
define_native_function(vm.names.valueOf, value_of, 0, attr);
|
||||
}
|
||||
|
||||
NumberPrototype::~NumberPrototype()
|
||||
{
|
||||
}
|
||||
|
||||
// https://tc39.es/ecma262/#thisnumbervalue
|
||||
static Value this_number_value(GlobalObject& global_object, StringView const& name)
|
||||
{
|
||||
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<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 {};
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string)
|
||||
{
|
||||
Value number_value;
|
||||
|
||||
auto this_value = vm.this_value(global_object);
|
||||
if (this_value.is_number()) {
|
||||
number_value = this_value;
|
||||
} else if (this_value.is_object() && is<NumberObject>(this_value.as_object())) {
|
||||
number_value = static_cast<NumberObject&>(this_value.as_object()).value_of();
|
||||
} else {
|
||||
vm.throw_exception<TypeError>(global_object, ErrorType::NumberIncompatibleThis, "toString");
|
||||
auto number_value = this_number_value(global_object, "toString");
|
||||
if (vm.exception())
|
||||
return {};
|
||||
}
|
||||
|
||||
int radix;
|
||||
auto argument = vm.argument(0);
|
||||
|
@ -123,4 +131,9 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string)
|
|||
return js_string(vm, String(characters.data(), characters.size()));
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::value_of)
|
||||
{
|
||||
return this_number_value(global_object, "valueOf");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ public:
|
|||
virtual ~NumberPrototype() override;
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -75,7 +75,10 @@ describe("correct behavior", () => {
|
|||
test("errors", () => {
|
||||
test("must be called with numeric |this|", () => {
|
||||
[true, [], {}, Symbol("foo"), "bar", 1n].forEach(value => {
|
||||
expect(() => Number.prototype.toString.call(value)).toThrow(TypeError);
|
||||
expect(() => Number.prototype.toString.call(value)).toThrowWithMessage(
|
||||
TypeError,
|
||||
"Number.prototype.toString() called with incompatible this target"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length", () => {
|
||||
expect(Number.prototype.valueOf).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
expect(new Number(42).valueOf()).toBe(42);
|
||||
expect(Number.prototype.valueOf.call(42)).toBe(42);
|
||||
});
|
||||
});
|
||||
|
||||
test("errors", () => {
|
||||
test("must be called with numeric |this|", () => {
|
||||
[true, [], {}, Symbol("foo"), "bar", 1n].forEach(value => {
|
||||
expect(() => Number.prototype.valueOf.call(value)).toThrowWithMessage(
|
||||
TypeError,
|
||||
"Number.prototype.valueOf() called with incompatible this target"
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue