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",
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.