diff options
Diffstat (limited to 'compat53/init.lua')
| -rw-r--r-- | compat53/init.lua | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/compat53/init.lua b/compat53/init.lua new file mode 100644 index 0000000..cdd5f34 --- /dev/null +++ b/compat53/init.lua | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | local _G, _VERSION, type, pairs, require = | ||
| 2 | _G, _VERSION, type, pairs, require | ||
| 3 | |||
| 4 | local M = require("compat53.base") | ||
| 5 | local lua_version = _VERSION:sub(-3) | ||
| 6 | |||
| 7 | |||
| 8 | -- apply other global effects | ||
| 9 | if lua_version == "5.1" then | ||
| 10 | |||
| 11 | -- cache globals | ||
| 12 | local error, rawset, select, setmetatable, type, unpack = | ||
| 13 | error, rawset, select, setmetatable, type, unpack | ||
| 14 | local debug, io, package, string = debug, io, package, string | ||
| 15 | local io_type, io_stdout = io.type, io.stdout | ||
| 16 | |||
| 17 | -- select the most powerful getmetatable function available | ||
| 18 | local gmt = type(debug) == "table" and debug.getmetatable or | ||
| 19 | getmetatable or function() return false end | ||
| 20 | |||
| 21 | -- detect LuaJIT (including LUAJIT_ENABLE_LUA52COMPAT compilation flag) | ||
| 22 | local is_luajit = (string.dump(function() end) or ""):sub(1, 3) == "\027LJ" | ||
| 23 | |||
| 24 | |||
| 25 | -- make package.searchers available as an alias for package.loaders | ||
| 26 | local p_index = { searchers = package.loaders } | ||
| 27 | setmetatable(package, { | ||
| 28 | __index = p_index, | ||
| 29 | __newindex = function(p, k, v) | ||
| 30 | if k == "searchers" then | ||
| 31 | rawset(p, "loaders", v) | ||
| 32 | p_index.searchers = v | ||
| 33 | else | ||
| 34 | rawset(p, k, v) | ||
| 35 | end | ||
| 36 | end | ||
| 37 | }) | ||
| 38 | |||
| 39 | |||
| 40 | if not is_luajit then | ||
| 41 | local function helper(st, var_1, ...) | ||
| 42 | if var_1 == nil then | ||
| 43 | if (...) ~= nil then | ||
| 44 | error((...), 2) | ||
| 45 | end | ||
| 46 | end | ||
| 47 | return var_1, ... | ||
| 48 | end | ||
| 49 | |||
| 50 | local function lines_iterator(st) | ||
| 51 | return helper(st, st.f:read(unpack(st, 1, st.n))) | ||
| 52 | end | ||
| 53 | |||
| 54 | local valid_format = { ["*l"] = true, ["*n"] = true, ["*a"] = true } | ||
| 55 | |||
| 56 | local file_meta = gmt(io_stdout) | ||
| 57 | if type(file_meta) == "table" and type(file_meta.__index) == "table" then | ||
| 58 | local file_write = file_meta.__index.write | ||
| 59 | file_meta.__index.write = function(self, ...) | ||
| 60 | local res, msg, errno = file_write(self, ...) | ||
| 61 | if res then | ||
| 62 | return self | ||
| 63 | else | ||
| 64 | return nil, msg, errno | ||
| 65 | end | ||
| 66 | end | ||
| 67 | |||
| 68 | file_meta.__index.lines = function(self, ...) | ||
| 69 | if io_type(self) == "closed file" then | ||
| 70 | error("attempt to use a closed file", 2) | ||
| 71 | end | ||
| 72 | local st = { f=self, n=select('#', ...), ... } | ||
| 73 | for i = 1, st.n do | ||
| 74 | if type(st[i]) ~= "number" and not valid_format[st[i]] then | ||
| 75 | error("bad argument #"..(i+1).." to 'for iterator' (invalid format)", 2) | ||
| 76 | end | ||
| 77 | end | ||
| 78 | return lines_iterator, st | ||
| 79 | end | ||
| 80 | end | ||
| 81 | end -- not luajit | ||
| 82 | |||
| 83 | end -- lua == 5.1 | ||
| 84 | |||
| 85 | |||
| 86 | -- handle exporting to global scope | ||
| 87 | local function extend_table(from, to) | ||
| 88 | for k,v in pairs(from) do | ||
| 89 | if type(v) == "table" and | ||
| 90 | type(to[k]) == "table" and | ||
| 91 | v ~= to[k] then | ||
| 92 | extend_table(v, to[k]) | ||
| 93 | else | ||
| 94 | to[k] = v | ||
| 95 | end | ||
| 96 | end | ||
| 97 | end | ||
| 98 | |||
| 99 | extend_table(M, _G) | ||
| 100 | |||
| 101 | return _G | ||
| 102 | |||
| 103 | -- vi: set expandtab softtabstop=3 shiftwidth=3 : | ||
