[][src]Macro neon::declare_types

macro_rules! declare_types {
    { $(#[$attr:meta])* pub class $cls:ident for $typ:ident { $($body:tt)* } $($rest:tt)* } => { ... };
    { $(#[$attr:meta])* class $cls:ident for $typ:ident { $($body:tt)* } $($rest:tt)* } => { ... };
    { $(#[$attr:meta])* pub class $cls:ident as $cname:ident for $typ:ty { $($body:tt)* } $($rest:tt)* } => { ... };
    { $(#[$attr:meta])* class $cls:ident as $cname:ident for $typ:ty { $($body:tt)* } $($rest:tt)* } => { ... };
    { } => { ... };
}

Declare custom native JavaScript types with Rust implementations.

Example:

This example is not tested
pub struct Greeter {
    greeting: String
}

declare_types! {

    /// A class for generating greeting strings.
    pub class JsGreeter for Greeter {
        init(mut cx) {
            let greeting = cx.argument(0)?.to_string(&mut cx)?.value();
            Ok(Greeter {
                greeting: greeting
            })
        }

        method hello(mut cx) {
            let name = cx.argument(0)?.to_string(&mut cx)?.value();
            let this = cx.this();
            let msg = {
                let guard = cx.lock();
                let greeter = this.borrow(&guard);
                format!("{}, {}!", greeter.greeting, name)
            };
            Ok(cx.string(&msg[..])?.upcast())
        }
    }

}