gpt4 book ai didi

javascript - 将 JavaScript 字符串传递给编译为 WebAssembly 的 Rust 函数

转载 作者:数据小太阳 更新时间:2023-10-29 04:33:10 30 4
gpt4 key购买 nike

我有这个简单的 Rust 函数:

#[no_mangle]
pub fn compute(operator: &str, n1: i32, n2: i32) -> i32 {
match operator {
"SUM" => n1 + n2,
"DIFF" => n1 - n2,
"MULT" => n1 * n2,
"DIV" => n1 / n2,
_ => 0
}
}

我正在成功地将其编译为 WebAssembly,但无法将 operator 参数从 JS 传递给 Rust。

调用 Rust 函数的 JS 行如下所示:

instance.exports.compute(operator, n1, n2);

operator是一个JS Stringn1,n2是JS Number

n1n2 被正确传递并且可以在编译函数中读取,所以我猜问题是我如何传递字符串。我想它是作为一个指针从 JS 传递到 WebAssembly,但找不到关于它如何工作的证据或 Material 。

我没有使用 Emscripten 并希望它保持独立(编译目标 wasm32-unknown-unknown),但我看到他们将编译后的函数包装在 Module.cwrap,也许这会有所帮助?

最佳答案

最简单和最惯用的解决方案

大多数人应该使用wasm-bindgen ,这让整个过程变得简单多了!

低级手动实现

要在 JavaScript 和 Rust 之间传输字符串数据,您需要决定

  1. 文本的编码:UTF-8(Rust 原生)或 UTF-16(JS 原生)。
  2. 谁将拥有内存缓冲区:JS(调用者)或 Rust(被调用者)。
  3. 如何表示字符串数据和长度:NUL 终止(C 风格)或不同长度(Rust 风格)。
  4. 如果数据和长度是分开的,如何传达它们。

通用设置

为 WASM 构建 C dylibs 以帮助它们更小是很重要的。

Cargo.toml

[package]
name = "quick-maths"
version = "0.1.0"
authors = ["An Devloper <an.devloper@example.com>"]

[lib]
crate-type = ["cdylib"]

.cargo/config

[target.wasm32-unknown-unknown]
rustflags = [
"-C", "link-args=--import-memory",
]

package.json

{
"name": "quick-maths",
"version": "0.1.0",
"main": "index.js",
"author": "An Devloper <an.devloper@example.com>",
"license": "MIT",
"scripts": {
"example": "node ./index.js"
},
"dependencies": {
"fs-extra": "^8.0.1",
"text-encoding": "^0.7.0"
}
}

我正在使用 NodeJS 12.1.0。

执行

$ rustup component add rust-std --target wasm32-unknown-unknown
$ cargo build --release --target wasm32-unknown-unknown

方案一

我决定:

  1. 将JS字符串转为UTF-8,即 TextEncoder JS API 是最合适的。
  2. 调用者应该拥有内存缓冲区。
  3. 让长度成为一个单独的值。
  4. 应该进行另一个结构和分配来保存指针和长度。

lib/src.rs

// A struct with a known memory layout that we can pass string information in
#[repr(C)]
pub struct JsInteropString {
data: *const u8,
len: usize,
}

// Our FFI shim function
#[no_mangle]
pub unsafe extern "C" fn compute(s: *const JsInteropString, n1: i32, n2: i32) -> i32 {
// Check for NULL (see corresponding comment in JS)
let s = match s.as_ref() {
Some(s) => s,
None => return -1,
};

// Convert the pointer and length to a `&[u8]`.
let data = std::slice::from_raw_parts(s.data, s.len);

// Convert the `&[u8]` to a `&str`
match std::str::from_utf8(data) {
Ok(s) => real_code::compute(s, n1, n2),
Err(_) => -2,
}
}

// I advocate that you keep your interesting code in a different
// crate for easy development and testing. Have a separate crate
// with the FFI shims.
mod real_code {
pub fn compute(operator: &str, n1: i32, n2: i32) -> i32 {
match operator {
"SUM" => n1 + n2,
"DIFF" => n1 - n2,
"MULT" => n1 * n2,
"DIV" => n1 / n2,
_ => 0,
}
}
}

index.js

const fs = require('fs-extra');
const { TextEncoder } = require('text-encoding');

// Allocate some memory.
const memory = new WebAssembly.Memory({ initial: 20, maximum: 100 });

// Connect these memory regions to the imported module
const importObject = {
env: { memory }
};

