Embind: char* as JS string?

527 views
Skip to first unread message

Lóránt Pintér

unread,
Aug 26, 2014, 4:28:03 PM8/26/14
to emscripte...@googlegroups.com
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

Chad Austin

unread,
Aug 26, 2014, 4:31:11 PM8/26/14
to emscripte...@googlegroups.com
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);
}



--
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/d/optout.



--
Chad Austin
Technical Director, IMVU

Lóránt Pintér

unread,
Aug 26, 2014, 5:22:19 PM8/26/14
to emscripte...@googlegroups.com
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

nagappan nachiappan

unread,
Aug 27, 2014, 11:45:24 PM8/27/14
to emscripte...@googlegroups.com
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 ):
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 <ch...@imvu.com> 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 <lorant...@prezi.com> 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 emscripten-discuss+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Chad Austin
Technical Director, IMVU

--
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-discuss+unsub...@googlegroups.com.

Lóránt Pintér

unread,
Aug 28, 2014, 5:58:40 AM8/28/14
to emscripte...@googlegroups.com
We use EXPORTED_FUNCTIONS/cwrap() currently, but that doesn’t take care of struct parameters and return types — or dies it?

Lóránt


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/d/optout.



--
Chad Austin
Technical Director, IMVU

--
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/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 emscripten-disc...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages