mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:47:37 +00:00
LibJS: Implement Temporal.Instant.prototype.toJSON()
This commit is contained in:
parent
463eb361ad
commit
c171aa40a8
3 changed files with 37 additions and 0 deletions
|
@ -42,6 +42,7 @@ void InstantPrototype::initialize(GlobalObject& global_object)
|
|||
define_native_function(vm.names.equals, equals, 1, attr);
|
||||
define_native_function(vm.names.toString, to_string, 0, attr);
|
||||
define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
|
||||
define_native_function(vm.names.toJSON, to_json, 0, attr);
|
||||
define_native_function(vm.names.valueOf, value_of, 0, attr);
|
||||
}
|
||||
|
||||
|
@ -354,6 +355,23 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_locale_string)
|
|||
return js_string(vm, *string);
|
||||
}
|
||||
|
||||
// 8.3.15 Temporal.Instant.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.tojson
|
||||
JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_json)
|
||||
{
|
||||
// 1. Let instant be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]).
|
||||
auto* instant = typed_this(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
// 3. Return ? TemporalInstantToString(instant, undefined, "auto").
|
||||
auto string = temporal_instant_to_string(global_object, *instant, js_undefined(), String { "auto"sv });
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
return js_string(vm, *string);
|
||||
}
|
||||
|
||||
// 8.3.16 Temporal.Instant.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.valueof
|
||||
JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::value_of)
|
||||
{
|
||||
|
|
|
@ -29,6 +29,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(equals);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_json);
|
||||
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 0", () => {
|
||||
expect(Temporal.Instant.prototype.toJSON).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const instant = new Temporal.Instant(1625614921123456789n);
|
||||
expect(instant.toJSON()).toBe("2021-07-06T23:42:01.123456789Z");
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.Instant object", () => {
|
||||
expect(() => {
|
||||
Temporal.Instant.prototype.toJSON.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not a Temporal.Instant");
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue