can't get template class structure as value_object

88 views
Skip to first unread message

Michael IV

unread,
Mar 7, 2014, 6:15:44 AM3/7/14
to emscripte...@googlegroups.com
I have a template class vec2  (GLM math lib)  .To expose it to JavaScript I first tried value_array.Works great.
But then I wanted to have more object oriented interface so that at JavaScript side we can do:

var  vector = Module.glm_add(vec1,vec2);

vector.x;
vector.y;
....
So I tried value_object which compiles ok but in JS I am getting blank screen.

EMSCRIPTEN_BINDINGS(my_module) {
/*
//this works
value_array<vec2>("vec2")
        .element(&vec2::x)
        .element(&vec2::y)
        ;
*/
//this doesn't
value_object<vec2>("vec2")
        .field("x", &vec2::x)
        .field("y", &vec2::y)
        ;
    function("glm_add", &glm_add);
}

Michael IV

unread,
Mar 7, 2014, 6:19:31 AM3/7/14
to emscripte...@googlegroups.com
my guess : it is because vec2 template preprocessed to C array.

Michael IV

unread,
Mar 7, 2014, 8:09:45 AM3/7/14
to emscripte...@googlegroups.com
Anyone?


--
You received this message because you are subscribed to a topic in the Google Groups "emscripten-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/emscripten-discuss/B4lujXZEZWU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Michael Ivanov
Independent Pixel Commander 
onlygraphix.com
Tel:+972 54 4962254

Chad Austin

unread,
Mar 7, 2014, 1:57:26 PM3/7/14
to emscripte...@googlegroups.com, emscripte...@googlegroups.com
Can you show the definition of vec2 in the C++?  It's hard to tell what the problem might be there.

Sent from my iPhone
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.

Michael IV

unread,
Mar 7, 2014, 4:18:53 PM3/7/14
to emscripte...@googlegroups.com
Well,it is heavily templated so there is not a single structure definitiion there for vec2 type, but I know it is resolved to a simple array by compiler.

Chad Austin

unread,
Mar 7, 2014, 4:47:45 PM3/7/14
to emscripte...@googlegroups.com
Can you reduce the problem code into a minimal example with reproduction steps?  I can't really help without seeing what's actually going wrong.

Chad Austin
Technical Director, IMVU

Michael IV

unread,
Mar 7, 2014, 5:09:22 PM3/7/14
to emscripte...@googlegroups.com
Ok,that is very simple.You just need to include glm lib .Then I am trying to do this:

vec2 glm_add(const vec2& a ,const vec2& b){

return  a + b;
}

EMSCRIPTEN_BINDINGS(my_module) {
 
value_object<vec2>("vec2")
        .field("x", &vec2::x)
        .field("y", &vec2::y)
        ;
    function("glm_add", &glm_add);
}


On the JavaScript side I would expect to operate on that method like that:

var vec = Module.glm_add([1,2],[2,3]);

document.write(vec.x + "/" + vec.y);


Chad Austin

unread,
Mar 7, 2014, 5:11:00 PM3/7/14
to emscripte...@googlegroups.com
Ah, got it.  The issue here is that you're using a mix of arrays and objects.

value_array is for mapping your type into JavaScript arrays.  That is, [x, y].
value_object is for mapping your type into JavaScript objects.  That is, {x: x, y: y}

What happens if you write:

var vec = Module.glm_add({x:1,y:2},{x:2,y:3});
document.write(vec.x + "/" + vec.y);

Michael IV

unread,
Mar 7, 2014, 5:16:21 PM3/7/14
to emscripte...@googlegroups.com
Btw,I also tried just this :

vec2 glm_add(){
return  vec2(1,2);
}

At the JavaScript end still nothing appears.

I suggest you to try it yourself.GLM include is a matter of seconds :)

Michael IV

unread,
Mar 7, 2014, 5:20:26 PM3/7/14
to emscripte...@googlegroups.com
Oops!Sorry my bad.Your solution works.Yeah,that was it.I was passing params as array.Thanks a lot for your patience.
Reply all
Reply to author
Forward
0 new messages