Skip to content

Commit

Permalink
Fixed compatibility between Lua 5.1 and 5.2+
Browse files Browse the repository at this point in the history
  • Loading branch information
madmaxoft committed Dec 15, 2016
1 parent c4cb9a5 commit 80c2548
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
21 changes: 18 additions & 3 deletions src/bin/lua/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ local tab = table
tinsert = tab.insert
tremove = tab.remove
sort = tab.sort
local unpack = table.unpack or unpack

-------------------------------------------------------------------
-- Debug library
Expand Down Expand Up @@ -80,7 +81,7 @@ max = math.max
min = math.min
mod = math.mod
PI = math.pi
--??? pow = math.pow
--??? pow = math.pow
rad = math.rad
random = math.random
randomseed = math.randomseed
Expand Down Expand Up @@ -178,7 +179,7 @@ function read (...)
if rawtype(arg[1]) == 'userdata' then
f = tab.remove(arg, 1)
end
return f:read(table.unpack(arg))
return f:read(unpack(arg))
end

function write (...)
Expand All @@ -187,6 +188,20 @@ function write (...)
if rawtype(arg[1]) == 'userdata' then
f = tab.remove(arg, 1)
end
return f:write(table.unpack(arg))
return f:write(unpack(arg))
end

function loadstringwithenv(a_String, a_Env)
if (_VERSION == "Lua 5.1") then
local res, err = loadstring(a_String, "loadstringwithenv")
if not(res) then
return nil, err
end
if (a_Env) then
setfenv(res, a_Env)
end
return res
else
return load(a_String, "loadstringwithenv", "t", a_Env)
end
end
4 changes: 2 additions & 2 deletions src/bin/lua/package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ function prep(file)
table.insert(chunk, string.sub(line, 3) .. "\n")
else
local last = 1
for text, expr, index in string.gfind(line, "(.-)$(%b())()") do
for text, expr, index in string.gfind(line, "(.-)$(%b())()") do
last = index
if text ~= "" then
table.insert(chunk, string.format('table.insert(__ret, %q )', text))
Expand All @@ -334,7 +334,7 @@ function prep(file)
end
end
table.insert(chunk, '\nreturn table.concat(__ret)\n')
local f,e = load(table.concat(chunk), "ld", "t", _extra_parameters)
local f,e = loadstringwithenv(table.concat(chunk), _extra_parameters)
if e then
error("#"..e)
end
Expand Down

0 comments on commit 80c2548

Please sign in to comment.