title: How to return a Rust closure to JavaScript via WebAssembly?
body: <p>The comments on <a href="https://github.com/rustwasm/wasm-bindgen/blob/master/src/closure.rs" rel="nofollow noreferrer">closure.rs</a> are pretty great, however I can't make it work for returning a closure from a WebAssembly library.</p>
  
  <p>I have a function like this:</p>
  
  <pre><code>#[wasm_bindgen]
  pub fn start_game(
      start_time: f64,
      screen_width: f32,
      screen_height: f32,
      on_render: &amp;js_sys::Function,
      on_collision: &amp;js_sys::Function,
  ) -&gt; ClosureTypeHere {
      // ...
  }
  </code></pre>
  
  <p>Inside that function I make a closure, assuming <code>Closure::wrap</code> is one piece of the puzzle, and copying from closure.rs):</p>
  
  <pre><code>let cb = Closure::wrap(Box::new(move |time| time * 4.2) as Box&lt;FnMut(f64) -&gt; f64&gt;);
  </code></pre>
  
  <p>How do I return this callback from <code>start_game</code> and what should <code>ClosureTypeHere</code> be?</p>
  
  <p>The idea is that <code>start_game</code> will create local mutable objects - like a camera, and the JavaScript side should be able to call the function Rust returns in order to update that camera.</p>