externref允许WebAssembly直接持有和传递JavaScript对象引用,解决了类型转换、性能损耗和复杂映射的痛点,实现了Wasm与JS间高效、自然的交互。

WebAssembly Reference Types,特别是其中的
externref
以前,想把一个JavaScript对象传给Wasm,那可真是个“体力活”。我们通常得把它序列化成字符串,或者在JS这边维护一个数组,把对象放进去,然后把数组的索引传给Wasm。Wasm拿到索引后,再通过一个JS导入函数去取这个对象。这中间的开销和心智负担,说实话,挺让人头疼的。
但有了
externref
externref
具体的交互流程,我们可以通过一个简单的WebAssembly Text Format (WAT) 模块和对应的JavaScript胶水代码来理解:
立即学习“Java免费学习笔记(深入)”;
(module
(type $t0 (func (param externref))) ;; 定义一个函数类型,接受一个externref参数
(type $t1 (func (param externref) (result externref))) ;; 定义一个函数类型,接受并返回一个externref
;; 导入一个JS函数,用于打印externref引用的JS对象
(import "js" "log_object" (func $log_object (type $t0)))
;; 导出函数:接受一个externref,并将其传给导入的JS函数
(func (export "process_object") (param $obj externref)
local.get $obj
call $log_object
)
;; 导出函数:接受一个externref,并原样返回
(func (export "pass_through_object") (param $obj externref) (result externref)
local.get $obj
)
;; 定义一个可变的全局变量来存储一个externref,初始值为null
(global $stored_ref (mut externref) (ref.null extern))
;; 导出函数:将传入的externref存储到全局变量
(func (export "store_object_globally") (param $obj externref)
local.get $obj
global.set $stored_ref
)
;; 导出函数:获取全局变量中存储的externref
(func (export "get_stored_object") (result externref)
global.get $stored_ref
)
)对应的JavaScript代码:
// 假设上述WAT内容已编译为your_module.wasm文件
async function initWasm() {
const importObject = {
js: {
log_object: (obj) => {
console.log("Wasm received JS object:", obj);
// 在JS侧,我们可以像操作普通JS对象一样操作这个obj
if (obj && typeof obj === 'object' && 'name' in obj) {
console.log("Object name property from JS:", obj.name);
}
}
}
};
const response = await fetch('your_module.wasm'); // 实际项目中,你需要将上述WAT编译成.wasm文件
const buffer = await response.arrayBuffer();
const module = await WebAssembly.instantiate(buffer, importObject);
const {
process_object,
pass_through_object,
store_object_globally,
get_stored_object
} = module.instance.exports;
const myJsObject = { id: 123, name: "WebAssembly is cool", data: [1, 2, 3] };
console.log("\n--- 测试 process_object ---");
process_object(myJsObject); // 将JS对象直接传给Wasm,Wasm再传给导入的JS函数
console.log("\n--- 测试 pass_through_object ---");
const returnedObj = pass_through_object(myJsObject); // Wasm接收并返回同一个JS对象
console.log("Wasm返回的JS对象与传入的是同一个吗?", returnedObj === myJsObject); // 预期为 true
console.log("\n--- 测试全局存储 ---");
const anotherJsObject = { message: "Stored globally in Wasm!", value: 42 };
store_object_globally(anotherJsObject); // 将JS对象存储到Wasm的全局变量
console.log("JS对象已存储到Wasm全局变量中。");
const retrievedObj = get_stored_object(); // 从Wasm全局变量中取回JS对象
console.log("从Wasm全局变量中取回的对象:", retrievedObj);
console.log("取回的对象与原对象是同一个吗?", retrievedObj === anotherJsObject); // 预期为 true
// 清除Wasm中的全局引用,使其有机会被JS GC回收
store_object_globally(null);
console.log("Wasm全局引用已清除。");
}
initWasm().catch(console.error);externref
在我看来,
externref
externref
externref
externref
externref
externref
以上就是如何用WebAssembly Reference Types与JavaScript对象交互?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号