Skip to content

Commit 887d884

Browse files
committed
implementation of Temporal.Instant.compare
1 parent 78c8329 commit 887d884

File tree

5 files changed

+69
-59
lines changed

5 files changed

+69
-59
lines changed

nova_vm/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ default = [
5252
"regexp",
5353
"set",
5454
"annex-b",
55-
"temporal",
55+
"temporal",
5656
]
5757
array-buffer = []
5858
atomics = ["array-buffer", "shared-array-buffer"]
@@ -64,7 +64,7 @@ shared-array-buffer = ["array-buffer"]
6464
weak-refs = []
6565
set = []
6666
typescript = []
67-
temporal = ["temporal_rs"]
67+
temporal = ["temporal_rs"]
6868

6969
# Enables features defined by [Annex B](https://tc39.es/ecma262/#sec-additional-ecmascript-features-for-web-browsers)
7070
annex-b = ["annex-b-string", "annex-b-global", "annex-b-date", "annex-b-regexp"]

nova_vm/src/ecmascript/builtins/temporal.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub mod instant;
77
use crate::{
88
ecmascript::{
99
builders::ordinary_object_builder::OrdinaryObjectBuilder,
10-
builtins::Builtin,
10+
builtins::{Builtin, temporal::instant::TemporalInstantConstructor},
1111
execution::{Agent, Realm},
1212
types::{BUILTIN_STRING_MEMORY, IntoValue},
1313
},
@@ -40,8 +40,8 @@ impl TemporalObject {
4040
builder
4141
.with_key(BUILTIN_STRING_MEMORY.Instant.into())
4242
.with_value(temporal_instant_constructor.into_value())
43-
.with_enumerable(instant::TemporalInstantConstructor::ENUMERABLE)
44-
.with_configurable(instant::TemporalInstantConstructor::CONFIGURABLE)
43+
.with_enumerable(TemporalInstantConstructor::ENUMERABLE)
44+
.with_configurable(TemporalInstantConstructor::CONFIGURABLE)
4545
.build()
4646
})
4747
.build();

nova_vm/src/ecmascript/builtins/temporal/instant.rs

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,61 @@ use crate::{
3535
IntrinsicConstructorIndexes, WorkQueues, indexes::BaseIndex,
3636
},
3737
};
38+
3839
/// Constructor function object for %Temporal.Instant%.
3940
pub(crate) struct TemporalInstantConstructor;
41+
4042
impl Builtin for TemporalInstantConstructor {
4143
const NAME: String<'static> = BUILTIN_STRING_MEMORY.Instant;
4244
const LENGTH: u8 = 1;
43-
const BEHAVIOUR: Behaviour = Behaviour::Constructor(TemporalInstantConstructor::construct);
45+
const BEHAVIOUR: Behaviour = Behaviour::Constructor(TemporalInstantConstructor::constructor);
4446
}
47+
4548
impl BuiltinIntrinsicConstructor for TemporalInstantConstructor {
4649
const INDEX: IntrinsicConstructorIndexes = IntrinsicConstructorIndexes::TemporalInstant;
4750
}
4851

