1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:47:45 +00:00

LibJS: Add Date.prototype[@@toPrimitive]()

This commit is contained in:
Idan Horowitz 2021-06-06 17:37:48 +03:00 committed by Linus Groh
parent 60e70e5ee4
commit ed7eb403fe
3 changed files with 26 additions and 0 deletions

View file

@ -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<TypeError>(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<TypeError>(global_object, ErrorType::InvalidHint, hint);
return {};
}
return this_value.as_object().ordinary_to_primitive(try_first);
}
}

View file

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

View file

@ -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") \