Skip to content

Instantly share code, notes, and snippets.

@meepen
Last active September 5, 2019 08:24
Show Gist options
  • Save meepen/a520a738e29d7c6e8663 to your computer and use it in GitHub Desktop.
Save meepen/a520a738e29d7c6e8663 to your computer and use it in GitHub Desktop.
// Copyright © Kat ‘MeepDarknessMeep’ LastNameHere
LOCALS = LOCALS or setmetatable( { }, {
__mode = "k";
} );
local _nil = {};
local _debug_getinfo = debug.getinfo;
local _debug_getlocal = debug.getlocal;
local _rawget = rawget;
local _rawset = rawset;
local function _initialize_locals( _func )
local _iter = 1;
if ( !LOCALS[ _func ] ) then LOCALS[ _func ] = { }; end
while ( _debug_getlocal( _func, _iter ) ) do
local _name, _value = _debug_getlocal( _func, _iter );
_iter = _iter + 1;
if ( type( _value ) == "function" ) then
_initialize_locals( _value );
LOCALS[ _value ].inherits = _func;
end
end
end
local function _set_local( _func, _name, _value, _dontforce )
_value = _value == nil && _nil || _value;
local _locals = LOCALS[ _func ];
if ( !_locals ) then return false; end
if ( _locals.inherits && _set_local( _locals.inherits, _name, _value, true ) ) then
return true;
end
if ( _locals[ _name ] ) then
_locals[ _name ] = _value;
return true;
end
if ( !_dontforce ) then
_locals[ _name ] = _value;
return true;
end
end
local function _get_local( _func, _name )
local _locals = LOCALS[ _func ];
if ( !_locals ) then return nil; end
if ( _locals[ _name ] ) then
return _locals[ _name ] == _nil && nil || _locals[ _name ];
end
if ( _locals.inherits ) then
return _get_local ( _locals.inherits, _name );
end
return nil;
end
setmetatable( getfenv(0), {
__index = function( _self, _k )
if ( _k:sub( 1, 1 ) == "_" ) then
local _function = _debug_getinfo( 2 ).func;
return _get_local( _function, _k );
end
return _rawget( _self, _k );
end;
__newindex = function( _self, _k, _v )
if ( type( _v ) == "function" ) then
_initialize_locals( _v );
LOCALS[ _v ].inherits = _debug_getinfo( 2 ).func;
end
if ( _k:sub( 1,1 ) == "_" ) then
local _function = _debug_getinfo( 2 ).func;
_initialize_locals( _function );
_set_local( _function, _k, _v );
return;
end
_rawset( _self, _k, _v );
end;
} );
@ExtReMLapin
Copy link

stop bullying acecool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment