Skip to content

Commit db6b7c0

Browse files
added data.rs
1 parent 5b80e6d commit db6b7c0

File tree

5 files changed

+114
-67
lines changed

5 files changed

+114
-67
lines changed

nova_vm/src/ecmascript/builtins/ordinary.rs

Lines changed: 12 additions & 11 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};
@@ -17,16 +15,12 @@ use crate::{
1715
ecmascript::{
1816
abstract_operations::operations_on_objects::{
1917
try_create_data_property, try_get, try_get_function_realm,
20-
},
21-
execution::agent::{TryError, TryResult, unwrap_try},
22-
types::{
23-
IntoValue, SetCachedProps, SetResult, TryGetResult, TryHasResult, handle_try_get_result,
24-
},
18+
}, builtins::temporal::instant::data::InstantHeapData, execution::agent::{unwrap_try, TryError, TryResult}, types::{
19+
handle_try_get_result, IntoValue, SetCachedProps, SetResult, TryGetResult, TryHasResult
20+
}
2521
},
2622
engine::{
27-
Scoped,
28-
context::{Bindable, GcScope, NoGcScope},
29-
rootable::Scopable,
23+
context::{Bindable, GcScope, NoGcScope}, rootable::Scopable, Scoped
3024
},
3125
heap::element_array::{ElementStorageRef, PropertyStorageRef},
3226
};
@@ -1674,6 +1668,11 @@ pub(crate) fn ordinary_object_create_with_intrinsics<'a>(
16741668
.heap
16751669
.create(ErrorHeapData::new(ExceptionType::SyntaxError, None, None))
16761670
.into_object(),
1671+
#[cfg(feature = "temporal")]
1672+
ProtoIntrinsics::TemporalInstant => agent
1673+
.heap
1674+
.create(InstantHeapData::default())
1675+
.into_object(),
16771676
ProtoIntrinsics::TypeError => agent
16781677
.heap
16791678
.create(ErrorHeapData::new(ExceptionType::TypeError, None, None))
@@ -1952,6 +1951,8 @@ pub(crate) fn get_prototype_from_constructor<'a>(
19521951
ProtoIntrinsics::RegExpStringIterator => None,
19531952
ProtoIntrinsics::Symbol => Some(intrinsics.symbol().into_function()),
19541953
ProtoIntrinsics::SyntaxError => Some(intrinsics.syntax_error().into_function()),
1954+
#[cfg(feature = "temporal")]
1955+
ProtoIntrinsics::TemporalInstant => Some(intrinsics.type_error().into_function()),
19551956
ProtoIntrinsics::TypeError => Some(intrinsics.type_error().into_function()),
19561957
#[cfg(feature = "array-buffer")]
19571958
ProtoIntrinsics::Uint16Array => Some(intrinsics.uint16_array().into_function()),

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
@@ -216,6 +216,8 @@ pub enum ProtoIntrinsics {
216216
RegExpStringIterator,
217217
Symbol,
218218
SyntaxError,
219+
#[cfg(feature = "temporal")]
220+
TemporalInstant,
219221
TypeError,
220222
#[cfg(feature = "array-buffer")]
221223
Uint16Array,
@@ -413,6 +415,7 @@ impl Intrinsics {
413415
ProtoIntrinsics::String => self.string().into(),
414416
ProtoIntrinsics::Symbol => self.symbol().into(),
415417
ProtoIntrinsics::SyntaxError => self.syntax_error().into(),
418+
ProtoIntrinsics::TemporalInstant => self.temporal_instant().into(),
416419
ProtoIntrinsics::TypeError => self.type_error().into(),
417420
ProtoIntrinsics::URIError => self.uri_error().into(),
418421
ProtoIntrinsics::AggregateError => self.aggregate_error().into(),
@@ -500,6 +503,7 @@ impl Intrinsics {
500503
ProtoIntrinsics::String => self.string_prototype().into(),
501504
ProtoIntrinsics::Symbol => self.symbol_prototype().into(),
502505
ProtoIntrinsics::SyntaxError => self.syntax_error_prototype().into(),
506+
ProtoIntrinsics::TemporalInstant => self.temporal().into(),
503507
ProtoIntrinsics::TypeError => self.type_error_prototype().into(),
504508
ProtoIntrinsics::URIError => self.uri_error_prototype().into(),
505509
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)