Tour of WebAssembly Table of Contents

[Untranslated] Returning References

We run into an issue with dynamic functions when we want to return a reference to an object. WebAssembly can only pass around numbers! To break past this challenge, we must establish a convention of using a numerical handle to represent a reference to our object in JavaScript.

let query_selector_handle = register_function("
  (context, selectorStart, selectorEnd) => {
    let selector = context.getUtf8FromMemory(
      selectorStart,selectorEnd);
    let domEl = document.querySelector(selector);
    let objHandle = context.storeObject(domEl);
    return objHandle;
  }");

let selector = "#fancy-button";

let dom_element_handle = js_invoke_with_2_params(
    log_handle,selector.as_ptr() as f64, 
    selector.len() as f64 );
Mascot Ferris