lua-users home
lua-l archive

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


Hi,

I have come across an inconsistency in lua5b2. Consider this fragment:

-------------------------------------------------------------------------
sb = { ['require'] = require, ['print']= print, ['tostring'] = tostring }

src =        'require "incl.lua" '
src = src .. 'b=2 '
src = src .. 'print("in sandbox: a=" .. tostring(a) .. ",b=" .. tostring(b))'
code = loadstring(src)

print("before sandbox: a=" .. tostring(a) .. ",b=" .. tostring(b))

setglobals(code, sb)
code()

print("after sandbox: a=" .. tostring(a) .. ",b=" .. tostring(b))
-------------------------------------------------------------------------

incl.lua just contains one statement:

-------------------------------------------------------------------------
a = 1
-------------------------------------------------------------------------

the result of executing the code looks like this:

-------------------------------------------------------------------------
before sandbox: a=nil,b=nil
in sandbox: a=nil,b=2
after sandbox: a=1,b=nil
-------------------------------------------------------------------------

this means that the global table I set before executing code is not
inherited by the require()d file. Instead, everything defined in the
external file goes to the initial global table. However, assignments
within the called functions go to my global table.

Is this behavious intentional?

Gunnar