lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


If you are calling code that modifies the thread's global environment, you'll typically need to do more than reassign _G, which is set as a convenience:

 

local _G = getfenv(0) -- shadows global _G name within this block; does not affect global assignments

acme = setmetatable({}, {__index = _G})

setfenv(0, acme) -- replaces thread environment with new table

 

-- invoke code which modifies global environment

 

setfenv(0, _G) -- restores table saved in local as global environment

 

N.

 

----- Original Message -----
From: "Julien Duminil" <julien.duminil@ubisoft.com>
To: "Lua mailing list" <lua-l@lists.lua.org>
Sent: Thursday, February 24, 2011 8:31:29 AM
Subject: RE: Namespace advice needed

Maybe you could do a thing like:

acme = {}
-- Replace global table with acme
local old_G = _G
setmetatable(acme, { __index = old_G })
_G = acme
-- Require sub namespaces
require "my_lib_with_a_lot_of_namespaces"
-- Restore global table
_G = old_G
setmetatable(acme, nil)

or a C equivalent, so you catch every sub namespaces ...

Regards, Julien.

PS: i've not tested this code, sorry if it's wrong :)