-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedrxpcall.lua
286 lines (258 loc) · 9.79 KB
/
edrxpcall.lua
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
-- This file:
-- https://github.com/edrx/edrxrepl/
-- http://angg.twu.net/edrxrepl/edrxpcall.lua.html
-- http://angg.twu.net/edrxrepl/edrxpcall.lua
-- (find-angg "edrxrepl/edrxpcall.lua")
-- Author: Eduardo Ochs <[email protected]>
--
-- In the HTML version the sexp hyperlinks work.
-- See: (find-eev-quick-intro "3. Elisp hyperlinks")
-- (find-eepitch-intro "3. Test blocks")
--
-- Author: Eduardo Ochs <[email protected]>
-- Version: 20210824
-- License: GPL3 at this moment.
-- If you need another license, get in touch!
--
-- Some eev-isms:
-- (defun o () (interactive) (find-angg "edrxrepl/README.org"))
-- (defun r () (interactive) (find-angg "edrxrepl/edrxrepl.lua"))
-- (defun x () (interactive) (find-angg "edrxrepl/edrxpcall.lua"))
-- Introduction
-- ============
-- When we do things like this in Lua,
--
-- status, error = pcall(f)
-- status, error = xpcall(f, errorhandler)
--
-- the function f is called in "protected mode", and any errors are
-- handled by pcall and xpcall. The argument "errorhandler" to xpcall
-- is a function that is run after the error occurs and before the
-- stack is unwinded, and it can be used to produce a traceback or
-- save it into a variable. See:
--
-- (find-lua51manual "#pdf-pcall" "pcall (f, arg1, ...)")
-- (find-lua51manual "#pdf-xpcall" "xpcall (f, err)")
-- (find-lua52manual "#pdf-xpcall" "xpcall (f, msgh [, arg1, ...])")
-- (find-lua51manual "#pdf-debug.traceback")
-- (find-lua51manual "#pdf-error")
-- (find-pil3page (+ 19 77) "8.4 Errors")
-- (find-pil3page (+ 19 79) "8.5 Error Handling and Exceptions")
-- (find-pil3page (+ 19 79) "8.6 Error Messages and Tracebacks")
--
-- I always found xpcall too hard to use, so I wrote the class
-- EdrxPcall below, that feels more hacker-friendly (to me)... it uses
-- eoo.lua, it saves all the intermediate values, and all its methods
-- can be overridden.
--
-- The six high-level ways of calling function with EdrxPcall are
-- these ones:
--
-- EdrxPcall.new(): call(F2, 3, 4): out()
-- EdrxPcall.new(): call(F2, 3, 4): out("=")
-- EdrxPcall.new(): call(F2, 3, 4): out("=", "\n")
-- EdrxPcall.new():prcall(F2, 3, 4):prout()
-- EdrxPcall.new():prcall(F2, 3, 4):prout("=")
-- EdrxPcall.new():prcall(F2, 3, 4):prout("=", "\n")
--
-- The option "=" behaves like an initial "=" in the REPLs of Lua5.1
-- and Lua5.2, in the sense that it makes the results of F2(3, 4) be
-- printed in case of success. The alternatives with "pr" redirect the
-- outputs of the "print"s called by F2(3, 4) to a string before
-- printing them, and used by emacs-lua. To understand how all this
-- works try the tests at the end of this file.
--
-- The alternatives with "\n" are harder to explain. Remember that in
-- the REPLs of Lua5.1 and Lua5.1 typing something like "= <exprs>" at
-- the REPL only prints something when "<exprs>" return something, and
-- remember that "return", "return nil", are "return nil, nil" are all
-- different, as they return lists of values of length 0, 1, and 2...
-- The option "\n" at the end adds a "\n" at the end of the output
-- only when the "=" receives a non-empty list of values.
-- «.from-init» (to "from-init")
-- «.Class» (to "Class")
-- «.EdrxPcall» (to "EdrxPcall")
-- «.EdrxPcall-tests» (to "EdrxPcall-tests")
-- «from-init» (to ".from-init")
-- Some functions from my initfile. See:
-- (find-angg "LUA/lua50init.lua" "pack-and-unpack")
-- (find-angg "LUA/lua50init.lua" "splitlines-5.3")
-- (find-angg "LUA/lua50init.lua" "split")
-- (find-es "lua5" "loadstring")
loadstring = loadstring or load
pack = table.pack or function (...) return {n=select("#", ...), ...} end
unpack = unpack or table.unpack
myunpack = function (arg) return unpack(arg, 1, arg.n) end
split = function (str, pat)
local arr = {}
string.gsub(str, pat or "([^%s]+)", function (word)
table.insert(arr, word)
end)
return arr
end
splitlines = function (bigstr)
local arr = split(bigstr, "([^\n]*)\n?")
if _VERSION:sub(5) < "5.3" then
table.remove(arr)
end
return arr
end
map = function (f, arr, n)
local brr = {}
for i=1,(n or #arr) do table.insert(brr, (f(arr[i]))) end
return brr
end
mapconcat = function (f, arr, sep, n)
return table.concat(map(f, arr, n), sep)
end
mapconcatpacked = function (f, arr, sep)
return mapconcat(f or tostring, arr, sep or " ", arr.n)
end
print_to_string = function (...)
return mapconcatpacked(tostring, pack(...), "\t")
end
-- «Class» (to ".Class")
-- Commented version:
-- (find-angg "dednat6/dednat6/eoo.lua" "Class")
Class = {
type = "Class",
__call = function (class, o) return setmetatable(o, class) end,
}
setmetatable(Class, Class)
-- «EdrxPcall» (to ".EdrxPcall")
-- My class for calling xpcall in configurable ways.
-- See the introduction at the top of this file.
--
EdrxPcall = Class {
type = "EdrxPcall",
new = function (T) return EdrxPcall(T or {}) end,
__tostring = tos_VTable,
__index = {
--
-- The method :call(f, ...) is the standard way to call
-- a function with arguments in protected mode.
call = function (xpc, f, ...)
return xpc:callprep(f, ...):callrun()
end,
callprep = function (xpc, f, ...)
xpc.f = f
xpc.f_args = pack(...)
xpc.g = function ()
xpc.f_results = pack(xpc.f(myunpack(xpc.f_args)))
end
xpc.eh = xpc:eh0()
return xpc
end,
callrun = function (xpc)
xpc.xp_results = pack(xpcall(xpc.g, xpc.eh))
return xpc
end,
--
-- Error handler (eh) and traceback (tb).
-- The method xpc:eh0() returns the error handler xpc.eh.
eh0 = function (xpc)
return function (errmsg)
xpc.err_msg = errmsg
xpc:tb()
end
end,
tb = function (xpc)
xpc.tb_string = debug.traceback("", xpc.tb_lvl)
end,
tb_lvl = 3,
tb_e = 6,
--
-- The method :out() returns the "output" of the call.
-- For example:
-- EdrxPcall.new():call(expr, "2, 3"):out("=") --> "2 3"
-- EdrxPcall.new():call(expr, "2, 3"):out("=", "\n") --> "2 3\n"
-- EdrxPcall.new():call(expr, "2, 3"):out() --> ""
-- EdrxPcall.new():call(expr, "2 + nil"):out() --> errmsg/traceback
success = function (xpc) return xpc.xp_results[1] end,
resultsempty = function (xpc) return (xpc.xp_results.n == 0) and "" end,
results0000 = function (xpc) return myunpack(xpc.f_results) end,
results000 = function (xpc) return print_to_string(xpc:results0000()) end,
results00 = function (xpc, nl) return xpc:results000()..(nl or "") end,
results0 = function (xpc, nl) return xpc:resultsempty() or xpc:results00(nl) end,
results = function (xpc, printresults, nl)
if printresults then return xpc:results0(nl) end
return ""
end,
tbshorter = function (xpc, tbe)
local lines = splitlines(xpc.tb_string)
return table.concat(lines, "\n", 1, #lines - (tbe or xpc.tb_e))
end,
outerror = function (xpc, nl)
return xpc.err_msg .. xpc:tbshorter() .. (nl or "")
end,
out = function (xpc, printresults, nl)
if xpc:success()
then return xpc:results(printresults, nl)
else return xpc:outerror(nl)
end
end,
--
-- The method :prcall(f, ...) is a variant of :call that
-- "captures the outputs of the prints in :call(f, ...)".
-- The method :prout() returns the "output" of the call,
-- _including the outputs of all "print"s in the call_.
-- The method :pr0() creates the setup for capturing "print"s.
-- The method :pr1() puts the outputs in xpc.pr_out.
pr0 = function (xpc)
xpc.pr_list = {}
xpc.pr_oldprint = print
print = function (...)
table.insert(xpc.pr_list, print_to_string(...).."\n")
end
return xpc
end,
pr1 = function (xpc)
print = xpc.pr_oldprint
xpc.pr_out = table.concat(xpc.pr_list, "")
return xpc
end,
prcall = function (xpc, ...)
return xpc:pr0():call(...):pr1()
end,
prout = function (xpc, printresults, nl)
return xpc.pr_out .. xpc:out(printresults, nl)
end,
},
}
-- «EdrxPcall-tests» (to ".EdrxPcall-tests")
--[[
(eepitch-lua51)
(eepitch-kill)
(eepitch-lua51)
dofile "edrxpcall.lua"
= EdrxPcall.new():call(expr, "2, 3"):out("=", "\n") --> "2 3\n"
= EdrxPcall.new():call(expr, "2, 3"):out("=") --> "2 3"
= EdrxPcall.new():call(expr, "2, 3"):out() --> ""
= EdrxPcall.new():call(expr, "2 + nil"):out() --> errmsg/traceback
F2 = function (a, b) print("F2", a, b); return F1(2, 3), 4 end
F1 = function (a, b) print("F1", a, b); return F0(1, 2), 3 end
F01 = function (a, b) print("F01", a, b); return 0, 1 end
F00 = function (a, b) print("F00", a, b); error("F00 ERROR!!!") end
F0 = F01
F2(3, 4) --> prints F2/F1/F01
= F2(3, 4) --> prints F2/F1/F01, 04
F0 = F00
F2(3, 4) --> prints F2/F1/F00, error, traceback
= F2(3, 4) --> prints F2/F1/F00, error, traceback
F0 = F01
xpc = EdrxPcall.new(): call(F2, 3, 4) --> prints F2/F1/F01
= xpc:out() --> prints ""
= xpc:out("=") --> prints "0 4"
xpc = EdrxPcall.new():prcall(F2, 3, 4) --> prints nothing
= xpc:prout() --> prints F2/F1/F01
= xpc:prout("=") --> prints F2/F1/F01, 04
= xpc
F0 = F00
xpc = EdrxPcall.new(): call(F2, 3, 4) --> prints F2/F1/F01
= xpc:out() --> prints error, traceback
= xpc:out("=") --> prints error, traceback
xpc = EdrxPcall.new():prcall(F2, 3, 4) --> prints nothing
= xpc:prout() --> prints F2/F1/F01, error, traceback
= xpc:prout("=") --> prints F2/F1/F01, error, traceback
= xpc
--]]