3 releases

0.1.2 Nov 4, 2021
0.1.1 Nov 4, 2021
0.1.0 Nov 4, 2021

#1195 in Embedded development

21 downloads per month

MIT license

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

Contains (ELF exe/lib, 525KB) rom.elf, (ELF exe/lib, 80KB) rom.elf

Build Status crates.io API Docs

no_std Rust mJS Bindings

From mJS repository:

mJS is designed for microcontrollers with limited resources. Main design goals are: small footprint and simple C/C++ interoperability. mJS implements a strict subset of ES6 (JavaScript version 6).

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

Example

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();
}

Dependencies