3

When calling a wasm function, I want to avoid frequent copying of function parameters and return values, so I want to use shared memory, but the running results are different from what I expected. How should I modify my code?

JavaScript:

import {b} from './release.js'

let out = new Float64Array(1)
b(out)
console.assert(out[0]==1) // Assertion failed

AssemblyScript:

export function b(out:Float64Array): void {
 out[0]=1.0;
}

version:

"assemblyscript": "^0.27.35"

config:

{
  "targets": {
    "debug": {
      "outFile": "build/debug.wasm",
      "textFile": "build/debug.wat",
      "sourceMap": true,
      "debug": true
    },
    "release": {
      "outFile": "../src/release.wasm",
      "sourceMap": false,
      "optimize": true,
      "optimizeLevel": 3,
      "shrinkLevel": 2,
      "converge": false,
      "noAssert": false
    }
  },
  "options": {
    "bindings": "esm"
  }
}

build:

asc assembly/index.ts --target release

Now there is a solution that uses store(ptr, value) to perform pointer operations in AssemblyScript.


I tried using the changetype function but got the error Segmentation fault (core dumped)

AssemblyScript:

export function b(ptr:usize):void{
 let arr=changetype<Float64Array>(ptr)
 arr[0]=1
}

export function new_f64_arr(n:i32): usize {
   let out = new Float64Array(n);
   return changetype<usize>(out);
}

JavaScript:

let arr=new Float64Array(memory.buffer,new_f64_arr(2),2)
arr[0]=77
b(arr.byteOffset)
4

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.