mach: Timer: wasm implementation using performance.now()

This commit is contained in:
iddev5 2022-05-23 12:54:15 +05:30 committed by Stephen Gutekanst
parent 9b7b8be285
commit e5050e856e
2 changed files with 23 additions and 10 deletions

View file

@ -7,26 +7,35 @@ backing_timer: BackingTimerType = undefined,
// TODO: verify declarations and its signatures
const BackingTimerType = if (builtin.cpu.arch == .wasm32) struct {
pad0: u8 = 0,
initial: f64 = undefined,
const js = struct {
extern fn machPerfNow() f64;
};
const WasmTimer = @This();
fn start() !WasmTimer {
return WasmTimer{};
return WasmTimer{ .initial = js.machPerfNow() };
}
fn read(_: *WasmTimer) u64 {
return 0;
fn read(timer: *WasmTimer) u64 {
return timeToNs(js.machPerfNow() - timer.initial);
}
fn reset(_: *WasmTimer) void {}
fn lap(_: *WasmTimer) u64 {
return 0;
fn reset(timer: *WasmTimer) void {
timer.initial = js.machPerfNow();
}
fn timeToNs(_: f64) u64 {
return 0;
fn lap(timer: *WasmTimer) u64 {
const now = js.machPerfNow();
const initial = timer.initial;
timer.initial = now;
return timeToNs(now - initial);
}
fn timeToNs(t: f64) u64 {
return @floatToInt(u64, t) * 1000000;
}
} else std.time.Timer;

View file

@ -85,6 +85,10 @@ const mach = {
const cv = mach.canvases[canvas];
return cv.canvas.height;
},
machPerfNow() {
return performance.now();
},
};
export { mach };