Emscripten:如何从javascript中获取c语言中的char数组

问题描述 投票:2回答:1

我是Emscripten / javascript的新手,因此,如果我的情况已经解决,请在此致歉。我在ionic3中有一个应用程序,我想在c中读取一个具有3个位置的数组,并且此位置具有在结构上声明的其他数组:

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;
}

[在javascript部分中,我将数组放在ascii中,然后使用malloc函数在was堆上分配内存,并将长度传递给c函数,但是wasm文件没有按我想要的方式读取。

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);

对于此测试,我修改了c函数:

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;
}

我这样生成了一个.js和一个.wasm文件(某些.c文件包括.h标头):

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`

感谢您的帮助。原谅我格式错误(这是我的第一篇文章)。谢谢!

javascript c emscripten arraybuffer
1个回答
0
投票

首先,您不能将对象分配给类型数组。您也可能不想使用浮点数组。将JavaScript字符串编码为类型数组的简单方法是:

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);
}

但这还不是全部。您也不应尝试在某些类型的数组中自行分配C struct。将来这会给您带来很多麻烦。相反,我建议使用转换功能,例如:

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);
}

我已经省略了使用struct_name数组的事实,但是它显示了大致的方向。或者,考虑emscripten是否不提供用于以JavaScript编写结构的API。

© www.soinside.com 2019 - 2024. All rights reserved.