Struct VM

Source
pub struct VM { /* private fields */ }
Expand description

mJS virtual machine

Implementations§

Source§

impl VM

Source

pub fn create() -> VM

Create new VM

Examples found in repository?
examples/basic.rs (line 2)
1fn main() {
2  let mut vm = mjs_sys::VM::create();
3
4  let val = vm.exec(b"1 / 2\0").unwrap();
5  if val.is_number() {
6    println!("Result: {}", val.as_double().unwrap());
7  }
8
9  let val2 = vm.exec(b"\"foobar\"\0").unwrap();
10  if val2.is_string() {
11    println!("Result: {}", val2.as_str().unwrap().unwrap());
12  }
13
14  // Call JS function from Rust
15  let mut js_function = vm
16    .exec(
17      b"
18  function foobar(x) {
19  return 42 + x;
20  }
21  foobar
22  \0",
23    )
24    .unwrap();
25
26  if js_function.is_function() {
27    let this = None;
28    let x = vm.make_number(10.);
29    let args = &[&x];
30    let res = js_function.call(this, args).unwrap();
31    if res.is_number() {
32      println!("Result: {}", res.as_double().unwrap());
33    }
34  }
35
36  // Call Rust function from JS
37  fn rust_function(mjs: *mut mjs_sys::mjs) {
38    let mut vm = mjs_sys::VM::from_inner(mjs);
39    let x = vm.arg(0).unwrap().as_int().unwrap();
40    println!("JS -> Rust: {}", x);
41  }
42
43  let js_function = vm.make_foreign(rust_function as _);
44  vm.global().set(b"rust", js_function).unwrap();
45  vm.exec(b"rust(42)\0").unwrap();
46
47  vm.destroy();
48}
Source

pub fn destroy(self)

Destroy VM

Examples found in repository?
examples/basic.rs (line 47)
1fn main() {
2  let mut vm = mjs_sys::VM::create();
3
4  let val = vm.exec(b"1 / 2\0").unwrap();
5  if val.is_number() {
6    println!("Result: {}", val.as_double().unwrap());
7  }
8
9  let val2 = vm.exec(b"\"foobar\"\0").unwrap();
10  if val2.is_string() {
11    println!("Result: {}", val2.as_str().unwrap().unwrap());
12  }
13
14  // Call JS function from Rust
15  let mut js_function = vm
16    .exec(
17      b"
18  function foobar(x) {
19  return 42 + x;
20  }
21  foobar
22  \0",
23    )
24    .unwrap();
25
26  if js_function.is_function() {
27    let this = None;
28    let x = vm.make_number(10.);
29    let args = &[&x];
30    let res = js_function.call(this, args).unwrap();
31    if res.is_number() {
32      println!("Result: {}", res.as_double().unwrap());
33    }
34  }
35
36  // Call Rust function from JS
37  fn rust_function(mjs: *mut mjs_sys::mjs) {
38    let mut vm = mjs_sys::VM::from_inner(mjs);
39    let x = vm.arg(0).unwrap().as_int().unwrap();
40    println!("JS -> Rust: {}", x);
41  }
42
43  let js_function = vm.make_foreign(rust_function as _);
44  vm.global().set(b"rust", js_function).unwrap();
45  vm.exec(b"rust(42)\0").unwrap();
46
47  vm.destroy();
48}
Source

pub fn from_inner(mjs: *mut mjs) -> VM

Create a VM from an existing instance

Examples found in repository?
examples/basic.rs (line 38)
37  fn rust_function(mjs: *mut mjs_sys::mjs) {
38    let mut vm = mjs_sys::VM::from_inner(mjs);
39    let x = vm.arg(0).unwrap().as_int().unwrap();
40    println!("JS -> Rust: {}", x);
41  }
Source

pub fn exec(&mut self, source: &[u8]) -> Result<Val, JSError<'_>>

Execute code. source should be null terminated.

