mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:57:35 +00:00
LibJS: Implement Temporal.Instant.prototype.epochSeconds
This commit is contained in:
parent
dad336c944
commit
5010e01223
4 changed files with 73 additions and 0 deletions
|
@ -113,6 +113,7 @@ namespace JS {
|
||||||
P(endsWith) \
|
P(endsWith) \
|
||||||
P(entries) \
|
P(entries) \
|
||||||
P(enumerable) \
|
P(enumerable) \
|
||||||
|
P(epochSeconds) \
|
||||||
P(error) \
|
P(error) \
|
||||||
P(errors) \
|
P(errors) \
|
||||||
P(escape) \
|
P(escape) \
|
||||||
|
|
|
@ -4,7 +4,9 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <LibCrypto/BigInt/SignedBigInteger.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
|
#include <LibJS/Runtime/Temporal/Instant.h>
|
||||||
#include <LibJS/Runtime/Temporal/InstantPrototype.h>
|
#include <LibJS/Runtime/Temporal/InstantPrototype.h>
|
||||||
|
|
||||||
namespace JS::Temporal {
|
namespace JS::Temporal {
|
||||||
|
@ -21,8 +23,42 @@ void InstantPrototype::initialize(GlobalObject& global_object)
|
||||||
|
|
||||||
auto& vm = this->vm();
|
auto& vm = this->vm();
|
||||||
|
|
||||||
|
define_native_accessor(vm.names.epochSeconds, epoch_seconds_getter, {}, Attribute::Configurable);
|
||||||
|
|
||||||
// 8.3.2 Temporal.Instant.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype-@@tostringtag
|
// 8.3.2 Temporal.Instant.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype-@@tostringtag
|
||||||
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.Instant"), Attribute::Configurable);
|
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.Instant"), Attribute::Configurable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Instant* typed_this(GlobalObject& global_object)
|
||||||
|
{
|
||||||
|
auto& vm = global_object.vm();
|
||||||
|
auto* this_object = vm.this_value(global_object).to_object(global_object);
|
||||||
|
if (!this_object)
|
||||||
|
return {};
|
||||||
|
if (!is<Instant>(this_object)) {
|
||||||
|
vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Temporal.Instant");
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
return static_cast<Instant*>(this_object);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 8.3.3 get Temporal.Instant.prototype.epochSeconds, https://tc39.es/proposal-temporal/#sec-get-temporal.instant.prototype.epochseconds
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::epoch_seconds_getter)
|
||||||
|
{
|
||||||
|
// 1. Let instant be the this value.
|
||||||
|
// 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]).
|
||||||
|
auto* instant = typed_this(global_object);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 3. Let ns be instant.[[Nanoseconds]].
|
||||||
|
auto& ns = instant->nanoseconds();
|
||||||
|
|
||||||
|
// 4. Let s be RoundTowardsZero(ℝ(ns) / 10^9).
|
||||||
|
auto [s, _] = ns.big_integer().divided_by(Crypto::SignedBigInteger::create_from(1'000'000'000));
|
||||||
|
|
||||||
|
// 5. Return 𝔽(s).
|
||||||
|
return Value((double)s.to_base(10).to_int<i64>().value());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,9 @@ public:
|
||||||
explicit InstantPrototype(GlobalObject&);
|
explicit InstantPrototype(GlobalObject&);
|
||||||
virtual void initialize(GlobalObject&) override;
|
virtual void initialize(GlobalObject&) override;
|
||||||
virtual ~InstantPrototype() override = default;
|
virtual ~InstantPrototype() override = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(epoch_seconds_getter);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
describe("correct behavior", () => {
|
||||||
|
test("basic functionality", () => {
|
||||||
|
expect(new Temporal.Instant(0n).epochSeconds).toBe(0);
|
||||||
|
expect(new Temporal.Instant(1n).epochSeconds).toBe(0);
|
||||||
|
expect(new Temporal.Instant(999_999_999n).epochSeconds).toBe(0);
|
||||||
|
expect(new Temporal.Instant(1_000_000_000n).epochSeconds).toBe(1);
|
||||||
|
expect(new Temporal.Instant(1_500_000_000n).epochSeconds).toBe(1);
|
||||||
|
expect(new Temporal.Instant(1_999_999_999n).epochSeconds).toBe(1);
|
||||||
|
expect(new Temporal.Instant(2_000_000_000n).epochSeconds).toBe(2);
|
||||||
|
expect(new Temporal.Instant(8_640_000_000_000_000_000_000n).epochSeconds).toBe(
|
||||||
|
8_640_000_000_000
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(new Temporal.Instant(-0n).epochSeconds).toBe(0);
|
||||||
|
expect(new Temporal.Instant(-1n).epochSeconds).toBe(0);
|
||||||
|
expect(new Temporal.Instant(-999_999_999n).epochSeconds).toBe(0);
|
||||||
|
expect(new Temporal.Instant(-1_000_000_000n).epochSeconds).toBe(-1);
|
||||||
|
expect(new Temporal.Instant(-1_500_000_000n).epochSeconds).toBe(-1);
|
||||||
|
expect(new Temporal.Instant(-1_999_999_999n).epochSeconds).toBe(-1);
|
||||||
|
expect(new Temporal.Instant(-2_000_000_000n).epochSeconds).toBe(-2);
|
||||||
|
expect(new Temporal.Instant(-8_640_000_000_000_000_000_000n).epochSeconds).toBe(
|
||||||
|
-8_640_000_000_000
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("errors", () => {
|
||||||
|
test("this value must be a Temporal.Instant object", () => {
|
||||||
|
expect(() => {
|
||||||
|
Reflect.get(Temporal.Instant.prototype, "epochSeconds", "foo");
|
||||||
|
}).toThrowWithMessage(TypeError, "Not a Temporal.Instant");
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue