#wasm-module #alloc #setup #package #dealloc #browser #nopackage

wasm-nopackage

为轻量级WASM模块提供基本的分配、释放和模块设置

3个版本

0.1.5 2023年8月5日
0.1.4 2023年8月5日

#193 in 内存管理

GPL-2.0-only

9.5MB
202

包含 (Rust库, 2.5MB) libtypenum-16e81b5c6d389767.rlib, (Mach-o可执行文件, 1.5MB) build-script-build, (Mach-o可执行文件, 1.5MB) build_script_build-97c11c13da657bee, (Rust库, 1.5MB) libcc-58404837f2f112f1.rlib, (Rust库, 2MB) liblibc-653ed1727f51485d.rlib, (Rust库, 1.5MB) libgeneric_array-364554543fbf6265.rlib 等20个.

#wasm-nopackage

一个简单的包,提供分配、释放以及将字符串发送到JavaScript的方法。

使用Rust

以下代码片段使用了 go_liveset_plugin_name,允许JavaScript有效地调用并响应,扩展了包的默认值。已使用函数使基础模块了解应用程序使用的值。

JavaScript将只调用 startup

use wasm_nopackage::{go_live,set_plugin_name};

// In the final module that exposes itself to JavaScript, 
// this method will be mapped to the JavaScript caller.
// This might have been included in the base crate, but 
// the crate publisher looks for the symbol and fails to publish.
// It is hoped that this is not too much for those who use 
// the crate to include these fiew lines.

#[link(wasm_import_module = "mod")]
extern "C" {
    fn message_js(s: *mut i8, size: usize);
}


// This function allows a mutable static reference to be set
// so that ex_message(alertable: &String) can be used generally.
// This module might just use this method. But, there may be other modules
// that can include wasm_nopackage::ex_message.
//
fn output_string(s: *mut i8, size: usize) -> () {
    unsafe {
        message_js(s,size);
    }
}


// The module name is a function here rather than in a global variable.
// This function will be called when plugin_name and plugin_name_len
// are called by JavaScript.
fn my_plugin_name() -> &'static str {
    let my_str : &str = "Test Lib";  // <- the name of the lib here.
    return my_str;
}


///
/// free memory taken up in linear memory.
/// The original pointer offset will be found in ptr, and the size of the previously allocated block is required.
#[no_mangle]
pub fn startup() {
    go_live(output_string);
    set_plugin_name(my_plugin_name);
}

JavaScript

以下代码是一个使用从wasm-nopackage派生出来的模块的类的测试程序。

<script src="js/wasm_wrap.js" ></script>
<script>
  async function main() {
    //
    let wasmod = new WASMInterface('/wasm/testup')
    await wasmod.init("mod")
    wasmod.startup()  // entrypoint is from the test module call tables
    wasmod.test_callback();  // wasm-nopackage provides this entry point.
    console.log(wasmod.plugin_name_str())
    //
  }
  //
  setTimeout(main,1000)

</script

在上面的例子中,wasm_wrap.js 在浏览器的window级别定义了WASMInterface。因此,此示例没有使用模块。但,可能有一个使用模块的版本。

WASMInterface类的定义包括此定义。将 wasm-nopackage 和使用它的WASM库暴露的所有方法添加到类中,可以作为JavaScript类的成员函数调用。

async init(module_name) {
    //
    let importObject = {};
    //
    importObject[module_name] = {
        __memory_base: 0,
        __table_base: 0,
        memory: new WebAssembly.Memory({ initial: 1 }),
        "message_js": (str_offset, size) => { this.wasm_alert(str_offset, size); }
    };
    this.mod = await this.get_wasm_module(this._mod_path, importObject);
    let self = this;
    for (let ky in this.mod) {
        self[ky] = this.mod[ky];
    }
    //
}

WASMInterface JavaScript类提供了一些读取和写入WASM内存中的字符串的方法,进行适当的分配。使用JavaScript方法设置调用,将它们管理的引用作为字符串或内存中的数据的指针可能很有帮助。

无运行时依赖