aboutsummaryrefslogtreecommitdiff
path: root/compat53/init.lua
blob: b952003c11c60242d627a0655163bb40eaf578ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
local _G, _VERSION, type, pairs, require =
      _G, _VERSION, type, pairs, require

local M = require("compat53.module")
local lua_version = _VERSION:sub(-3)


-- apply other global effects
if lua_version == "5.1" then

   -- cache globals
   local error, pcall, rawset, select, setmetatable, tostring, unpack, xpcall =
         error, pcall, rawset, select, setmetatable, tostring, unpack, xpcall
   local coroutine, debug, io, package, string =
         coroutine, debug, io, package, string
   local coroutine_create = coroutine.create
   local coroutine_resume = coroutine.resume
   local coroutine_running = coroutine.running
   local coroutine_status = coroutine.status
   local coroutine_yield = coroutine.yield
   local io_type, io_stdout = io.type, io.stdout

   -- select the most powerful getmetatable function available
   local gmt = type(debug) == "table" and debug.getmetatable or
               getmetatable or function() return false end

   -- detect LuaJIT (including LUAJIT_ENABLE_LUA52COMPAT compilation flag)
   local is_luajit = (string.dump(function() end) or ""):sub(1, 3) == "\027LJ"
   local is_luajit52 = is_luajit and
     #setmetatable({}, { __len = function() return 1 end }) == 1


   -- make package.searchers available as an alias for package.loaders
   local p_index = { searchers = package.loaders }
   setmetatable(package, {
      __index = p_index,
      __newindex = function(p, k, v)
         if k == "searchers" then
            rawset(p, "loaders", v)
            p_index.searchers = v
         else
            rawset(p, k, v)
         end
      end
   })


   if not is_luajit then
      local function helper(_, var_1, ...)
         if var_1 == nil then
            if (...) ~= nil then
               error((...), 2)
            end
         end
         return var_1, ...
      end

      local function lines_iterator(st)
         return helper(st, st.f:read(unpack(st, 1, st.n)))
      end

      local valid_format = { ["*l"] = true, ["*n"] = true, ["*a"] = true }

      local file_meta = gmt(io_stdout)
      if type(file_meta) == "table" and type(file_meta.__index) == "table" then
         local file_write = file_meta.__index.write
         file_meta.__index.write = function(self, ...)
            local res, msg, errno = file_write(self, ...)
            if res then
               return self
            else
               return nil, msg, errno
            end
         end

         file_meta.__index.lines = function(self, ...)
            if io_type(self) == "closed file" then
               error("attempt to use a closed file", 2)
            end
            local st = { f=self, n=select('#', ...), ... }
            for i = 1, st.n do
               if type(st[i]) ~= "number" and not valid_format[st[i]] then
                  error("bad argument #"..(i+1).." to 'for iterator' (invalid format)", 2)
               end
            end
            return lines_iterator, st
         end
      end
   end -- not luajit


   -- the (x)pcall implementations start a new coroutine internally
   -- to allow yielding even in Lua 5.1. to allow for accurate
   -- stack traces we keep track of the nested coroutine activations
   -- in the weak tables below:
   local weak_meta = { __mode = "kv" }
   -- maps the internal pcall coroutines to the user coroutine that
   -- *should* be running if pcall didn't use coroutines internally
   local pcall_mainOf = setmetatable({}, weak_meta)
   -- table that maps each running coroutine started by pcall to
   -- the coroutine that resumed it (user coroutine *or* pcall
   -- coroutine!)
   local pcall_previous = setmetatable({}, weak_meta)
   -- reverse of `pcall_mainOf`. maps a user coroutine to the
   -- currently active pcall coroutine started within it
   local pcall_callOf = setmetatable({}, weak_meta)
   -- similar to `pcall_mainOf` but is used only while executing
   -- the error handler of xpcall (thus no nesting is necessary!)
   local xpcall_running = setmetatable({}, weak_meta)

   -- handle debug functions
   if type(debug) == "table" then
      local debug_getinfo = debug.getinfo
      local debug_traceback = debug.traceback

      if not is_luajit then
         local function calculate_trace_level(co, level)
            if level ~= nil then
               for out = 1, 1/0 do
                  local info = (co==nil) and debug_getinfo(out, "") or debug_getinfo(co, out, "")
                  if info == nil then
                     local max = out-1
                     if level <= max then
                        return level
                     end
                     return nil, level-max
                  end
               end
            end
            return 1
         end

         local stack_pattern = "\nstack traceback:"
         local stack_replace = ""
         function debug.traceback(co, msg, level)
            local lvl
            local nilmsg
            if type(co) ~= "thread" then
               co, msg, level = coroutine_running(), co, msg
            end
            if msg == nil then
               msg = ""
               nilmsg = true
            elseif type(msg) ~= "string" then
               return msg
            end
            if co == nil then
               msg = debug_traceback(msg, level or 1)
            else
               local xpco = xpcall_running[co]
               if xpco ~= nil then
                  lvl, level = calculate_trace_level(xpco, level)
                  if lvl then
                     msg = debug_traceback(xpco, msg, lvl)
                  else
                     msg = msg..stack_pattern
                  end
                  lvl, level = calculate_trace_level(co, level)
                  if lvl then
                     local trace = debug_traceback(co, "", lvl)
                     msg = msg..trace:gsub(stack_pattern, stack_replace)
                  end
               else
                  co = pcall_callOf[co] or co
                  lvl, level = calculate_trace_level(co, level)
                  if lvl then
                     msg = debug_traceback(co, msg, lvl)
                  else
                     msg = msg..stack_pattern
                  end
               end
               co = pcall_previous[co]
               while co ~= nil do
                  lvl, level = calculate_trace_level(co, level)
                  if lvl then
                     local trace = debug_traceback(co, "", lvl)
                     msg = msg..trace:gsub(stack_pattern, stack_replace)
                  end
                  co = pcall_previous[co]
               end
            end
            if nilmsg then
               msg = msg:gsub("^\n", "")
            end
            msg = msg:gsub("\n\t%(tail call%): %?", "\000")
            msg = msg:gsub("\n\t%.%.%.\n", "\001\n")
            msg = msg:gsub("\n\t%.%.%.$", "\001")
            msg = msg:gsub("(%z+)\001(%z+)", function(some, other)
               return "\n\t(..."..#some+#other.."+ tail call(s)...)"
            end)
            msg = msg:gsub("\001(%z+)", function(zeros)
               return "\n\t(..."..#zeros.."+ tail call(s)...)"
            end)
            msg = msg:gsub("(%z+)\001", function(zeros)
               return "\n\t(..."..#zeros.."+ tail call(s)...)"
            end)
            msg = msg:gsub("%z+", function(zeros)
               return "\n\t(..."..#zeros.." tail call(s)...)"
            end)
            msg = msg:gsub("\001", function()
               return "\n\t..."
            end)
            return msg
         end
      end -- is not luajit
   end -- debug table available


   local main_coroutine = coroutine_create(function() end)

   if not is_luajit52 then
      function M.coroutine.running()
         local co = coroutine_running()
         if co then
            return pcall_mainOf[co] or co, false
         else
            return main_coroutine, true
         end
      end
   end

   if not is_luajit then
      function M.coroutine.resume(co, ...)
         if co == main_coroutine then
            return false, "cannot resume non-suspended coroutine"
         else
            return coroutine_resume(co, ...)
         end
      end

      function M.coroutine.status(co)
         local notmain = coroutine_running()
         if co == main_coroutine then
            return notmain and "normal" or "running"
         else
            return coroutine_status(co)
         end
      end

      local function pcall_results(current, call, success, ...)
         if coroutine_status(call) == "suspended" then
            return pcall_results(current, call, coroutine_resume(call, coroutine_yield(...)))
         end
         if pcall_previous then
            pcall_previous[call] = nil
            local main = pcall_mainOf[call]
            if main == current then current = nil end
            pcall_callOf[main] = current
         end
         pcall_mainOf[call] = nil
         return success, ...
      end

      local function pcall_exec(current, call, ...)
         local main = pcall_mainOf[current] or current
         pcall_mainOf[call] = main
         if pcall_previous then
            pcall_previous[call] = current
            pcall_callOf[main] = call
         end
         return pcall_results(current, call, coroutine_resume(call, ...))
      end

      local coroutine_create52 = M.coroutine.create

      local function pcall_coroutine(func)
         if type(func) ~= "function" then
            local callable = func
            func = function (...) return callable(...) end
         end
         return coroutine_create52(func)
      end

      function M.pcall(func, ...)
         local current = coroutine_running()
         if not current then return pcall(func, ...) end
         return pcall_exec(current, pcall_coroutine(func), ...)
      end

      local function xpcall_catch(current, call, msgh, success, ...)
         if not success then
            xpcall_running[current] = call
            local ok, result = pcall(msgh, ...)
            xpcall_running[current] = nil
            if not ok then
               return false, "error in error handling ("..tostring(result)..")"
            end
            return false, result
         end
         return true, ...
      end

      function M.xpcall(f, msgh, ...)
         local current = coroutine_running()
         if not current then
            local args, n = { ... }, select('#', ...)
            return xpcall(function() return f(unpack(args, 1, n)) end, msgh)
         end
         local call = pcall_coroutine(f)
         return xpcall_catch(current, call, msgh, pcall_exec(current, call, ...))
      end
   end -- not luajit

end -- lua == 5.1


-- handle exporting to global scope
local function extend_table(from, to)
   if from ~= to then
      for k,v in pairs(from) do
         if type(v) == "table" and
            type(to[k]) == "table" and
            v ~= to[k] then
            extend_table(v, to[k])
         else
            to[k] = v
         end
      end
   end
end

extend_table(M, _G)

-- vi: set expandtab softtabstop=3 shiftwidth=3 :