2

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!

Jorge GH
  • 21
  • 3

1 Answers1

0

First, you cannot assign object to typed array. You also probably do not want to use float array. Naive way to encode JavaScript string to typed array would be:

const myString = "test";
// clamped array will convert anything above 255 to 255 instead of overflowing
// Note that we leave the last byte as zero
const myString_c_style = new Uint8ClampedArray(myString.length + 1);
for (let i = 0; i < myString.length; ++i) {
    myString_c_style[i] = myString.charCodeAt(i);
}

But that's not all. You also shouldn't try to allocate C struct on your own in some typed array. This will bring you a lot of headache in the future. Instead, I recommend having a conversion function, such as:

struct struct_name
{
  char text1[10];
  char text2[30];
  char text3[50];
  int int1;
  int int2;
  int int3;
};

float EMSCRIPTEN_KEEPALIVE calcArrayFromJS(const char* text1, const char* text2, const char* text3, int i1, int i2, int i3)
{
  struct_name tmp;
  // copy strings
  strncpy(&(tmp.text1[0]), text1, sizeof(tmp.text1) / sizeof(char));
  strncpy(&(tmp.text2[0]), text2, sizeof(tmp.text2) / sizeof(char));
  strncpy(&(tmp.text3[0]), text3, sizeof(tmp.text3) / sizeof(char));
  // Copy integers
  tmp.int1 = i1;
  tmp.int2 = i2;
  tmp.int3 = i3;
  return calcArray(tmp);
}

I have omitted the fact that you use array of struct_name but it shows the general direction. Alternatively consider if emscripten does not provide an API for writing in structs in JavaScript.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778