码迷,mamicode.com
首页 > 编程语言 > 详细

[Rust] Pass a JavaScript Function to WebAssembly and Invoke it from Rust

时间:2018-10-21 10:14:27      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:which   promise   anti   col   rip   java   ack   imp   prot   

In some cases it’s useful to be able to invoke a JavaScript function inside Rust. This session showcases how this can be done, by passing along our JavaScript functions to the WebAssembly module instantiation.

 

First let‘s create a function in js:

      const appendNumberToBody = (number) => {
        const text = document.createTextNode(number);
        document.body.appendChild(text);
      }

 

Wrap this function into a single object which contains ‘env‘:

      const importObject = {
        env: {
          appendNumberToBody: appendNumberToBody
        }
      };

 

When we load wasm, we can pass in the object:

WebAssembly.instantiateStreaming(fetch("utils.gc.wasm"), importObject)

It return a promise, we can run the exported function return by wasm inside the promise.

 

Now we are going to create a function in Rust:

extern {
    fn appendNumberToBody(x: u32);
}

#[no_mangle]
pub extern fn run() {
    unsafe { // we need to wrap with unsafe if getting the passed in value from third party
        appendNumberToBody(42);
    }
}

We exprot ‘run‘ function, then we can invoke it inside promise:

      WebAssembly.instantiateStreaming(fetch("utils.gc.wasm"), importObject)
      .then(wasmModule => {
          wasmModule.instance.exports.run();
        });

 

---------

 

Full code: Github

index.html:

<!DOCTYPE html>
<html>
  <head>
    <script> 

      // pass the data from Js to Rust
      const appendNumberToBody = (number) => {
        const text = document.createTextNode(number);
        document.body.appendChild(text);
      }

      const importObject = {
        env: {
          appendNumberToBody: appendNumberToBody,
          alert: alert
        }
      };

      WebAssembly.instantiateStreaming(fetch("utils.gc.wasm"), importObject)
      .then(wasmModule => {
          wasmModule.instance.exports.run();
        });
    </script>
  <head>
  <body></body>
<html>

 

lib.rs:

extern {
    fn appendNumberToBody(x: u32);
    fn alert(x: u32);
}

#[no_mangle]
pub extern fn run() {
    unsafe {
        appendNumberToBody(42);
        alert(33)
    }
}

 

[Rust] Pass a JavaScript Function to WebAssembly and Invoke it from Rust

标签:which   promise   anti   col   rip   java   ack   imp   prot   

原文地址:https://www.cnblogs.com/Answer1215/p/9823954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!