1+ pub ( crate ) mod data;
2+ pub mod duration_constructor;
3+ pub mod duration_prototype;
4+
15use crate :: {
26 ecmascript:: {
37 abstract_operations:: {
48 operations_on_objects:: get, type_conversion:: to_integer_if_integral,
59 } ,
6- execution:: { Agent , JsResult , agent:: ExceptionType } ,
7- types:: { BUILTIN_STRING_MEMORY , Object , String , Value } ,
10+ execution:: { Agent , JsResult , ProtoIntrinsics , agent:: ExceptionType } ,
11+ types:: {
12+ BUILTIN_STRING_MEMORY , InternalMethods , InternalSlots , Object , OrdinaryObject , String ,
13+ Value ,
14+ } ,
815 } ,
916 engine:: {
1017 context:: { Bindable , GcScope , NoGcScope , bindable_handle} ,
11- rootable:: Scopable ,
18+ rootable:: { HeapRootData , HeapRootRef , Rootable , Scopable } ,
19+ } ,
20+ heap:: {
21+ CompactionLists , CreateHeapData , Heap , HeapMarkAndSweep , HeapSweepWeakReference ,
22+ WorkQueues , indexes:: BaseIndex ,
1223 } ,
13- heap:: indexes:: BaseIndex ,
1424} ;
1525use core:: ops:: { Index , IndexMut } ;
1626
17- pub ( crate ) mod data;
1827use self :: data:: DurationHeapData ;
1928#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
2029#[ repr( transparent) ]
@@ -35,42 +44,49 @@ impl TemporalDuration<'_> {
3544bindable_handle ! ( TemporalDuration ) ;
3645
3746impl < ' a > From < TemporalDuration < ' a > > for Value < ' a > {
38- fn from ( _value : TemporalDuration < ' a > ) -> Self {
39- todo ! ( )
40- //Value::Duration(value)
47+ fn from ( value : TemporalDuration < ' a > ) -> Self {
48+ Value :: Duration ( value)
4149 }
4250}
4351impl < ' a > From < TemporalDuration < ' a > > for Object < ' a > {
44- fn from ( _value : TemporalDuration < ' a > ) -> Self {
45- todo ! ( )
46- //Object::Duration(value)
52+ fn from ( value : TemporalDuration < ' a > ) -> Self {
53+ Object :: Duration ( value)
4754 }
4855}
4956impl < ' a > TryFrom < Value < ' a > > for TemporalDuration < ' a > {
5057 type Error = ( ) ;
5158
52- fn try_from ( _value : Value < ' a > ) -> Result < Self , ( ) > {
53- todo ! ( )
54- // match value {
55- // Value::Duration(idx) => Ok(idx),
56- // _ => Err(()),
57- // }
59+ fn try_from ( value : Value < ' a > ) -> Result < Self , ( ) > {
60+ match value {
61+ Value :: Duration ( idx) => Ok ( idx) ,
62+ _ => Err ( ( ) ) ,
63+ }
64+ }
65+ }
66+
67+ impl < ' a > InternalSlots < ' a > for TemporalDuration < ' a > {
68+ const DEFAULT_PROTOTYPE : ProtoIntrinsics = ProtoIntrinsics :: TemporalDuration ;
69+ fn get_backing_object ( self , agent : & Agent ) -> Option < OrdinaryObject < ' static > > {
70+ agent[ self ] . object_index
71+ }
72+ fn set_backing_object ( self , agent : & mut Agent , backing_object : OrdinaryObject < ' static > ) {
73+ assert ! ( agent[ self ] . object_index. replace( backing_object) . is_none( ) ) ;
5874 }
5975}
6076
77+ impl < ' a > InternalMethods < ' a > for TemporalDuration < ' a > { }
78+
6179impl Index < TemporalDuration < ' _ > > for Agent {
6280 type Output = DurationHeapData < ' static > ;
6381
64- fn index ( & self , _index : TemporalDuration < ' _ > ) -> & Self :: Output {
65- unimplemented ! ( )
66- //&self.heap.durations[index]
82+ fn index ( & self , index : TemporalDuration < ' _ > ) -> & Self :: Output {
83+ & self . heap . durations [ index]
6784 }
6885}
6986
7087impl IndexMut < TemporalDuration < ' _ > > for Agent {
71- fn index_mut ( & mut self , _index : TemporalDuration ) -> & mut Self :: Output {
72- unimplemented ! ( )
73- //&mut self.heap.durations[index]
88+ fn index_mut ( & mut self , index : TemporalDuration ) -> & mut Self :: Output {
89+ & mut self . heap . durations [ index]
7490 }
7591}
7692
@@ -89,6 +105,52 @@ impl IndexMut<TemporalDuration<'_>> for Vec<DurationHeapData<'static>> {
89105 . expect ( "heap access out of bounds" )
90106 }
91107}
108+
109+ impl Rootable for TemporalDuration < ' _ > {
110+ type RootRepr = HeapRootRef ;
111+
112+ fn to_root_repr ( value : Self ) -> Result < Self :: RootRepr , HeapRootData > {
113+ Err ( HeapRootData :: Duration ( value. unbind ( ) ) )
114+ }
115+
116+ fn from_root_repr ( value : & Self :: RootRepr ) -> Result < Self , HeapRootRef > {
117+ Err ( * value)
118+ }
119+
120+ fn from_heap_ref ( heap_ref : HeapRootRef ) -> Self :: RootRepr {
121+ heap_ref
122+ }
123+
124+ fn from_heap_data ( heap_data : HeapRootData ) -> Option < Self > {
125+ match heap_data {
126+ HeapRootData :: Duration ( object) => Some ( object) ,
127+ _ => None ,
128+ }
129+ }
130+ }
131+
132+ impl HeapMarkAndSweep for TemporalDuration < ' static > {
133+ fn mark_values ( & self , queues : & mut WorkQueues ) {
134+ queues. durations . push ( * self ) ;
135+ }
136+ fn sweep_values ( & mut self , compactions : & CompactionLists ) {
137+ compactions. durations . shift_index ( & mut self . 0 ) ;
138+ }
139+ }
140+
141+ impl HeapSweepWeakReference for TemporalDuration < ' static > {
142+ fn sweep_weak_reference ( self , compactions : & CompactionLists ) -> Option < Self > {
143+ compactions. durations . shift_weak_index ( self . 0 ) . map ( Self )
144+ }
145+ }
146+
147+ impl < ' a > CreateHeapData < DurationHeapData < ' a > , TemporalDuration < ' a > > for Heap {
148+ fn create ( & mut self , data : DurationHeapData < ' a > ) -> TemporalDuration < ' a > {
149+ self . durations . push ( data. unbind ( ) ) ;
150+ self . alloc_counter += core:: mem:: size_of :: < DurationHeapData < ' static > > ( ) ;
151+ TemporalDuration ( BaseIndex :: last_t ( & self . durations ) )
152+ }
153+ }
92154/// 7.5.19 CreateTemporalDuration ( years, months, weeks,
93155/// days, hours, minutes, seconds,
94156/// milliseconds, microseconds, nanoseconds [ , newTarget ] )
@@ -144,7 +206,7 @@ pub(crate) fn create_temporal_duration<'gc>(// years,
144206pub ( crate ) fn to_temporal_duration < ' gc > (
145207 agent : & mut Agent ,
146208 item : Value ,
147- mut gc : GcScope < ' gc , ' _ > ,
209+ gc : GcScope < ' gc , ' _ > ,
148210) -> JsResult < ' gc , temporal_rs:: Duration > {
149211 let item = item. bind ( gc. nogc ( ) ) ;
150212 // 1. If item is an Object and item has an [[InitializedTemporalDuration]] internal slot, then
@@ -388,21 +450,19 @@ pub(crate) fn to_temporal_partial_duration_record<'gc>(
388450/// It returns a new Temporal.Duration instance that is the
389451/// negation of duration.
390452pub ( crate ) fn create_negated_temporal_duration < ' gc > (
391- agent : & mut Agent ,
392- item : temporal_rs:: Duration ,
393- mut gc : GcScope < ' gc , ' _ > ,
453+ _agent : & mut Agent ,
454+ _item : temporal_rs:: Duration ,
455+ mut _gc : GcScope < ' gc , ' _ > ,
394456) -> JsResult < ' gc , temporal_rs:: Duration > {
395457 // 1. Return ! CreateTemporalDuration(-duration.[[Years]], -duration.[[Months]], -duration.[[Weeks]], -duration.[[Days]], -duration.[[Hours]], -duration.[[Minutes]], -duration.[[Seconds]], -duration.[[Milliseconds]], -duration.[[Microseconds]], -duration.[[Nanoseconds]]).
396- let duration = temporal_rs:: Duration :: negated ( & item) ;
397- //TODO: IMPL create_temporal_duration()
398458 unimplemented ! ( )
399459}
400460
401461#[ inline( always) ]
402462fn require_internal_slot_temporal_duration < ' a > (
403- agent : & mut Agent ,
404- value : Value ,
405- gc : NoGcScope < ' a , ' _ > ,
463+ _agent : & mut Agent ,
464+ _value : Value ,
465+ _gc : NoGcScope < ' a , ' _ > ,
406466) -> JsResult < ' a , TemporalDuration < ' a > > {
407467 unimplemented ! ( )
408468 // TODO:
0 commit comments