pub struct Val { /* private fields */ }Expand description
A wrapper around a javascript handle
Implementations§
Source§impl Val
impl Val
Sourcepub fn take_ownership(handle: Handle) -> Val
pub fn take_ownership(handle: Handle) -> Val
Takes the ownership of a handle
Sourcepub fn global_this() -> Val
pub fn global_this() -> Val
Returns the globalThis object
Sourcepub fn get<T: Into<Val>>(&self, prop: T) -> Val
pub fn get<T: Into<Val>>(&self, prop: T) -> Val
Gets the property prop
Examples found in repository?
3fn main() {
4 let document = Val::global("document");
5 let elem = document.call("createElement", &argv!["BUTTON"]);
6 elem.set("textContent", Val::from("Click"));
7 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
8 elem.call(
9 "addEventListener",
10 &argv![
11 "click",
12 Val::make_fn(|ev| {
13 let console = Console::get();
14 console.call("clear", &[]);
15 console.log(&[ev[0].get("clientX")]);
16 println!(
17 "client x: {}",
18 ev[0].get("clientX").as_i32()
19 );
20 println!("hello from Rust");
21 Val::undefined()
22 })
23 ],
24 );
25 body.call("appendChild", &argv![elem]);
26}More examples
3fn main() {
4 #[allow(non_snake_case)]
5 let mut AudioContext = Val::global("AudioContext");
6 if !AudioContext.as_bool() {
7 println!("No global AudioContext, trying webkitAudioContext");
8 AudioContext = Val::global("webkitAudioContext");
9 }
10
11 println!("Got an AudioContext");
12 let context = AudioContext.new(&[]);
13 let oscillator = context.call("createOscillator", &[]);
14
15 println!("Configuring oscillator");
16 oscillator.set("type", "triangle");
17 oscillator.get("frequency").set("value", Val::from(261.63)); // Middle C
18
19 let document = Val::global("document");
20 let elem = document.call("createElement", &argv!["BUTTON"]);
21 elem.set("textContent", "Click");
22 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
23 elem.call(
24 "addEventListener",
25 &argv![
26 "click",
27 Val::make_fn(move |_| {
28 println!("Playing");
29 oscillator.call("connect", &argv![context.get("destination")]);
30 oscillator.call("start", &argv![0]);
31 println!("All done!");
32 Val::undefined()
33 })
34 ],
35 );
36 body.call("appendChild", &argv![elem]);
37}Sourcepub fn global(name: &str) -> Val
pub fn global(name: &str) -> Val
Gets a global object by name
Examples found in repository?
3fn main() {
4 let document = Val::global("document");
5 let elem = document.call("createElement", &argv!["BUTTON"]);
6 elem.set("textContent", Val::from("Click"));
7 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
8 elem.call(
9 "addEventListener",
10 &argv![
11 "click",
12 Val::make_fn(|ev| {
13 let console = Console::get();
14 console.call("clear", &[]);
15 console.log(&[ev[0].get("clientX")]);
16 println!(
17 "client x: {}",
18 ev[0].get("clientX").as_i32()
19 );
20 println!("hello from Rust");
21 Val::undefined()
22 })
23 ],
24 );
25 body.call("appendChild", &argv![elem]);
26}More examples
3fn main() {
4 #[allow(non_snake_case)]
5 let mut AudioContext = Val::global("AudioContext");
6 if !AudioContext.as_bool() {
7 println!("No global AudioContext, trying webkitAudioContext");
8 AudioContext = Val::global("webkitAudioContext");
9 }
10
11 println!("Got an AudioContext");
12 let context = AudioContext.new(&[]);
13 let oscillator = context.call("createOscillator", &[]);
14
15 println!("Configuring oscillator");
16 oscillator.set("type", "triangle");
17 oscillator.get("frequency").set("value", Val::from(261.63)); // Middle C
18
19 let document = Val::global("document");
20 let elem = document.call("createElement", &argv!["BUTTON"]);
21 elem.set("textContent", "Click");
22 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
23 elem.call(
24 "addEventListener",
25 &argv![
26 "click",
27 Val::make_fn(move |_| {
28 println!("Playing");
29 oscillator.call("connect", &argv![context.get("destination")]);
30 oscillator.call("start", &argv![0]);
31 println!("All done!");
32 Val::undefined()
33 })
34 ],
35 );
36 body.call("appendChild", &argv![elem]);
37}Sourcepub fn undefined() -> Val
pub fn undefined() -> Val
Gets a js undefined Val
Examples found in repository?
3fn main() {
4 let document = Val::global("document");
5 let elem = document.call("createElement", &argv!["BUTTON"]);
6 elem.set("textContent", Val::from("Click"));
7 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
8 elem.call(
9 "addEventListener",
10 &argv![
11 "click",
12 Val::make_fn(|ev| {
13 let console = Console::get();
14 console.call("clear", &[]);
15 console.log(&[ev[0].get("clientX")]);
16 println!(
17 "client x: {}",
18 ev[0].get("clientX").as_i32()
19 );
20 println!("hello from Rust");
21 Val::undefined()
22 })
23 ],
24 );
25 body.call("appendChild", &argv![elem]);
26}More examples
3fn main() {
4 #[allow(non_snake_case)]
5 let mut AudioContext = Val::global("AudioContext");
6 if !AudioContext.as_bool() {
7 println!("No global AudioContext, trying webkitAudioContext");
8 AudioContext = Val::global("webkitAudioContext");
9 }
10
11 println!("Got an AudioContext");
12 let context = AudioContext.new(&[]);
13 let oscillator = context.call("createOscillator", &[]);
14
15 println!("Configuring oscillator");
16 oscillator.set("type", "triangle");
17 oscillator.get("frequency").set("value", Val::from(261.63)); // Middle C
18
19 let document = Val::global("document");
20 let elem = document.call("createElement", &argv!["BUTTON"]);
21 elem.set("textContent", "Click");
22 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
23 elem.call(
24 "addEventListener",
25 &argv![
26 "click",
27 Val::make_fn(move |_| {
28 println!("Playing");
29 oscillator.call("connect", &argv![context.get("destination")]);
30 oscillator.call("start", &argv![0]);
31 println!("All done!");
32 Val::undefined()
33 })
34 ],
35 );
36 body.call("appendChild", &argv![elem]);
37}Sourcepub fn set<K: Into<Val>, V: Into<Val>>(&self, prop: K, val: V)
pub fn set<K: Into<Val>, V: Into<Val>>(&self, prop: K, val: V)
Set the underlying js object property prop to val
Examples found in repository?
3fn main() {
4 let document = Val::global("document");
5 let elem = document.call("createElement", &argv!["BUTTON"]);
6 elem.set("textContent", Val::from("Click"));
7 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
8 elem.call(
9 "addEventListener",
10 &argv![
11 "click",
12 Val::make_fn(|ev| {
13 let console = Console::get();
14 console.call("clear", &[]);
15 console.log(&[ev[0].get("clientX")]);
16 println!(
17 "client x: {}",
18 ev[0].get("clientX").as_i32()
19 );
20 println!("hello from Rust");
21 Val::undefined()
22 })
23 ],
24 );
25 body.call("appendChild", &argv![elem]);
26}More examples
3fn main() {
4 #[allow(non_snake_case)]
5 let mut AudioContext = Val::global("AudioContext");
6 if !AudioContext.as_bool() {
7 println!("No global AudioContext, trying webkitAudioContext");
8 AudioContext = Val::global("webkitAudioContext");
9 }
10
11 println!("Got an AudioContext");
12 let context = AudioContext.new(&[]);
13 let oscillator = context.call("createOscillator", &[]);
14
15 println!("Configuring oscillator");
16 oscillator.set("type", "triangle");
17 oscillator.get("frequency").set("value", Val::from(261.63)); // Middle C
18
19 let document = Val::global("document");
20 let elem = document.call("createElement", &argv!["BUTTON"]);
21 elem.set("textContent", "Click");
22 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
23 elem.call(
24 "addEventListener",
25 &argv![
26 "click",
27 Val::make_fn(move |_| {
28 println!("Playing");
29 oscillator.call("connect", &argv![context.get("destination")]);
30 oscillator.call("start", &argv![0]);
31 println!("All done!");
32 Val::undefined()
33 })
34 ],
35 );
36 body.call("appendChild", &argv![elem]);
37}Sourcepub fn has_own_property(&self, prop: &str) -> bool
pub fn has_own_property(&self, prop: &str) -> bool
Checks whether a non-inherited property prop exists
Sourcepub fn at<T: Into<Val>>(&self, idx: T) -> Val
pub fn at<T: Into<Val>>(&self, idx: T) -> Val
Gets the element at index idx. Assumes the underlying js type is indexable
Examples found in repository?
3fn main() {
4 let document = Val::global("document");
5 let elem = document.call("createElement", &argv!["BUTTON"]);
6 elem.set("textContent", Val::from("Click"));
7 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
8 elem.call(
9 "addEventListener",
10 &argv![
11 "click",
12 Val::make_fn(|ev| {
13 let console = Console::get();
14 console.call("clear", &[]);
15 console.log(&[ev[0].get("clientX")]);
16 println!(
17 "client x: {}",
18 ev[0].get("clientX").as_i32()
19 );
20 println!("hello from Rust");
21 Val::undefined()
22 })
23 ],
24 );
25 body.call("appendChild", &argv![elem]);
26}More examples
3fn main() {
4 #[allow(non_snake_case)]
5 let mut AudioContext = Val::global("AudioContext");
6 if !AudioContext.as_bool() {
7 println!("No global AudioContext, trying webkitAudioContext");
8 AudioContext = Val::global("webkitAudioContext");
9 }
10
11 println!("Got an AudioContext");
12 let context = AudioContext.new(&[]);
13 let oscillator = context.call("createOscillator", &[]);
14
15 println!("Configuring oscillator");
16 oscillator.set("type", "triangle");
17 oscillator.get("frequency").set("value", Val::from(261.63)); // Middle C
18
19 let document = Val::global("document");
20 let elem = document.call("createElement", &argv!["BUTTON"]);
21 elem.set("textContent", "Click");
22 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
23 elem.call(
24 "addEventListener",
25 &argv![
26 "click",
27 Val::make_fn(move |_| {
28 println!("Playing");
29 oscillator.call("connect", &argv![context.get("destination")]);
30 oscillator.call("start", &argv![0]);
31 println!("All done!");
32 Val::undefined()
33 })
34 ],
35 );
36 body.call("appendChild", &argv![elem]);
37}Sourcepub fn as_i32(&self) -> i32
pub fn as_i32(&self) -> i32
Gets the underlying i32 value of a js object
Examples found in repository?
3fn main() {
4 let document = Val::global("document");
5 let elem = document.call("createElement", &argv!["BUTTON"]);
6 elem.set("textContent", Val::from("Click"));
7 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
8 elem.call(
9 "addEventListener",
10 &argv![
11 "click",
12 Val::make_fn(|ev| {
13 let console = Console::get();
14 console.call("clear", &[]);
15 console.log(&[ev[0].get("clientX")]);
16 println!(
17 "client x: {}",
18 ev[0].get("clientX").as_i32()
19 );
20 println!("hello from Rust");
21 Val::undefined()
22 })
23 ],
24 );
25 body.call("appendChild", &argv![elem]);
26}Sourcepub fn as_bool(&self) -> bool
pub fn as_bool(&self) -> bool
Gets the underlying boolean value of a js object
Examples found in repository?
3fn main() {
4 #[allow(non_snake_case)]
5 let mut AudioContext = Val::global("AudioContext");
6 if !AudioContext.as_bool() {
7 println!("No global AudioContext, trying webkitAudioContext");
8 AudioContext = Val::global("webkitAudioContext");
9 }
10
11 println!("Got an AudioContext");
12 let context = AudioContext.new(&[]);
13 let oscillator = context.call("createOscillator", &[]);
14
15 println!("Configuring oscillator");
16 oscillator.set("type", "triangle");
17 oscillator.get("frequency").set("value", Val::from(261.63)); // Middle C
18
19 let document = Val::global("document");
20 let elem = document.call("createElement", &argv!["BUTTON"]);
21 elem.set("textContent", "Click");
22 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
23 elem.call(
24 "addEventListener",
25 &argv![
26 "click",
27 Val::make_fn(move |_| {
28 println!("Playing");
29 oscillator.call("connect", &argv![context.get("destination")]);
30 oscillator.call("start", &argv![0]);
31 println!("All done!");
32 Val::undefined()
33 })
34 ],
35 );
36 body.call("appendChild", &argv![elem]);
37}Sourcepub fn to_vec_i32(&self) -> Vec<i32>
pub fn to_vec_i32(&self) -> Vec<i32>
Converts the underlying js array to an Vec of i32
Sourcepub fn to_vec_f64(&self) -> Vec<f64>
pub fn to_vec_f64(&self) -> Vec<f64>
Converts the underlying js array to an Vec of f64
Sourcepub fn call(&self, f: &str, args: &[Val]) -> Val
pub fn call(&self, f: &str, args: &[Val]) -> Val
Calls the method f with args, can return an undefined js value
Examples found in repository?
3fn main() {
4 let document = Val::global("document");
5 let elem = document.call("createElement", &argv!["BUTTON"]);
6 elem.set("textContent", Val::from("Click"));
7 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
8 elem.call(
9 "addEventListener",
10 &argv![
11 "click",
12 Val::make_fn(|ev| {
13 let console = Console::get();
14 console.call("clear", &[]);
15 console.log(&[ev[0].get("clientX")]);
16 println!(
17 "client x: {}",
18 ev[0].get("clientX").as_i32()
19 );
20 println!("hello from Rust");
21 Val::undefined()
22 })
23 ],
24 );
25 body.call("appendChild", &argv![elem]);
26}More examples
3fn main() {
4 #[allow(non_snake_case)]
5 let mut AudioContext = Val::global("AudioContext");
6 if !AudioContext.as_bool() {
7 println!("No global AudioContext, trying webkitAudioContext");
8 AudioContext = Val::global("webkitAudioContext");
9 }
10
11 println!("Got an AudioContext");
12 let context = AudioContext.new(&[]);
13 let oscillator = context.call("createOscillator", &[]);
14
15 println!("Configuring oscillator");
16 oscillator.set("type", "triangle");
17 oscillator.get("frequency").set("value", Val::from(261.63)); // Middle C
18
19 let document = Val::global("document");
20 let elem = document.call("createElement", &argv!["BUTTON"]);
21 elem.set("textContent", "Click");
22 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
23 elem.call(
24 "addEventListener",
25 &argv![
26 "click",
27 Val::make_fn(move |_| {
28 println!("Playing");
29 oscillator.call("connect", &argv![context.get("destination")]);
30 oscillator.call("start", &argv![0]);
31 println!("All done!");
32 Val::undefined()
33 })
34 ],
35 );
36 body.call("appendChild", &argv![elem]);
37}Sourcepub fn new(&self, args: &[Val]) -> Val
pub fn new(&self, args: &[Val]) -> Val
Calls the object’s constructor with args constructing a new object
Examples found in repository?
3fn main() {
4 #[allow(non_snake_case)]
5 let mut AudioContext = Val::global("AudioContext");
6 if !AudioContext.as_bool() {
7 println!("No global AudioContext, trying webkitAudioContext");
8 AudioContext = Val::global("webkitAudioContext");
9 }
10
11 println!("Got an AudioContext");
12 let context = AudioContext.new(&[]);
13 let oscillator = context.call("createOscillator", &[]);
14
15 println!("Configuring oscillator");
16 oscillator.set("type", "triangle");
17 oscillator.get("frequency").set("value", Val::from(261.63)); // Middle C
18
19 let document = Val::global("document");
20 let elem = document.call("createElement", &argv!["BUTTON"]);
21 elem.set("textContent", "Click");
22 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
23 elem.call(
24 "addEventListener",
25 &argv![
26 "click",
27 Val::make_fn(move |_| {
28 println!("Playing");
29 oscillator.call("connect", &argv![context.get("destination")]);
30 oscillator.call("start", &argv![0]);
31 println!("All done!");
32 Val::undefined()
33 })
34 ],
35 );
36 body.call("appendChild", &argv![elem]);
37}Sourcepub fn invoke(&self, args: &[Val]) -> Val
pub fn invoke(&self, args: &[Val]) -> Val
Invokes the function object with args, can return an undefined js value
Sourcepub fn make_fn_raw(f: fn(Handle, Handle) -> Handle, data: Handle) -> Val
pub fn make_fn_raw(f: fn(Handle, Handle) -> Handle, data: Handle) -> Val
Creates js function from a function pointer and returns its handle wrapped in a Val object
Sourcepub fn make_fn<F: FnMut(&[Val]) -> Val>(cb: F) -> Val
pub fn make_fn<F: FnMut(&[Val]) -> Val>(cb: F) -> Val
Creates a js function from a Rust closure and returns a Val
Examples found in repository?
3fn main() {
4 let document = Val::global("document");
5 let elem = document.call("createElement", &argv!["BUTTON"]);
6 elem.set("textContent", Val::from("Click"));
7 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
8 elem.call(
9 "addEventListener",
10 &argv![
11 "click",
12 Val::make_fn(|ev| {
13 let console = Console::get();
14 console.call("clear", &[]);
15 console.log(&[ev[0].get("clientX")]);
16 println!(
17 "client x: {}",
18 ev[0].get("clientX").as_i32()
19 );
20 println!("hello from Rust");
21 Val::undefined()
22 })
23 ],
24 );
25 body.call("appendChild", &argv![elem]);
26}More examples
3fn main() {
4 #[allow(non_snake_case)]
5 let mut AudioContext = Val::global("AudioContext");
6 if !AudioContext.as_bool() {
7 println!("No global AudioContext, trying webkitAudioContext");
8 AudioContext = Val::global("webkitAudioContext");
9 }
10
11 println!("Got an AudioContext");
12 let context = AudioContext.new(&[]);
13 let oscillator = context.call("createOscillator", &[]);
14
15 println!("Configuring oscillator");
16 oscillator.set("type", "triangle");
17 oscillator.get("frequency").set("value", Val::from(261.63)); // Middle C
18
19 let document = Val::global("document");
20 let elem = document.call("createElement", &argv!["BUTTON"]);
21 elem.set("textContent", "Click");
22 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
23 elem.call(
24 "addEventListener",
25 &argv![
26 "click",
27 Val::make_fn(move |_| {
28 println!("Playing");
29 oscillator.call("connect", &argv![context.get("destination")]);
30 oscillator.call("start", &argv![0]);
31 println!("All done!");
32 Val::undefined()
33 })
34 ],
35 );
36 body.call("appendChild", &argv![elem]);
37}Sourcepub fn instanceof(&self, v: Val) -> bool
pub fn instanceof(&self, v: Val) -> bool
Checks whether this Val is an instanceof v