[WASM] Run WebAssembly in Node.js using the node-loader
WebAssembly is great for targeting performance bottlenecks in the browser. Now with node-loader, we can do the same on the server through Node.js
While Node offers also bindings to native extensions for C/C++ via node-gyp, there was no straight forward way to write Rust extensions. In addition did the node-gyp API change often and developers had to be careful to make their C/C++ code work with various operating systems.
Compiling Rust to WebAssembly solves both of these issues. The API is stable and once compiled to WebAssembly it will run on every operating system supported by Node.
Since Node doesn't support loading .wasm
files a loader is needed. The loader flag though is only supported when using the --experimental-modules
flag in Node 10 or higher.
Install:
npm i --save @wasm-tool/node
Run:
cargo new crate --name=utils --lib
Cargo.toml:
[package] name = "utils" version = "0.1.0" authors = ["zhentian-wan <answer881215@gmail.com>"] edition = "2018" [lib] crate-type = ["cdylib"] [dependencies]
Build:
cd crate
cargo build --target wasm32-unknown-unknown --release
Create js file:
index.mjs
import { add_one } from "./crate/target/wasm32-unknown-unknown/release/output.wasm" console.log(add_one(1)); console.log(add_one(41));
Run:
node --experimental-modules --loader @wasm-tool/node index.mjs
相关文章
- [Node.js] Show More Lines in a Node.js Error Stack Trace
- [WASM] Run WebAssembly in Node.js using the node-loader
- [Node.js] Creating Demo APIs with json-server
- [Node.js] Broswerify -- 2
- vue.js开发SPA常见问题及解决方法
- [Node.js] Polling
- [Node.js] Sequelize Intro - 2 Migrations
- [Node.js] Gzip + crypto in stream
- [Node.js] Trigger a File Download in Express
- [Node.js] Test Node RESTful API with Mocha and Chai
- [Node.js] Using npm link to use node modules that are "in progress"
- [Node.js] Configuring npm package.json scripts
- vue.js3:分享到微信好友/朋友圈(卡片形式/图片文字形式)(vue@3.2.33 / weixin-js-sdk@1.6.0)
- vue.js3: 使用js-md5 (js-md5@0.7.3 / vue@3.2.26)
- Atitit node.js问题解决总结t99 目录 1.1. 找不到node程序1 1.2. GBK编码问题1 2. 按行读取gbk编码问题2 2.1. 先写入txt utf8 encode
- 华为OD机试 - 最大平分数组(Java & JS & Python)
- 慕课网前端项目:Vue2.0+Node.js+MongoDB全栈打造商城系统 笔记 整理【6/18】