Assemblyscript: Incorrect result in normal `for` loop [Question]

Created on 26 Mar 2020  路  7Comments  路  Source: AssemblyScript/assemblyscript

I am trying to build this function, but its result is always 0. What am I doing wrong?

wasm.ts

export function for_array(arr: Array<f32>): f32 {
  let sum: f32 = 0.0
  for (let i: i32 = 0, l: i32 = arr.length; i < l; ++i) {
    sum += arr[i]
  }
  return sum
}

loader.ts:

const fs = require("fs")
const loader = require("@assemblyscript/loader")
export const myWasm = loader.instantiateSync(
    fs.readFileSync("./build/release.wasm")
)

index.ts

import {myWasm} from './loader'
console.log('AS =', myWasm.for_array(arr));

scripts commands:

    "start": "node --experimental-modules --experimental-wasm-modules ./dist/index.js",
    "asbuild ": "asc wasm.ts -b build/release.wasm -t build/release.wat --validate -O3",
    "copy": "shx rm -rf dist/build/ && shx cp -r build dist/build/",
    "build": "npm run asbuild && tsc -p ./tsconfig.json && npm run copy",
question

All 7 comments

This works for me on master:

function for_array(arr: Array<f32>): f32 {
  let sum: f32 = 0.0
  for (let i: i32 = 0, l: i32 = arr.length; i < l; ++i) {
    sum += arr[i]
  }
  return sum
}

assert(for_array([1, 1]) == 2)

Are you trying to pass a normal JS array here?

console.log('AS =', myWasm.for_array(arr));

Are you trying to pass a normal JS array here?

console.log('AS =', myWasm.for_array(arr));

Yes. Do I need to convert it or something?

Yes. Do I need to convert it or something?

Yes, the array must first be allocated in the Wasm module's memory. The loader provides the necessary utility to do so:

export const ArrayF32_ID = idof<Array<f32>>();
...
var arrPtr = myWasm.__retain(myWasm.__allocArray(myWasm.ArrayF32_ID, [1, 2, 3]));
console.log('AS =', myWasm.for_array(arrPtr));
myWasm.__release(arrPtr);

Yes. Do I need to convert it or something?

Yes, the array must first be allocated in the Wasm module's memory. The loader provides the necessary utility to do so:

export const ArrayF32_ID = idof<Array<f32>>();
...
var arrPtr = myWasm.__retain(myWasm.__allocArray(myWasm.ArrayF32_ID, [1, 2, 3]));
console.log('AS =', myWasm.for_array(arrPtr));
myWasm.__release(arrPtr);

Thank you!

Could you add all these information to the documentation? A main section for interoperability with JavaScript. The documentation is a little spread all over the place and vague in this regard.

@dcodeIO I get a different result from the typescript code if I use f32. If I use f64 this is solved.
Is it because of lower precision?

import { Chance } from "chance"
const chance = new Chance()
const arr = chance.n(chance.floating, 100)

const arrPtr = myWasm.__retain(myWasm.__allocArray(myWasm.ArrayF64_ID, arr));
console.log('AS64 =', myWasm.for_array(arrPtr));
myWasm.__release(arrPtr);

const arrPtr32 = myWasm.__retain(myWasm.__allocArray(myWasm.ArrayF32_ID, arr));
console.log('AS32 =', myWasm.for_array32(arrPtr32));
myWasm.__release(arrPtr32);

console.log('TS =', for_array_ts(arr));
AS64 = 72119061630.1568
AS32 = 72119058432
TS = 72119061630.1568

f32 has lower precision and dynamic range so it expected behaviour

Other than my request for more documentation and examples in the docs, I have no other question. Thank you for your help.

Could you add all these information to the documentation? A main section for interoperability with JavaScript. The documentation is a little spread all over the place and vague in this regard.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

emil14 picture emil14  路  3Comments

andy-hanson picture andy-hanson  路  4Comments

MaxGraey picture MaxGraey  路  3Comments

torch2424 picture torch2424  路  3Comments

kungfooman picture kungfooman  路  5Comments