Skip to content

Commit

Permalink
Implement $cpu_now in wasm32
Browse files Browse the repository at this point in the history
  • Loading branch information
aarroyoc committed Nov 28, 2023
1 parent de5de0c commit 4c79501
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ console_error_panic_hook = "0.1"
wasm-bindgen = "0.2.87"
wasm-bindgen-futures = "0.4"
serde-wasm-bindgen = "0.5"
web-sys = { version = "0.3", features = ["Document", "Window", "Element"] }
web-sys = { version = "0.3", features = ["Document", "Window", "Element", "Performance"] }
js-sys = "0.3"

[target.'cfg(target_os = "wasi")'.dependencies]
Expand Down
11 changes: 9 additions & 2 deletions src/machine/system_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4191,7 +4191,14 @@ impl Machine {
#[cfg(target_arch = "wasm32")]
#[inline(always)]
pub(crate) fn cpu_now(&mut self) {
// TODO
let millisecs = web_sys::window()
.expect("window global object should be available")
.performance()
.expect("performance property in window should be available")
.now();
let secs = float_alloc!(millisecs / 1000.0, self.machine_st.arena);

self.machine_st.unify_f64(secs, self.deref_register(1));
}

#[inline(always)]
Expand Down Expand Up @@ -4894,7 +4901,7 @@ impl Machine {
let code = self.deref_register(1);
let result_reg = self.deref_register(2);
if let Some(code) = self.machine_st.value_to_str_like(code) {
let result = match js_sys::eval(&code.as_str()) {
match js_sys::eval(&code.as_str()) {
Ok(result) => self.unify_js_value(result, result_reg),
Err(result) => self.unify_js_value(result, result_reg),
};
Expand Down

0 comments on commit 4c79501

Please sign in to comment.