From b661363dfe1c307bb7dfe06ce0acf609cd4c0fdb Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 6 Jun 2021 16:53:32 +0100 Subject: [PATCH] LibJS: Implement String.prototype[@@toPrimitive]() --- Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp | 12 ++++++++++-- Userland/Libraries/LibJS/Runtime/SymbolPrototype.h | 1 + .../Symbol/Symbol.prototype.@@toPrimitive.js | 5 +++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Symbol/Symbol.prototype.@@toPrimitive.js diff --git a/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp b/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp index b7e21e48773..d2a254a1a3d 100644 --- a/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Matthew Olsson + * Copyright (c) 2021, Linus Groh * * 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)); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/SymbolPrototype.h b/Userland/Libraries/LibJS/Runtime/SymbolPrototype.h index 3996c09c65e..90e9e87450d 100644 --- a/Userland/Libraries/LibJS/Runtime/SymbolPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/SymbolPrototype.h @@ -23,6 +23,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(value_of); + JS_DECLARE_NATIVE_FUNCTION(symbol_to_primitive); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Symbol/Symbol.prototype.@@toPrimitive.js b/Userland/Libraries/LibJS/Tests/builtins/Symbol/Symbol.prototype.@@toPrimitive.js new file mode 100644 index 00000000000..6539341c4e1 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Symbol/Symbol.prototype.@@toPrimitive.js @@ -0,0 +1,5 @@ +test("basic functionality", () => { + const s = Symbol(); + expect(s[Symbol.toPrimitive]("string")).toBe(s); + expect(s[Symbol.toPrimitive]("number")).toBe(s); +});