Passing/returning array pointers to emscripten compiled C code

2,668 views
Skip to first unread message

fjc...@gmail.com

unread,
Jul 25, 2013, 9:04:57 AM7/25/13
to emscripte...@googlegroups.com
Hello --

I'm new to Emscripten/javascript as well as this discussion 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\n", cnt, doubleVector[cnt]);

doubleVector[0] = 98;
doubleVector[1] = 99;
doubleVector[2] = 100;

for (int cnt1 = 0; cnt1 < 3; cnt1++) 
  printf(" modified doubleVector[%d] = %f\n", 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\n", cntr, array1[cntr]);
for (int cnt = 0; cnt < 3; cnt++)
printf("array2[%d] = %f\n", 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]'])
function () { return ccallFunc(func, returnType, argTypes, Array.prototype.slice.call(arguments)); }
> 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.  


Thanks in advance,
FJC





Alon Zakai

unread,
Jul 26, 2013, 6:50:56 PM7/26/13
to emscripte...@googlegroups.com
ccall and cwrap are quite limited. Best to look in the source (src/preamble.js) where the comments explain the types that are supported. In particular the only array type supported so far is 8-bit.

Embind is a more general tool to connect the two worlds, see https://github.com/kripken/emscripten/wiki/Interacting-with-code

But for a double array like in that example, I would just write low-level code manually. Pointers are just integers. You can malloc some space, write into that, then ccall and pass the pointer (which is just a number), etc.

- Alon









--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

fjc...@gmail.com

unread,
Jul 27, 2013, 12:01:19 AM7/27/13
to emscripte...@googlegroups.com
Hi Alon -

Thanks for the response/info.   I've successfully run the simple example illustrated in the Embind.   Am currently looking into this...

I'm not sure I quite understand what you mean by "low-level code" that you mention below.   Perhaps an example of this low-level code would help to illustrate and help me to understand?  


Thanks again,
FJC

Alon Zakai

unread,
Jul 29, 2013, 1:33:32 PM7/29/13
to emscripte...@googlegroups.com
It mallocs a buffer, does some work on it using normal JS typed array stuff, then passes a "pointer" to it to a compiled C function.

- Alon



Reply all
Reply to author
Forward
0 new messages