2 个版本 (1 个稳定版)

1.0.0 2021 年 9 月 12 日
0.9.0 2021 年 9 月 12 日

#11 in #console-log

MIT/Apache

7KB
115 行代码(不含注释)

WebAssembly



这是一个用于测试 WebAssembly 的项目。

链接到 rust 项目文档

链接到 wasm-bindgen 文档

链接到此项目生成的 npm 包

测试库的 JS/TS 代码


  • 首先安装包 => npm i web-assembly-whipshout

  • 复制以下代码

信息


  • Rust 生成用于 TS 的 *.d.ts 类型文件

  • 返回的 JS 值块使用 JS 变量类型

  • 返回的 Rust 值块使用 Rust 变量类型

  • Provider.new 使用 Rust 结构体创建新对象

  • 不能直接从提供者获取数据,就像在 JS 中那样,我们必须使用在 Struct 中声明的 Rust 方法来获取数据

  • 我们可以将 Rust 结构体序列化和反序列化到 JS 对象,反之亦然

  • 对于 JS 数组,我们可以使用 Rust 向量或 Rust 库中的 JsArray

  • 取消 for 循环的注释以测试 Rust 和 JS 中的 fibonacci 性能(50 次迭代花费很多时间)


import {
  Provider,
  fibonacci,
  parse_string,
  parse_string_option,
  convert_to_provider,
  send_vector,
  send_array,
  return_array_modified
} from 'web-assembly-whipshout';

// JS values returned
let js_ok = parse_string("6")
let js_fail = parse_string("hello")

// Rust values returned
let rust_ok = parse_string_option("7")
let rust_fail = parse_string_option("world")

console.log("---------")
console.log("---------")
console.log(js_ok)
console.log(js_fail)
console.log("---------")
console.log("---------")
console.log(rust_ok)
console.log(rust_fail)
console.log("*********")
console.log("*********")
// -------------------------

const provider = Provider.new(1,"Magic")
const id = provider.id
const name = provider.name
console.log("---------")
console.log("---------")
// Returns the structure with a pointer
console.log(provider)
// Returns the correct info, needed specific methods to extract it
console.log(id)
console.log(name)
console.log("---------")
console.log("---------")
// Changes info of the structure using Rust setters
provider.id = 2
provider.name = "JetBrains"
const id2 = provider.id
const name2 = provider.name
console.log(id2)
console.log(name2)
console.log("---------")
console.log("---------")
// Convert Rust structure to JS object
const object = provider.get_object()
console.log(object)
console.log("---------")
console.log("---------")
// Convert JS object to Rust structure
const struct = convert_to_provider(object)
if (struct) {
  const id3 = struct.id
  const name3 = struct.name
  console.log(struct)
  console.log(id3)
  console.log(name3)
}
console.log("---------")
console.log("---------")
// Rust vector equals to JS array
const vector = send_vector()
console.log(vector)
console.log(vector[0])
console.log("---------")
console.log("---------")
// Gets JS array from Rust
const arr = send_array()
console.log(arr)
console.log(arr[0])
console.log("---------")
console.log("---------")
// Send JS array and get JS array modified
const arr_modified = return_array_modified([1, 2, 3, 4, 5])
console.log(arr_modified)
console.log("*********")
console.log("*********")
// -------------------------

function fibonacciJS(x: number): number {
  if (x === 0) {
    return 0
  }

  if (x === 1) {
    return 1
  }

  return fibonacciJS(x - 1) + fibonacciJS(x - 2)
}

// Test performance (rust > js, around +35 rust gets so much much much better)
// Shows execution times for each step, left rust, right js
// for (let i = 0; i < 50; i++) {
//   const wasmStart = Date.now()
//   fibonacci(i)
//   const wasmDuration = Date.now() - wasmStart
//
//   const jsStart = Date.now()
//   fibonacciJS(i)
//   const jsDuration = Date.now() - jsStart
//
//   console.log(`${i},${wasmDuration},${jsDuration}`)
// }

依赖项

~7–9.5MB
~185K SLoC