Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function new( object )
- local mt = {}
- for k, v in pairs( getmetatable( object ) ) do
- mt[ k ] = v
- end
- mt.inherits = { object.class(), unpack( mt.inherits ) }
- mt.__super = object
- mt.realValues = {}
- return setmetatable( {}, mt )
- end
- local Class = setmetatable( {}, {
- __call = function( t, ... )
- local tArgs = { ... }
- if type( tArgs[ 1 ] ) == "table" and tArgs[ 1 ].name then
- local n = new( t )
- local mt = getmetatable( n )
- mt.inherits[ 1 ] = tArgs[ 1 ].name
- for k, v in pairs( tArgs[ 1 ] ) do
- n[ k ] = v
- end
- return n
- end
- return new( t )
- end,
- inherits = { "Class" },
- realValues = {},
- __super = {},
- __index = function( t, k )
- local temp = rawget( getmetatable( t ).realValues, k )
- local result = temp ~= nil and temp or getmetatable( t ).__super[ k ]
- if type( result ) == "function" then
- local env = getfenv( result )
- env.this, env.super = t, getmetatable( t ).__super
- setfenv( result, env )
- end
- return result
- end,
- __lt = function( left, right )
- return left.extends( right.class() )
- end,
- __newindex = function( t, k, v )
- if type( v ) == "function" then
- setfenv( v, setmetatable( {}, {__index = getfenv( v ) } ) )
- end
- getmetatable( t ).realValues[ k ] = v
- end,
- })
- Class.Class = function() end
- Class.name = "Class"
- Class.class = function()
- local inherits = getmetatable( this ).inherits
- return inherits[ 1 ]
- end
- Class.extends = function( s )
- local inherits = getmetatable( this ).inherits
- for k, v in pairs( inherits ) do
- if v == s then
- return true
- end
- end
- return false
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement