Skip to content

Commit 74512b7

Browse files
sebastianjacmattaapoalas
authored andcommitted
added engine and heap backing to %temporal.instant%
1 parent 5452a50 commit 74512b7

File tree

10 files changed

+163
-15
lines changed

10 files changed

+163
-15
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ sonic-rs = "0.3.17"
5050
unicode-normalization = "0.1.24"
5151
usdt = { git = "https://github.com/aapoalas/usdt.git", branch = "nova-aarch64-branch" }
5252
wtf8 = "0.1"
53-
temporal_rs = "0.0.16"
53+
temporal_rs = "0.1.0"
5454

5555
[workspace.metadata.dylint]
5656
libraries = [{ path = "nova_lint" }]

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

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
use core::ops::{Index, IndexMut};
2+
13
use crate::{
24
ecmascript::{
35
builders::{builtin_function_builder::BuiltinFunctionBuilder, ordinary_object_builder::OrdinaryObjectBuilder},
46
builtins::{
57
ArgumentsList, Behaviour, Builtin, BuiltinIntrinsicConstructor
68
},
7-
execution::{agent::{Agent}, JsResult, Realm},
9+
execution::{agent::Agent, JsResult, Realm},
810
types::{
9-
InternalSlots, IntoObject, Object, OrdinaryObject, String, Value, BUILTIN_STRING_MEMORY
11+
InternalMethods, InternalSlots, IntoObject, Object, OrdinaryObject, String, Value, BUILTIN_STRING_MEMORY
1012
},
1113
},
12-
engine::context::{bindable_handle, GcScope, NoGcScope},
13-
heap::{indexes::BaseIndex, CompactionLists, CreateHeapData, Heap, HeapMarkAndSweep, IntrinsicConstructorIndexes, WorkQueues},
14+
engine::{context::{bindable_handle, GcScope, NoGcScope}, rootable::{HeapRootRef, Rootable}},
15+
heap::{indexes::BaseIndex, CompactionLists, CreateHeapData, Heap, HeapMarkAndSweep, HeapSweepWeakReference, IntrinsicConstructorIndexes, WorkQueues},
1416
};
1517
/// Constructor function object for %Temporal.Instant%.
1618
pub(crate) struct InstantConstructor;
@@ -56,7 +58,7 @@ impl InstantPrototype {
5658
.build();
5759
}
5860
}
59-
/// HEAP DATA
61+
/// HEAP DATA -- Move to internal instant/data.rs
6062
#[derive(Debug, Clone, Copy)]
6163
pub(crate) struct InstantValue(/*TODO:BigInt*/);
6264

@@ -84,33 +86,48 @@ impl HeapMarkAndSweep for InstantHeapData<'static> {
8486
}
8587
}
8688

87-
// HANDLES
89+
// HANDLES -- Keep public facing within instant.rs
8890
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
8991
pub struct Instant<'a>(BaseIndex<'a, InstantHeapData<'static>>);
9092
impl Instant<'_> {
9193
//TODO
9294
pub(crate) const fn _def() -> Self {
93-
todo!()
95+
Instant(BaseIndex::from_u32_index(0))
96+
}
97+
98+
pub(crate) const fn get_index(self) -> usize {
99+
self.0.into_index()
94100
}
95101
}
96102
bindable_handle!(Instant);
97103

