3个版本

0.1.2 2021年11月4日
0.1.1 2021年11月4日
0.1.0 2021年11月4日

#1195 in 嵌入式开发

每月21次下载

MIT 许可证

1.5MB
41K SLoC

C 36K SLoC // 0.1% comments GNU Style Assembly 2.5K SLoC // 0.0% comments Python 1.5K SLoC // 0.2% comments Rust 339 SLoC // 0.1% comments JavaScript 299 SLoC // 0.3% comments Batch 72 SLoC Shell 6 SLoC // 0.3% comments

包含 (ELF 可执行文件/库, 525KB) rom.elf, (ELF 可执行文件/库, 80KB) rom.elf

Build Status crates.io API Docs

无_std Rust mJS 绑定

来自 mJS 仓库

mJS是为资源有限的微控制器设计的。主要设计目标是:占用空间小和简单的C/C++互操作性。mJS实现了ES6(JavaScript第6版)的严格子集。

[dependencies]
mjs-sys = { version = "0.1", features = ["platform-nrf52"] }

示例

fn main() {
  let mut vm = mjs_sys::VM::create();

  // Basic call
  let val = vm.exec(b"1 / 2\0").unwrap();
  if val.is_number() {
    println!("Result: {}", val.as_double().unwrap());
  }

  // Call JS function from Rust
  let mut js_function = vm.exec(b"
  function foobar(x) {
  return 42 + x;
  }
  foobar
  \0").unwrap();

  if js_function.is_function() {
    let this = None;
    let x = vm.make_number(10.);
    let args = &[&x];
    let res = js_function.call(this, args).unwrap();
    if res.is_number() {
      println!("Result: {}", res.as_double().unwrap());
    }
  }

  // Call Rust function from JS
  fn rust_function(mjs: *mut mjs_sys::mjs) {
    let mut vm = mjs_sys::VM::from_inner(mjs);
    let x = vm.arg(0).unwrap().as_int().unwrap();
    println!("JS -> Rust: {}", x);
  }

  let js_function = vm.make_foreign(rust_function as _);
  vm.global().set(b"rust", js_function).unwrap();
  vm.exec(b"rust(42)\0").unwrap();

  vm.destroy();
}

依赖项