Skip to content

Loader cannot access the subarray correctly #2091

Closed
@ghost

Description

I am trying to use Loader to access the TypedArray of wasm.
I can access the original array, but there is a problem when accessing its subarray.
When I get the subarray with __getArrayView, it appears as an empty array.
I guess that it is probably a problem of Loader, since subarray is working correctly inside wasm.
I think it would be useful to have access in this way.

MWE: AssemblyScript Editor

// module.ts
let arr: Int32Array | null = null
let subarr: Int32Array | null = null

export function ga(): Int32Array {
  if (!arr) {
    arr = new Int32Array(10)
  }
  return arr as Int32Array
}

export function gb(): Int32Array {
  if (!subarr) {
    subarr = ga().subarray(5)
  }
  return subarr as Int32Array
}

export function gc(): void {
  const s = gb()
  s[0] = 42
}
// index.html
<textarea id="output" style="height: 100%; width: 100%" readonly></textarea>
<script>
loader.instantiate(module_wasm, { /* imports */ })
  .then(({ exports }) => {
    const output = document.querySelector('#output')
    output.value = ''
    try {
      const ptr = exports.__pin(exports.ga())
      exports.gc()
      const arr = exports.__getArrayView(ptr)
      output.value += `arr: length=${arr.length}, byteLength=${arr.byteLength}, data=${arr}\n`
      const sptr = exports.__pin(exports.gb())
      const subarr = exports.__getArrayView(sptr)
      output.value += `subarr: length=${subarr.length}, byteLength=${subarr.byteLength}, data=${subarr}\n`
    } catch (err) {
      output.value = `${err}`
    }
  })
</script>

Expected Behavior:

arr: length=10, byteLength=40, data=0,0,0,0,0,42,0,0,0,0
subarr: length=5, byteLength=20, data=42,0,0,0,0

Actual behavior (the subarray appears as an empty array):

arr: length=10, byteLength=40, data=0,0,0,0,0,42,0,0,0,0
subarr: length=0, byteLength=0, data=

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions