creating a binding for a struct with a raw pointer using embind

1,074 views
Skip to first unread message

Aleksander Balicki

unread,
May 29, 2013, 1:12:31 PM5/29/13
to emscripte...@googlegroups.com
I was trying to generate a binding for a C struct included by an extern "C" block, but the effects are the same on clean C++ structs.

The code examples of the problem are shown here:


After looking at the code here: 

I've noticed that the functions for generating bindings like 'function', 'method', 'constructor' have a policies... argument, but the value_tuple and value_struct don't have such an argument, yet they trigger a an error that says I have to use allow_raw_pointers() for the stuff to work.

The allow_raw_pointers() argument work if I want to bind a function that takes a raw_pointer argument, but still I see no way of binding the struct with a pointer field.

Am I missing something?

Chad Austin

unread,
May 29, 2013, 4:31:41 PM5/29/13
to emscripte...@googlegroups.com
Why do you want to use value_struct with a raw pointer in it?  I would instead recommend using std::string instead of char*.  (char* is ambiguous: what's the memory management policy? array or scalar? null-terminated?)


--
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.
 
 



--
Chad Austin
Technical Director, IMVU

Aleksander Balicki

unread,
May 29, 2013, 4:35:55 PM5/29/13
to emscripte...@googlegroups.com
I want to wrap an existing API, and it not only has char* but also other arbitrary struct pointers.

I would like to know how to do it? or even how to work around it? for the general case, not just std::string

also for std::string i have to write a wrapper for each function returing char*?
Aleksander "Alistra" Balicki

Chad Austin

unread,
May 30, 2013, 1:53:44 AM5/30/13
to emscripte...@googlegroups.com
You probably want to use class_ for all of your objects, then.  value_struct and value_tuple are just for lightweight objects that you want to pass by value between JS and C++.  In fact I should rename them to value_object and value_array for consistency with JavaScript terminology.

For now you'll have to write a wrapper for char*.  I'm assuming that char* is convertible to a JavaScript string in some encoding?  And assuming they're null-terminated.

Perhaps we could generalize with a raw_pointer_policy or something: property("name", &S::name, pointer_policy<convert_to_latin1_string>()) or something like that...

Aleksander Balicki

unread,
May 30, 2013, 2:50:40 AM5/30/13
to emscripte...@googlegroups.com
1. Yeah, but still what about pointers other than strings.

For example my api can return an opaque type, that I have to pass along to other objects.

Do you think rewriting the structs so they use an integer type with sizeof(void *) instead of the pointer field would work?

2. If I were to use class, how would the stored pointers be treated? Or would I have to write a lot of C++ boilerplate like getters and setters?


--
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.
 
 



--
Aleksander "Alistra" Balicki

Aleksander Balicki

unread,
May 30, 2013, 2:24:28 PM5/30/13
to emscripte...@googlegroups.com
Thanks for the tip with class_, it worked for those structs instead of value_struct.

Unfortunately implementations of functions are C and I encountered this problem now: https://github.com/kripken/emscripten/issues/1152.
--
Aleksander "Alistra" Balicki

Chad Austin

unread,
May 30, 2013, 4:22:49 PM5/30/13
to emscripte...@googlegroups.com
Hi Aleksander,

Those errors that you're running into are caused by the compiler trying to compile with C++03 instead of C++11.

Are you passing --bind to emcc?  You may also need to pass -std=c++11

Aleksander Balicki

unread,
May 31, 2013, 2:50:03 AM5/31/13
to emscripte...@googlegroups.com
Yes, I was doing the --bind option all along

As I answered in the github issue, -std=c++11 is a no go (gives an error) when I mix it up with C files.
Reply all
Reply to author
Forward
0 new messages