AssemblyScript:动态局部数组大小
AssemblyScript: Dynamic local array size
尝试创建一个大小为从 JS 导入的变量的数组似乎不起作用。
例如:
汇编脚本:
export declare arrSize: u32
const arr = new Uint32Array(arrSize)
export function init (): void {
unchecked(testArr[0]) = 1
store<u32>(0, unchecked(testArr[0]))
}
JS:
const memory = new WebAssembly.Memory({ initial: 1 })
WebAssembly.instantiateStreaming(fetch('module.wasm'), {
env: { memory },
index: { arrSize: 2 }
}).then(module => {
const { init } = module.instance.exports
init()
const arr = new Uint32Array(memory.buffer, 0, 2)
console.log(arr)
})
而如果我在 AssemblyScript 文件中更改 const arr = new Uint32Array(2)
,它会起作用。
有没有办法在 WebAssembly 模块中动态调整本地数组的大小?
这个有效:
export declare const count: i32;
const arr = new Array<i32>(count);
export function getCount(): i32 {
return arr.length;
}
并且在 WebAssembly.instantiate
的导入对象中你应该有:
{
index: {
count: 22
}
}
前提是你的 assemblyscript 文件被命名为 index.ts
尝试创建一个大小为从 JS 导入的变量的数组似乎不起作用。 例如:
汇编脚本:
export declare arrSize: u32
const arr = new Uint32Array(arrSize)
export function init (): void {
unchecked(testArr[0]) = 1
store<u32>(0, unchecked(testArr[0]))
}
JS:
const memory = new WebAssembly.Memory({ initial: 1 })
WebAssembly.instantiateStreaming(fetch('module.wasm'), {
env: { memory },
index: { arrSize: 2 }
}).then(module => {
const { init } = module.instance.exports
init()
const arr = new Uint32Array(memory.buffer, 0, 2)
console.log(arr)
})
而如果我在 AssemblyScript 文件中更改 const arr = new Uint32Array(2)
,它会起作用。
有没有办法在 WebAssembly 模块中动态调整本地数组的大小?
这个有效:
export declare const count: i32;
const arr = new Array<i32>(count);
export function getCount(): i32 {
return arr.length;
}
并且在 WebAssembly.instantiate
的导入对象中你应该有:
{
index: {
count: 22
}
}
前提是你的 assemblyscript 文件被命名为 index.ts