lua-users home
lua-l archive

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


That is very interesting. I modified Lua in the way you describe, and I get these results. Is these the same with what you are proposing?

----- example0.lua
-- Original example
local proxy = setmetatable({}, {__index = function(tab) return tab.greeting end})
local test = setmetatable({ greeting = "hello" }, {__index = proxy})
print(test.unknownkey)

-- Lua: C stack overflow
-- Modified Lua: hello

-------------------------
----- example1.lua
-- Changed tab.greeting to rawget(tab, "greeting")
local proxy = setmetatable({}, {__index = function(tab) return rawget(tab, "greeting") end})
local test = setmetatable({ greeting = "hello" }, {__index = proxy})
print(test.unknownkey)

-- Lua: nil
-- Modified Lua: hello


-------------------------
----- example2.lua
-- Set a greeting value in proxy
local proxy = setmetatable({greeting = "hi"}, {__index = function(tab) return rawget(tab, "greeting") end})
local test = setmetatable({ greeting = "hello" }, {__index = proxy})
print(test.unknownkey)

-- Lua: hi
-- Modified Lua: hello


-------------------------
----- example3.lua
-- Removed greeting value in test
local proxy = setmetatable({greeting = "hi"}, {__index = function(tab) return rawget(tab, "greeting") end})
local test = setmetatable({ }, {__index = proxy})
print(test.unknownkey)

-- Lua: hi
-- Modified Lua: nil