Examples found in repository?
examples/basic.rs (line 4)
1fn main() {
2  let mut vm = mjs_sys::VM::create();
3
4  let val = vm.exec(b"1 / 2\0").unwrap();
5  if val.is_number() {
6    println!("Result: {}", val.as_double().unwrap());
7  }
8
9  let val2 = vm.exec(b"\"foobar\"\0").unwrap();
10  if val2.is_string() {
11    println!("Result: {}", val2.as_str().unwrap().unwrap());
12  }
13
14  // Call JS function from Rust
15  let mut js_function = vm
16    .exec(
17      b"
18  function foobar(x) {
19  return 42 + x;
20  }
21  foobar
22  \0",
23    )
24    .unwrap();
25
26  if js_function.is_function() {
27    let this = None;
28    let x = vm.make_number(10.);
29    let args = &[&x];
30    let res = js_function.call(this, args).unwrap();
31    if res.is_number() {
32      println!("Result: {}", res.as_double().unwrap());
33    }
34  }
35
36  // Call Rust function from JS
37  fn rust_function(mjs: *mut mjs_sys::mjs) {
38    let mut vm = mjs_sys::VM::from_inner(mjs);
39    let x = vm.arg(0).unwrap().as_int().unwrap();
40    println!("JS -> Rust: {}", x);
41  }
42
43  let js_function = vm.make_foreign(rust_function as _);
44  vm.global().set(b"rust", js_function).unwrap();
45  vm.exec(b"rust(42)\0").unwrap();
46
47  vm.destroy();
48}
Source

pub fn global(&mut self) -> Val

Return the VM global

Examples found in repository?
examples/basic.rs (line 44)
1fn main() {
2  let mut vm = mjs_sys::VM::create();
3
4  let val = vm.exec(b"1 / 2\0").unwrap();
5  if val.is_number() {
6    println!("Result: {}", val.as_double().unwrap());
7  }
8
9  let val2 = vm.exec(b"\"foobar\"\0").unwrap();
10  if val2.is_string() {
11    println!("Result: {}", val2.as_str().unwrap().unwrap());
12  }
13
14  // Call JS function from Rust
15  let mut js_function = vm
16    .exec(
17      b"
18  function foobar(x) {
19  return 42 + x;
20  }
21  foobar
22  \0",
23    )
24    .unwrap();
25
26  if js_function.is_function() {
27    let this = None;
28    let x = vm.make_number(10.);
29    let args = &[&x];
30    let res = js_function.call(this, args).unwrap();
31    if res.is_number() {
32      println!("Result: {}", res.as_double().unwrap());
33    }
34  }
35
36  // Call Rust function from JS
37  fn rust_function(mjs: *mut mjs_sys::mjs) {
38    let mut vm = mjs_sys::VM::from_inner(mjs);
39    let x = vm.arg(0).unwrap().as_int().unwrap();
40    println!("JS -> Rust: {}", x);
41  }
42
43  let js_function = vm.make_foreign(rust_function as _);
44  vm.global().set(b"rust", js_function).unwrap();
45  vm.exec(b"rust(42)\0").unwrap();
46
47  vm.destroy();
48}
Source

pub fn this(&mut self) -> Val

Return scope’s this

Source

pub fn nargs(&mut self) -> i32

In a function, return the number of arguments.

Source

pub fn arg(&mut self, i: i32) -> Option<Val>

Return argument i

Examples found in repository?
examples/basic.rs (line 39)
37  fn rust_function(mjs: *mut mjs_sys::mjs) {
38    let mut vm = mjs_sys::VM::from_inner(mjs);
39    let x = vm.arg(0).unwrap().as_int().unwrap();
40    println!("JS -> Rust: {}", x);
41  }
Source

pub fn make_undefined(&mut self) -> Val

Create a JS undefined value

Source

pub fn make_foreign(&mut self, f: *mut c_void) -> Val

Create a JS value that wraps a native pointer

