Wrapping "Classes" seamlessly

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Wrapping "Classes" seamlessly

jdarling
I've worked up quite a few object wrappers for my applications, and have
run into a common problem with all.  If I want to create a new
descendent from the object I get errors back when trying to call the
methods.

My solution to this is to create a new "Class" in Lua and embed the
wrapped class into it.  This works fine, except that I have to create
wrapper methods for each method and property that exists in my new
version.  Can anyone give me some quick pointers to how to setup my
MetaTable to manage this for me?  I'd like something like a fall
through where if the method/property doesn't exist or isn't found in
the new version it falls back to the wrapped type.

Also does anyone know how hard it is to overload/override the default
require's handler?  I'd like to be able to redirect require and
dofile when editing the scripts so that if a modified version exists
in memory it is used over the one on disk.

 - Jeremy

"Help I suffer from the oxymoron Corporate Security."


Reply | Threaded
Open this post in threaded view
|

Re: Wrapping "Classes" seamlessly

Stefan Sandberg
It's painfully simple to see what's being done if you poke around a bit
in compat-5.1.lua.

Keffo.

[hidden email] wrote:

> I've worked up quite a few object wrappers for my applications, and have
> run into a common problem with all.  If I want to create a new
> descendent from the object I get errors back when trying to call the
> methods.
>
> My solution to this is to create a new "Class" in Lua and embed the
> wrapped class into it.  This works fine, except that I have to create
> wrapper methods for each method and property that exists in my new
> version.  Can anyone give me some quick pointers to how to setup my
> MetaTable to manage this for me?  I'd like something like a fall
> through where if the method/property doesn't exist or isn't found in
> the new version it falls back to the wrapped type.
>
> Also does anyone know how hard it is to overload/override the default
> require's handler?  I'd like to be able to redirect require and
> dofile when editing the scripts so that if a modified version exists
> in memory it is used over the one on disk.
>
>  - Jeremy
>
> "Help I suffer from the oxymoron Corporate Security."
>
>
>
>  

Reply | Threaded
Open this post in threaded view
|

RE: Wrapping "Classes" seamlessly

jdarling
In reply to this post by jdarling
never herd of compat...  Does it exist in 5.0, and if so where do I find
it :)?

 - Jeremy

"Help I suffer from the oxymoron Corporate Security."


> -------- Original Message --------
> Subject: Re: Wrapping "Classes" seamlessly
> From: Stefan Sandberg <[hidden email]>
> Date: Mon, May 15, 2006 2:55 pm
> To: Lua list <[hidden email]>
>
> It's painfully simple to see what's being done if you poke around a bit
> in compat-5.1.lua.
>
> Keffo.
>
> [hidden email] wrote:
> > I've worked up quite a few object wrappers for my applications, and have
> > run into a common problem with all.  If I want to create a new
> > descendent from the object I get errors back when trying to call the
> > methods.
> >
> > My solution to this is to create a new "Class" in Lua and embed the
> > wrapped class into it.  This works fine, except that I have to create
> > wrapper methods for each method and property that exists in my new
> > version.  Can anyone give me some quick pointers to how to setup my
> > MetaTable to manage this for me?  I'd like something like a fall
> > through where if the method/property doesn't exist or isn't found in
> > the new version it falls back to the wrapped type.
> >
> > Also does anyone know how hard it is to overload/override the default
> > require's handler?  I'd like to be able to redirect require and
> > dofile when editing the scripts so that if a modified version exists
> > in memory it is used over the one on disk.
> >
> >  - Jeremy
> >
> > "Help I suffer from the oxymoron Corporate Security."
> >
> >
> >
> >

Reply | Threaded
Open this post in threaded view
|

Re: Wrapping "Classes" seamlessly

Petite Abeille

On May 15, 2006, at 21:58, [hidden email] wrote:

> never herd of compat...  Does it exist in 5.0, and if so where do I
> find
> it :)?

It's a Kepler add on:

http://www.keplerproject.org/compat/

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/

Reply | Threaded
Open this post in threaded view
|

Re: Wrapping "Classes" seamlessly

Petite Abeille
In reply to this post by jdarling

On May 15, 2006, at 20:33, [hidden email] wrote:

> Also does anyone know how hard it is to overload/override the default
> require's handler?  I'd like to be able to redirect require and
> dofile when editing the scripts so that if a modified version exists
> in memory it is used over the one on disk.

Assuming you are keeping track of your "edited" file, this should be
pretty trivial as far as 'require' goes:

package.loaded[ aModuleName ] = loadstring( aModuleSource )()

Doing the opposite -e.g. reloading a module if its source has changed
on disk- is a bit more convoluted as you need to figure out where the
module was loaded from and keep track of its time stamp.

Here is a tortuous and probably boggy example, using LuaFileSystem [1]
to access a package modification date:

----8<----

require( "lfs" )

local _require = require
local _packages = {}

require = function( aName )
         local aModule = _require( aName )
         local aPackage = _packages[ aName ]

         if type( aModule ) ~= "table" then
                 aModule = _G[ aName ]
         end

         if aPackage == nil and type( aModule ) == "table" then
                 for aKey, aValue in pairs( aModule ) do
                         if type( aValue ) == "function" then
                                 local someInfo = debug.getinfo( aValue,
"S" )
                                 local aSource = someInfo.source

                                 if aSource ~= nil and aSource:byte() ==
64 then
                                         aPackage = {}
                                         aPackage.source = aSource:sub(
2 )
                                         aPackage.time = lfs.attributes(
aPackage.source, "modification" )

                                         _packages[ aName ] = aPackage

                                         break
                                 end
                         end
                 end
         end

         if aPackage ~= nil then
                 local aTime = lfs.attributes( aPackage.source,
"modification" )

                 if aTime > aPackage.time then
                         aPackage.time = aTime
                         package.loaded[ aName ] = nil

                         return require( aName )
                 end
         end

         return aModule
end

----8<----


Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/


[1] http://www.keplerproject.org/luafilesystem/

Reply | Threaded
Open this post in threaded view
|

RE: Wrapping "Classes" seamlessly

Andre Carregal
In reply to this post by jdarling
> PA wrote
> It's a Kepler add on:
> http://www.keplerproject.org/compat/

True, although Compat is not bound to Kepler in any way (in fact it is the
opposite, Kepler is bound to Compat).

Compat-5.1 is an implementation in Lua 5.0 of the Lua 5.1 module system
(basically the functions module and require).

You can use it with your 5.0 modules as a migration path to Lua 5.1.

Andre

Reply | Threaded
Open this post in threaded view
|

RE: Wrapping "Classes" seamlessly

jdarling
In reply to this post by jdarling
Thanks everyone, I'll start looking more into Compat and how I can use
it to fit my needs :).

 - Jeremy

"Help I suffer from the oxymoron Corporate Security."


> -------- Original Message --------
> Subject: RE: Wrapping "Classes" seamlessly
> From: "Andre Carregal" <[hidden email]>
> Date: Tue, May 16, 2006 11:06 am
> To: "'Lua list'" <[hidden email]>
>
> > PA wrote
> > It's a Kepler add on:
> > http://www.keplerproject.org/compat/
>
> True, although Compat is not bound to Kepler in any way (in fact it is the
> opposite, Kepler is bound to Compat).
>
> Compat-5.1 is an implementation in Lua 5.0 of the Lua 5.1 module system
> (basically the functions module and require).
>
> You can use it with your 5.0 modules as a migration path to Lua 5.1.
>
> Andre

Reply | Threaded
Open this post in threaded view
|

Re: Wrapping "Classes" seamlessly

Gavin Kistner
In reply to this post by jdarling
On May 15, 2006, at 2:33 PM, [hidden email] wrote:
> I've worked up quite a few object wrappers for my applications, and  
> have
> run into a common problem with all.  If I want to create a new
> descendent from the object I get errors back when trying to call the
> methods.

FWIW, here's my own Class system that I wrote in Lua, which supports  
class inheritance:

http://lua-users.org/lists/lua-l/2006-02/msg00263.html

That has the code for the class mechanism, and at the bottom includes  
some tests that also show how to use the system. It's not written as  
an example of how to wrap things up simply, but instead happens to be  
an example of code that handles the specific problems I was looking  
to solve.