我正在关注此处的解决方案: 如何从 WebAssembly 函数返回 JavaScript 字符串 和此处: 如何在 WebAssembly 中从 Rust 返回字符串(或类似的)?
但是,当从内存中读取时,我没有得到想要的结果。
AssemblyScript 文件,helloWorldModule.ts:
export function getMessageLocation(): string {
return "Hello World";
}
索引.html:
<script>
fetch("helloWorldModule.wasm").then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes, {imports: {}})
).then(results => {
var linearMemory = results.instance.exports.memory;
var offset = results.instance.exports.getMessageLocation();
var stringBuffer = new Uint8Array(linearMemory.buffer, offset, 11);
let str = '';
for (let i=0; i<stringBuffer.length; i++) {
str += String.fromCharCode(stringBuffer[i]);
}
debugger;
});
</script>
这将返回 32 的偏移量。最后产生一个开始太早并且在“Hello World”的每个字母之间有空格的字符串:
但是,如果我将数组更改为 Int16Array,并在偏移量(即 32)上加 8,则偏移量为 40。像这样:
<script>
fetch("helloWorldModule.wasm").then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes, {imports: {}})
).then(results => {
var linearMemory = results.instance.exports.memory;
var offset = results.instance.exports.getMessageLocation();
var stringBuffer = new Int16Array(linearMemory.buffer, offset+8, 11);
let str = '';
for (let i=0; i<stringBuffer.length; i++) {
str += String.fromCharCode(stringBuffer[i]);
}
debugger;
});
</script>
为什么第一组代码不像我提供的链接中那样工作?例如,为什么我需要将其更改为与 Int16Array 一起使用以消除“H”和“e”之间的空格?为什么我需要向偏移量添加 8 个字节?
总之,这里到底发生了什么?
编辑:另一个线索,如果我在 UInt8 数组上使用 TextDecoder,解码为 UTF-16 看起来比解码为 UTF-8 更正确: