LibJS: Implement String.prototype[@@toPrimitive]()

This commit is contained in:
Linus Groh 2021-06-06 16:53:32 +01:00 committed by Andreas Kling
parent e9a0759b16
commit b661363dfe
Notes: sideshowbarker 2024-07-18 12:45:12 +09:00
3 changed files with 16 additions and 2 deletions

View File

@ -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));
}
}

View File

@ -23,6 +23,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(symbol_to_primitive);
};
}

View File

@ -0,0 +1,5 @@
test("basic functionality", () => {
const s = Symbol();
expect(s[Symbol.toPrimitive]("string")).toBe(s);
expect(s[Symbol.toPrimitive]("number")).toBe(s);
});