Proto-Lua: Lua Types and Helpers For Prototypal-Based Design

Hey Corona Community,

I’ve been a Lua user for about a year now and I truly love the language but, as a professional ActipScript and JavaScript developer I too wish it were a little easier to ‘type’ tables and extend those types much like JavaScripts prototypes and ActionScript classes offer. So, I decided to experiment by implementing a prototypal API for Lua.

This API follows the preferred module design pattern and anyone who knows a bit about JavaScript constructors and prototypes will be able to create ‘typed’ tables in no time.

Take a look at it in use: https://github.com/dschnare/Proto-Lua/blob/master/src/example.lua

Here’s the source for those who just want to know how it works: https://github.com/dschnare/Proto-Lua/blob/master/src/proto.lua

The source file is fully documented and also contains a few other functions you can take advantage of mixin/adheresTo/instanceof and the is__ functions.

It’s opensource so download it, use it, fork it, let me know if you like it or don’t like it.

If you come from a JavaScript background you might find my Purejs API a useful starting ground since Proto-Lua was inspired by the Purejs API. [import]uid: 111848 topic_id: 19292 reply_id: 319292[/import]

I am naive. What is the purpose of this? [import]uid: 79135 topic_id: 19292 reply_id: 74424[/import]

Just to offer a design pattern for Lua developers to use to help make “custom types” in Lua. It’s useful since Lua only really offers tables for use as complex datatypes (that and only the C++ types exposed by the Corona API developers).

Check out the links for better examples and documentation.

Simple example:
[lua]-- Create our Animal constructor.
local Animal = proto.constructor.create({
init = function(self, name)
self.name = name
end,
– Copy method that facilitates our copy constructor.
– Up to us to initialize the instance properly.
copy = function(self, other)
self:init(other:getName())
end
})

– ‘manually’ add a new prototype method for all Animals.
function Animal.prototype:getName()
return self.name
end
– Create our Cat constructor that inherits from Animal’s prototype.
local Cat = proto.constructor.create(Animal, {
init = function(self, name)
Animal.prototype.init(self, name … ’ the cat’)
end
})
– Usage:
local felix = Cat(‘felix’)
print('felix’s name is: ', felix:getName()) – felix
print('is animal a Cat: ', felix:instanceof(Cat)) – true[/lua] [import]uid: 111848 topic_id: 19292 reply_id: 74425[/import]

Looks very interesting. Have you used it in any projects yet? This seems simpler than using classes and more in line with Lua’s style of doing things. I still haven’t looked extensively at the source code but from what I’ve seen it seems to be possible to change a base prototype’s method at run time and have that change available to all descendants automagically right? That is, unless that specific method is overridden in the descendant.
What about performance? Have you done any tests in that regard? [import]uid: 61899 topic_id: 19292 reply_id: 75977[/import]

Looks very interesting, however I too am wondering about performance [import]uid: 70847 topic_id: 19292 reply_id: 76011[/import]

That’s correct, any changes done to a prototype will be immediately available to all instances with the prototype in its prototype chain. In this respect it is identical to Javascript prototypes. I’ll perform some tests/examples for you when I get back from holidays and share the results.

I was almost considering writing a classical pattern but, you’re right in that the prototype approach is much more suited for Lua.

I tend to use it for all my Lua projects really, although most projects tend to be much smaller in scope than an entire game. I would assume it would be equally feasible for use in larger projects as well.

As others have shown interest in the performance implications of this approach ( me included ) I’ll perform benchmark testing and make optimizations where necessary in the next week or so and report back with the results.

Thanks for your interest! [import]uid: 111848 topic_id: 19292 reply_id: 76032[/import]

interesting, but i cant see any usage of it right now, can anyone show me an example what can i do with this prototyping?

thanks [import]uid: 16142 topic_id: 19292 reply_id: 76049[/import]

@darkconsoles You can use it to create your own custom objects that need to share a set of methods and properties, amongst other advantages.
Suppose that in your game you have an Enemy prototype and then you create objects (enemies) based on this prototype. You can define a set of properties in the original prototype that all the enemy characters are going to inherit. This simplifies the creation of objects and makes them more dynamic. This can be useful if you need to change some characteristic in all enemies at run time. Suppose that due to some event in the game the difficulty increases, and that increase is reflected in the amount of life points the enemies have. You just change the life points property of the enemy prototype and all enemies will instantly receive this change. You can even change the prototype of an object at runtime so that you “transform” that object into something else. I didn’t analyse this Lua prototypal implementation thoroughly, but at least in JavaScript changing the prototype at runtime works.
More info about prototype based programming in here: http://en.wikipedia.org/wiki/Prototype-based_programming [import]uid: 61899 topic_id: 19292 reply_id: 76769[/import]

it seems like something similar to using metatables, or is it completely different? [import]uid: 16142 topic_id: 19292 reply_id: 76770[/import]

This implementation uses metatables, but also provides some useful methods for dealing with objects, hierarchy, relations between objects and inheritance. [import]uid: 61899 topic_id: 19292 reply_id: 76777[/import]

Thanks ClulessIdeas, everything you describe is indeed correct. I’ve spent some time during my holidays optimizing and adding new features so that Proto-Lua is more akin to Javascript prototypes. And even though the ability to change the prototype of an instance at runtime has long been disabled in Javascript, I decided to implement this feature anyways.

The new features/improvements are:

  • Instances can have their prototypes changed at runtime so an instance can have its “type” changed at will via the ‘__proto’ property.

  • All instances share a single metatbale, saving huge on memory usage over creating a new metatable for each constructor.

  • Metamethods are now supported throughout an instances protoype chain. This also means that metamethods can be inherited, extended and modified with ease.

  • The ‘instanceof’ function now works in the same fashion as the Javascript operator of the same name.

  • Added support for C++ style copy constructors via the ‘__copy’ method.

  • The special methods ‘copy’ and ‘init’ have been renamed to ‘__init’ and ‘__copy’ respectively to align with metamethod naming conventions.
    I haven’t had the time to run any benchmarks yet, but I have spent several days writing a Wiki that will hopefully answer anyone’s questions and hopefully entice people to try it out.

The Wiki is organized in a step-wise fashion where each page takes you through more and more advanced examples and topics. [import]uid: 111848 topic_id: 19292 reply_id: 77104[/import]

thanks for your hard work, i will definitely check this Wiki out [import]uid: 16142 topic_id: 19292 reply_id: 77106[/import]