pub struct Console { /* private fields */ }
Expand description
A console wrapper
Implementations§
Source§impl Console
impl Console
Sourcepub const fn get() -> Console
pub const fn get() -> Console
Gets the console
Examples found in repository?
More examples
examples/eval.rs (line 5)
3fn main() {
4 emlite::init();
5 let con = Console::get();
6 let ret = eval!(
7 r#"
8 let con = EMLITE_VALMAP.toValue({});
9 con.log("Hello");
10 6
11 "#,
12 con.as_handle()
13 );
14 con.log(&[ret]);
15}
examples/bind.rs (line 80)
66fn main() {
67 emlite::init();
68 MyJsClass::define();
69 let c = MyJsClass::new(5, 6);
70 c.call("print", &[]);
71 let b = eval!(
72 r#"
73 let b = new MyJsClass(6, 7);
74 b.print();
75 b
76 "#
77 );
78 let a = b.as_::<MyJsClass>();
79 a.print();
80 let console = Console::get();
81 console.log(&[a.into()]);
82}
examples/dom.rs (line 14)
3fn main() {
4 emlite::init();
5 let document = Val::global("document");
6 let elem = document.call("createElement", &argv!["BUTTON"]);
7 elem.set("textContent", Val::from("Click"));
8 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
9 elem.call(
10 "addEventListener",
11 &argv![
12 "click",
13 Val::make_fn(|ev| {
14 let console = Console::get();
15 console.call("clear", &[]);
16 console.log(&[ev[0].get("clientX")]);
17 println!("client x: {}", ev[0].get("clientX").as_::<i32>());
18 println!("hello from Rust");
19 Val::undefined()
20 })
21 ],
22 );
23 body.call("appendChild", &argv![elem]);
24}
Sourcepub fn log(&self, args: &[Val])
pub fn log(&self, args: &[Val])
Logs into the console
Examples found in repository?
More examples
examples/eval.rs (line 14)
3fn main() {
4 emlite::init();
5 let con = Console::get();
6 let ret = eval!(
7 r#"
8 let con = EMLITE_VALMAP.toValue({});
9 con.log("Hello");
10 6
11 "#,
12 con.as_handle()
13 );
14 con.log(&[ret]);
15}
examples/bind.rs (line 81)
66fn main() {
67 emlite::init();
68 MyJsClass::define();
69 let c = MyJsClass::new(5, 6);
70 c.call("print", &[]);
71 let b = eval!(
72 r#"
73 let b = new MyJsClass(6, 7);
74 b.print();
75 b
76 "#
77 );
78 let a = b.as_::<MyJsClass>();
79 a.print();
80 let console = Console::get();
81 console.log(&[a.into()]);
82}
examples/dom.rs (line 16)
3fn main() {
4 emlite::init();
5 let document = Val::global("document");
6 let elem = document.call("createElement", &argv!["BUTTON"]);
7 elem.set("textContent", Val::from("Click"));
8 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
9 elem.call(
10 "addEventListener",
11 &argv![
12 "click",
13 Val::make_fn(|ev| {
14 let console = Console::get();
15 console.call("clear", &[]);
16 console.log(&[ev[0].get("clientX")]);
17 println!("client x: {}", ev[0].get("clientX").as_::<i32>());
18 println!("hello from Rust");
19 Val::undefined()
20 })
21 ],
22 );
23 body.call("appendChild", &argv![elem]);
24}
Sourcepub fn as_handle(&self) -> Handle
pub fn as_handle(&self) -> Handle
Returns the underlying handle of the console
Examples found in repository?
examples/eval.rs (line 12)
3fn main() {
4 emlite::init();
5 let con = Console::get();
6 let ret = eval!(
7 r#"
8 let con = EMLITE_VALMAP.toValue({});
9 con.log("Hello");
10 6
11 "#,
12 con.as_handle()
13 );
14 con.log(&[ret]);
15}
Methods from Deref<Target = Val>§
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?
examples/dom.rs (line 16)
3fn main() {
4 emlite::init();
5 let document = Val::global("document");
6 let elem = document.call("createElement", &argv!["BUTTON"]);
7 elem.set("textContent", Val::from("Click"));
8 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
9 elem.call(
10 "addEventListener",
11 &argv![
12 "click",
13 Val::make_fn(|ev| {
14 let console = Console::get();
15 console.call("clear", &[]);
16 console.log(&[ev[0].get("clientX")]);
17 println!("client x: {}", ev[0].get("clientX").as_::<i32>());
18 println!("hello from Rust");
19 Val::undefined()
20 })
21 ],
22 );
23 body.call("appendChild", &argv![elem]);
24}
More examples
examples/audio.rs (line 18)
3fn main() {
4 emlite::init();
5 #[allow(non_snake_case)]
6 let mut AudioContext = Val::global("AudioContext");
7 if !AudioContext.as_::<bool>() {
8 println!("No global AudioContext, trying webkitAudioContext");
9 AudioContext = Val::global("webkitAudioContext");
10 }
11
12 println!("Got an AudioContext");
13 let context = AudioContext.new(&[]);
14 let oscillator = context.call("createOscillator", &[]);
15
16 println!("Configuring oscillator");
17 oscillator.set("type", "triangle");
18 oscillator.get("frequency").set::<_, f64>("value", 261.63); // Middle C
19
20 let document = Val::global("document");
21 let elem = document.call("createElement", &argv!["BUTTON"]);
22 elem.set("textContent", "Click");
23 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
24 elem.call(
25 "addEventListener",
26 &argv![
27 "click",
28 Val::make_fn(move |_| {
29 println!("Playing");
30 oscillator.call("connect", &argv![context.get("destination")]);
31 oscillator.call("start", &argv![0]);
32 println!("All done!");
33 Val::undefined()
34 })
35 ],
36 );
37 body.call("appendChild", &argv![elem]);
38}
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?
examples/dom.rs (line 7)
3fn main() {
4 emlite::init();
5 let document = Val::global("document");
6 let elem = document.call("createElement", &argv!["BUTTON"]);
7 elem.set("textContent", Val::from("Click"));
8 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
9 elem.call(
10 "addEventListener",
11 &argv![
12 "click",
13 Val::make_fn(|ev| {
14 let console = Console::get();
15 console.call("clear", &[]);
16 console.log(&[ev[0].get("clientX")]);
17 println!("client x: {}", ev[0].get("clientX").as_::<i32>());
18 println!("hello from Rust");
19 Val::undefined()
20 })
21 ],
22 );
23 body.call("appendChild", &argv![elem]);
24}
More examples
examples/audio.rs (line 17)
3fn main() {
4 emlite::init();
5 #[allow(non_snake_case)]
6 let mut AudioContext = Val::global("AudioContext");
7 if !AudioContext.as_::<bool>() {
8 println!("No global AudioContext, trying webkitAudioContext");
9 AudioContext = Val::global("webkitAudioContext");
10 }
11
12 println!("Got an AudioContext");
13 let context = AudioContext.new(&[]);
14 let oscillator = context.call("createOscillator", &[]);
15
16 println!("Configuring oscillator");
17 oscillator.set("type", "triangle");
18 oscillator.get("frequency").set::<_, f64>("value", 261.63); // Middle C
19
20 let document = Val::global("document");
21 let elem = document.call("createElement", &argv!["BUTTON"]);
22 elem.set("textContent", "Click");
23 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
24 elem.call(
25 "addEventListener",
26 &argv![
27 "click",
28 Val::make_fn(move |_| {
29 println!("Playing");
30 oscillator.call("connect", &argv![context.get("destination")]);
31 oscillator.call("start", &argv![0]);
32 println!("All done!");
33 Val::undefined()
34 })
35 ],
36 );
37 body.call("appendChild", &argv![elem]);
38}
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?
examples/dom.rs (line 8)
3fn main() {
4 emlite::init();
5 let document = Val::global("document");
6 let elem = document.call("createElement", &argv!["BUTTON"]);
7 elem.set("textContent", Val::from("Click"));
8 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
9 elem.call(
10 "addEventListener",
11 &argv![
12 "click",
13 Val::make_fn(|ev| {
14 let console = Console::get();
15 console.call("clear", &[]);
16 console.log(&[ev[0].get("clientX")]);
17 println!("client x: {}", ev[0].get("clientX").as_::<i32>());
18 println!("hello from Rust");
19 Val::undefined()
20 })
21 ],
22 );
23 body.call("appendChild", &argv![elem]);
24}
More examples
examples/audio.rs (line 23)
3fn main() {
4 emlite::init();
5 #[allow(non_snake_case)]
6 let mut AudioContext = Val::global("AudioContext");
7 if !AudioContext.as_::<bool>() {
8 println!("No global AudioContext, trying webkitAudioContext");
9 AudioContext = Val::global("webkitAudioContext");
10 }
11
12 println!("Got an AudioContext");
13 let context = AudioContext.new(&[]);
14 let oscillator = context.call("createOscillator", &[]);
15
16 println!("Configuring oscillator");
17 oscillator.set("type", "triangle");
18 oscillator.get("frequency").set::<_, f64>("value", 261.63); // Middle C
19
20 let document = Val::global("document");
21 let elem = document.call("createElement", &argv!["BUTTON"]);
22 elem.set("textContent", "Click");
23 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
24 elem.call(
25 "addEventListener",
26 &argv![
27 "click",
28 Val::make_fn(move |_| {
29 println!("Playing");
30 oscillator.call("connect", &argv![context.get("destination")]);
31 oscillator.call("start", &argv![0]);
32 println!("All done!");
33 Val::undefined()
34 })
35 ],
36 );
37 body.call("appendChild", &argv![elem]);
38}
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?
examples/bind.rs (line 28)
27 fn print(&self) {
28 self.val.call("print", &[]);
29 }
30}
31
32impl FromVal for MyJsClass {
33 fn from_val(v: &Val) -> Self {
34 MyJsClass { val: v.clone() }
35 }
36 fn take_ownership(v: Handle) -> Self {
37 Self::from_val(&Val::take_ownership(v))
38 }
39 fn as_handle(&self) -> Handle {
40 self.val.as_handle()
41 }
42}
43
44impl Deref for MyJsClass {
45 type Target = Val;
46
47 fn deref(&self) -> &Self::Target {
48 &self.val
49 }
50}
51
52impl DerefMut for MyJsClass {
53 fn deref_mut(&mut self) -> &mut Self::Target {
54 &mut self.val
55 }
56}
57
58impl From<MyJsClass> for Val {
59 fn from(s: MyJsClass) -> Val {
60 let handle = s.as_handle();
61 std::mem::forget(s);
62 Val::take_ownership(handle)
63 }
64}
65
66fn main() {
67 emlite::init();
68 MyJsClass::define();
69 let c = MyJsClass::new(5, 6);
70 c.call("print", &[]);
71 let b = eval!(
72 r#"
73 let b = new MyJsClass(6, 7);
74 b.print();
75 b
76 "#
77 );
78 let a = b.as_::<MyJsClass>();
79 a.print();
80 let console = Console::get();
81 console.log(&[a.into()]);
82}
More examples
examples/dom.rs (line 6)
3fn main() {
4 emlite::init();
5 let document = Val::global("document");
6 let elem = document.call("createElement", &argv!["BUTTON"]);
7 elem.set("textContent", Val::from("Click"));
8 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
9 elem.call(
10 "addEventListener",
11 &argv![
12 "click",
13 Val::make_fn(|ev| {
14 let console = Console::get();
15 console.call("clear", &[]);
16 console.log(&[ev[0].get("clientX")]);
17 println!("client x: {}", ev[0].get("clientX").as_::<i32>());
18 println!("hello from Rust");
19 Val::undefined()
20 })
21 ],
22 );
23 body.call("appendChild", &argv![elem]);
24}
examples/audio.rs (line 14)
3fn main() {
4 emlite::init();
5 #[allow(non_snake_case)]
6 let mut AudioContext = Val::global("AudioContext");
7 if !AudioContext.as_::<bool>() {
8 println!("No global AudioContext, trying webkitAudioContext");
9 AudioContext = Val::global("webkitAudioContext");
10 }
11
12 println!("Got an AudioContext");
13 let context = AudioContext.new(&[]);
14 let oscillator = context.call("createOscillator", &[]);
15
16 println!("Configuring oscillator");
17 oscillator.set("type", "triangle");
18 oscillator.get("frequency").set::<_, f64>("value", 261.63); // Middle C
19
20 let document = Val::global("document");
21 let elem = document.call("createElement", &argv!["BUTTON"]);
22 elem.set("textContent", "Click");
23 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
24 elem.call(
25 "addEventListener",
26 &argv![
27 "click",
28 Val::make_fn(move |_| {
29 println!("Playing");
30 oscillator.call("connect", &argv![context.get("destination")]);
31 oscillator.call("start", &argv![0]);
32 println!("All done!");
33 Val::undefined()
34 })
35 ],
36 );
37 body.call("appendChild", &argv![elem]);
38}
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?
More examples
examples/audio.rs (line 13)
3fn main() {
4 emlite::init();
5 #[allow(non_snake_case)]
6 let mut AudioContext = Val::global("AudioContext");
7 if !AudioContext.as_::<bool>() {
8 println!("No global AudioContext, trying webkitAudioContext");
9 AudioContext = Val::global("webkitAudioContext");
10 }
11
12 println!("Got an AudioContext");
13 let context = AudioContext.new(&[]);
14 let oscillator = context.call("createOscillator", &[]);
15
16 println!("Configuring oscillator");
17 oscillator.set("type", "triangle");
18 oscillator.get("frequency").set::<_, f64>("value", 261.63); // Middle C
19
20 let document = Val::global("document");
21 let elem = document.call("createElement", &argv!["BUTTON"]);
22 elem.set("textContent", "Click");
23 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
24 elem.call(
25 "addEventListener",
26 &argv![
27 "click",
28 Val::make_fn(move |_| {
29 println!("Playing");
30 oscillator.call("connect", &argv![context.get("destination")]);
31 oscillator.call("start", &argv![0]);
32 println!("All done!");
33 Val::undefined()
34 })
35 ],
36 );
37 body.call("appendChild", &argv![elem]);
38}
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 instanceof(&self, v: Val) -> bool
pub fn instanceof(&self, v: Val) -> bool
Checks whether this Val is an instanceof v
pub fn is_number(&self) -> bool
pub fn is_bool(&self) -> bool
pub fn is_string(&self) -> bool
pub fn is_null(&self) -> bool
pub fn is_undefined(&self) -> bool
pub fn is_error(&self) -> bool
pub fn is_function(&self) -> bool
Sourcepub fn as_<T>(&self) -> Twhere
T: FromVal,
pub fn as_<T>(&self) -> Twhere
T: FromVal,
Examples found in repository?
examples/bind.rs (line 78)
66fn main() {
67 emlite::init();
68 MyJsClass::define();
69 let c = MyJsClass::new(5, 6);
70 c.call("print", &[]);
71 let b = eval!(
72 r#"
73 let b = new MyJsClass(6, 7);
74 b.print();
75 b
76 "#
77 );
78 let a = b.as_::<MyJsClass>();
79 a.print();
80 let console = Console::get();
81 console.log(&[a.into()]);
82}
More examples
examples/dom.rs (line 17)
3fn main() {
4 emlite::init();
5 let document = Val::global("document");
6 let elem = document.call("createElement", &argv!["BUTTON"]);
7 elem.set("textContent", Val::from("Click"));
8 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
9 elem.call(
10 "addEventListener",
11 &argv![
12 "click",
13 Val::make_fn(|ev| {
14 let console = Console::get();
15 console.call("clear", &[]);
16 console.log(&[ev[0].get("clientX")]);
17 println!("client x: {}", ev[0].get("clientX").as_::<i32>());
18 println!("hello from Rust");
19 Val::undefined()
20 })
21 ],
22 );
23 body.call("appendChild", &argv![elem]);
24}
examples/audio.rs (line 7)
3fn main() {
4 emlite::init();
5 #[allow(non_snake_case)]
6 let mut AudioContext = Val::global("AudioContext");
7 if !AudioContext.as_::<bool>() {
8 println!("No global AudioContext, trying webkitAudioContext");
9 AudioContext = Val::global("webkitAudioContext");
10 }
11
12 println!("Got an AudioContext");
13 let context = AudioContext.new(&[]);
14 let oscillator = context.call("createOscillator", &[]);
15
16 println!("Configuring oscillator");
17 oscillator.set("type", "triangle");
18 oscillator.get("frequency").set::<_, f64>("value", 261.63); // Middle C
19
20 let document = Val::global("document");
21 let elem = document.call("createElement", &argv!["BUTTON"]);
22 elem.set("textContent", "Click");
23 let body = document.call("getElementsByTagName", &argv!["body"]).at(0);
24 elem.call(
25 "addEventListener",
26 &argv![
27 "click",
28 Val::make_fn(move |_| {
29 println!("Playing");
30 oscillator.call("connect", &argv![context.get("destination")]);
31 oscillator.call("start", &argv![0]);
32 println!("All done!");
33 Val::undefined()
34 })
35 ],
36 );
37 body.call("appendChild", &argv![elem]);
38}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Console
impl RefUnwindSafe for Console
impl Send for Console
impl Sync for Console
impl Unpin for Console
impl UnwindSafe for Console
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