【问题标题】:AssemblyScript: Dynamic local array sizeAssemblyScript:动态本地数组大小
【发布时间】:2021-04-16 10:41:28
【问题描述】:

尝试创建一个大小为从 JS 导入的变量的数组似乎不起作用。 例如:

AssemblyScript:

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 模块中设置动态大小的本地数组?

【问题讨论】:

    标签: javascript webassembly assemblyscript


    【解决方案1】:

    这行得通:

    export declare const count: i32;
    
    const arr = new Array<i32>(count);
    export function getCount(): i32 {
        return arr.length;
    }
    

    WebAssembly.instantiate 的导入对象中,您应该有:

    {
        index: {
            count: 22
        }
    }
    

    前提是你的汇编脚本文件名为index.ts

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-11
      • 1970-01-01
      • 2014-07-21
      • 2011-05-03
      • 2019-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多