File tree Expand file tree Collapse file tree 2 files changed +11
-12
lines changed Expand file tree Collapse file tree 2 files changed +11
-12
lines changed Original file line number Diff line number Diff line change 22
33package runtime
44
5- type timeUnit float64 // time in milliseconds, just like Date.now() in JavaScript
5+ type timeUnit int64
66
77var handleEvent func ()
88
@@ -11,17 +11,16 @@ func setEventHandler(fn func()) {
1111 handleEvent = fn
1212}
1313
14+ // We use 1ns per tick, to simplify things.
15+ // It would probably be fine to use 1µs per tick, since performance.now only
16+ // promises a resolution of 5µs, but 1ns makes the conversions here a bit more
17+ // straightforward (since nothing needs to be converted).
1418func ticksToNanoseconds (ticks timeUnit ) int64 {
15- // The JavaScript API works in float64 milliseconds, so convert to
16- // nanoseconds first before converting to a timeUnit (which is a float64),
17- // to avoid precision loss.
18- return int64 (ticks * 1e6 )
19+ return int64 (ticks )
1920}
2021
2122func nanosecondsToTicks (ns int64 ) timeUnit {
22- // The JavaScript API works in float64 milliseconds, so convert to timeUnit
23- // (which is a float64) first before dividing, to avoid precision loss.
24- return timeUnit (ns ) / 1e6
23+ return timeUnit (ns )
2524}
2625
2726// This function is called by the scheduler.
Original file line number Diff line number Diff line change 283283 } ,
284284 } ,
285285 gojs : {
286- // func ticks() float64
286+ // func ticks() int64
287287 "runtime.ticks" : ( ) => {
288- return timeOrigin + performance . now ( ) ;
288+ return BigInt ( ( timeOrigin + performance . now ( ) ) * 1e6 ) ;
289289 } ,
290290
291- // func sleepTicks(timeout float64 )
291+ // func sleepTicks(timeout int64 )
292292 "runtime.sleepTicks" : ( timeout ) => {
293293 // Do not sleep, only reactivate scheduler after the given timeout.
294294 setTimeout ( ( ) => {
298298 } catch ( e ) {
299299 if ( e !== wasmExit ) throw e ;
300300 }
301- } , timeout ) ;
301+ } , Number ( timeout ) / 1e6 ) ;
302302 } ,
303303
304304 // func finalizeRef(v ref)
You can’t perform that action at this time.
0 commit comments