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的更多相关文章

  1. [WASM] Call a JavaScript Function from WebAssembly

    Using WASM Fiddle, we show how to write a simple number logger function that calls a consoleLog func ...

  2. Understanding JavaScript Function Invocation and "this"

    Understanding JavaScript Function Invocation and "this" 11 Aug 2011 Over the years, I've s ...

  3. href="javascript:function()" 和onclick的区别

    href='javascript:function()'和onclick能起到同样的效果,一般来说,如果要调用脚本还是在onclick事件里面写代码,而不推荐在href='javascript:fun ...

  4. 关于<a href='javascript:function()'>

    <a href='javascript:function()'> 这样写是为了让这个链接不要链接到新页面转而执行一段js代码.和onclick能起到同样的效果,一般来说,如果要调用脚本还是 ...

  5. javascript function对象

    <html> <body> <script type="text/javascript"> Function.prototype.get_my_ ...

  6. JavaScript function函数种类(转)

    转自:http://www.cnblogs.com/polk6/p/3284839.html JavaScript function函数种类 本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通 ...

  7. JavaScript function函数种类介绍

    JavaScript function函数种类介绍 本篇主要介绍普通函数.匿名函数.闭包函数 1.普通函数介绍 1.1 示例 ? 1 2 3 function ShowName(name) {     ...

  8. 【转】onclick事件与href='javascript:function()'的区别

    href='javascript:function()'和onclick能起到同样的效果,一般来说,如果要调用脚本还是在onclick事件里面写代码,而不推荐在href='javascript:fun ...

  9. javascript:function 函数声明和函数表达式 详解

    函数声明(缩写为FD)是这样一种函数: 有一个特定的名称 在源码中的位置:要么处于程序级(Program level),要么处于其它函数的主体(FunctionBody)中 在进入上下文阶段创建 影响 ...

随机推荐

  1. JS原生增删,判断class是否存在方法

    function hasClass(obj, cls) { if (obj.className) { return obj.className.match(new RegExp('(\\s|^)' + ...

  2. 对linux中source,fork,exec的理解以及case的 使用

    fork   使用 fork 方式运行 script 时, 就是让 shell(parent process) 产生一个 child process 去执行该 script, 当 child proc ...

  3. Linux服务器硬件设备信息查看

    一.cpu信息 cpu信息存储在/proc文件系统的cpuinfo(/proc/cpuinfo)文件里,可以直接查看这个文件以获得cpu信息,所列字段解释如下: processor : 核心编号,如: ...

  4. Selenium启动项参数设置

    再Selenium中使用不同的Webdriver可能会有不一样的方法,有些相同的操作会得到不同的结果, 本文主要介绍的是Chrome()的使用方法. 其他的Webdriver可以参考官方文档 Chro ...

  5. Java-从字符串或一个子字符串中搜索一个字符

    indexOf函数 package com.tj; public class MyClass implements Cloneable { public static void main(String ...

  6. 【01】在 issue 中创建 list

    [01]在 issue 中创建 list 你想在你的 issue中看到可多选的 list 么? 当你查看问题时,你想不想让它变成 2 of 5 这样的形式. 如果想,你可以在 issue 中使用以下句 ...

  7. GT使用说明

    GT文档:https://gt.qq.com/docs.html GT Android版的详细使用手册: https://gt.qq.com/docs/a/GTAndroidUserGuide.pdf

  8. NYOJ 232 How to eat more Banana

    How to eat more Banana 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 A group of researchers are designing ...

  9. LINQ学习笔记 Join 与 Group join

    LINQ中的Join对应T-SQL中的内连接,并无左连接的方法,当然也没有右连接. 要达成Left join必须依靠GroupJoin来完成. GroupJoin顾名思义就是先集团在做加入,加入的不同 ...

  10. 转 C++STL之string

    http://www.cnblogs.com/wangkangluo1/archive/2011/07/22/2114118.html string类的构造函数: string(const char ...