Skip to content

Commit 5ceba18

Browse files
sebastianjacmattaapoalas
authored andcommitted
added data.rs
1 parent 74512b7 commit 5ceba18

File tree

5 files changed

+109
-62
lines changed

5 files changed

+109
-62
lines changed

nova_vm/src/ecmascript/builtins/ordinary.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ pub(crate) mod caches;
66
pub mod shape;
77

88
use std::{
9-
collections::{TryReserveError, hash_map::Entry},
10-
ops::ControlFlow,
11-
vec,
9+
collections::{hash_map::Entry, TryReserveError}, ops::ControlFlow, time::Instant, vec
1210
};
1311

1412
use caches::{CacheToPopulate, Caches, PropertyLookupCache, PropertyOffset};
@@ -30,9 +28,7 @@ use crate::{
3028
},
3129
},
3230
engine::{
33-
Scoped,
34-
context::{Bindable, GcScope, NoGcScope},
35-
rootable::Scopable,
31+
context::{Bindable, GcScope, NoGcScope}, rootable::Scopable, Scoped
3632
},
3733
heap::element_array::{ElementStorageRef, PropertyStorageRef},
3834
};
@@ -1680,6 +1676,11 @@ pub(crate) fn ordinary_object_create_with_intrinsics<'a>(
16801676
.heap
16811677
.create(ErrorHeapData::new(ExceptionType::SyntaxError, None, None))
16821678
.into_object(),
1679+
#[cfg(feature = "temporal")]
1680+
ProtoIntrinsics::TemporalInstant => agent
1681+
.heap
1682+
.create(InstantHeapData::default())
1683+
.into_object(),
16831684
ProtoIntrinsics::TypeError => agent
16841685
.heap
16851686
.create(ErrorHeapData::new(ExceptionType::TypeError, None, None))

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

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
use core::ops::{Index, IndexMut};
22

3+
pub(crate) mod data;
4+
35
use crate::{
46
ecmascript::{
57
builders::{builtin_function_builder::BuiltinFunctionBuilder, ordinary_object_builder::OrdinaryObjectBuilder},
68
builtins::{
79
ArgumentsList, Behaviour, Builtin, BuiltinIntrinsicConstructor
810
},
9-
execution::{agent::Agent, JsResult, Realm},
11+
execution::{agent::Agent, JsResult, ProtoIntrinsics, Realm},
1012
types::{
1113
InternalMethods, InternalSlots, IntoObject, Object, OrdinaryObject, String, Value, BUILTIN_STRING_MEMORY
1214
},
1315
},
14-
engine::{context::{bindable_handle, GcScope, NoGcScope}, rootable::{HeapRootRef, Rootable}},
16+
engine::{context::{bindable_handle, Bindable, GcScope, NoGcScope}, rootable::{HeapRootData, HeapRootRef, Rootable}},
1517
heap::{indexes::BaseIndex, CompactionLists, CreateHeapData, Heap, HeapMarkAndSweep, HeapSweepWeakReference, IntrinsicConstructorIndexes, WorkQueues},
1618
};
1719
/// Constructor function object for %Temporal.Instant%.
@@ -58,36 +60,11 @@ impl InstantPrototype {
5860
.build();
5961
}
6062
}
61-
/// HEAP DATA -- Move to internal instant/data.rs
62-
#[derive(Debug, Clone, Copy)]
63-
pub(crate) struct InstantValue(/*TODO:BigInt*/);
64-
65-
impl InstantValue {
66-
// TODO
67-
}
68-
#[derive(Debug, Clone, Copy)]
69-
pub struct InstantHeapData<'a> {
70-
pub(crate) object_index: Option<OrdinaryObject<'a>>,
71-
pub(crate) date: InstantValue,
72-
}
73-
74-
impl InstantHeapData<'_> {
75-
// TODO
76-
}
7763

78-
bindable_handle!(InstantHeapData);
7964

80-
impl HeapMarkAndSweep for InstantHeapData<'static> {
81-
fn mark_values(&self, queues: &mut crate::heap::WorkQueues) {
82-
todo!()
83-
}
84-
fn sweep_values(&mut self, compactions: &crate::heap::CompactionLists) {
85-
todo!()
86-
}
87-
}
88-
89-
// HANDLES -- Keep public facing within instant.rs
65+
use self::data::InstantHeapData;
9066
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
67+
#[repr(transparent)]
9168
pub struct Instant<'a>(BaseIndex<'a, InstantHeapData<'static>>);
9269
impl Instant<'_> {
9370
//TODO
@@ -103,20 +80,20 @@ bindable_handle!(Instant);
10380

