[][src]Macro stdweb::js

macro_rules! js {
    (@($($flags:tt),*) $($token:tt)*) => { ... };
    ($($token:tt)*) => { ... };
}

Embeds JavaScript code into your Rust program.

This macro supports normal JavaScript syntax, albeit with a few limitations:

  • String literals delimited with ' are not supported.
  • Semicolons are always required.
  • The macro will hit the default recursion limit pretty fast, so you'll probably want to increase it with #![recursion_limit="500"]. (This is planned to be fixed once procedural macros land in stable Rust.)
  • Any callbacks passed into JavaScript will leak memory by default! You need to call .drop() on the callback from the JavaScript side to free it.

You can pass Rust expressions into the JavaScript code with @{...expr...}. The value returned by this macro is an instance of Value.

Examples

Regular Usage

let name = "Bob";
let result = js! {
    console.log( "Hello " + @{name} + "!" );
    return 2 + 2;
};

println!( "2 + 2 = {:?}", result );

Note: you must include the return ...; statement to get a value.

No Return

If you don't need to return a value from your snippet you can add a @(no_return) attribute to slightly improve performance.

let name = "Bob";
js! { @(no_return)
    console.log( "Hello " + @{name} + "!" );
};