Przeglądaj źródła

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

Idan Horowitz 4 lat temu
rodzic
commit
ed7eb403fe

+ 24 - 0
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<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);
+}
+
 }

+ 1 - 0
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);
 };
 
 }

+ 1 - 0
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")                                                                                               \