Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

inheritance chain from C++ to javascript

46 views
Skip to first unread message

rolando

unread,
Apr 19, 2012, 12:59:18 PM4/19/12
to
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

rolando

unread,
Apr 24, 2012, 7:52:24 PM4/24/12
to
(bump)

Any idea on how to fix this?
From what I could track, this is what is happening (following the same
structure as the previous email)

* I create class D
* class D defines some properties (the array that will be passed to
JS_InitClass)
* class D *should* inherit some properties from class A, since it
inherits from prototype C, C from B and B from A
* when trying to set a property p in class D, it will fail because
it's not defined either in D or in the immediate parent C
* the default getter I'm using for the class is JS_PropertyStub. For
the explicit properties for D (and any other class) I use a same
getter/setter that will use integer ids for variables.

Should the prototype object for future objects be concatenated along
the inheritance chain?
What would be the good way to implement this?

Thanks!
Rolando

rolando

unread,
May 2, 2012, 6:46:23 PM5/2/12
to
Just to close this... it was my own stupidity:

Everything was ok, the problem was the order in which I was registering (and creating) the different classes/prototypes :)

It's working just fine now.

Rolando
0 new messages