In this lesson we are going to setup a project from scratch by introducing the JavaScript snippet to load a WebAssembly module. We demonstrate two different ways and showcase the benefit of the streaming solution. Once the module is loaded we can invoke a function previously exported from our Rust code.

Create a new project as a library:

cargo new --lib utils

Go to the project folder, modify Cargo.toml:

[package]
name = "utils"
version = "0.1.0"
authors = ["zhentian-wan <answer881215@gmail.com>"]
edition = "" [dependencies] [lib]
crate-type = ["cdylib"]

Create an function in src/lib.rs:

#[no_mangle]
pub extern fn add_one(x: u32) -> u32 {
x + 1
}

The extern keyword is needed to create an interface, so that this function can be invoked from other languages. The function accepts a value x, which is an unsigned integer, which is incremented by one and returned. In addition, we need to add a no-mangle annotation to tell the Rust compiler not to mangle the name of this function.

Run:

cargo build --target wasm32-unknown-unknown --release
wasm-gc target/wasm32-unknown-unknown/release/utils.wasm -o utils.gc.wasm

Create an index.html file and load utils.gc.wasm file we just generated:

<!DOCTYPE html>
<html>
<head>
<script>
WebAssembly.instantiateStreaming(fetch("utils.gc.wasm"))
.then(wasmModule => {
const result = wasmModeult.instance.exports.add_one(3);
const text = document.createTextNode(result);
document.body.appendChild(text);
});
</script>
<head>
<body></body>
<html>

Run:

http

Open the broswer we should see the output 4.

Be aware, instantiate streaming requires that our WASM file must be served with a content type, application/wasm. Fortunately, our HTTP server already does so.

 

[Rust] Load a WebAssembly Function Written in Rust and Invoke it from JavaScript的更多相关文章

  1. [WASM + Rust] Debug a WebAssembly Module Written in Rust using console.log

    Having some kind of debugging tool in our belt is extremely useful before writing a lot of code. In ...

  2. [WASM] Create and Run a Native WebAssembly Function

    In this introduction, we show a simple WebAssembly function that returns the square root of a number ...

  3. Rust初步(四):在rust中处理时间

    这个看起来是一个很小的问题,我们如果是在.NET里面的话,很简单地可以直接使用System.DateTime.Now获取到当前时间,还可以进行各种不同的计算或者输出.但是这样一个问题,在rust里面, ...

  4. Rust 1.31正式发布,首次引入Rust 2018新功能

    Rust 1.31是第一个实现了Rust 2018独有新功能并且不保证与现有代码库兼容的版本.Rust 2018相关工作正在进行中,而Rust 1.31只是整个三年开发周期的开始,这个开发周期将对这门 ...

  5. (function($, window, document) {}) jQuery 调用解决与其他javascript库冲突的写法

    将函数包在红色字体内部,可以解决$符号与其他插件的冲突. <script type="text/javascript"> (function($, window, do ...

  6. 用 Function.apply() 的参数数组化来提高 JavaScript程序性能

    我们再来聊聊Function.apply() 在提升程序性能方面的技巧. 我们先从 Math.max() 函数说起, Math.max后面可以接任意个参数,最后返回所有参数中的最大值. 比如 aler ...

  7. JN_0004:轻松解码类似eval(function(p,a,c,k,e,d){}))的JavaScript代码

    百度访问统计代码JavaScript源码:红色加粗部分将是要修改的地方.eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"&qu ...

  8. JavaScript 之 解码类似eval(function(p,a,c,k,e,d){}))的JavaScript代码

    这里以解码百度访问统计代码构造函数为示例: 以下为要统计JavaScript源码:红色加粗部分将是要修改的地方. eval(function(p,a,c,k,e,d){e=function(c){re ...

  9. Servo: The Embeddable Browser Engine

    Embedding, in the context of this article, is the process of hosting a web rendering engine inside a ...

随机推荐

  1. Metinfo 5.3.19管理员密码重置漏洞复现

     Metinfo 5.3.19管理员密码重置漏洞 操作系统:Windows 10专业版   kali linux  网站环境:UPUPW 5.3 使用工具:burpsuite 1.7 beta 漏洞分 ...

  2. Java POI 导出EXCEL经典实现 Java导出Excel弹出下载框(转载)

    https://blog.csdn.net/evangel_z/article/details/7332535

  3. LCIS 最长上升公共子序列问题

    首先点名一个串叫 L1,另一个叫L2. 明显的是一个DP,那么我们来探讨下如何求得答案. 朴素的算法 首先我们定义状态$dp[ i ][ j ]$表示L1中前i个与L2中前j个的最长公共上升子序列. ...

  4. 配置c3p0-config.xml数据库连接池,jdbcurl配置项报错Type The reference to entity "useUnicode" must end with the ';' delimiter.

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <c3p0-confi ...

  5. Python自动化测试框架——生成测试报告

    如何才能让用例自动运行完之后,生成一张直观可看易懂的测试报告呢? 小编使用的是unittest的一个扩展HTMLTestRunner 环境准备 使用之前,我们需要下载HTMLTestRunner.py ...

  6. 条款6:若不想使用编译器自动生成的函数,就该明确拒绝(Explicity disallow the use of compiler-generated functions you do not want)

    class uncopyable{ protected: uncopyable(){};                                                         ...

  7. git 知识(转)

    转自:http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html Workspace:工作区 Index / Stage:暂存区 Repos ...

  8. pygame试水,写一个贪吃蛇

    最近学完python基础知识,就想着做一个游戏玩玩,于是就在https://www.pygame.org/docs/学着做了个贪吃蛇游戏. 首先要导入模块. import pygame import ...

  9. jquery ajax示例

    $.ajax({ type: "POST",//方法类型 dataType: "json",//预期服务器返回的数据类型 url: "${ctx }/ ...

  10. LeetCode(14)Longest Common Prefix

    题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...