52+
struct TemporalInstantFrom;
53+
impl Builtin for TemporalInstantFrom {
54+
const NAME: String<'static> = BUILTIN_STRING_MEMORY.from;
55+
56+
const LENGTH: u8 = 1;
57+
58+
const BEHAVIOUR: Behaviour = Behaviour::Regular(TemporalInstantConstructor::from);
59+
}
60+
61+
struct TemporalInstantFromEpochMilliseconds;
62+
impl Builtin for TemporalInstantFromEpochMilliseconds {
63+
const NAME: String<'static> = BUILTIN_STRING_MEMORY.fromEpochMilliseconds;
64+
65+
const LENGTH: u8 = 1;
66+
67+
const BEHAVIOUR: Behaviour =
68+
Behaviour::Regular(TemporalInstantConstructor::from_epoch_milliseconds);
69+
}
70+
71+
struct TemporalInstantFromEpochNanoseconds;
72+
impl Builtin for TemporalInstantFromEpochNanoseconds {
73+
const NAME: String<'static> = BUILTIN_STRING_MEMORY.fromEpochNanoseconds;
74+
75+
const LENGTH: u8 = 1;
76+
77+
const BEHAVIOUR: Behaviour =
78+
Behaviour::Regular(TemporalInstantConstructor::from_epoch_nanoseconds);
79+
}
80+
81+
struct TemporalInstantCompare;
82+
impl Builtin for TemporalInstantCompare {
83+
const NAME: String<'static> = BUILTIN_STRING_MEMORY.compare;
84+
85+
const LENGTH: u8 = 2;
86+
87+
const BEHAVIOUR: Behaviour = Behaviour::Regular(TemporalInstantConstructor::compare);
88+
}
89+
4990
impl TemporalInstantConstructor {
5091
/// ### [8.1.1 Temporal.Instant ( epochNanoseconds )](https://tc39.es/proposal-temporal/#sec-temporal.instant)
51-
fn construct<'gc>(
92+
fn constructor<'gc>(
5293
agent: &mut Agent,
5394
_: Value,
5495
args: ArgumentsList,
@@ -101,10 +142,10 @@ impl TemporalInstantConstructor {
101142
fn from<'gc>(
102143
agent: &mut Agent,
103144
_this_value: Value,
104-
arguments: ArgumentsList,
145+
args: ArgumentsList,
105146
gc: GcScope<'gc, '_>,
106147
) -> JsResult<'gc, Value<'gc>> {
107-
let item = arguments.get(0).bind(gc.nogc());
148+
let item = args.get(0).bind(gc.nogc());
108149
// 1. Return ? ToTemporalInstant(item).
109150
let instant = to_temporal_instant(agent, item.unbind(), gc)?;
110151
let instant = agent.heap.create(InstantRecord {
@@ -118,10 +159,10 @@ impl TemporalInstantConstructor {
118159
fn from_epoch_milliseconds<'gc>(
119160
agent: &mut Agent,
120161
_this_value: Value,
121-
arguments: ArgumentsList,
162+
args: ArgumentsList,
122163
mut gc: GcScope<'gc, '_>,
123164
) -> JsResult<'gc, Value<'gc>> {
124-
let epoch_ms = arguments.get(0).bind(gc.nogc());
165+
let epoch_ms = args.get(0).bind(gc.nogc());
125166
// 1. Set epochMilliseconds to ? ToNumber(epochMilliseconds).
126167
let epoch_ms_number = epoch_ms
127168
.unbind()
@@ -190,19 +231,23 @@ impl TemporalInstantConstructor {
190231
Ok(value)
191232
}
192233

193-
/// [8.2.5 Temporal.Instant.compare ( one, two )](https://tc39.es/proposal-temporal/#sec-temporal.instant.compare)
234+
/// ### [8.2.5 Temporal.Instant.compare ( one, two )](https://tc39.es/proposal-temporal/#sec-temporal.instant.compare)
194235
fn compare<'gc>(
195236
agent: &mut Agent,
196237
_this_value: Value,
197-
arguments: ArgumentsList,
238+
args: ArgumentsList,
198239
mut gc: GcScope<'gc, '_>,
199240
) -> JsResult<'gc, Value<'gc>> {
241+
let one = args.get(0).bind(gc.nogc());
242+
let two = args.get(0).bind(gc.nogc());
243+
let two = two.scope(agent, gc.nogc());
200244
// 1. Set one to ? ToTemporalInstant(one).
201-
let one = arguments.get(0).bind(gc.nogc());
245+
let one_instant = to_temporal_instant(agent, one.unbind(), gc.reborrow()).unbind()?;
202246
// 2. Set two to ? ToTemporalInstant(two).
203-
let two = arguments.get(1).bind(gc.nogc());
247+
let two_value = two.get(agent).bind(gc.nogc());
248+
let two_instant = to_temporal_instant(agent, two_value.unbind(), gc.reborrow()).unbind()?;
204249
// 3. Return 𝔽(CompareEpochNanoseconds(one.[[EpochNanoseconds]], two.[[EpochNanoseconds]])).
205-
todo!()
250+
Ok((one_instant.cmp(&two_instant) as i8).into())
206251
}
207252

208253
pub(crate) fn create_intrinsic(agent: &mut Agent, realm: Realm<'static>, _gc: NoGcScope) {
@@ -308,44 +353,6 @@ fn to_temporal_instant<'gc>(
308353
/// %Temporal.Instant.Prototype%
309354
pub(crate) struct TemporalInstantPrototype;
310355

311-
struct TemporalInstantFrom;
312-
impl Builtin for TemporalInstantFrom {
313-
const NAME: String<'static> = BUILTIN_STRING_MEMORY.from;
314-
315-
const LENGTH: u8 = 1;
316-
317-
const BEHAVIOUR: Behaviour = Behaviour::Regular(TemporalInstantConstructor::from);
318-
}
319-
320-
struct TemporalInstantFromEpochMilliseconds;
321-
impl Builtin for TemporalInstantFromEpochMilliseconds {
322-
const NAME: String<'static> = BUILTIN_STRING_MEMORY.fromEpochMilliseconds;
323-
324-
const LENGTH: u8 = 1;
325-
326-
const BEHAVIOUR: Behaviour =
327-
Behaviour::Regular(TemporalInstantConstructor::from_epoch_milliseconds);
328-
}
329-
330-
struct TemporalInstantFromEpochNanoseconds;
331-
impl Builtin for TemporalInstantFromEpochNanoseconds {
332-
const NAME: String<'static> = BUILTIN_STRING_MEMORY.fromEpochNanoseconds;
333-
334-
const LENGTH: u8 = 1;
335-
336-
const BEHAVIOUR: Behaviour =
337-
Behaviour::Regular(TemporalInstantConstructor::from_epoch_nanoseconds);
338-
}
339-
340-
struct TemporalInstantCompare;
341-
impl Builtin for TemporalInstantCompare {
342-
const NAME: String<'static> = BUILTIN_STRING_MEMORY.compare;
343-
344-
const LENGTH: u8 = 2;
345-
346-
const BEHAVIOUR: Behaviour = Behaviour::Regular(TemporalInstantConstructor::compare);
347-
}
348-
349356
impl TemporalInstantPrototype {
350357
pub fn create_intrinsic(agent: &mut Agent, realm: Realm<'static>, _: NoGcScope) {
351358
let intrinsics = agent.get_realm_record_by_id(realm).intrinsics();

nova_vm/src/ecmascript/builtins/temporal/instant/data.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

5+
use crate::engine::context::NoGcScope;
56
use crate::{
67
ecmascript::types::OrdinaryObject,
7-
engine::context::bindable_handle,
8+
engine::context::{bindable_handle, trivially_bindable},
89
heap::{CompactionLists, HeapMarkAndSweep, WorkQueues},
910
};
1011

@@ -23,6 +24,7 @@ impl InstantRecord<'_> {
2324
}
2425
}
2526

27+
trivially_bindable!(temporal_rs::Instant);
2628
bindable_handle!(InstantRecord);
2729

2830
impl HeapMarkAndSweep for InstantRecord<'static> {

nova_vm/src/ecmascript/execution/realm/intrinsics.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,15 +1033,16 @@ impl Intrinsics {
10331033
IntrinsicObjectIndexes::TemporalObject.get_backing_object(self.object_index_base)
10341034
}
10351035

1036+
/// %Temporal.Instant.Prototype%
1037+
pub(crate) const fn temporal_instant_prototype(&self) -> OrdinaryObject<'static> {
1038+
IntrinsicObjectIndexes::TemporalInstantPrototype.get_backing_object(self.object_index_base)
1039+
}
1040+
10361041
/// %Temporal.Instant%
10371042
pub(crate) const fn temporal_instant(&self) -> BuiltinFunction<'static> {
10381043
IntrinsicConstructorIndexes::TemporalInstant
10391044
.get_builtin_function(self.builtin_function_index_base)
10401045
}
1041-
/// %Temporal.Instant.Prototype%
1042-
pub(crate) const fn temporal_instant_prototype(&self) -> OrdinaryObject<'static> {
1043-
IntrinsicObjectIndexes::TemporalInstantPrototype.get_backing_object(self.object_index_base)
1044-
}
10451046

10461047
/// %Number.prototype%
10471048
pub(crate) fn number_prototype(&self) -> PrimitiveObject<'static> {

0 commit comments

Comments
 (0)