Skip to content

Commit 8356b49

Browse files
committed
fix flood of errors in repl when frame is nil
ref #76
1 parent 7c4b626 commit 8356b49

File tree

2 files changed

+132
-107
lines changed

2 files changed

+132
-107
lines changed

lua/osv/init.lua

Lines changed: 104 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,14 @@ function M.prepare_attach(blocking)
323323
function handlers.evaluate(request)
324324
local args = request.arguments
325325
if args.context == "repl" then
326-
local frame = frames[args.frameId]
326+
local frame = args.frameId and frames[args.frameId]
327327
local a = 1
328328
local prev
329329
local cur = {}
330330
local first = cur
331331

332332
while true do
333+
if not frame then break end
333334
local succ, ln, lv = pcall(debug.getlocal, frame+1, a)
334335
if not succ then
335336
break
@@ -355,31 +356,35 @@ function M.prepare_attach(blocking)
355356

356357
a = 1
357358

358-
local succ, info = pcall(debug.getinfo, frame+1)
359-
if succ and info and info.func then
360-
local func = info.func
361-
local a = 1
362-
while true do
363-
local succ, ln, lv = pcall(debug.getupvalue, func, a)
364-
if not succ then
365-
break
366-
end
359+
if frame then
360+
local succ, info = pcall(debug.getinfo, frame+1)
361+
if succ and info and info.func then
362+
local func = info.func
363+
local a = 1
364+
while true do
365+
local succ, ln, lv = pcall(debug.getupvalue, func, a)
366+
if not succ then
367+
break
368+
end
367369

368-
if not ln then
369-
break
370-
else
371-
-- Avoid shadowing of the globals if a local variable is nil
372-
cur[ln] = lv or vim.NIL
373-
a = a + 1
370+
if not ln then
371+
break
372+
else
373+
-- Avoid shadowing of the globals if a local variable is nil
374+
cur[ln] = lv or vim.NIL
375+
a = a + 1
376+
end
374377
end
375378
end
376379
end
377380

378-
local succ, info = pcall(debug.getinfo, frame+1)
379-
if succ and info and info.func then
380-
setmetatable(cur, {
381-
__index = getfenv(info.func)
382-
})
381+
if frame then
382+
local succ, info = pcall(debug.getinfo, frame+1)
383+
if succ and info and info.func then
384+
setmetatable(cur, {
385+
__index = getfenv(info.func)
386+
})
387+
end
383388
end
384389

385390
local expr = args.expression
@@ -416,13 +421,14 @@ function M.prepare_attach(blocking)
416421
}))
417422

418423
elseif args.context == "hover" then
419-
local frame = frames[args.frameId]
424+
local frame = args.frameId and frames[args.frameId]
420425
local a = 1
421426
local prev
422427
local cur = {}
423428
local first = cur
424429

425430
while true do
431+
if not frame then break end
426432
local succ, ln, lv = pcall(debug.getlocal, frame+1, a)
427433
if not succ then
428434
break
@@ -448,31 +454,35 @@ function M.prepare_attach(blocking)
448454

449455
a = 1
450456

451-
local succ, info = pcall(debug.getinfo, frame+1)
452-
if succ and info and info.func then
453-
local func = info.func
454-
local a = 1
455-
while true do
456-
local succ, ln, lv = pcall(debug.getupvalue, func, a)
457-
if not succ then
458-
break
459-
end
457+
if frame then
458+
local succ, info = pcall(debug.getinfo, frame+1)
459+
if succ and info and info.func then
460+
local func = info.func
461+
local a = 1
462+
while true do
463+
local succ, ln, lv = pcall(debug.getupvalue, func, a)
464+
if not succ then
465+
break
466+
end
460467

461-
if not ln then
462-
break
463-
else
464-
-- Avoid shadowing of the globals if a local variable is nil
465-
cur[ln] = lv or vim.NIL
466-
a = a + 1
468+
if not ln then
469+
break
470+
else
471+
-- Avoid shadowing of the globals if a local variable is nil
472+
cur[ln] = lv or vim.NIL
473+
a = a + 1
474+
end
467475
end
468476
end
469477
end
470478

471-
local succ, info = pcall(debug.getinfo, frame+1)
472-
if succ and info and info.func then
473-
setmetatable(cur, {
474-
__index = getfenv(info.func)
475-
})
479+
if frame then
480+
local succ, info = pcall(debug.getinfo, frame+1)
481+
if succ and info and info.func then
482+
setmetatable(cur, {
483+
__index = getfenv(info.func)
484+
})
485+
end
476486
end
477487

478488
local expr = args.expression
@@ -1244,6 +1254,7 @@ function M.prepare_attach(blocking)
12441254
local first = cur
12451255

12461256
while true do
1257+
if not frame then break end
12471258
local succ, ln, lv = pcall(debug.getlocal, frame+1, a)
12481259
if not succ then
12491260
break
@@ -1269,31 +1280,35 @@ function M.prepare_attach(blocking)
12691280

12701281
a = 1
12711282

1272-
local succ, info = pcall(debug.getinfo, frame+1)
1273-
if succ and info and info.func then
1274-
local func = info.func
1275-
local a = 1
1276-
while true do
1277-
local succ, ln, lv = pcall(debug.getupvalue, func, a)
1278-
if not succ then
1279-
break
1280-
end
1281-
1282-
if not ln then
1283-
break
1284-
else
1285-
-- Avoid shadowing of the globals if a local variable is nil
1286-
cur[ln] = lv or vim.NIL
1287-
a = a + 1
1283+
if frame then
1284+
local succ, info = pcall(debug.getinfo, frame+1)
1285+
if succ and info and info.func then
1286+
local func = info.func
1287+
local a = 1
1288+
while true do
1289+
local succ, ln, lv = pcall(debug.getupvalue, func, a)
1290+
if not succ then
1291+
break
1292+
end
1293+
1294+
if not ln then
1295+
break
1296+
else
1297+
-- Avoid shadowing of the globals if a local variable is nil
1298+
cur[ln] = lv or vim.NIL
1299+
a = a + 1
1300+
end
12881301
end
12891302
end
12901303
end
12911304

1292-
local succ, info = pcall(debug.getinfo, frame+1)
1293-
if succ and info and info.func then
1294-
setmetatable(cur, {
1295-
__index = getfenv(info.func)
1296-
})
1305+
if frame then
1306+
local succ, info = pcall(debug.getinfo, frame+1)
1307+
if succ and info and info.func then
1308+
setmetatable(cur, {
1309+
__index = getfenv(info.func)
1310+
})
1311+
end
12971312
end
12981313

12991314
local succ, f = pcall(loadstring, "return " .. expr)
@@ -1323,6 +1338,7 @@ function M.prepare_attach(blocking)
13231338
local first = cur
13241339

13251340
while true do
1341+
if not frame then break end
13261342
local succ, ln, lv = pcall(debug.getlocal, frame+1, a)
13271343
if not succ then
13281344
break
@@ -1348,31 +1364,35 @@ function M.prepare_attach(blocking)
13481364

13491365
a = 1
13501366

1351-
local succ, info = pcall(debug.getinfo, frame+1)
1352-
if succ and info and info.func then
1353-
local func = info.func
1354-
local a = 1
1355-
while true do
1356-
local succ, ln, lv = pcall(debug.getupvalue, func, a)
1357-
if not succ then
1358-
break
1359-
end
1360-
1361-
if not ln then
1362-
break
1363-
else
1364-
-- Avoid shadowing of the globals if a local variable is nil
1365-
cur[ln] = lv or vim.NIL
1366-
a = a + 1
1367+
if frame then
1368+
local succ, info = pcall(debug.getinfo, frame+1)
1369+
if succ and info and info.func then
1370+
local func = info.func
1371+
local a = 1
1372+
while true do
1373+
local succ, ln, lv = pcall(debug.getupvalue, func, a)
1374+
if not succ then
1375+
break
1376+
end
1377+
1378+
if not ln then
1379+
break
1380+
else
1381+
-- Avoid shadowing of the globals if a local variable is nil
1382+
cur[ln] = lv or vim.NIL
1383+
a = a + 1
1384+
end
13671385
end
13681386
end
13691387
end
13701388

1371-
local succ, info = pcall(debug.getinfo, frame+1)
1372-
if succ and info and info.func then
1373-
setmetatable(cur, {
1374-
__index = getfenv(info.func)
1375-
})
1389+
if frame then
1390+
local succ, info = pcall(debug.getinfo, frame+1)
1391+
if succ and info and info.func then
1392+
setmetatable(cur, {
1393+
__index = getfenv(info.func)
1394+
})
1395+
end
13761396
end
13771397

13781398
local succ, f = pcall(loadstring, "return " .. expr)

src/handlers/evaluate.lua.t2

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
function handlers.evaluate(request)
44
local args = request.arguments
55
if args.context == "repl" then
6-
local frame = frames[args.frameId]
6+
local frame = args.frameId and frames[args.frameId]
77
; retrieve locals in frame
88
; retrieve upvalues in frame
99
; retrieve globals
@@ -13,7 +13,7 @@ function handlers.evaluate(request)
1313
; make evaluate reponse
1414
; send repl evaluate response
1515
elseif args.context == "hover" then
16-
local frame = frames[args.frameId]
16+
local frame = args.frameId and frames[args.frameId]
1717
; retrieve locals in frame
1818
; retrieve upvalues in frame
1919
; retrieve globals
@@ -34,6 +34,7 @@ local cur = {}
3434
local first = cur
3535

3636
while true do
37+
if not frame then break end
3738
local succ, ln, lv = pcall(debug.getlocal, frame+1, a)
3839
if not succ then
3940
break
@@ -88,32 +89,36 @@ setmetatable(prev, {
8889

8990
a = 1
9091

91-
local succ, info = pcall(debug.getinfo, frame+1)
92-
if succ and info and info.func then
93-
local func = info.func
94-
local a = 1
95-
while true do
96-
local succ, ln, lv = pcall(debug.getupvalue, func, a)
97-
if not succ then
98-
break
99-
end
100-
101-
if not ln then
102-
break
103-
else
104-
-- Avoid shadowing of the globals if a local variable is nil
105-
cur[ln] = lv or vim.NIL
106-
a = a + 1
92+
if frame then
93+
local succ, info = pcall(debug.getinfo, frame+1)
94+
if succ and info and info.func then
95+
local func = info.func
96+
local a = 1
97+
while true do
98+
local succ, ln, lv = pcall(debug.getupvalue, func, a)
99+
if not succ then
100+
break
101+
end
102+
103+
if not ln then
104+
break
105+
else
106+
-- Avoid shadowing of the globals if a local variable is nil
107+
cur[ln] = lv or vim.NIL
108+
a = a + 1
109+
end
107110
end
108111
end
109112
end
110113

111114
;; retrieve globals
112-
local succ, info = pcall(debug.getinfo, frame+1)
113-
if succ and info and info.func then
114-
setmetatable(cur, {
115-
__index = getfenv(info.func)
116-
})
115+
if frame then
116+
local succ, info = pcall(debug.getinfo, frame+1)
117+
if succ and info and info.func then
118+
setmetatable(cur, {
119+
__index = getfenv(info.func)
120+
})
121+
end
117122
end
118123

119124
;; make evaluate reponse

0 commit comments

Comments
 (0)