lua-users home
lua-l archive

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


On Jan 07, 2003 at 11:24 -0200, Luiz Henrique de Figueiredo wrote:
> >If I predefine a "foo" slot in g, then I get the expected error on "x = 1".
> 
> I think you mean to define "foo" in "G":
> 
> function G.foo() x = 1 end

This is what I did:

-- this code is extracted from test/readonly.lua
local f=function (t,i) error("cannot redefine global variable
`"..i.."'",2) end
local g={}
local G=getglobals()
g["foo"] = 0 -- tu: predefine global "foo", so "function foo() ..." doesn't fail
setmetatable(g,{__index=G,__newindex=f})
setglobals(1,g)

-- test function
function foo() x = 1 end

foo()  -- no error (tu: actually this *does* cause an error for me)
y = 1  -- error


-- 
Thatcher Ulrich
http://tulrich.com