I am new to Emscripten/javascript as well as this Overstack community. I apologize, in advance, if my situation has already been addressed.
From a windows 7 environment, I've used emcc to compile a simple c program which accepts an array and modifies it (see below).
double* displayArray(double *doubleVector) {
for (int cnt = 0; cnt < 3; cnt++)
printf("doubleVector[%d] = %f
", cnt, doubleVector[cnt]);
doubleVector[0] = 98;
doubleVector[1] = 99;
doubleVector[2] = 100;
for (int cnt1 = 0; cnt1 < 3; cnt1++)
printf("modified doubleVector[%d] = %f
", cnt1, doubleVector[cnt1]);
return doubleVector;
}
int main() {
double d1, d2, d3;
double array1[3];
double *array2;
array1[0] = 1.00000;
array1[1] = 2.000000;
array1[2] = 3.000000;
array2 = displayArray(array1);
for (int cntr =0; cntr < 3; cntr++)
printf("array1[%d] = %f
", cntr, array1[cntr]);
for (int cnt = 0; cnt < 3; cnt++)
printf("array2[%d] = %f
", cnt, array2[cnt]);
return 1;
}
Using the -o options for emcc, I generated a .html file which I loaded to a browser (Chrome).
python emcc displayArray7.c -o displayArray7.html -s EXPORTED_FUNCTIONS="['_main', '_displayArray'
Upon loading, I see that the output being generated within the browser window is as expected (see below).
doubleVector[0] = 1.000000
doubleVector[1] = 2.000000
doubleVector[2] = 3.000000
modified doubleVector[0] = 98.000000
modified doubleVector[1] = 99.000000
modified doubleVector[2] = 100.000000
array1[0] = 98.000000
array1[1] = 99.000000
array1[2] = 100.000000
array2[0] = 98.000000
array2[1] = 99.000000
array2[2] = 100.000000
However, when using the module.cwrap() command via javascript console and attempting to invoke the function directly (outside of main()) ,
> displayArray=Module.cwrap('displayArray', '[number]', ['[number]'])
> result = displayArray([1.0,2.0,3.0])
[1, 2, 3]
> result
[1, 2, 3]
I am seeing the following being generated/displayed in the browser which is NOT what I expect to see.
doubleVector[0] = 0.000000
doubleVector[1] = 0.000000
doubleVector[2] = 0.000000
modified doubleVector[0] = 100.000000
modified doubleVector[1] = 100.000000
modified doubleVector[2] = 100.000000
I have the following questions:
Do I have the syntax correct for the return type and parameter listing correct in my call to Module.cwrap()? I've successfully run the simple, straight-forward example of int_sqrt() in the "Interacting with code" section of the tutorial which deals with passing non-pointer variables to the int_sqrt() routine.
Is there something different that is happening when arrays and/or pointers are passed to (or returned from) the emscripten-generated javascript code?
How is that the generated output in the browser of the function, displayArray(), works (as expected) when called from main(); but not via the javascript console?
I am new to Emscripten/javascript so any info/assistance will be greatly appreciated.
Thank you,
FC
See Question&Answers more detail:
os