WebAssembly 初探

SSVM简介

SSVM是适用于云,AI和区块链应用程序的高性能,企业级WebAssembly(WASM)虚拟机。包括以下用例:

  • Node.js应用程序中Rust函数的高性能和安全运行时
  • 针对ONNX AI模型的硬件优化的运行时
  • 适用于领先的区块链平台的智能合约运行时引擎

一个简单的Web App

功能:base64的编码和解码

首先在rust的lib文件中编写实际的用来编码和解码的代码:

use base64::{decode, encode};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn base64Encode(s: &str) -> String {
    encode(s)
}

#[wasm_bindgen]
pub fn base64Decode(s: &str) -> String {
    String::from_utf8(decode(s).unwrap()).unwrap()
}

然后在node中调用rust代码:

const {
    base64Encode,
    base64Decode,
} = require('../pkg/ssvm_nodejs_starter_lib.js')

const http = require('http')
const url = require('url')
const hostname = '0.0.0.0'
const port = 3000

const server = http.createServer((req, res) => {
    const queryObject = url.parse(req.url, true).query
    if (queryObject['encodeStr']) {
        res.end(base64Encode(queryObject['encodeStr']) + '\n')
    } else if (queryObject['decodeStr']) {
        console.log(queryObject['decodeStr'])
        res.end(base64Decode(queryObject['decodeStr']) + '\n')
    } else {
        res.end(
`Please use command curl http://${hostname}:${port}/?encodeStr=string
or
http://${hostname}:${port}/?decodeStr=string \n`
        )
    }
})

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`)
})

实际运行效果

编码.png
解码.png

你可能感兴趣的:(WebAssembly 初探)