0

I am trying to access elements of an Array<u32> in AssemblyScript.

I initialize and fill my arrays as follows:

let totalSize: u32 = 512;

let leaderPtsX: u32[];
let leaderPtsY: u32[];
let leaderPtsType: u32[];

export function createPoints(): void {
  leaderPtsX = new Array<u32>();
  leaderPtsY = new Array<u32>();
  leaderPtsType = new Array<u32>();
  for (let i: u32 = 0; i < VP_COUNT; i++) {
    let x = <u32>(Math.random() * totalSize);
    let y = <u32>(Math.random() * totalSize);
    leaderPtsType.push(Math.random() > 0.5 ? 0xffffffff : 0xff00ff00);
    leaderPtsX.push(x);
    leaderPtsY.push(y);
  }
}

I have a function which needs to access the aforementioned arrays as so:

function getClosestNeighbourType(x: u32, y: u32): u32 {
  let minDistance = 5000;
  let minDistanceType = -1;
  for (let i = 0; i < VP_COUNT; i++) {
    let dist =
      Math.abs(x - unchecked(leaderPtsX[i])) +
      Math.abs(y - unchecked(leaderPtsY[i]));
    if (dist < minDistance) {
      minDistance = <u32>dist;
      minDistanceType = unchecked(leaderPtsType[i]);
    }
  }
  return <u32>minDistanceType;
}

But on compiling and running this in the browser i get the following console error:

array.ts:104 Uncaught RuntimeError: memory access out of bounds
    at ~lib/array/Array<u32>#__unchecked_get (wasm-function[29]:0x1b4a)
    at assembly/index/getClosestNeighbourType (wasm-function[30]:0x1b84)

I have tried lots of different methods and gone through a lot of documentation but no dice. Any help or links appreciated.

Thanks!

psychnaut
  • 64
  • 1
  • 3
  • I haven't worked with this language so I'm guessing, but did you confirm that the arrays are populated? Have getClosestNeighbourType print the array lengths before the loop. Also (despite the documentation's example) I wonder if the getClosestNeighbourType's i variable should be declared as i32. – SkySpiral7 May 16 '20 at 21:31
  • Could you share full example? I created fiddle which based on your example but can't reproduce this issue: https://webassembly.studio/?f=fhzcqeb64s5 – MaxGraey May 16 '20 at 22:18
  • @SkySpiral7 Yes i am a hundred percent sure that the arrays are getting populated before being accessed as `createPoints` is always called first. On the advice of @MaxGraey i created a fiddle at [link](https://webassembly.studio/?f=izxfulhx9up) but now i am getting a compile error which doesn't occur while working on my local machine. I think i am missing something – psychnaut May 17 '20 at 08:25
  • @psychnaut You incorrectly use `store`: store((i + j * totalSize) << 2, value, getClosestNeighbourType(i,j)); it could be at least: store((i + j * totalSize) << 2, value); But I recommend don't use `load` / `store` if you don't know what you doing. It's unsafe and could lead to memory corruptions. – MaxGraey May 17 '20 at 10:28
  • @MaxGraey I'm really sorry i made the fiddle in a hurry. I have fixed it: [link](https://webassembly.studio/?f=3zr0y9x8pd5). I was able to easily use `load`/`store` after rigourously reading the docs and following the Game of Life example [here](https://github.com/AssemblyScript/examples/tree/master/game-of-life). As soon as i incorporated this array logic things stopped working. Any articles/links would be appreciated :( – psychnaut May 17 '20 at 14:28
  • @MaxGraey the fiddle compiles successfully and throws no errors. But i'm still facing this issue on my machine. Is something wrong with my setup? – psychnaut May 17 '20 at 14:31
  • @psychnaut Game of Life don't use runtime `--runtime none`. That's why it could safely read / write to linear memory via `load` / `store`. But in the same time it don't use managed objects like Array, String and etc because with runtime none its never deallocate. I recommend read docs about different runtimes: https://docs.assemblyscript.org/details/runtime#runtime-variants `--runtime none` is low level mode like `C`. `--runtime full` or `--runtime half` is managed (uses GC / ARC) and this mean you shodn't use intrinsics like load / store becouse you could damage heap. – MaxGraey May 17 '20 at 18:32
  • Also recommend this basics tutorials: https://wasmbyexample.dev/examples/webassembly-linear-memory/webassembly-linear-memory.assemblyscript.en-us.html# it could be more clear than docs If still want mix manual load/store with managed runtime you should reserve space via `--memoryBase `. After that memory manager will start the heap after `` and you could write to space before this number safely without fearing corrupt data in heap – MaxGraey May 17 '20 at 18:40
  • That's fixed example: https://webassembly.studio/?f=4h3ggn4q4sy See main.js and gulpfile.js – MaxGraey May 17 '20 at 18:45
  • @MaxGraey thanks a lot sir. `--memoryBase` along with the runtime reading solved my problem. I reserved some space and added the half runtime option. The error is gone now. Thank you so so much. I will read all the links you have mentioned to understand my concepts better. Thanks again. – psychnaut May 17 '20 at 21:42

0 Answers0