Console

Struct Console 

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

A console wrapper

Implementations§

Source§

impl Console

Source

pub const fn get() -> Console

Gets the console

Examples found in repository?
examples/console.rs (line 5)
3fn main() {
4    emlite::init();
5    let con = Console::get();
6    con.log(&argv!["Hello from Emlite!"]);
7}
More examples
Hide additional 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}
Source

pub fn log(&self, args: &[Val])

Logs into the console

Examples found in repository?
examples/console.rs (line 6)
3fn main() {
4    emlite::init();
5    let con = Console::get();
6    con.log(&argv!["Hello from Emlite!"]);
7}
More examples
Hide additional 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}
Source

pub fn warn(&self, args: &[Val])

console.warn

Source

pub fn info(&self, args: &[Val])

console.info

Source

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>§

Source

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
Hide additional 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}
Source

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
Hide additional 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}
Source

pub fn has<T: Into<Val>>(&self, prop: T) -> bool

Checks whether a property prop exists

Source

pub fn has_own_property(&self, prop: &str) -> bool

Checks whether a non-inherited property prop exists

Source

pub fn type_of(&self) -> String

Gets the typeof the underlying js object

Source

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
Hide additional 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}
Source

pub fn to_vec<V: FromVal>(&self) -> Vec<V>

Converts the underlying js array to a Vec of V

Source

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
Hide additional 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}
Source

pub fn new(&self, args: &[Val]) -> Val

Calls the object’s constructor with args constructing a new object

Examples found in repository?
examples/bind.rs (line 24)
22    fn new(x: i32, y: i32) -> Self {
23        Self {
24            val: Val::global("MyJsClass").new(&argv![x, y]),
25        }
26    }
More examples
Hide additional 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}
Source

pub fn invoke(&self, args: &[Val]) -> Val

Invokes the function object with args, can return an undefined js value

Source

pub fn await_(&self) -> Val

Awaits the invoked function object

Source

pub fn instanceof(&self, v: Val) -> bool

Checks whether this Val is an instanceof v

Source

pub fn is_number(&self) -> bool

Source

pub fn is_bool(&self) -> bool

Source

pub fn is_string(&self) -> bool

Source

pub fn is_null(&self) -> bool

Source

pub fn is_undefined(&self) -> bool

Source

pub fn is_error(&self) -> bool

Source

pub fn is_function(&self) -> bool

Source

pub fn as_<T>(&self) -> T
where 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
Hide additional 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}
Source

pub fn to_utf16(&self) -> Option<Vec<u16>>

Extracts UTF-16 data as Option<Vec>

Source

pub fn to_utf16_result(&self) -> Result<Vec<u16>, Val>

Extracts UTF-16 data, returning error if null or if self is error

Trait Implementations§

Source§

impl Clone for Console

Source§

fn clone(&self) -> Console

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

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Console

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for Console

Source§

type Target = Val

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for Console

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl From<Console> for Val

Source§

fn from(val: Console) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.