|
local protectMT = {__newindex=error}
local levelMT = {__index=_G}
local MTindex
local function protectTable(tbl,index)
for k,v in pairs(tbl) do
if type(v)==table and getmetatable(v) ~= protectMT then
index[k] = {getmetatable(v),{}}
protectTable(v,index[k][2])
setmetatable(v,protectMT)
end
end
end
local function unprotectTable(tbl,index)
for k,v in pairs(tbl) do
if type(v)=="table" then
setmetatable(v,index[k][1])
-------------line 18 is here
unprotectTable(v,index[k][2])
end
end
end
local loadLevel = function(levelpath)
local levelenv=setmetatable({},levelMT)
local levelcode=loadfile(levelpath)
setfenv(levelcode,levelenv)
protectTable(_G,MTindex)
local ret = levelcode()
unprotectTable(_G,MTindex)
return ret
end
local success = loadLevel("c.lua")
collectgarbage()
if success then
loadNextLevel()
else
restartLevel()
end I run this lua script and get an error :
2010-03-25
bravefly
发件人: Pierre-Yves_Gérardy
发送时间: 2010-03-25 19:01:57
收件人: Lua list
抄送:
主题: Re: LuaJIT 2.0 and external
C libraries
On Thu, Mar 25, 2010 at 11:27, A.S. Bradbury <asb@asbradbury.org> wrote:
> Maybe I'm not understanding you properly, but isn't your C skiplist
> library just as efficient in LuaJIT as in standard Lua. It's just that
> with LuaJIT the transition disables optimisations and so your other
> plain Lua code is not as efficient as it once was.
I'll first have to write the C version and benchmark it in 5.1 vs the
pure Lua version in LuaJIT. There are some cases where C(++) is still
faster than LuaJIT, and most C libs don't have a pure Lua
counterpart...
BTW, would this code trick the JIT?
result = (function() return callHeavyCfunction() end)()
-- Pierre-Yves |