ScriptWrappable, GarbageCollected and diamong problem

47 views
Skip to first unread message

Giovanni

unread,
Apr 23, 2015, 10:23:15 PM4/23/15
to blin...@chromium.org

Hello blink-dev,

I have an idl interface (Bluetooth) that implements two idl interfaces (BluetoothDiscovery and BluetoothInteraction). In the first version of the spec, Bluetooth only implemented BluetoothDiscovery, so BluetoothDiscovery.h inherited GarbageCollected and ScriptWrappable and everything worked fine.

Now I'm trying to add the new BluetoothInteraction interface but I'm running into some problems with ScriptWrappable. I tried inheriting ScriptWrappable only from Bluetooth.h but the compiler complains that there is no "s_wrapperTypeInfo". If I try to inhering ScriptWrappable from BluetoothInteraction.h and BluetoothDiscovery.h I get the diamond problem since Bluetooth.h gets ScriptWrappable from both.

So my question is what is the best way to use ScriptWrappable when a class inherits from two classes that also need ScriptWrappable.

Thanks,

Gio


Adam Barth

unread,
Apr 23, 2015, 11:30:27 PM4/23/15
to Giovanni, blin...@chromium.org
Neither BluetoothDiscovery nor BluetoothInteraction should inherit from ScriptWrappable.  Instead, Bluetooth should inherit from BluetoothDiscovery, BluetoothInteraction, and ScriptWrappable.

The Bluetooth interface is defined in the specification as follows:

[NoInterfaceObject]
interface Bluetooth {
};
Bluetooth implements BluetoothDiscovery;
Bluetooth implements BluetoothInteraction;

The JavaScript prototype structure that this specification implies is as follows:

[Instance object]
-- proto -->
[Bluetooth prototype]
-- proto -->
[Object prototype]

In particular, neither the BluetoothDiscovery prototype nor the BluetoothInteraction prototype is on the prototype chains.  Instead, the Bluetooth prototype has own properties for each of the member functions from the BluetoothDiscovery and BluetoothInteraction interfaces.

Said another way, instances of the Bluetooth interface conform to the duck type you would expect from BluetoothDiscovery and BluetoothInteraction but these instance object don't have an "is a" relationship with the BluetoothDiscovery and BluetoothInteraction interfaces.

Compare with how the Element interface is specified:

interface Element : Node {
  [...]
]

In this case, instances of the Element interface have the Node prototype on their prototype chain has have an "is a" relationship with the Node interface.  In this case, in C++, Element doesn't inherit from ScriptWrappable.  Instead, it inherits the ScriptWrappable base class through Node.

Hope that helps,
Adam

Giovanni Ortuño

unread,
Apr 24, 2015, 1:43:33 AM4/24/15
to Adam Barth, blin...@chromium.org
Ah gotcha. Thanks for the example, that was great. It matches what I though at the beginning but I can't translate it into C++ code.

I tried writing:

class Bluetooth 
    : public BluetoothDiscovery
    , public BluetoothInteraction
    , ScriptWrappable {...

But I keep getting the following error:

"...v8/V8BluetoothDiscovery.cpp:29:44: error: no member named 's_wrapperTypeInfo' in 'blink::BluetoothDiscovery'"

"core/v8/ScriptWrappable.h:67:16: error: static_cast from 'blink::ScriptWrappable *' to 'blink::BluetoothDiscovery *', which are not related by inheritance, is not allowed"

Not sure what I'm missing...

Gio


Kentaro Hara

unread,
Apr 24, 2015, 1:45:36 AM4/24/15
to Giovanni Ortuño, Adam Barth, blink-dev
--
Kentaro Hara, Tokyo, Japan

Giovanni Ortuño

unread,
Apr 24, 2015, 1:52:41 AM4/24/15
to Kentaro Hara, Adam Barth, blink-dev

Kentaro Hara

unread,
Apr 24, 2015, 2:00:42 AM4/24/15
to Giovanni Ortuño, Adam Barth, blink-dev
Added comments on the CL.

You need to add DEFINE_WRAPPERTYPEINFO() to BluetoothInteraction as well. DEFINE_WRAPPERTYPEINFO() is necessary for each type that has an IDL interface.

Giovanni Ortuño

unread,
Apr 24, 2015, 2:12:40 AM4/24/15
to Kentaro Hara, Adam Barth, blink-dev
I tried that as well, I get the following error:

.../WebKit/Source/modules/bluetooth/BluetoothDiscovery.h:18:5: error: 'wrapperTypeInfo' marked 'override' but does not override any member functions

Kentaro Hara

unread,
Apr 24, 2015, 2:15:47 AM4/24/15
to Giovanni Ortuño, Adam Barth, blink-dev
Let's discuss on the CL :)

Reply all
Reply to author
Forward
0 new messages