Struct Val

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

JS value

Implementations§

Source§

impl Val

Source

pub fn own(&self)

Make sure the value don’t get garbage collected next time exec or call is used

Source

pub fn disown(&self)

Disown value

Source

pub fn is_number(&self) -> bool

Is value a number

Examples found in repository?
examples/basic.rs (line 5)
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 is_object(&self) -> bool

Is value an object

Source

pub fn is_string(&self) -> bool

Is value a string

Examples found in repository?
examples/basic.rs (line 10)
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 is_function(&self) -> bool

Is value a function

Examples found in repository?
examples/basic.rs (line 26)
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 is_foreign(&self) -> bool

Is value a wrapper pointer

Source

pub fn as_int(&self) -> Option<i32>

Get number as int

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 as_double(&self) -> Option<f64>

Get number as double

Examples found in repository?
examples/basic.rs (line 6)
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 as_bytes(&self) -> Option<&[u8]>

Get string as bytes

Source

pub fn as_str(&self) -> Option<Result<&str, Utf8Error>>

Get string as str

Examples found in repository?
examples/basic.rs (line 11)
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 as_ptr(&self) -> Option<*const c_void>

Get wrapper pointer

Source

pub fn delete(&self, name: &[u8])

Delete property

Source

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

Set property

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 get(&self, name: &[u8]) -> Option<Val>

Get property

Source

pub fn call( &mut self, this: Option<Val>, args: &[&Val], ) -> Result<Val, JSError<'_>>

Execute function

Examples found in repository?
examples/basic.rs (line 30)
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}

Auto Trait Implementations§

§

impl Freeze for Val

§

impl RefUnwindSafe for Val

§

impl !Send for Val

§

impl !Sync for Val

§

impl Unpin for Val

§

impl UnwindSafe for Val

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> 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, 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.