From ed7eb403fe197770a5d74d7f7ffa3366a5a0f9d9 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sun, 6 Jun 2021 17:37:48 +0300 Subject: [PATCH] LibJS: Add Date.prototype[@@toPrimitive]() --- .../Libraries/LibJS/Runtime/DatePrototype.cpp | 24 +++++++++++++++++++ .../Libraries/LibJS/Runtime/DatePrototype.h | 1 + Userland/Libraries/LibJS/Runtime/ErrorTypes.h | 1 + 3 files changed, 26 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp index 2b02851d4b..7d11f24e90 100644 --- a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -82,6 +82,8 @@ void DatePrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.toTimeString, to_time_string, 0, attr); define_native_function(vm.names.toString, to_string, 0, attr); + define_native_function(vm.well_known_symbol_to_primitive(), symbol_to_primitive, 1, Attribute::Configurable); + // Aliases. define_native_function(vm.names.valueOf, get_time, 0, attr); @@ -762,4 +764,26 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_string) return js_string(vm, move(string)); } +JS_DEFINE_NATIVE_FUNCTION(DatePrototype::symbol_to_primitive) +{ + auto this_value = vm.this_value(global_object); + if (!this_value.is_object()) { + vm.throw_exception(global_object, ErrorType::NotAnObject); + return {}; + } + auto hint = vm.argument(0).to_string(global_object); + if (vm.exception()) + return {}; + Value::PreferredType try_first; + if (hint == "string" || hint == "default") { + try_first = Value::PreferredType::String; + } else if (hint == "number") { + try_first = Value::PreferredType::Number; + } else { + vm.throw_exception(global_object, ErrorType::InvalidHint, hint); + return {}; + } + return this_value.as_object().ordinary_to_primitive(try_first); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/DatePrototype.h b/Userland/Libraries/LibJS/Runtime/DatePrototype.h index 93769f4791..9d9ec317e5 100644 --- a/Userland/Libraries/LibJS/Runtime/DatePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/DatePrototype.h @@ -56,6 +56,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(to_locale_time_string); JS_DECLARE_NATIVE_FUNCTION(to_time_string); JS_DECLARE_NATIVE_FUNCTION(to_string); + JS_DECLARE_NATIVE_FUNCTION(symbol_to_primitive); }; } diff --git a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h index af2b0a9d79..d35e150261 100644 --- a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h +++ b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h @@ -30,6 +30,7 @@ M(InOperatorWithObject, "'in' operator must be used on an object") \ M(InstanceOfOperatorBadPrototype, "'prototype' property of {} is not an object") \ M(InvalidAssignToConst, "Invalid assignment to const variable") \ + M(InvalidHint, "Invalid hint: \"{}\"") \ M(InvalidIndex, "Index must be a positive integer") \ M(InvalidLeftHandAssignment, "Invalid left-hand side in assignment") \ M(InvalidLength, "Invalid {} length") \