I am new to Emscripten/javascript, so I apologize in advance, if my situation has already been addressed. I have an app in ionic3 and I want to read an array in c that have 3 positions, and this positions have other array, declared on a struct:
struct struct_name {
char text1[10];
char text2[30];
char text3[50];
int int1;
int int2;
int int3;
};
float EMSCRIPTEN_KEEPALIVE calcArray(struct struct_name miArray[3]){
int value = 0;
for (int x = 0; x < 3; x ++) {
if (strcmp(miArray[x].text3, “PRUEBA”) == 0)
value += miArray[x].int3;
}
return value;
}
In the javascript part, I put the array in ascii and then allocate memory on was heap using the function malloc, and pass the length to the c function, but the wasm file haven’t read this like I want.
let miArray = [
{
text1: "lorem ipsum ",
text2: "lorem ipsum lorem ipsum",
text3: "lorem ipsum PRUEBA",
int1: 1,
int2: 2,
int3: 3
}
];
// Init the typed array with the same length as the number of items in the array parameter
const typed_miArray = new Float32Array(miArray.length);
// Populate the array with the values
for (let i = 0; i < miArray.length; i++) {
typed_miArray[i] = miArray[i];
}
//allocate memory on wasm heap for the array
const miArray_buffer = Module._malloc(typed_miArray.length * typed_miArray.BYTES_PER_ELEMENT);
Module.HEAPF32.set(typed_miArray, miArray_buffer >> 2);
const value = Module._calcArray(miArray_buffer, miArray_buffer.length);
For this test, I modify the c function:
float EMSCRIPTEN_KEEPALIVE calcArray(struct struct_name miArray, int arraySize){
int value = 0;
for (int x = 0; x < arraySize; x ++) {
if ( x == 2 ){
//text3
if (strcmp(miArray[x].text3, “PRUEBA”) == 0)
value += miArray[x].int3;
}
}
return value;
}
I generated a .js and a .wasm file like this (some .c files includes .h headers):
emcc data.c data2.c data3.c -O0 -s WASM=1 -s MODULARIZE=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s ASSERTIONS=1 -s EMBIND_STD_STRING_IS_UTF8=1 -s ALLOW_MEMORY_GROWTH=1 -s NO_FILESYSTEM=1 -s -o results.js`
Any help is appreciated. Forgive me for the incorrect formatting (it's my first post here). Thank You!