10481
impl<'a> From<Instant<'a>> for Value<'a> {
10582
fn from(value: Instant<'a>) -> Self {
106-
Value::Instant(value) // todo: add to value.rs
83+
Value::Instant(value)
10784
}
10885
}
10986
impl<'a> From<Instant<'a>> for Object<'a> {
11087
fn from(value: Instant<'a>) -> Self {
111-
Object::Instant(value) // todo: add to object.rs
88+
Object::Instant(value)
11289
}
11390
}
11491
impl<'a> TryFrom<Value<'a>> for Instant<'a> {
11592
type Error = ();
11693

11794
fn try_from(value: Value<'a>) -> Result<Self, ()> {
11895
match value {
119-
Value::Instant(idx) => Ok(idx), // todo: add to value.rs
96+
Value::Instant(idx) => Ok(idx),
12097
_ => Err(()),
12198
}
12299
}
@@ -125,66 +102,103 @@ impl<'a> TryFrom<Object<'a>> for Instant<'a> {
125102
type Error = ();
126103
fn try_from(object: Object<'a>) -> Result<Self, ()> {
127104
match object {
128-
Object::Instant(idx) => Ok(idx), // todo: add to object.rs
105+
Object::Instant(idx) => Ok(idx),
129106
_ => Err(()),
130107
}
131108
}
132109
}
133110

134-
// TODO impl trait bounds properly
135111
impl<'a> InternalSlots<'a> for Instant<'a> {
136-
// TODO: Add TemporalInstant to ProtoIntrinsics
137-
//const DEFAULT_PROTOTYPE: ProtoIntrinsics = ProtoIntrinsics::TemporalInstant;
112+
const DEFAULT_PROTOTYPE: ProtoIntrinsics = ProtoIntrinsics::TemporalInstant;
138113
fn get_backing_object(self, agent: &Agent) -> Option<OrdinaryObject<'static>> {
139-
todo!()
114+
agent[self].object_index // not implemented for `agent::Agent`
140115
}
141116
fn set_backing_object(self, agent: &mut Agent, backing_object: OrdinaryObject<'static>) {
142-
todo!()
117+
assert!(agent[self].object_index.replace(backing_object).is_none()); // not implemented for `agent::Agent`
143118
}
144119

145120
}
146121

147122
impl<'a> InternalMethods<'a> for Instant<'a> {}
148123

149-
impl HeapMarkAndSweep for Instant<'static> {
150-
fn mark_values(&self, queues: &mut WorkQueues) {
151-
todo!()
124+
impl Index<Instant<'_>> for Agent {
125+
type Output = InstantHeapData<'static>;
126+
127+
fn index(&self, index: Instant<'_>) -> &Self::Output {
128+
&self.heap.instants[index]
152129
}
153-
fn sweep_values(&mut self, compactions: &CompactionLists) {
154-
todo!()
130+
}
131+
132+
impl IndexMut<Instant<'_>> for Agent {
133+
fn index_mut(&mut self, index: Instant) -> &mut Self::Output {
134+
&mut self.heap.instants[index]
155135
}
156136
}
157137

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)
138+
impl Index<Instant<'_>> for Vec<Option<InstantHeapData<'static>>> {
139+
type Output = InstantHeapData<'static>;
140+
141+
fn index(&self, index: Instant<'_>) -> &Self::Output {
142+
self.get(index.get_index())
143+
.expect("heap access out of bounds")
144+
.as_ref()
145+
.expect("")
161146
}
162147
}
163148