98104
impl<'a> From<Instant<'a>> for Value<'a> {
99-
fn from(v: Instant<'a>) -> Self { todo!() }
105+
fn from(value: Instant<'a>) -> Self {
106+
Value::Instant(value) // todo: add to value.rs
107+
}
100108
}
101109
impl<'a> From<Instant<'a>> for Object<'a> {
102-
fn from(v: Instant<'a>) -> Self { todo!() }
110+
fn from(value: Instant<'a>) -> Self {
111+
Object::Instant(value) // todo: add to object.rs
112+
}
103113
}
104114
impl<'a> TryFrom<Value<'a>> for Instant<'a> {
105115
type Error = ();
106-
fn try_from(v: Value<'a>) -> Result<Self, ()> {
107-
todo!()
116+
117+
fn try_from(value: Value<'a>) -> Result<Self, ()> {
118+
match value {
119+
Value::Instant(idx) => Ok(idx), // todo: add to value.rs
120+
_ => Err(()),
121+
}
108122
}
109123
}
110124
impl<'a> TryFrom<Object<'a>> for Instant<'a> {
111125
type Error = ();
112-
fn try_from(o: Object<'a>) -> Result<Self, ()> {
113-
todo!()
126+
fn try_from(object: Object<'a>) -> Result<Self, ()> {
127+
match object {
128+
Object::Instant(idx) => Ok(idx), // todo: add to object.rs
129+
_ => Err(()),
130+
}
114131
}
115132
}
116133

@@ -127,6 +144,8 @@ impl<'a> InternalSlots<'a> for Instant<'a> {
127144

128145
}
129146

147+
impl<'a> InternalMethods<'a> for Instant<'a> {}
148+
130149
impl HeapMarkAndSweep for Instant<'static> {
131150
fn mark_values(&self, queues: &mut WorkQueues) {
132151
todo!()
@@ -136,8 +155,36 @@ impl HeapMarkAndSweep for Instant<'static> {
136155
}
137156
}
138157

158+
impl HeapSweepWeakReference for Instant<'static> {
159+
fn sweep_weak_reference(self, compactions: &CompactionLists) -> Option<Self> {
160+
compactions.dates.shift_weak_index(self.0).map(Self)
161+
}
162+
}
163+
139164
impl<'a> CreateHeapData<InstantHeapData<'a>, Instant<'a>> for Heap {
140165
fn create(&mut self, data: InstantHeapData<'a>) -> Instant<'a> {
141166
todo!()
142167
}
143-
}
168+
}
169+
170+
/* todo - impl keep public facing in temporal/instant.rs
171+
impl Rootable for Instant<'_> {
172+
type RootRepr = HeapRootRef;
173+
174+
fn to_root_repr(value: Self) -> Result<Self::RootRepr, crate::engine::rootable::HeapRootData> {
175+
todo!()
176+
}
177+
178+
fn from_root_repr(value: &Self::RootRepr) -> Result<Self, crate::engine::rootable::HeapRootRef> {
179+
todo!()
180+
}
181+
182+
fn from_heap_ref(heap_ref: crate::engine::rootable::HeapRootRef) -> Self::RootRepr {
183+
todo!()
184+
}
185+
186+
fn from_heap_data(heap_data: crate::engine::rootable::HeapRootData) -> Option<Self> {
187+
todo!()
188+
}
189+
}
190+
*/

nova_vm/src/ecmascript/execution/weak_key.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ pub(crate) enum WeakKey<'a> {
141141
Array(Array<'a>) = ARRAY_DISCRIMINANT,
142142
#[cfg(feature = "date")]
143143
Date(Date<'a>) = DATE_DISCRIMINANT,
144+
#[cfg(feature = "temporal")]
145+
Instant(Instant<'a>) = INSTANT_DISCRIMINANT,
144146
Error(Error<'a>) = ERROR_DISCRIMINANT,
145147
FinalizationRegistry(FinalizationRegistry<'a>) = FINALIZATION_REGISTRY_DISCRIMINANT,
146148
Map(Map<'a>) = MAP_DISCRIMINANT,
@@ -253,6 +255,8 @@ impl<'a> From<WeakKey<'a>> for Value<'a> {
253255
WeakKey::Array(d) => Self::Array(d),
254256
#[cfg(feature = "date")]
255257
WeakKey::Date(d) => Self::Date(d),
258+
#[cfg(feature = "temporal")]
259+
WeakKey::Instant(d) => Self::Instant(d),
256260
WeakKey::Error(d) => Self::Error(d),
257261
WeakKey::FinalizationRegistry(d) => Self::FinalizationRegistry(d),
258262
WeakKey::Map(d) => Self::Map(d),
@@ -360,6 +364,8 @@ impl<'a> From<Object<'a>> for WeakKey<'a> {
360364
Object::Array(d) => Self::Array(d),
361365
#[cfg(feature = "date")]
362366
Object::Date(d) => Self::Date(d),
367+
#[cfg(feature = "temporal")]
368+
Object::Instant(d) => Self::Instant(d),
363369
Object::Error(d) => Self::Error(d),
364370
Object::FinalizationRegistry(d) => Self::FinalizationRegistry(d),
365371
Object::Map(d) => Self::Map(d),
@@ -471,6 +477,8 @@ impl<'a> TryFrom<WeakKey<'a>> for Object<'a> {
471477
WeakKey::Array(d) => Ok(Self::Array(d)),
472478
#[cfg(feature = "date")]
473479
WeakKey::Date(d) => Ok(Self::Date(d)),
480+
#[cfg(feature = "temporal")]
481+
WeakKey::Instant(d) => Ok(Self::Instant(d)),
474482
WeakKey::Error(d) => Ok(Self::Error(d)),
475483
WeakKey::FinalizationRegistry(d) => Ok(Self::FinalizationRegistry(d)),
476484
WeakKey::Map(d) => Ok(Self::Map(d)),
@@ -613,6 +621,8 @@ impl HeapMarkAndSweep for WeakKey<'static> {
613621
Self::Array(d) => d.mark_values(queues),
614622
#[cfg(feature = "date")]
615623
Self::Date(d) => d.mark_values(queues),
624+
#[cfg(feature = "temporal")]
625+
Self::Instant(d) => d.mark_values(queues),
616626
Self::Error(d) => d.mark_values(queues),
617627
Self::FinalizationRegistry(d) => d.mark_values(queues),
618628
Self::Map(d) => d.mark_values(queues),
@@ -718,6 +728,8 @@ impl HeapMarkAndSweep for WeakKey<'static> {
718728
Self::Array(d) => d.sweep_values(compactions),
719729
#[cfg(feature = "date")]
720730
Self::Date(d) => d.sweep_values(compactions),
731+
#[cfg(feature = "temporal")]
732+
Self::Instant(d) => d.sweep_values(compactions),
721733
Self::Error(d) => d.sweep_values(compactions),
722734
Self::FinalizationRegistry(d) => d.sweep_values(compactions),
723735
Self::Map(d) => d.sweep_values(compactions),
@@ -839,6 +851,8 @@ impl HeapSweepWeakReference for WeakKey<'static> {
839851
Self::Array(data) => data.sweep_weak_reference(compactions).map(Self::Array),
840852
#[cfg(feature = "date")]
841853
Self::Date(data) => data.sweep_weak_reference(compactions).map(Self::Date),
854+
#[cfg(feature = "temporal")]
855+
Self::Instant(data) => data.sweep_weak_reference(compactions).map(Self::Instant),
842856
Self::Error(data) => data.sweep_weak_reference(compactions).map(Self::Error),
843857
Self::FinalizationRegistry(data) => data
844858
.sweep_weak_reference(compactions)

nova_vm/src/ecmascript/types/language/object.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ pub enum Object<'a> {
179179
Array(Array<'a>) = ARRAY_DISCRIMINANT,
180180
#[cfg(feature = "date")]
181181
Date(Date<'a>) = DATE_DISCRIMINANT,
182+
Instant(Instant<'a>) = INSTANT_DISCRIMINANT,
182183
Error(Error<'a>) = ERROR_DISCRIMINANT,
183184
FinalizationRegistry(FinalizationRegistry<'a>) = FINALIZATION_REGISTRY_DISCRIMINANT,
184185
Map(Map<'a>) = MAP_DISCRIMINANT,
@@ -775,6 +776,8 @@ impl<'a> From<Object<'a>> for Value<'a> {
775776
Object::Array(data) => Self::Array(data),
776777
#[cfg(feature = "date")]
777778
Object::Date(data) => Self::Date(data),
779+
#[cfg(feature = "temporal")]
780+
Object::Instant(data) => Value::Instant(data),
778781
Object::Error(data) => Self::Error(data),
779782
Object::FinalizationRegistry(data) => Self::FinalizationRegistry(data),
780783
Object::Map(data) => Self::Map(data),
@@ -883,6 +886,8 @@ impl<'a> TryFrom<Value<'a>> for Object<'a> {
883886
Value::Array(x) => Ok(Self::from(x)),
884887
#[cfg(feature = "date")]
885888
Value::Date(x) => Ok(Self::Date(x)),
889+
#[cfg(feature = "temporal")]
890+
Value::Instant(x) => Ok(Self::Instant(x)),
886891
Value::Error(x) => Ok(Self::from(x)),
887892
Value::BoundFunction(x) => Ok(Self::from(x)),
888893
Value::BuiltinFunction(x) => Ok(Self::from(x)),
@@ -999,6 +1004,8 @@ macro_rules! object_delegate {
9991004
Self::Array(data) => data.$method($($arg),+),
10001005
#[cfg(feature = "date")]
10011006
Self::Date(data) => data.$method($($arg),+),
1007+
#[cfg(feature = "temporal")]
1008+
Object::Instant(data) => data.$method($($arg),+),
10021009
Self::Error(data) => data.$method($($arg),+),
10031010
Self::BoundFunction(data) => data.$method($($arg),+),
10041011
Self::BuiltinFunction(data) => data.$method($($arg),+),
@@ -1665,6 +1672,8 @@ impl TryFrom<HeapRootData> for Object<'_> {
16651672
HeapRootData::Array(array) => Ok(Self::Array(array)),
16661673
#[cfg(feature = "date")]
16671674
HeapRootData::Date(date) => Ok(Self::Date(date)),
1675+
#[cfg(feature = "temporal")]
1676+
HeapRootData::Instant(instant) => Ok(Self::Instant(instant)),
16681677
HeapRootData::Error(error) => Ok(Self::Error(error)),
16691678
HeapRootData::FinalizationRegistry(finalization_registry) => {
16701679
Ok(Self::FinalizationRegistry(finalization_registry))

nova_vm/src/ecmascript/types/language/value.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
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+
//#[cfg(feature = "temporal")]
6+
//use temporal_rs::Instant as Instant; // shadow regular instant, should probably change name to TemporalInstant
7+
58
use super::{
69
BigInt, BigIntHeapData, IntoValue, Number, Numeric, OrdinaryObject, Primitive, String,
710
StringHeapData, Symbol, bigint::HeapBigInt, number::HeapNumber, string::HeapString,
@@ -178,6 +181,8 @@ pub enum Value<'a> {
178181
Array(Array<'a>),
179182
#[cfg(feature = "date")]
180183
Date(Date<'a>),
184+
#[cfg(feature = "temporal")]
185+
Instant(Instant<'a>),
181186
Error(Error<'a>),
182187
FinalizationRegistry(FinalizationRegistry<'a>),
183188
Map(Map<'a>),
@@ -313,6 +318,8 @@ pub(crate) const OBJECT_DISCRIMINANT: u8 =
313318
pub(crate) const ARRAY_DISCRIMINANT: u8 = value_discriminant(Value::Array(Array::_def()));
314319
#[cfg(feature = "date")]
315320
pub(crate) const DATE_DISCRIMINANT: u8 = value_discriminant(Value::Date(Date::_def()));
321+
#[cfg(feature = "temporal")]
322+
pub(crate) const INSTANT_DISCRIMINANT: u8 = value_discriminant(Value::Instant(Instant::_def()));
316323
pub(crate) const ERROR_DISCRIMINANT: u8 = value_discriminant(Value::Error(Error::_def()));
317324
pub(crate) const BUILTIN_FUNCTION_DISCRIMINANT: u8 =
318325
value_discriminant(Value::BuiltinFunction(BuiltinFunction::_def()));
@@ -986,6 +993,8 @@ impl Rootable for Value<'_> {
986993
Self::Array(array) => Err(HeapRootData::Array(array.unbind())),
987994
#[cfg(feature = "date")]
988995
Self::Date(date) => Err(HeapRootData::Date(date.unbind())),
996+
#[cfg(feature = "temporal")]
997+
Self::Instant(instant) => Err(HeapRootData::Instant(instant.unbind())),
989998
Self::Error(error) => Err(HeapRootData::Error(error.unbind())),
990999
Self::FinalizationRegistry(finalization_registry) => Err(
9911000
HeapRootData::FinalizationRegistry(finalization_registry.unbind()),
@@ -1147,6 +1156,8 @@ impl Rootable for Value<'_> {
11471156
HeapRootData::Array(array) => Some(Self::Array(array)),
11481157
#[cfg(feature = "date")]
11491158
HeapRootData::Date(date) => Some(Self::Date(date)),
1159+
#[cfg(feature = "temporal")]
1160+
HeapRootData::Instant(instant) => Some(Self::Instant(instant)),
11501161
HeapRootData::Error(error) => Some(Self::Error(error)),
11511162
HeapRootData::FinalizationRegistry(finalization_registry) => {
11521163
Some(Self::FinalizationRegistry(finalization_registry))
@@ -1563,6 +1574,8 @@ fn map_object_to_static_string_repr(value: Value) -> String<'static> {
15631574
Object::SharedFloat16Array(_) => BUILTIN_STRING_MEMORY._object_Object_,
15641575
#[cfg(feature = "date")]
15651576
Object::Date(_) => BUILTIN_STRING_MEMORY._object_Object_,
1577+
#[cfg(feature = "temporal")]
1578+
Object::Instant(_) => BUILTIN_STRING_MEMORY._object_Object_,
15661579
#[cfg(feature = "set")]
15671580
Object::Set(_) | Object::SetIterator(_) => BUILTIN_STRING_MEMORY._object_Object_,
15681581
#[cfg(feature = "weak-refs")]

nova_vm/src/engine/bytecode/vm.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,8 @@ fn typeof_operator(agent: &Agent, val: Value, gc: NoGcScope) -> String<'static>
13261326
Value::SharedFloat16Array(_) => BUILTIN_STRING_MEMORY.object,
13271327
#[cfg(feature = "date")]
13281328
Value::Date(_) => BUILTIN_STRING_MEMORY.object,
1329+
#[cfg(feature = "temporal")]
1330+
Value::Instant(_) => BUILTIN_STRING_MEMORY.object,
13291331
// 13. If val has a [[Call]] internal slot, return "function".
13301332
Value::BoundFunction(_) | Value::BuiltinFunction(_) | Value::ECMAScriptFunction(_) |
13311333
Value::BuiltinConstructorFunction(_) |

nova_vm/src/engine/rootable.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ pub mod private {
229229
impl RootableSealed for BuiltinPromiseFinallyFunction<'_> {}
230230
#[cfg(feature = "date")]
231231
impl RootableSealed for Date<'_> {}
232+
#[cfg(feature = "temporal")]
233+
impl RootableSealed for Instant<'_> {}
232234
impl RootableSealed for ECMAScriptFunction<'_> {}
233235
impl RootableSealed for EmbedderObject<'_> {}
234236
impl RootableSealed for Error<'_> {}
@@ -543,6 +545,8 @@ pub enum HeapRootData {
543545
Array(Array<'static>) = ARRAY_DISCRIMINANT,
544546
#[cfg(feature = "date")]
545547
Date(Date<'static>) = DATE_DISCRIMINANT,
548+
#[cfg(feature = "temporal")]
549+
Instant(Instant<'static>) = INSTANT_DISCRIMINANT,
546550
Error(Error<'static>) = ERROR_DISCRIMINANT,
547551
FinalizationRegistry(FinalizationRegistry<'static>) = FINALIZATION_REGISTRY_DISCRIMINANT,
548552
Map(Map<'static>) = MAP_DISCRIMINANT,
@@ -676,6 +680,8 @@ impl From<Object<'static>> for HeapRootData {
676680
Object::Array(array) => Self::Array(array),
677681
#[cfg(feature = "date")]
678682
Object::Date(date) => Self::Date(date),
683+
#[cfg(feature = "temporal")]
684+
Object::Instant(instant) => Self::Instant(instant),
679685
Object::Error(error) => Self::Error(error),
680686
Object::FinalizationRegistry(finalization_registry) => {
681687
Self::FinalizationRegistry(finalization_registry)
@@ -835,6 +841,8 @@ impl HeapMarkAndSweep for HeapRootData {
835841
Self::Array(array) => array.mark_values(queues),
836842
#[cfg(feature = "date")]
837843
Self::Date(date) => date.mark_values(queues),
844+
#[cfg(feature = "temporal")]
845+
Self::Instant(instant) => instant.mark_values(queues),
838846
Self::Error(error) => error.mark_values(queues),
839847
Self::FinalizationRegistry(finalization_registry) => {
840848
finalization_registry.mark_values(queues)
@@ -984,6 +992,8 @@ impl HeapMarkAndSweep for HeapRootData {
984992
Self::Array(array) => array.sweep_values(compactions),
985993
#[cfg(feature = "date")]
986994
Self::Date(date) => date.sweep_values(compactions),
995+
#[cfg(feature = "temporal")]
996+
Self::Instant(date) => date.sweep_values(compactions),
987997
Self::Error(error) => error.sweep_values(compactions),
988998
Self::FinalizationRegistry(finalization_registry) => {
989999
finalization_registry.sweep_values(compactions)

nova_vm/src/heap.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub(crate) use self::object_entry::{ObjectEntry, ObjectEntryPropertyDescriptor};
3131
use crate::ecmascript::builtins::date::data::DateHeapData;
3232
#[cfg(feature = "shared-array-buffer")]
3333
use crate::ecmascript::builtins::shared_array_buffer::data::SharedArrayBufferRecord;
34+
#[cfg(feature = "temporal")]
35+
use crate::ecmascript::builtins::temporal::instant::InstantHeapData;
3436
#[cfg(feature = "array-buffer")]
3537
use crate::ecmascript::builtins::{
3638
ArrayBuffer, ArrayBufferHeapData,
@@ -141,6 +143,8 @@ pub(crate) struct Heap {
141143
pub(crate) caches: Caches<'static>,
142144
#[cfg(feature = "date")]
143145
pub(crate) dates: Vec<Option<DateHeapData<'static>>>,
146+
#[cfg(feature = "temporal")]
147+
pub(crate) instants: Vec<Option<InstantHeapData<'static>>>,
144148
pub(crate) ecmascript_functions: Vec<Option<ECMAScriptFunctionHeapData<'static>>>,
145149
/// ElementsArrays is where all keys and values arrays live;
146150
/// Element arrays are static arrays of Values plus
@@ -288,6 +292,8 @@ impl Heap {
288292
caches: Caches::with_capacity(1024),
289293
#[cfg(feature = "date")]
290294
dates: Vec::with_capacity(1024),
295+
#[cfg(feature = "temporal")]
296+
instants: Vec::with_capacity(1024), // todo: assign appropriate value for instants
291297
ecmascript_functions: Vec::with_capacity(1024),
292298
elements: ElementArrays {
293299
e2pow1: ElementArray2Pow1::with_capacity(1024),

0 commit comments

Comments
 (0)