Browse files

utils.lua51 is now defined; missing unpack or loadstring for 5.2 has …

…been sorted
  • Loading branch information...
1 parent c566e6a commit ee7c83df8c189c4547d755cea9e9c041ce44b1a2 @stevedonovan committed Apr 23, 2012
Showing with 42 additions and 11 deletions.
  1. +2 −1 lua/pl/data.lua
  2. +1 −1 lua/pl/input.lua
  3. +1 −1 lua/pl/stringio.lua
  4. +1 −1 lua/pl/tablex.lua
  5. +9 −5 lua/pl/utils.lua
  6. +28 −2 tests/test-pretty.lua
View
3 lua/pl/data.lua
@@ -24,7 +24,8 @@ local patterns,function_arg,usplit = utils.patterns,utils.function_arg,utils.spl
local append,concat = table.insert,table.concat
local gsub = string.gsub
local io = io
-local _G,print,loadstring,type,tonumber,ipairs,setmetatable,pcall,error,setfenv = _G,print,loadstring,type,tonumber,ipairs,setmetatable,pcall,error,setfenv
+local _G,print,type,tonumber,ipairs,setmetatable,pcall,error,setfenv = _G,print,type,tonumber,ipairs,setmetatable,pcall,error,setfenv
+
local data = {}
View
2 lua/pl/input.lua
@@ -11,7 +11,7 @@
local strfind = string.find
local strsub = string.sub
local strmatch = string.match
-local pairs,type,unpack,tonumber = pairs,type,unpack,tonumber
+local pairs,type,unpack,tonumber = pairs,type,unpack or table.unpack,tonumber
local utils = require 'pl.utils'
local patterns = utils.patterns
local io = io
View
2 lua/pl/stringio.lua
@@ -12,7 +12,7 @@
-- See @{03-strings.md.File_style_I_O_on_Strings|the Guide}.
-- @module pl.stringio
-local getmetatable,tostring,unpack,tonumber = getmetatable,tostring,unpack,tonumber
+local getmetatable,tostring,unpack,tonumber = getmetatable,tostring,unpack or table.unpack,tonumber
local concat,append = table.concat,table.insert
local stringio = {}
View
2 lua/pl/tablex.lua
@@ -7,7 +7,7 @@
local getmetatable,setmetatable,require = getmetatable,setmetatable,require
local append,remove = table.insert,table.remove
local min,max = math.min,math.max
-local pairs,type,unpack,next,select,tostring = pairs,type,unpack,next,select,tostring
+local pairs,type,unpack,next,select,tostring = pairs,type,unpack or table.unpack,next,select,tostring
local utils = require ('pl.utils')
local function_arg = utils.function_arg
local Set = utils.stdmt.Set
View
14 lua/pl/utils.lua
@@ -12,7 +12,12 @@ local utils = {}
utils._VERSION = "1.0.1"
-unpack = unpack or table.unpack -- Lua 5.2 compatibility
+local lua51 = rawget(_G,'unpack')
+utils.lua51 = lua51
+if not lua51 then -- Lua 5.2 compatibility
+ unpack = table.unpack
+ loadstring = load
+end
utils.dir_separator = _G.package.config:sub(1,1)
@@ -198,10 +203,9 @@ function utils.splitv (s,re)
return unpack(utils.split(s,re))
end
-local lua52 = table.pack ~= nil
local lua51_load = load
-if not lua52 then -- define Lua 5.2 style load()
+if utils.lua51 then -- define Lua 5.2 style load()
function utils.load(str,src,mode,env)
local chunk,err
if type(str) == 'string' then
@@ -256,14 +260,14 @@ end
-- @return actual return code
function utils.execute (cmd)
local res1,res2,res2 = os.execute(cmd)
- if not lua52 then
+ if lua51 then
return res1==0,res1
else
return res1,res2
end
end
-if not lua52 then
+if lua51 then
function table.pack (...)
local n = select('#',...)
return {n=n; ...}
View
30 tests/test-pretty.lua
@@ -1,5 +1,7 @@
local pretty = require 'pl.pretty'
-local asserteq = require('pl.test').asserteq
+local utils = require 'pl.utils'
+local test = require 'pl.test'
+local asserteq, assertmatch = test.asserteq, test.assertmatch
t1 = {
'one','two','three',{1,2,3},
@@ -30,7 +32,31 @@ res,err = pretty.read [[
['do'] = function() return end
}
]]
-assert(err == 'cannot have Lua keywords in table definition')
+assertmatch(err,'cannot have functions in table definition')
+
+res,err = pretty.load([[
+-- comments are ok
+a = 2
+bonzo = 'dog'
+t = {1,2,3}
+]])
+
+asserteq(res,{a=2,bonzo='dog',t={1,2,3}})
+
+--- another potential problem is string functions called implicitly as methods--
+res,err = pretty.read [[
+{s = ('woo'):gsub('w','wwwwww'):gsub('w','wwwwww')}
+]]
+
+assertmatch(err,utils.lua51 and 'attempt to index a string value' or "attempt to index constant 'woo'")
+
+---- pretty.load has a _paranoid_ option
+res,err = pretty.load([[
+k = 0
+for i = 1,1e12 do k = k + 1 end
+]],{},true)
+
+assertmatch(err,'looping not allowed')
-- Check to make sure that no spaces exist when write is told not to
local tbl = { "a", 2, "c", false, 23, 453, "poot", 34 }

0 comments on commit ee7c83d

Please sign in to comment.