fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
a8034e9
v2.1.6
1local class =require ( "class" ) 2 3local OBJECT ={} 4 5function OBJECT : __call ( ... ) 6local class =rawget ( self , "class" ) 7local classname =rawget ( class , "classname" ) 8local constructor =rawget ( class , classname ) 9if not constructor then 10-- constructor doesn't exist, error 11return error ( string . format ( "can not create object for class '%s' (no constructor)" , classname )) 12end 13-- call our constructor method 14constructor ( self , ... ) 15-- return the object 16return self 17end 18 19local function recursiveIndex ( class , key ) 20local value =rawget ( class , key ) 21if value ~= nil then 22return value 23else 24class =rawget ( class , "class" ) 25if class then 26-- Check the inherited class 27return recursiveIndex ( class , key ) 28end 29end 30return nil 31end 32 33function OBJECT : __index ( key ) 34-- Check for any specific, class-based, methods like "super" 35local meta_index =rawget ( getmetatable ( self ), key ) -- OBJECT 36if meta_index ~= nil then 37-- Only return if not nil 38return meta_index 39end 40 41-- Get our "self" class structure 42local class =rawget ( self , "__self" ) 43 44-- Recursive lookup down the class line 45return recursiveIndex ( class , key ) 46end 47 48function OBJECT : instanceof ( other ) 49local class =rawget ( self , "class" ) 50local name 51local tp =type ( other ) 52 53if tp == "table" then 54-- User passed another object, get the classname 55name =rawget ( rawget ( self , "class" ), "classname" ) 56elseif tp == "string" then 57-- User passed the classname 58name =other 59end 60 61-- Handle invalid arguments 62if not name then return error ( string . format ( "unexpected argument #2 (string or object expected, got %s" , tp )) end 63 64while class do 65-- Loop through the class tree and see if any match 66if class : getName () == name then 67return true 68end 69class =rawget ( class , "class" ) 70end 71 72-- Nothing found 73return false 74end 75 76function OBJECT : super ( method , ... ) 77local this =rawget ( self , "__self" ) 78local class =rawget ( this , "class" ) 79 80-- Error if the scope of the super call has no inheritance (AKA the main class) 81if not class then return error ( "attempted to call method 'super' in class with no baseclass" ) end 82 83-- Use the method name or the class name (for constructor) if nil 84method =method or class . classname 85 86-- Recursively lookup the method starting at our "self" class 87local func =recursiveIndex ( class , method ) 88 89if func == nil or type ( func ) ~= "function" then 90-- Method doesn't exist, throw an error 91return error ( string . format ( "attempted to call method '%s' (a %s value)" , method , type ( func ))) 92end 93 94-- The "self" is now inside the inherited class structure 95rawset ( self , "__self" , class ) 96 97-- Call the method 98-- This can trigger another super call, so thats why we need "self" as our defined scope 99local ret =func ( self , ... ) 100 101-- The "self" is back to the main class structure 102rawset ( self , "__self" , this ) 103 104-- return whatever our method returned 105return ret 106end 107 108function OBJECT : getBaseClass () 109return rawget ( rawget ( self , "class" ), "class" ) 110end 111 112function OBJECT : getClass () 113return rawget ( self , "class" ) 114end 115 116function OBJECT : type () 117return "object" 118end 119 120function OBJECT : __tostring () 121return string . format ( "%s: %p" , rawget ( rawget ( self , "class" ), "classname" ), self ) 122end 123 124local CLASS ={} 125CLASS . __index =CLASS 126 127function CLASS : getBaseClass () 128return rawget ( self , "class" ) 129end 130 131function CLASS : getName () 132return rawget ( self , "classname" ) 133end 134 135function OBJECT : type () 136return "class" 137end 138 139function CLASS : __tostring () 140return string . format ( "Class[%q]: %p" , self : getName (), self ) 141end 142 143local object ={} 144 145do 146local function copy ( object ) 147local lookup_table ={} 148local function _copy ( object ) 149if type ( object ) ~= "table" then 150return object 151elseif lookup_table [ object ] then 152return lookup_table [ object ] 153end 154local new_table ={} 155lookup_table [ object ] =new_table 156for index , value in pairs ( object ) do 157new_table [ _copy ( index )] =_copy ( value ) 158end 159return setmetatable ( new_table , getmetatable ( object )) 160end 161return _copy ( object ) 162end 163 164local function createObjectClass ( name ) 165local main =class . get ( name ) 166local base =class . getBase ( name ) 167local struct =setmetatable ( copy ( main ), CLASS ) 168if base then 169struct . class =createObjectClass ( base ) 170end 171return struct 172end 173 174function object . new ( name , ... ) 175if not class . get ( name ) then 176return error (( "failed to create object for unknown class '%s'" ): format ( name )) 177end 178 179-- Create a copy of the class structure, with metatable and all 180local obj =setmetatable ( { class =createObjectClass ( name ) } , OBJECT ) 181 182-- Set our current "self" to our class structure 183obj . __self =obj . class 184 185-- Return our new object and call constructor 186return obj ( ... ) 187end 188end 189 190return object