Accessing javascript objects from C

45 views
Skip to first unread message

jlongster

unread,
Nov 1, 2012, 1:49:36 PM11/1/12
to emscripte...@googlegroups.com
Hey guys,

I'm researching emscripten and writing up a few test apps to get to know it (and do a little benchmarking too). Currently I'm trying to write a simple Canvas wrapper so that I can access the 2d API via C.

I have `drawRect` and other functions working, they are easy. I need the function `drawImage` though, which takes an "image" argument, which is a javascript object. How can I work with a javascript object in C?

The API I'm thinking is (it's dumb and simple):

Img* getImage(char *name);
void drawImg(Img *img, int x, int y);

The problem is telling the C compiler about the Img object. I don't think making a struct is the right way to go, because emscripten will allocate it in its own heap, but I need a normal javascript object.

- James

jlongster

unread,
Nov 1, 2012, 4:04:04 PM11/1/12
to emscripte...@googlegroups.com
I'll answer my own question: it seems that you can't do that, and for valid reasons. What I'm doing is mapping the js objects to strings and passing the strings back and forth in C.

- James

Alon Zakai

unread,
Nov 1, 2012, 8:47:53 PM11/1/12
to emscripte...@googlegroups.com
Yeah, you need to represent the JS objects in something that C can
understand, like an integer handle or such.

- azakai
Reply all
Reply to author
Forward
0 new messages