164-
impl<'a> CreateHeapData<InstantHeapData<'a>, Instant<'a>> for Heap {
165-
fn create(&mut self, data: InstantHeapData<'a>) -> Instant<'a> {
166-
todo!()
149+
impl IndexMut<Instant<'_>> for Vec<Option<InstantHeapData<'static>>> {
150+
fn index_mut(&mut self, index: Instant<'_>) -> &mut Self::Output {
151+
self.get_mut(index.get_index())
152+
.expect("dasdas")
153+
.as_mut()
154+
.expect("")
167155
}
168156
}
169157

170-
/* todo - impl keep public facing in temporal/instant.rs
158+
171159
impl Rootable for Instant<'_> {
172160
type RootRepr = HeapRootRef;
173161

174162
fn to_root_repr(value: Self) -> Result<Self::RootRepr, crate::engine::rootable::HeapRootData> {
175-
todo!()
163+
Err(HeapRootData::Instant(value.unbind()))
176164
}
177165

178166
fn from_root_repr(value: &Self::RootRepr) -> Result<Self, crate::engine::rootable::HeapRootRef> {
179-
todo!()
167+
Err(*value)
180168
}
181169

182170
fn from_heap_ref(heap_ref: crate::engine::rootable::HeapRootRef) -> Self::RootRepr {
183-
todo!()
171+
heap_ref
184172
}
185173

186174
fn from_heap_data(heap_data: crate::engine::rootable::HeapRootData) -> Option<Self> {
187-
todo!()
175+
match heap_data {
176+
HeapRootData::Instant(object) => Some(object),
177+
_ => None,
178+
}
179+
}
180+
}
181+
182+
183+
impl HeapMarkAndSweep for Instant<'static> {
184+
fn mark_values(&self, queues: &mut WorkQueues) {
185+
queues.instants.push(*self);
186+
}
187+
fn sweep_values(&mut self, compactions: &CompactionLists) {
188+
compactions.instants.shift_index(&mut self.0);
189+
}
190+
}
191+
192+
impl HeapSweepWeakReference for Instant<'static> {
193+
fn sweep_weak_reference(self, compactions: &CompactionLists) -> Option<Self> {
194+
compactions.dates.shift_weak_index(self.0).map(Self)
195+
}
196+
}
197+
198+
impl<'a> CreateHeapData<InstantHeapData<'a>, Instant<'a>> for Heap {
199+
fn create(&mut self, data: InstantHeapData<'a>) -> Instant<'a> {
200+
self.instants.push(Some(data.unbind()));
201+
self.alloc_counter += core::mem::size_of::<Option<InstantHeapData<'static>>>();
202+
Instant(BaseIndex::last(&self.instants))
188203
}
189204
}
190-
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
use crate::{ecmascript::types::{OrdinaryObject,bigint::BigInt}, engine::context::bindable_handle, heap::HeapMarkAndSweep};
3+
4+
#[derive(Debug, Clone, Copy)]
5+
pub struct InstantHeapData<'a> {
6+
pub(crate) object_index: Option<OrdinaryObject<'a>>,
7+
pub(crate) instant: BigInt<'a>,
8+
}
9+
10+
impl InstantHeapData<'_> {
11+
pub fn default() -> Self {
12+
Self {
13+
object_index: None,
14+
instant: BigInt::zero(),
15+
}
16+
}
17+
}
18+
19+
bindable_handle!(InstantHeapData);
20+
21+
impl HeapMarkAndSweep for InstantHeapData<'static> {
22+
fn mark_values(&self, queues: &mut crate::heap::WorkQueues) {
23+
todo!()
24+
}
25+
fn sweep_values(&mut self, compactions: &crate::heap::CompactionLists) {
26+
todo!()
27+
}
28+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ pub enum ProtoIntrinsics {
218218
RegExpStringIterator,
219219
Symbol,
220220
SyntaxError,
221+
#[cfg(feature = "temporal")]
222+
TemporalInstant,
221223
TypeError,
222224
#[cfg(feature = "array-buffer")]
223225
Uint16Array,
@@ -415,6 +417,7 @@ impl Intrinsics {
415417
ProtoIntrinsics::String => self.string().into(),
416418
ProtoIntrinsics::Symbol => self.symbol().into(),
417419
ProtoIntrinsics::SyntaxError => self.syntax_error().into(),
420+
ProtoIntrinsics::TemporalInstant => self.temporal_instant().into(),
418421
ProtoIntrinsics::TypeError => self.type_error().into(),
419422
ProtoIntrinsics::URIError => self.uri_error().into(),
420423
ProtoIntrinsics::AggregateError => self.aggregate_error().into(),
@@ -504,6 +507,7 @@ impl Intrinsics {
504507
ProtoIntrinsics::String => self.string_prototype().into(),
505508
ProtoIntrinsics::Symbol => self.symbol_prototype().into(),
506509
ProtoIntrinsics::SyntaxError => self.syntax_error_prototype().into(),
510+
ProtoIntrinsics::TemporalInstant => self.temporal().into(),
507511
ProtoIntrinsics::TypeError => self.type_error_prototype().into(),
508512
ProtoIntrinsics::URIError => self.uri_error_prototype().into(),
509513
ProtoIntrinsics::AggregateError => self.aggregate_error_prototype().into(),

nova_vm/src/heap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::ecmascript::builtins::date::data::DateHeapData;
3232
#[cfg(feature = "shared-array-buffer")]
3333
use crate::ecmascript::builtins::shared_array_buffer::data::SharedArrayBufferRecord;
3434
#[cfg(feature = "temporal")]
35-
use crate::ecmascript::builtins::temporal::instant::InstantHeapData;
35+
use crate::ecmascript::builtins::temporal::instant::data::InstantHeapData;
3636
#[cfg(feature = "array-buffer")]
3737
use crate::ecmascript::builtins::{
3838
ArrayBuffer, ArrayBufferHeapData,

0 commit comments

Comments
 (0)