Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local blockTables = {getfenv()}
- --local blockTables = {_G,getfenv()} --blocks _G even in a sub program
- local FakeToRealTableMap = {} --shadowTable: fake --> orig mapping
- local RealToFakeTableMap = {} --reverse
- local tEnv
- local ErrorOnIndex = function( tableToIndex,key,value )
- error(
- string.format( "attempt to %s global %s %q%s",
- FakeToRealTableMap[ tableToIndex ] and FakeToRealTableMap[ tableToIndex ][ key ] ~= nil and "change" or "create",
- type( FakeToRealTableMap[ tableToIndex ] and FakeToRealTableMap[ tableToIndex ][ key ] or value ),
- key or "Unknown",tableToIndex==_G and " in _G" or ( tableToIndex==getfenv( ) or tableToIndex==tEnv ) and " in the current environment" or ""
- ),
- 2 )
- end
- local writeBlockingMetatable
- local createNewBlockedTable = function( ) return setmetatable( {},writeBlockingMetatable ) end
- local localType = type
- local blockingIndex = function( tableToIndex,key )
- if localType( FakeToRealTableMap[ tableToIndex ][ key ] ) == "table" then
- if RealToFakeTableMap[ FakeToRealTableMap[ tableToIndex ][ key ] ] then
- return RealToFakeTableMap[ FakeToRealTableMap[ tableToIndex ][ key ] ]
- else
- local newBlockedTable = createNewBlockedTable( )
- FakeToRealTableMap[ newBlockedTable ] = FakeToRealTableMap[ tableToIndex ][ key ]
- RealToFakeTableMap[ FakeToRealTableMap[ tableToIndex ][ key ] ] = newBlockedTable
- return newBlockedTable
- end
- else
- return FakeToRealTableMap[ tableToIndex ][ key ]
- end
- end
- writeBlockingMetatable = {__index=blockingIndex,__newindex=ErrorOnIndex,__metatable="Allready used"}
- local args = {...}
- local localPairs=pairs
- local localRawset = rawset
- local localGetmetatable = getmetatable
- local localSetmetatable = setmetatable
- local localNext = _G.next
- _G.setmetatable = function( ... ) return select( 2,pcall( localSetmetatable,... ) ) end
- if #args > 0 then
- local env = {shell=shell,multishell=multishell}
- setmetatable( env,{__index=_G} )
- tEnv = createNewBlockedTable( )
- FakeToRealTableMap[ tEnv ] = env
- RealToFakeTableMap[ env ] = tEnv
- os.run( tEnv,shell.resolveProgram( args[ 1 ] ),unpack( args,2 ) )
- _G.setmetatable = localSetmetatable
- return
- end
- _G.next = function( tableToIndex,key )
- if FakeToRealTableMap[ tableToIndex ] then
- return localNext( FakeToRealTableMap[ tableToIndex ],key )
- else
- return localNext( tableToIndex,key )
- end
- end
- _G.pairs = function( tableToIndex ) return _G.next,tableToIndex,nil end
- local CopyOfTable
- for taskNumber,oTable in localPairs( blockTables ) do
- CopyOfTable = {}
- for i,j in localPairs( oTable ) do
- CopyOfTable[ i ] = j
- end
- FakeToRealTableMap[ oTable ]= CopyOfTable
- RealToFakeTableMap[ CopyOfTable ] = oTable
- local oldMeta = localGetmetatable( oTable )
- if oldMeta and type( oldMeta.__index ) == "table" then
- setmetatable( CopyOfTable,FakeToRealTableMap[ oldMeta.__index ] and rawset( oldMeta,"__index",FakeToRealTableMap[ oldMeta.__index ] ) or oldMeta )
- end
- setmetatable( oTable,writeBlockingMetatable )
- for i in localPairs( CopyOfTable ) do
- localRawset( oTable,i,nil )
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment