[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Emulating .prototype semantics of JavaScript in Lua. What does ittake?
- From: "Virgil Smith" <Virgil@...>
- Date: Tue, 17 Feb 2004 12:03:24 -0600
>From a quick skim through your referenced tutorial it sounds like this will
be extremely simple to implement in Lua.
<<< With regards to tables as the objects in question >>>
Just use the _index and _newindex metamethods and catch use of the key
"prototype". When referencing "prototype" use a shared table to contain the
"prototype"d properties, otherwise just set direct members of the table in
question. Of course you'll want all or tables of a particular
"type"/"class" to use the same metatable (containing the mentioned _index
and _newindex methods and shared "prototype" table).
<<< Extending to userdata >>>
The only complexity regarding userdata as opposed to tables is the question
of where to store non-"prototype"d properties. Basically, you are going to
want to associate a Lua table with each userdata instance (in addition to
the metatable associated with all userdata of a particular "type"/"class").
To do that I suggest you add a weak keyed table to the metatable for that
"type"/"class" of userdata. Use the userdata instances as the keys and the
per instance tables as the values.
-----
Related reading.
1. Go back through the section in the manual on metatables (with special
attention to the _index and _newindex methods).
2. Find the manual section on weak tables and go through that.
3. Check out the code for the auxiliary library. Some of it makes for very
useful suggestions on handling metatables of userdata.
4. Search the list archives for threads related to metatables and instance
specific data. There have been several of these, but unfortunately no good
keywords leap to mind to aide you in your search.
5. For some more "advanced" material search the list archives for threads
related to weak tables, there has been quite a few discussions on aspects of
their use.
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Vijay Aswadhati
Sent: Tuesday, February 17, 2004 11:26 AM
To: lua-l@lua.org
Subject: Emulating .prototype semantics of JavaScript in Lua. What does
ittake?
Greetings:
Back from a two month long sabbatical - offered as an excuse for me not
thinking too hard on the subject!
I have dabbled with metatables and I think I have basic yet fuzzy
understanding of how it all works. In one of the projects that I will be
working on, I have already made my decision to incorporate Lua as the
scripting language of choice for backend systems. However the browser-based
front end systems are chockfull of JavaScripts unavoidably. To aid the poor
programmer who will be working on both backend and front-end systems, I am
contemplating on hiding all the details of metatables of Lua and present
instead something similar to JavaScript "prototype" based inheritance in
terms of look and feel. My gut feeling says that it should be doable and it
stands at that. Now for the questions:
a) Can I emulate this using the Lua API itself or will I have to modify the
source code?
b) If doable either way, any gotchas about performance - pay as you go
versus pay upfront?
c) Any other issues that I need to be aware of.
My intention is not start a debate on changing Lua to be something else - I
understand and appreciate the differences in opinion. For the curious there
is a very simple tutorial illustrating the use of ".prototype" at
http://www.javascriptkit.com/javatutors/proto.shtml
Thanks for your time.
Vijay Aswadhati