Macro stdweb::js [] [src]

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

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

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