From 577fa2d29b87565bbecdd8c1d5fadccac79c404a Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 21 May 2022 16:35:06 +0100 Subject: [PATCH] Update `time` procedures for js targets --- core/time/time_js.odin | 25 +++++++++++++++++-------- vendor/wasm/js/runtime.js | 10 ++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/core/time/time_js.odin b/core/time/time_js.odin index 9a7163f38..226f921f9 100644 --- a/core/time/time_js.odin +++ b/core/time/time_js.odin @@ -2,22 +2,31 @@ //+build js package time -_IS_SUPPORTED :: false +foreign import "odin_env" + +_IS_SUPPORTED :: true _now :: proc "contextless" () -> Time { - return {} + foreign odin_env { + time_now :: proc "contextless" () -> i64 --- + } + return Time{time_now()} } _sleep :: proc "contextless" (d: Duration) { + foreign odin_env { + time_sleep :: proc "contextless" (ms: u32) --- + } + if d > 0 { + time_sleep(u32(d/1e6)) + } } _tick_now :: proc "contextless" () -> Tick { - // mul_div_u64 :: proc "contextless" (val, num, den: i64) -> i64 { - // q := val / den - // r := val % den - // return q * num + r * num / den - // } - return {} + foreign odin_env { + tick_now :: proc "contextless" () -> i64 --- + } + return Tick{tick_now()} } _yield :: proc "contextless" () { diff --git a/vendor/wasm/js/runtime.js b/vendor/wasm/js/runtime.js index 6cec2d0c0..f24c85bcd 100644 --- a/vendor/wasm/js/runtime.js +++ b/vendor/wasm/js/runtime.js @@ -1280,8 +1280,18 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { evaluate: (str_ptr, str_len) => { eval.call(null, wasmMemoryInterface.loadString(str_ptr, str_len)); }, time_now: () => { + // convert ms to ns + return Date.now() * 1e6; + }, + time_tick_now: () => { + // convert ms to ns return performance.now() * 1e6; }, + time_sleep: (duration_ms) => { + if (duration_ms > 0) { + // TODO(bill): Does this even make any sense? + } + }, sqrt: (x) => Math.sqrt(x), sin: (x) => Math.sin(x),