zoukankan      html  css  js  c++  java
  • [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
  • 相关阅读:
    MySql8安装使用中的一些注意
    如何在CentOS 8主机上安装Nginx Web服务器
    centos安装sqlserver
    VSCode快捷键
    C#中的委托
    Winform加载loading界面
    JayRock的一些用法:json and json rpc for .Net
    winform picturebox控件 定时让图片轮播
    sql server创建存储过程
    ftp上传单一文件示例
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9955883.html
Copyright © 2011-2022 走看看