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
版权声明:本文标题:webassembly - AssemblyScript and JavaScript share memory - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744118859a2591642.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论