Examples found in repository?
examples/basic.rs (line 43)
1fn main() {
2  let mut vm = mjs_sys::VM::create();
3
4  let val = vm.exec(b"1 / 2\0").unwrap();
5  if val.is_number() {
6    println!("Result: {}", val.as_double().unwrap());
7  }
8
9  let val2 = vm.exec(b"\"foobar\"\0").unwrap();
10  if val2.is_string() {
11    println!("Result: {}", val2.as_str().unwrap().unwrap());
12  }
13
14  // Call JS function from Rust
15  let mut js_function = vm
16    .exec(
17      b"
18  function foobar(x) {
19  return 42 + x;
20  }
21  foobar
22  \0",
23    )
24    .unwrap();
25
26  if js_function.is_function() {
27    let this = None;
28    let x = vm.make_number(10.);
29    let args = &[&x];
30    let res = js_function.call(this, args).unwrap();
31    if res.is_number() {
32      println!("Result: {}", res.as_double().unwrap());
33    }
34  }
35
36  // Call Rust function from JS
37  fn rust_function(mjs: *mut mjs_sys::mjs) {
38    let mut vm = mjs_sys::VM::from_inner(mjs);
39    let x = vm.arg(0).unwrap().as_int().unwrap();
40    println!("JS -> Rust: {}", x);
41  }
42
43  let js_function = vm.make_foreign(rust_function as _);
44  vm.global().set(b"rust", js_function).unwrap();
45  vm.exec(b"rust(42)\0").unwrap();
46
47  vm.destroy();
48}
Source

pub fn make_number(&mut self, number: f64) -> Val

Create a JS number

Examples found in repository?
examples/basic.rs (line 28)
1fn main() {
2  let mut vm = mjs_sys::VM::create();
3
4  let val = vm.exec(b"1 / 2\0").unwrap();
5  if val.is_number() {
6    println!("Result: {}", val.as_double().unwrap());
7  }
8
9  let val2 = vm.exec(b"\"foobar\"\0").unwrap();
10  if val2.is_string() {
11    println!("Result: {}", val2.as_str().unwrap().unwrap());
12  }
13
14  // Call JS function from Rust
15  let mut js_function = vm
16    .exec(
17      b"
18  function foobar(x) {
19  return 42 + x;
20  }
21  foobar
22  \0",
23    )
24    .unwrap();
25
26  if js_function.is_function() {
27    let this = None;
28    let x = vm.make_number(10.);
29    let args = &[&x];
30    let res = js_function.call(this, args).unwrap();
31    if res.is_number() {
32      println!("Result: {}", res.as_double().unwrap());
33    }
34  }
35
36  // Call Rust function from JS
37  fn rust_function(mjs: *mut mjs_sys::mjs) {
38    let mut vm = mjs_sys::VM::from_inner(mjs);
39    let x = vm.arg(0).unwrap().as_int().unwrap();
40    println!("JS -> Rust: {}", x);
41  }
42
43  let js_function = vm.make_foreign(rust_function as _);
44  vm.global().set(b"rust", js_function).unwrap();
45  vm.exec(b"rust(42)\0").unwrap();
46
47  vm.destroy();
48}
Source

pub fn make_boolean(&mut self, b: bool) -> Val

Create a JS bool

Source

pub fn make_object(&mut self) -> Val

Create a JS object

Source

pub fn make_string(&mut self, bytes: &'static [u8]) -> Result<Val, JSError<'_>>

Create a JS string. Bytes don’t need to be null terminated. Content of the string is not copied.

Source

pub fn make_string_copy(&mut self, bytes: &[u8]) -> Result<Val, JSError<'_>>

Create a JS string. Bytes don’t need to be null terminated. Content of the string is copied.

Trait Implementations§

Source§

impl Clone for VM

Source§

fn clone(&self) -> VM

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for VM

§

impl RefUnwindSafe for VM

§

impl !Send for VM

§

impl !Sync for VM

§

impl Unpin for VM

§

impl UnwindSafe for VM

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.