我希望能够编写一个非常小的Rust包并将其编译为wasm
,从而能够读取系统RAM/CPU资源。
这是由于PC的浏览器保护所致吗?
我可以使用依赖项sysinfo
依赖项,并在Rust中运行它,但没有问题,但是一旦编译为wasm
,值将不再可用。他们只是返回为0
。
use sysinfo::SystemExt;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: String);
}
#[wasm_bindgen]
pub fn specs() {
let mut system = sysinfo::System::new();
// First we update all information of our system struct.
system.refresh_all();
// And finally the RAM:
let total_ram = system.get_total_memory();
let used_ram = system.get_used_memory();
let available_ram = total_ram - used_ram;
log(format!("Total memory: {} kB", total_ram)); <--- Work in Rust but return 0 in wasm
log(format!("Used memory: {} kB", used_ram)); <--- Work in Rust but return 0 in wasm
log(format!("Available memory: {} kB", available_ram)); <--- Work in Rust but return 0 in wasm
}
最佳答案
sysinfo不支持Web程序集。这将触发返回0
的虚拟实现。
如此处所示:https://github.com/GuillaumeGomez/sysinfo/blob/6315474256150e5f6e23bc80c0fd922b40998a58/src/unknown/system.rs#L96
对于不久的将来,我不会寄希望于看到对Web程序集的支持。至少对我来说,我也不清楚这些值的含义是什么。这些值将引用整个系统,还是仅引用Web Assembly可用的内存?
关于javascript - wasm可以使用sysinfo依赖项吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65617116/