mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 06:47:34 +00:00
LibJS: Implement String.prototype[@@toPrimitive]()
This commit is contained in:
parent
e9a0759b16
commit
b661363dfe
3 changed files with 16 additions and 2 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
|
||||
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -30,8 +31,8 @@ void SymbolPrototype::initialize(GlobalObject& global_object)
|
|||
define_native_property(vm.names.description, description_getter, {}, Attribute::Configurable);
|
||||
define_native_function(vm.names.toString, to_string, 0, Attribute::Writable | Attribute::Configurable);
|
||||
define_native_function(vm.names.valueOf, value_of, 0, Attribute::Writable | Attribute::Configurable);
|
||||
|
||||
define_property(global_object.vm().well_known_symbol_to_string_tag(), js_string(global_object.heap(), "Symbol"), Attribute::Configurable);
|
||||
define_native_function(vm.well_known_symbol_to_primitive(), symbol_to_primitive, Attribute::Configurable);
|
||||
define_property(vm.well_known_symbol_to_string_tag(), js_string(global_object.heap(), "Symbol"), Attribute::Configurable);
|
||||
}
|
||||
|
||||
SymbolPrototype::~SymbolPrototype()
|
||||
|
@ -70,4 +71,11 @@ JS_DEFINE_NATIVE_FUNCTION(SymbolPrototype::value_of)
|
|||
{
|
||||
return this_symbol_value(global_object, vm.this_value(global_object));
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(SymbolPrototype::symbol_to_primitive)
|
||||
{
|
||||
// The hint argument is ignored.
|
||||
return this_symbol_value(global_object, vm.this_value(global_object));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ private:
|
|||
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
||||
JS_DECLARE_NATIVE_FUNCTION(symbol_to_primitive);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
test("basic functionality", () => {
|
||||
const s = Symbol();
|
||||
expect(s[Symbol.toPrimitive]("string")).toBe(s);
|
||||
expect(s[Symbol.toPrimitive]("number")).toBe(s);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue