lua-users home
lua-l archive

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


Ok, Nick, here is what I've come up with:

It gives all functions and tables within a particular table *scoped and
shared workspaces*.

Now all I need to do is make the default global context read-only to all
code that is loaded and I might have a safe environment... :-/

Of course now I have a massive overhead on all uses of "=" to variables
within object :-(  How about building something like this into the language
itself?

Regards

--- snip ---

object = { }        -- *the* object

function settable_event (table, key, value)
 rawset( table, key, value )
 if type(value) == "table" then
  setmetatable( rawget( table, key ), { __newindex = settable_event } ) -- 
inherit this metatable setting
 elseif type(value) == "function" then
  setfenv( value, table )    -- if its a function, set its global
environment to be the table its in
 end
end

setmetatable( object, { __newindex = settable_event } )    -- operator
override

object.willy1 = function( )    -- "member" function of "object"
 bum = 70
end

object.willy2 = function( )    -- "member" function of "object"
 bum = 80
end


object.nadgers = { }

object.nadgers.willy3 = function( )    -- "member" function of
"object.nadgers" which is another table
 bum = 90
end

bum = 50

object.willy1( )
object.willy2( )
object.nadgers.willy3( )

print( bum )
print( object.bum )
print( object.nadgers.bum )

print( "finished" )

--- snip ---

displays:

50
80
90
finished

-- 
---------------------------------
Q-Games, Dylan Cuthbert.
http://www.q-games.com