pub struct VM { /* private fields */ }
Expand description
mJS virtual machine
Implementations§
Source§impl VM
impl VM
Sourcepub fn create() -> VM
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}
Sourcepub fn destroy(self)
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}
Sourcepub fn from_inner(mjs: *mut mjs) -> VM
pub fn from_inner(mjs: *mut mjs) -> VM
Create a VM from an existing instance
Sourcepub fn exec(&mut self, source: &[u8]) -> Result<Val, JSError<'_>>
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}
Sourcepub fn global(&mut self) -> Val
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}
Sourcepub fn make_undefined(&mut self) -> Val
pub fn make_undefined(&mut self) -> Val
Create a JS undefined
value
Sourcepub fn make_foreign(&mut self, f: *mut c_void) -> Val
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}
Sourcepub fn make_number(&mut self, number: f64) -> Val
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}
Sourcepub fn make_boolean(&mut self, b: bool) -> Val
pub fn make_boolean(&mut self, b: bool) -> Val
Create a JS bool
Sourcepub fn make_object(&mut self) -> Val
pub fn make_object(&mut self) -> Val
Create a JS object
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more