Lua _G 元表不工作

Lua _G metatable not working

我正在尝试控制交互环境。这是我的尝试:

home: lua
Lua 5.0.3  Copyright (C) 1994-2006 Tecgraf, PUC-Rio
> for n in pairs(_G) do io.write(n) end
stringxpcallLUA_PATHtostringgcinfoloadlibosunpackrequiregetfenvsetmetatablenext_TRACEBACKasserttonumberiorawequalcollectgarbagegetmetatable_LOADEDrawsetmathLUA_CPATHpcalldebug__powtypetablecoroutineprint_Gnewproxyrawgetloadstring_VERSIONdofilesetfenvpairsipairserrorloadfile> 
> G=_G
> _G={}
> setmetatable(_G,{__index=G,__newindex=function() print("nope") end})
> for n in pairs(_G) do io.write(n) end
> x=3
> 

我希望在命令 "x=3" 之后看到 "nope"。然而,它通过了。 _G 不是空的吗,_G 的任何 access/update 都会通过定义的元方法?

来自 Lua 5.0 手册:

_G

A global variable (not a function) that holds the global environment (that is, _G._G = _G). Lua itself does not use this variable; changing its value does not affect any environment. (Use setfenv to change environments.)

更具体地说,在交互式 Lua 解释器中,您需要 setfenv( 0, new_global_env ) 将当前 运行 线程的环境更改为 new_global_env

示例:

Lua 5.0.3  Copyright (C) 1994-2006 Tecgraf, PUC-Rio
> do
>> local rawget, print, _G = rawget, print, _G
>> local new_global_env = setmetatable( { tostring = tostring }, {
>>   __index = function( t, k )
>>     print( "get", k )
>>     return rawget( _G, k )
>>   end
>> } )
>> setfenv( 0, new_global_env )
>> end
> print( "hello" )
get print
hello
> 

rawgetprint_G 的上值以及新的 tostring 全局变量对于避免无限递归是必要的。)