返回没有预定义大小的数组的函数

时间:2019-01-16 09:36:24

标签: javascript webassembly

在我的C代码中,我有一个函数,该函数会在调用该函数之前生成一个大小未知的数组,例如,该数组将包含红色> 128的所有像素。

C程序很简单,没有问题。我使用malloc和realloc。

现在,在javascript中,在调用之后,我想在Uint8ClampedArray中获取结果(数组)。

在我阅读的所有教程中,从C端返回的数组具有给定的长度,例如L字节。 因此,我们使用:

  pointer = Module._malloc(L)
  Module.HEAPU8.set(my_Uint8ClampedArray, pointerResult);

但是,在我的代码中,直到C函数结束时才知道结果数组的长度:

int imageGrayscale(unsigned char *data) {

    int l = some_function(...)
    data = malloc( l * sizeof(unsigned char));
    for (int i = 0; i < l ; i++) { 
         data[i]=  //some_value//; }

    return l; 
   // return the size of *data in case of...
   // but the value that matter is *data.
}

这是我不管理的Javascript代码* data:

var wa_imageGrayscale =module.cwrap('imageGrayscale',['number'],['number']);
var arrayOut = Module._malloc(// how_get_the_size_before_calling //);
var size_of_arrayOut = wa_imageGrayscale(arrayOut);
var  = new Uint8ClampedArray(size_of_arrayOut);
...???

}

谢谢!

0 个答案:

没有答案
相关问题