Macro neon::declare_types [] [src]

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:

pub struct Greeter {
    greeting: String
}

declare_types! {

    /// A class for generating greeting strings.
    pub class JsGreeter for Greeter {
        init(call) {
            let scope = call.scope;
            let greeting = try!(try!(call.arguments.require(scope, 0)).to_string(scope)).value();
            Ok(Greeter {
                greeting: greeting
            })
        }

        method hello(call) {
            let scope = call.scope;
            let name = try!(try!(call.arguments.require(scope, 0)).to_string(scope)).value();
            let msg = vm::lock(call.arguments.this(scope), |greeter| {
                format!("{}, {}!", greeter.greeting, name)
            });
            Ok(try!(JsString::new_or_throw(scope, &msg[..])).upcast())
        }
    }

}