admin管理员组

文章数量:1402801

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)

本文标签: webassemblyAssemblyScript and JavaScript share memoryStack Overflow