Hi,
I'm creating a javascript binding for cocos2d-x (
https://github.com/
funkaster/cocos2d-x/tree/js-bindings/js). So far so good, but I just
found an issue when facing an inheritance chain.
Let's say I have this class inheritance in C++:
class A;
class B : A;
class C : B;
class D : C;
To create the bindings, what I'm doing is sub-classing every C++ with
a custom S_<klass> that inherits *only* from the real C++ class, and
then in javascript, I set the parent proto to reflect the actual C++
inheritance, more or less like this:
class S_A : A;
class S_B : B;
class S_C : C;
class S_D : D;
I do this because I need to override methods in order to catch
possible events coming from C++ and then redirect that to javascript
(and this was the easiest way to do it without modifying the original C
++ code). Also, the bindings/classes/etc are being (almost)
automatically generated from a script that parses the AST from a C++
header.
When registering the javascript classes, this is what I'm doing:
S_A::jsObject = JS_InitClass(cx, globalObj, NULL, S_A::jsClass,
S_A::jsConstructor, 0, properties, funcs, NULL, st_funcs);
S_B::jsObject = JS_InitClass(cx, globalObj, S_A::jsObject,
S_B::jsClass, S_B::jsConstructor, 0, properties, funcs, NULL,
st_funcs);
S_C::jsObject = JS_InitClass(cx, globalObj, S_B::jsObject,
S_C::jsClass, S_C::jsConstructor, 0, properties, funcs, NULL,
st_funcs);
S_D::jsObject = JS_InitClass(cx, globalObj, S_C::jsObject,
S_D::jsClass, S_D::jsConstructor, 0, properties, funcs, NULL,
st_funcs);
If you're wondering, the actual hierarchy for this would be:
A: CCNode
B: CCMenuItem
C: CCMenuItemSprite
D: CCMenuItemImage
The problem I have right now is that for classes that reach only until
level "B" this works perfectly: they can get and set properties
inherited by their parent's prototype with no problem (every class has
its own getter and setter), but for classes that extend this further,
apparently the search for prototypes stops after the first level...
Any ideas on how to properly implement/fix this?
Thanks!
Rolando