// Create an object that handles converting our strings for us
const memoryManager = (memory) => {
var base = 0;

// NULL is conventionally at address 0, so we "use up" the first 4
// bytes of address space to make our lives a bit simpler.
base += 4;

return {
encodeString: (jsString) => {
// Convert the JS String to UTF-8 data
const encoder = new TextEncoder();
const encodedString = encoder.encode(jsString);

// Organize memory with space for the JsInteropString at the
// beginning, followed by the UTF-8 string bytes.
const asU32 = new Uint32Array(memory.buffer, base, 2);
const asBytes = new Uint8Array(memory.buffer, asU32.byteOffset + asU32.byteLength, encodedString.length);

// Copy the UTF-8 into the WASM memory.
asBytes.set(encodedString);

// Assign the data pointer and length values.
asU32[0] = asBytes.byteOffset;
asU32[1] = asBytes.length;

// Update our memory allocator base address for the next call
const originalBase = base;
base += asBytes.byteOffset + asBytes.byteLength;

return originalBase;
}
};
};

const myMemory = memoryManager(memory);

fs.readFile('./target/wasm32-unknown-unknown/release/quick_maths.wasm')
.then(bytes => WebAssembly.instantiate(bytes, importObject))
.then(({ instance }) => {
const argString = "MULT";
const argN1 = 42;
const argN2 = 100;

const s = myMemory.encodeString(argString);
const result = instance.exports.compute(s, argN1, argN2);

console.log(result);
});

执行

$ yarn run example
4200

解决方案2

我决定:

  1. 将JS字符串转为UTF-8,即 TextEncoder JS API 是最合适的。
  2. 模块应该拥有内存缓冲区。
  3. 让长度成为一个单独的值。
  4. 使用Box<String>作为底层数据结构。这允许 Rust 代码进一步使用分配。

src/lib.rs

// Very important to use `transparent` to prevent ABI issues
#[repr(transparent)]
pub struct JsInteropString(*mut String);

impl JsInteropString {
// Unsafe because we create a string and say it's full of valid
// UTF-8 data, but it isn't!
unsafe fn with_capacity(cap: usize) -> Self {
let mut d = Vec::with_capacity(cap);
d.set_len(cap);
let s = Box::new(String::from_utf8_unchecked(d));
JsInteropString(Box::into_raw(s))
}

unsafe fn as_string(&self) -> &String {
&*self.0
}

unsafe fn as_mut_string(&mut self) -> &mut String {
&mut *self.0
}

unsafe fn into_boxed_string(self) -> Box<String> {
Box::from_raw(self.0)
}

unsafe fn as_mut_ptr(&mut self) -> *mut u8 {
self.as_mut_string().as_mut_vec().as_mut_ptr()
}
}

#[no_mangle]
pub unsafe extern "C" fn stringPrepare(cap: usize) -> JsInteropString {
JsInteropString::with_capacity(cap)
}

#[no_mangle]
pub unsafe extern "C" fn stringData(mut s: JsInteropString) -> *mut u8 {
s.as_mut_ptr()
}

#[no_mangle]
pub unsafe extern "C" fn stringLen(s: JsInteropString) -> usize {
s.as_string().len()
}

#[no_mangle]
pub unsafe extern "C" fn compute(s: JsInteropString, n1: i32, n2: i32) -> i32 {
let s = s.into_boxed_string();
real_code::compute(&s, n1, n2)
}

mod real_code {
pub fn compute(operator: &str, n1: i32, n2: i32) -> i32 {
match operator {
"SUM" => n1 + n2,
"DIFF" => n1 - n2,
"MULT" => n1 * n2,
"DIV" => n1 / n2,
_ => 0,
}
}
}

index.js

const fs = require('fs-extra');
const { TextEncoder } = require('text-encoding');

class QuickMaths {
constructor(instance) {
this.instance = instance;
}

difference(n1, n2) {
const { compute } = this.instance.exports;
const op = this.copyJsStringToRust("DIFF");
return compute(op, n1, n2);
}

copyJsStringToRust(jsString) {
const { memory, stringPrepare, stringData, stringLen } = this.instance.exports;

const encoder = new TextEncoder();
const encodedString = encoder.encode(jsString);

// Ask Rust code to allocate a string inside of the module's memory
const rustString = stringPrepare(encodedString.length);

// Get a JS view of the string data
const rustStringData = stringData(rustString);
const asBytes = new Uint8Array(memory.buffer, rustStringData, encodedString.length);

// Copy the UTF-8 into the WASM memory.
asBytes.set(encodedString);

return rustString;
}
}

async function main() {
const bytes = await fs.readFile('./target/wasm32-unknown-unknown/release/quick_maths.wasm');
const { instance } = await WebAssembly.instantiate(bytes);
const maffs = new QuickMaths(instance);

console.log(maffs.difference(100, 201));
}

main();

执行

$ yarn run example
-101

请注意,此过程可用于其他类型。您“只需”决定如何将数据表示为双方同意的一组字节,然后将其发送。

另见:

关于javascript - 将 JavaScript 字符串传递给编译为 WebAssembly 的 Rust 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49014610/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com