Macro embed_js::js [] [src]

macro_rules! js {
    ([$($name:ident as $t:ident),*$(,)*] $($tt:tt)*) => { ... };
    ({$($tt:tt)*}) => { ... };
}

Call javascript, inline.

This macro must not be called from within any other macro, otherwise it will fail to work.

The javascript written inside calls to this macro must adhere to some additional rules:

  • Every statement must end in a semi-colon.
  • No single-quote multi-character strings are allowed.

There are three forms for calling this macro:

  • js!([arg1 as type1, arg2 as type2, ...] -> ret_type { /*javascript*/ })

    In this form, you specify arguments and a return type. Every argument must have a type attached to it via as. Each argument is cast to this type before being passed to the javascript. Every type specified, including the return type, must be one of i32, i64, f32 or f64. These are the only raw types supported by WebAssembly for interop at the moment. More complicated types can be passed by using integers to index into memory in the WebAssembly module.

    Examples:

    Be careful when using this code, it's not being tested!
    let x = 2;
    let y = js!([x as i32] -> i32 {
      return x + 1;
    });
    // y is now 3
    Be careful when using this code, it's not being tested!
    let one_half = js!([] -> f64 {
      return 1.0 / 2.0;
    });
  • js!([arg1 as type1, arg2 as type2, ...] { /*javascript*/ })

    Like the previous form, but without a return type. Any value returned from javascript is discarded.

  • js!({ /*javascript*/ })

    No arguments or return type.