Just answering the part - "how can I call a C function with a char* 
parameter from JS?"

You may want to try this ( modified version of stackoverflow  post 21816960 
<http://stackoverflow.com/a/25497901/868791> ):
Test.c

#include <emscripten.h>#include <string.h>
int stringLen(char* p){
  return strlen(p);}

Use the following command to compile the cpp code : 

emcc Test.c -s EXPORTED_FUNCTIONS="['_stringLen']

Sample test code :
Test.html

<!DOCTYPE html><html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Hello World !</title>
        <script src="a.out.js"></script>
        <script>
             var strLenFunction =  Module.cwrap('stringLen', 'number', 
['string']);
             var len1 = strLenFunction("hi.");  // alerts 3
             alert(len1);
             var len2 = strLenFunction("Hello World"); // alerts 11
             alert(len2);
        </script>
    </head></html>


-Nagappan

On Wednesday, August 27, 2014 5:22:19 AM UTC+8, Lóránt Pintér wrote:
>
> I managed to add bindings to my library, thanks. However, most of the 
> functions in the library rely on passing char* back-and-forth, so adding 
> wrapper functions to all of them is unfeasible. It would also make the API 
> look different from JS than from C, which is a blocker for me. Do you maybe 
> know when char* support would be added to embind?
> — 
> Lóránt
>
>
> On Tue, Aug 26, 2014 at 10:31 PM, Chad Austin <[email protected] 
> <javascript:>> wrote:
>
>>  I believe that's on the embind todo list.  (Since pointers don't have 
>> unambiguous semantics, the bindings would need explicit pointer policies, 
>> and that work hasn't been done yet.)
>>
>> However, there's a workaround!  You can do something like:
>>
>> #include <Library.h>
>> // assume Library.h declares: void libraryFunction(const char*);
>>
>> static void libraryFunctionWrapper(const std::string& s) {
>>     libraryFunction(s.c_str());
>> }
>>  
>> EMSCRIPTEN_BINDINGS(Library) {
>>     function("libraryFunction", &libraryFunctionWrapper);
>> }
>>
>>  
>>
>> On Tue, Aug 26, 2014 at 1:28 PM, Lóránt Pintér <[email protected] 
>> <javascript:>> wrote:
>>
>>>  Hi,
>>>
>>> Is it possible in embind to treat char* as a JS string? If not, how can 
>>> I call a C function with a char* parameter from JS?
>>>
>>> Thanks
>>> — 
>>> Lóránt 
>>>   
>>> -- 
>>> 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 [email protected] 
>>> <javascript:>.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>  
>>
>>
>> -- 
>> Chad Austin
>> Technical Director, IMVU
>>  http://engineering.imvu.com <http://www.imvu.com/members/Chad/>
>>  http://chadaustin.me
>>
>>
>>   -- 
>> 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 [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to