【发布时间】:2021-06-12 00:47:39
【问题描述】:
我想调用wasm的init函数,它的C代码如下,所以我需要将数组从javascript传递给wasm。
int EMSCRIPTEN_KEEPALIVE init(int* playRecipes1, int* playChefs1, int* recipe2Change1,int* scoreCacheNoEquipIndex1, int* scoreAddCacheNoEquip1,short* disordePermuation1) {
playRecipes = playRecipes1;
playChefs = playChefs1;
recipe2ChangeLength = recipe2Change1[0];
scoreAddCacheNoEquipLength = scoreAddCacheNoEquip1[1];
scoreCacheNoEquipIndexLength = scoreCacheNoEquipIndex1[1];
recipe2Change = (int*)(recipe2Change1+1);
scoreCacheNoEquipIndex = (int * )(scoreCacheNoEquipIndex1 + 2);
scoreAddCacheNoEquip = (int *)(scoreAddCacheNoEquip1 + 2);
disordePermuation = (short *)(disordePermuation1+2);
return recipe2ChangeLength;
}
了解到传递数组,可以使用Module._malloc()来创建数组。但是_malloc的返回值很大, 所以,当我使用 Module.HEAPU8.set (data, dataPointer) 时,我遇到了错误: Uncaught (in promise) RangeError: offset is out of bounds。 在 Int32Array.set()
每次运行程序,第一次调用Module._malloc(),返回值总是5245672。
如:
function arrayToIntMemorySegment( arrays) {
let length1 = arrays.length;
let length2 = arrays[0].length;
let size = (length1 * length2 + 2);
let ptr = Module._malloc(size * 4);
let segment = new Int32Array(size);
let index = 0;
segment[index++]=length1;
segment[index++]=length2;
for (let i = 0; i < length1; i++) {
for (let i2 = 0; i2 < length2; i2++) {
segment[index++]=arrays[i][i2];
}
}
Module.HEAP32.set(segment, ptr);
return ptr;
}
【问题讨论】:
标签: javascript c++ memory webassembly emscripten