lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2013/6/29 David Demelier <demelier.david@gmail.com>:
> Hi there,
>
> I would like some comments on my multiple inheritance implementation
> from C++. But first, I would like to thanks levi on #lua@freenode.net
> who helped me much on this :-).
>
> First, let's explain how I did.
>
> Let say I have a base object, here it's "Object". I create a metatable
> in the registry named "Object" and then add a table of it's method
> into its __index field.
>
> So when I try to access any field, it will lookup over the __index
> table of method, this is much easy and the most common usage.
>
> Now, I create a subclass Widget with some methods, I create a
> metatable again, plus a table of its method to __index just like
> before. This class will derive from Object.
>
> The only thing change now is, I create a new metatable and add
> "parents" field set to table which contains "Object", then I add a
> __index field that points to a special lookup function. This function
> will iterate over all "parents" and call getfield on it. Finally I set
> this new table as the metatable of the Widget's method tables (aka
> __index).
>
> To summary :
>
> Object is a metatable store in the registry, it has a __index field
> set to a table of functions.
>
> Widget is also a metatable stored in the registry, also has an __index
> field set to table of functions, however this __index table has a
> metatable with "parents" set to { "Object" } and has a __index
> function that lookup over parents.
>
> I've attached my C++ implementation, I didn't have much ideas about
> classes so sorry about the non-sense of them but we have :
>
> Object
>  ^
>  |
> Widget
>  ^
>  |
> Button      Foo
>        ^          ^
>        |           |
>         Multiple
>
> And with the attached file, if you push a userdata with "Multiple" you
> are able to call any method from any classes that inherits from.
>
> I would like some comments on my implementation as it is a very
> important part of my future project, so I want to be sure it's good,
> secure and performant.
>
> Can you please tell me :
>
> 1. Any security issue?
> 2. Performance issue?
> 3. Too complex implementation?
>
> Any comments are welcome :-)
>
> The essential functions to check are createClass, createMetatable,
> createInheritance and the lookup function.
>
> Regards,
>

I said "parents" in the primary mail, but in the code I've set a field
"inherits", sorry for that typo.

--
Demelier David