mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:57:34 +00:00
LibJS: Add Date.prototype.toJSON()
This commit is contained in:
parent
ed7eb403fe
commit
c4530e95f4
2 changed files with 15 additions and 5 deletions
|
@ -81,6 +81,7 @@ void DatePrototype::initialize(GlobalObject& global_object)
|
||||||
define_native_function(vm.names.toLocaleTimeString, to_locale_time_string, 0, attr);
|
define_native_function(vm.names.toLocaleTimeString, to_locale_time_string, 0, attr);
|
||||||
define_native_function(vm.names.toTimeString, to_time_string, 0, attr);
|
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.names.toString, to_string, 0, attr);
|
||||||
|
define_native_function(vm.names.toJSON, to_json, 1, attr);
|
||||||
|
|
||||||
define_native_function(vm.well_known_symbol_to_primitive(), symbol_to_primitive, 1, Attribute::Configurable);
|
define_native_function(vm.well_known_symbol_to_primitive(), symbol_to_primitive, 1, Attribute::Configurable);
|
||||||
|
|
||||||
|
@ -91,11 +92,6 @@ void DatePrototype::initialize(GlobalObject& global_object)
|
||||||
// The function object that is the initial value of Date.prototype.toGMTString
|
// The function object that is the initial value of Date.prototype.toGMTString
|
||||||
// is the same function object that is the initial value of Date.prototype.toUTCString.
|
// is the same function object that is the initial value of Date.prototype.toUTCString.
|
||||||
define_property(vm.names.toGMTString, get(vm.names.toUTCString), attr);
|
define_property(vm.names.toGMTString, get(vm.names.toUTCString), attr);
|
||||||
|
|
||||||
// toJSON() isn't quite an alias for toISOString():
|
|
||||||
// - it returns null instead of throwing RangeError
|
|
||||||
// - its .length is 1, not 0
|
|
||||||
// - it can be transferred to other prototypes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DatePrototype::~DatePrototype()
|
DatePrototype::~DatePrototype()
|
||||||
|
@ -764,6 +760,19 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_string)
|
||||||
return js_string(vm, move(string));
|
return js_string(vm, move(string));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_json)
|
||||||
|
{
|
||||||
|
auto* this_object = vm.this_value(global_object).to_object(global_object);
|
||||||
|
if (!this_object)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
auto time_value = Value(this_object).to_primitive(global_object, Value::PreferredType::Number);
|
||||||
|
if (time_value.is_number() && !time_value.is_finite_number())
|
||||||
|
return js_null();
|
||||||
|
|
||||||
|
return this_object->invoke(vm.names.toISOString);
|
||||||
|
}
|
||||||
|
|
||||||
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::symbol_to_primitive)
|
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::symbol_to_primitive)
|
||||||
{
|
{
|
||||||
auto this_value = vm.this_value(global_object);
|
auto this_value = vm.this_value(global_object);
|
||||||
|
|
|
@ -56,6 +56,7 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(to_locale_time_string);
|
JS_DECLARE_NATIVE_FUNCTION(to_locale_time_string);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(to_time_string);
|
JS_DECLARE_NATIVE_FUNCTION(to_time_string);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(to_json);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(symbol_to_primitive);
|
JS_DECLARE_NATIVE_FUNCTION(symbol_to_primitive);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue