Skip to content

Commit abc373d

Browse files
aykevldeadprogram
authored andcommitted
wasm: use int64 instead of float64 for the timeUnit
This makes wasm consistent with all the other targets, where timeUnit is already int64.
1 parent 02c5c42 commit abc373d

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/runtime/runtime_wasm_js.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
package runtime
44

5-
type timeUnit float64 // time in milliseconds, just like Date.now() in JavaScript
5+
type timeUnit int64
66

77
var 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).
1418
func 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

2122
func 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.

targets/wasm_exec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,12 @@
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(() => {
@@ -298,7 +298,7 @@
298298
} catch (e) {
299299
if (e !== wasmExit) throw e;
300300
}
301-
}, timeout);
301+
}, Number(timeout)/1e6);
302302
},
303303

304304
// func finalizeRef(v ref)

0 commit comments

Comments
 (0)