|
1 |
| ----------------- |
2 |
| ---- Lua 5.1/5.2 compatibility |
3 |
| --- Ensures that `table.pack` and `package.searchpath` are available |
4 |
| --- for Lua 5.1 and LuaJIT. |
5 |
| --- The exported function `load` is Lua 5.2 compatible. |
6 |
| --- `compat.setfenv` and `compat.getfenv` are available for Lua 5.2, although |
7 |
| --- they are not always guaranteed to work. |
8 |
| --- @module pl.compat |
9 |
| - |
10 |
| -local compat = {} |
11 |
| - |
12 |
| -compat.lua51 = _VERSION == 'Lua 5.1' |
13 |
| - |
14 |
| ---- execute a shell command. |
15 |
| --- This is a compatibility function that returns the same for Lua 5.1 and Lua 5.2 |
16 |
| --- @param cmd a shell command |
17 |
| --- @return true if successful |
18 |
| --- @return actual return code |
19 |
| -function compat.execute (cmd) |
20 |
| - local res1,res2,res2 = os.execute(cmd) |
21 |
| - if compat.lua51 then |
22 |
| - return res1==0,res1 |
23 |
| - else |
24 |
| - return not not res1,res2 |
25 |
| - end |
26 |
| -end |
27 |
| - |
28 |
| ----------------- |
29 |
| --- Load Lua code as a text or binary chunk. |
30 |
| --- @param ld code string or loader |
31 |
| --- @param[opt] source name of chunk for errors |
32 |
| --- @param[opt] mode 'b', 't' or 'bt' |
33 |
| --- @param[opt] env environment to load the chunk in |
34 |
| --- @function compat.load |
35 |
| - |
36 |
| ---------------- |
37 |
| --- Get environment of a function. |
38 |
| --- With Lua 5.2, may return nil for a function with no global references! |
39 |
| --- Based on code by [Sergey Rozhenko](http://lua-users.org/lists/lua-l/2010-06/msg00313.html) |
40 |
| --- @param f a function or a call stack reference |
41 |
| --- @function compat.setfenv |
42 |
| - |
43 |
| ---------------- |
44 |
| --- Set environment of a function |
45 |
| --- @param f a function or a call stack reference |
46 |
| --- @param env a table that becomes the new environment of `f` |
47 |
| --- @function compat.setfenv |
48 |
| - |
49 |
| -if compat.lua51 then -- define Lua 5.2 style load() |
50 |
| - if not tostring(assert):match 'builtin' then -- but LuaJIT's load _is_ compatible |
51 |
| - local lua51_load = load |
52 |
| - function compat.load(str,src,mode,env) |
53 |
| - local chunk,err |
54 |
| - if type(str) == 'string' then |
55 |
| - if str:byte(1) == 27 and not (mode or 'bt'):find 'b' then |
56 |
| - return nil,"attempt to load a binary chunk" |
57 |
| - end |
58 |
| - chunk,err = loadstring(str,src) |
59 |
| - else |
60 |
| - chunk,err = lua51_load(str,src) |
61 |
| - end |
62 |
| - if chunk and env then setfenv(chunk,env) end |
63 |
| - return chunk,err |
64 |
| - end |
65 |
| - else |
66 |
| - compat.load = load |
67 |
| - end |
68 |
| - compat.setfenv, compat.getfenv = setfenv, getfenv |
69 |
| -else |
70 |
| - compat.load = load |
71 |
| - -- setfenv/getfenv replacements for Lua 5.2 |
72 |
| - -- by Sergey Rozhenko |
73 |
| - -- http://lua-users.org/lists/lua-l/2010-06/msg00313.html |
74 |
| - -- Roberto Ierusalimschy notes that it is possible for getfenv to return nil |
75 |
| - -- in the case of a function with no globals: |
76 |
| - -- http://lua-users.org/lists/lua-l/2010-06/msg00315.html |
77 |
| - function compat.setfenv(f, t) |
78 |
| - f = (type(f) == 'function' and f or debug.getinfo(f + 1, 'f').func) |
79 |
| - local name |
80 |
| - local up = 0 |
81 |
| - repeat |
82 |
| - up = up + 1 |
83 |
| - name = debug.getupvalue(f, up) |
84 |
| - until name == '_ENV' or name == nil |
85 |
| - if name then |
86 |
| - debug.upvaluejoin(f, up, function() return name end, 1) -- use unique upvalue |
87 |
| - debug.setupvalue(f, up, t) |
88 |
| - end |
89 |
| - if f ~= 0 then return f end |
90 |
| - end |
91 |
| - |
92 |
| - function compat.getfenv(f) |
93 |
| - local f = f or 0 |
94 |
| - f = (type(f) == 'function' and f or debug.getinfo(f + 1, 'f').func) |
95 |
| - local name, val |
96 |
| - local up = 0 |
97 |
| - repeat |
98 |
| - up = up + 1 |
99 |
| - name, val = debug.getupvalue(f, up) |
100 |
| - until name == '_ENV' or name == nil |
101 |
| - return val |
102 |
| - end |
103 |
| -end |
104 |
| - |
105 |
| ---- Lua 5.2 Functions Available for 5.1 |
106 |
| --- @section lua52 |
107 |
| - |
108 |
| ---- pack an argument list into a table. |
109 |
| --- @param ... any arguments |
110 |
| --- @return a table with field n set to the length |
111 |
| --- @return the length |
112 |
| --- @function table.pack |
113 |
| -if not table.pack then |
114 |
| - function table.pack (...) |
115 |
| - return {n=select('#',...); ...} |
116 |
| - end |
117 |
| -end |
118 |
| - |
119 |
| ------- |
120 |
| --- return the full path where a Lua module name would be matched. |
121 |
| --- @param mod module name, possibly dotted |
122 |
| --- @param path a path in the same form as package.path or package.cpath |
123 |
| --- @see path.package_path |
124 |
| --- @function package.searchpath |
125 |
| -if not package.searchpath then |
126 |
| - local sep = package.config:sub(1,1) |
127 |
| - function package.searchpath (mod,path) |
128 |
| - mod = mod:gsub('%.',sep) |
129 |
| - for m in path:gmatch('[^;]+') do |
130 |
| - local nm = m:gsub('?',mod) |
131 |
| - local f = io.open(nm,'r') |
132 |
| - if f then f:close(); return nm end |
133 |
| - end |
134 |
| - end |
135 |
| -end |
136 |
| - |
137 |
| -return compat |
| 1 | +---------------- |
| 2 | +--- Lua 5.1/5.2 compatibility |
| 3 | +-- Ensures that `table.pack` and `package.searchpath` are available |
| 4 | +-- for Lua 5.1 and LuaJIT. |
| 5 | +-- The exported function `load` is Lua 5.2 compatible. |
| 6 | +-- `compat.setfenv` and `compat.getfenv` are available for Lua 5.2, although |
| 7 | +-- they are not always guaranteed to work. |
| 8 | +-- @module pl.compat |
| 9 | + |
| 10 | +local compat = {} |
| 11 | + |
| 12 | +compat.lua51 = _VERSION == 'Lua 5.1' |
| 13 | + |
| 14 | +--- execute a shell command. |
| 15 | +-- This is a compatibility function that returns the same for Lua 5.1 and Lua 5.2 |
| 16 | +-- @param cmd a shell command |
| 17 | +-- @return true if successful |
| 18 | +-- @return actual return code |
| 19 | +function compat.execute (cmd) |
| 20 | + local res1,res2,res2 = os.execute(cmd) |
| 21 | + if compat.lua51 then |
| 22 | + return res1==0,res1 |
| 23 | + else |
| 24 | + return not not res1,res2 |
| 25 | + end |
| 26 | +end |
| 27 | + |
| 28 | +---------------- |
| 29 | +-- Load Lua code as a text or binary chunk. |
| 30 | +-- @param ld code string or loader |
| 31 | +-- @param[opt] source name of chunk for errors |
| 32 | +-- @param[opt] mode 'b', 't' or 'bt' |
| 33 | +-- @param[opt] env environment to load the chunk in |
| 34 | +-- @function compat.load |
| 35 | + |
| 36 | +--------------- |
| 37 | +-- Get environment of a function. |
| 38 | +-- With Lua 5.2, may return nil for a function with no global references! |
| 39 | +-- Based on code by [Sergey Rozhenko](http://lua-users.org/lists/lua-l/2010-06/msg00313.html) |
| 40 | +-- @param f a function or a call stack reference |
| 41 | +-- @function compat.setfenv |
| 42 | + |
| 43 | +--------------- |
| 44 | +-- Set environment of a function |
| 45 | +-- @param f a function or a call stack reference |
| 46 | +-- @param env a table that becomes the new environment of `f` |
| 47 | +-- @function compat.setfenv |
| 48 | + |
| 49 | +if compat.lua51 then -- define Lua 5.2 style load() |
| 50 | + if not tostring(assert):match 'builtin' then -- but LuaJIT's load _is_ compatible |
| 51 | + local lua51_load = load |
| 52 | + function compat.load(str,src,mode,env) |
| 53 | + local chunk,err |
| 54 | + if type(str) == 'string' then |
| 55 | + if str:byte(1) == 27 and not (mode or 'bt'):find 'b' then |
| 56 | + return nil,"attempt to load a binary chunk" |
| 57 | + end |
| 58 | + chunk,err = loadstring(str,src) |
| 59 | + else |
| 60 | + chunk,err = lua51_load(str,src) |
| 61 | + end |
| 62 | + if chunk and env then setfenv(chunk,env) end |
| 63 | + return chunk,err |
| 64 | + end |
| 65 | + else |
| 66 | + compat.load = load |
| 67 | + end |
| 68 | + compat.setfenv, compat.getfenv = setfenv, getfenv |
| 69 | +else |
| 70 | + compat.load = load |
| 71 | + -- setfenv/getfenv replacements for Lua 5.2 |
| 72 | + -- by Sergey Rozhenko |
| 73 | + -- http://lua-users.org/lists/lua-l/2010-06/msg00313.html |
| 74 | + -- Roberto Ierusalimschy notes that it is possible for getfenv to return nil |
| 75 | + -- in the case of a function with no globals: |
| 76 | + -- http://lua-users.org/lists/lua-l/2010-06/msg00315.html |
| 77 | + function compat.setfenv(f, t) |
| 78 | + f = (type(f) == 'function' and f or debug.getinfo(f + 1, 'f').func) |
| 79 | + local name |
| 80 | + local up = 0 |
| 81 | + repeat |
| 82 | + up = up + 1 |
| 83 | + name = debug.getupvalue(f, up) |
| 84 | + until name == '_ENV' or name == nil |
| 85 | + if name then |
| 86 | + debug.upvaluejoin(f, up, function() return name end, 1) -- use unique upvalue |
| 87 | + debug.setupvalue(f, up, t) |
| 88 | + end |
| 89 | + if f ~= 0 then return f end |
| 90 | + end |
| 91 | + |
| 92 | + function compat.getfenv(f) |
| 93 | + local f = f or 0 |
| 94 | + f = (type(f) == 'function' and f or debug.getinfo(f + 1, 'f').func) |
| 95 | + local name, val |
| 96 | + local up = 0 |
| 97 | + repeat |
| 98 | + up = up + 1 |
| 99 | + name, val = debug.getupvalue(f, up) |
| 100 | + until name == '_ENV' or name == nil |
| 101 | + return val |
| 102 | + end |
| 103 | +end |
| 104 | + |
| 105 | +--- Lua 5.2 Functions Available for 5.1 |
| 106 | +-- @section lua52 |
| 107 | + |
| 108 | +--- pack an argument list into a table. |
| 109 | +-- @param ... any arguments |
| 110 | +-- @return a table with field n set to the length |
| 111 | +-- @return the length |
| 112 | +-- @function table.pack |
| 113 | +if not table.pack then |
| 114 | + function table.pack (...) |
| 115 | + return {n=select('#',...); ...} |
| 116 | + end |
| 117 | +end |
| 118 | + |
| 119 | +------ |
| 120 | +-- return the full path where a Lua module name would be matched. |
| 121 | +-- @param mod module name, possibly dotted |
| 122 | +-- @param path a path in the same form as package.path or package.cpath |
| 123 | +-- @see path.package_path |
| 124 | +-- @function package.searchpath |
| 125 | +if not package.searchpath then |
| 126 | + local sep = package.config:sub(1,1) |
| 127 | + function package.searchpath (mod,path) |
| 128 | + mod = mod:gsub('%.',sep) |
| 129 | + for m in path:gmatch('[^;]+') do |
| 130 | + local nm = m:gsub('?',mod) |
| 131 | + local f = io.open(nm,'r') |
| 132 | + if f then f:close(); return nm end |
| 133 | + end |
| 134 | + end |
| 135 | +end |
| 136 | + |
| 137 | +return compat |
0 commit comments