@@ -140,7 +140,7 @@ impl TemporalInstantConstructor {
140140 let item = args. get ( 0 ) . bind ( gc. nogc ( ) ) ;
141141 // 1. Return ? ToTemporalInstant(item).
142142 let instant = to_temporal_instant ( agent, item. unbind ( ) , gc) ?;
143- let instant = agent. heap . create ( InstantRecord {
143+ let instant = agent. heap . create ( InstantHeapData {
144144 object_index : None ,
145145 instant,
146146 } ) ;
@@ -345,6 +345,22 @@ fn to_temporal_instant<'gc>(
345345/// %Temporal.Instant.Prototype%
346346pub ( crate ) struct TemporalInstantPrototype ;
347347
348+ struct TemporalInstantPrototypeGetEpochMilliseconds ;
349+ impl Builtin for TemporalInstantPrototypeGetEpochMilliseconds {
350+ const NAME : String < ' static > = BUILTIN_STRING_MEMORY . getEpochMilliseconds ;
351+ const LENGTH : u8 = 0 ;
352+ const BEHAVIOUR : Behaviour =
353+ Behaviour :: Regular ( TemporalInstantPrototype :: get_epoch_milliseconds) ;
354+ }
355+
356+ struct TemporalInstantPrototypeGetEpochNanoSeconds ;
357+ impl Builtin for TemporalInstantPrototypeGetEpochNanoSeconds {
358+ const NAME : String < ' static > = BUILTIN_STRING_MEMORY . getEpochNanoSeconds ;
359+ const LENGTH : u8 = 0 ;
360+ const BEHAVIOUR : Behaviour =
361+ Behaviour :: Regular ( TemporalInstantPrototype :: get_epoch_nanoseconds) ;
362+ }
363+
348364impl TemporalInstantPrototype {
349365 pub fn create_intrinsic ( agent : & mut Agent , realm : Realm < ' static > , _: NoGcScope ) {
350366 let intrinsics = agent. get_realm_record_by_id ( realm) . intrinsics ( ) ;
@@ -353,18 +369,62 @@ impl TemporalInstantPrototype {
353369 let instant_constructor = intrinsics. temporal_instant ( ) ;
354370
355371 OrdinaryObjectBuilder :: new_intrinsic_object ( agent, realm, this)
356- . with_property_capacity ( 1 )
372+ . with_property_capacity ( 3 )
357373 . with_prototype ( object_prototype)
358374 . with_constructor_property ( instant_constructor)
375+ . with_builtin_function_property :: < TemporalInstantPrototypeGetEpochMilliseconds > ( )
376+ . with_builtin_function_property :: < TemporalInstantPrototypeGetEpochNanoSeconds > ( )
359377 . build ( ) ;
360378 }
379+
380+ /// ### [8.3.3 get Temporal.Instant.prototype.epochMilliseconds](https://tc39.es/proposal-temporal/#sec-get-temporal.instant.prototype.epochmilliseconds)
381+ fn get_epoch_milliseconds < ' gc > (
382+ agent : & mut Agent ,
383+ this_value : Value ,
384+ _: ArgumentsList ,
385+ gc : GcScope < ' gc , ' _ > ,
386+ ) -> JsResult < ' gc , Value < ' gc > > {
387+ // 1. Let instant be the this value.
388+ // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]).
389+ let instant = requrire_temporal_instant_internal_slot ( agent, this_value, gc. nogc ( ) )
390+ . unbind ( ) ?
391+ . bind ( gc. nogc ( ) ) ;
392+ // 3. Let ns be instant.[[EpochNanoseconds]].
393+ // 4. Let ms be floor(ℝ(ns) / 10**6).
394+ // 5. Return 𝔽(ms).
395+ let value = instant. inner_instant ( agent) . epoch_milliseconds ( ) ;
396+ Ok ( Value :: from_i64 ( agent, value, gc. into_nogc ( ) ) )
397+ }
398+
399+ /// ### [8.3.4 get Temporal.Instant.prototype.epochNanoseconds](https://tc39.es/proposal-temporal/#sec-get-temporal.instant.prototype.epochnanoseconds)
400+ fn get_epoch_nanoseconds < ' gc > (
401+ agent : & mut Agent ,
402+ this_value : Value ,
403+ _: ArgumentsList ,
404+ gc : GcScope < ' gc , ' _ > ,
405+ ) -> JsResult < ' gc , Value < ' gc > > {
406+ // 1. Let instant be the this value.
407+ // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]).
408+ let instant = requrire_temporal_instant_internal_slot ( agent, this_value, gc. nogc ( ) )
409+ . unbind ( ) ?
410+ . bind ( gc. nogc ( ) ) ;
411+ // 3. Return instant.[[EpochNanoseconds]].
412+ let value = instant. inner_instant ( agent) . epoch_nanoseconds ( ) . as_i128 ( ) ;
413+ todo ! ( )
414+ }
361415}
362416
363- use self :: data:: InstantRecord ;
417+ use self :: data:: InstantHeapData ;
418+
364419#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
365420#[ repr( transparent) ]
366- pub struct TemporalInstant < ' a > ( BaseIndex < ' a , InstantRecord < ' static > > ) ;
421+ pub struct TemporalInstant < ' a > ( BaseIndex < ' a , InstantHeapData < ' static > > ) ;
422+
367423impl TemporalInstant < ' _ > {
424+ pub ( crate ) fn inner_instant ( self , agent : & Agent ) -> temporal_rs:: Instant {
425+ agent[ self ] . instant
426+ }
427+
368428 //TODO
369429 pub ( crate ) const fn _def ( ) -> Self {
370430 TemporalInstant ( BaseIndex :: from_u32_index ( 0 ) )
@@ -431,7 +491,7 @@ impl<'a> InternalMethods<'a> for TemporalInstant<'a> {}
431491
432492// TODO: get rid of Index impls, replace with get/get_mut/get_direct/get_direct_mut functions
433493impl Index < TemporalInstant < ' _ > > for Agent {
434- type Output = InstantRecord < ' static > ;
494+ type Output = InstantHeapData < ' static > ;
435495
436496 fn index ( & self , index : TemporalInstant < ' _ > ) -> & Self :: Output {
437497 & self . heap . instants [ index]
@@ -444,16 +504,16 @@ impl IndexMut<TemporalInstant<'_>> for Agent {
444504 }
445505}
446506
447- impl Index < TemporalInstant < ' _ > > for Vec < InstantRecord < ' static > > {
448- type Output = InstantRecord < ' static > ;
507+ impl Index < TemporalInstant < ' _ > > for Vec < InstantHeapData < ' static > > {
508+ type Output = InstantHeapData < ' static > ;
449509
450510 fn index ( & self , index : TemporalInstant < ' _ > ) -> & Self :: Output {
451511 self . get ( index. get_index ( ) )
452512 . expect ( "heap access out of bounds" )
453513 }
454514}
455515
456- impl IndexMut < TemporalInstant < ' _ > > for Vec < InstantRecord < ' static > > {
516+ impl IndexMut < TemporalInstant < ' _ > > for Vec < InstantHeapData < ' static > > {
457517 fn index_mut ( & mut self , index : TemporalInstant < ' _ > ) -> & mut Self :: Output {
458518 self . get_mut ( index. get_index ( ) )
459519 . expect ( "heap access out of bounds" )
@@ -498,10 +558,26 @@ impl HeapSweepWeakReference for TemporalInstant<'static> {
498558 }
499559}
500560
501- impl < ' a > CreateHeapData < InstantRecord < ' a > , TemporalInstant < ' a > > for Heap {
502- fn create ( & mut self , data : InstantRecord < ' a > ) -> TemporalInstant < ' a > {
561+ impl < ' a > CreateHeapData < InstantHeapData < ' a > , TemporalInstant < ' a > > for Heap {
562+ fn create ( & mut self , data : InstantHeapData < ' a > ) -> TemporalInstant < ' a > {
503563 self . instants . push ( data. unbind ( ) ) ;
504- self . alloc_counter += core:: mem:: size_of :: < InstantRecord < ' static > > ( ) ;
564+ self . alloc_counter += core:: mem:: size_of :: < InstantHeapData < ' static > > ( ) ;
505565 TemporalInstant ( BaseIndex :: last_t ( & self . instants ) )
506566 }
507567}
568+
569+ #[ inline( always) ]
570+ fn requrire_temporal_instant_internal_slot < ' a > (
571+ agent : & mut Agent ,
572+ value : Value ,
573+ gc : NoGcScope < ' a , ' _ > ,
574+ ) -> JsResult < ' a , TemporalInstant < ' a > > {
575+ match value {
576+ Value :: Instant ( instant) => Ok ( instant. bind ( gc) ) ,
577+ _ => Err ( agent. throw_exception_with_static_message (
578+ ExceptionType :: TypeError ,
579+ "Object is not a Temporal Instant" ,
580+ gc,
581+ ) ) ,
582+ }
583+ }
0 commit comments