kripken/emscripten — Issue #149: Make it easier to implement C++ interfaces in JavaScript
Author: mstange
Created: 2011-12-20 23:11:48+00:00
State: open

I'd like to replace some parts of my original C++ code by handwritten JavaScript code. I want to have an interface with the common API, and separate concrete classes that implement the interface in C++ and JavaScript, respectively. Thus, when I pass a pointer to the JavaScript implementation to my common emscriptened code, it should be able to call back into JavaScript code by simply calling the virtual methods on the provided object.

I've read http://syntensity.blogspot.com/2011/09/vtable-customization-hack.html and I know that I can use customizeVTable for this. But this has several disadvantages:
- I need to provide C++ code for the concrete subclass myself, filled with stub functions which will be replaced during runtime. (The original C++ implementation might not compile with emscripten because it might use platform-specific functions.)
- I need to call customizeVTable for all methods of the interface by hand.
- I don't have a nice JavaScript representation of the resulting frankenclass.
Also, vtable munging makes me uncomfortable and needs the zero termination hack.

Instead, I'd like bindings_generator.py to create a skeleton for the JS implementation of the interface, and the necessary glue code. Here's a very rough patch that attempts to add the basic functionality: https://gist.github.com/1503542
The implementation approach is described in the comment at the top.

Here's the example code that I tested my changes with: https://github.com/mstange/emscriptenvirtualjs

Obviously, this is far from finished, but would this be a step in the right direction? Or is there another simple way (which I have missed) of doing what I want to do?