-
Notifications
You must be signed in to change notification settings - Fork 1
/
assertdbg.lua
41 lines (39 loc) · 1.27 KB
/
assertdbg.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
function assert_handler(state, symbol)
function resolve_label(needed)
if (state.cpu.registers[needed] == nil) then
state:_break()
error("unable to resolve '" .. needed .. "' for assertion evaluation (halted vm)")
else
return state.cpu.registers[needed]
end
end
-- check to see if it's our kind of symbol.
if (string.sub(symbol, 0, #"assertion:") == "assertion:") then
-- handle assertion
local expr = expression_create(string.sub(symbol, #"assertion:" + 1))
if (expr:evaluate(resolve_label) ~= 1) then
-- attempt to find line information
local lineinfo = ""
for i, v in ipairs(state:lines()) do
if (v.address < state.cpu.registers.pc) then
lineinfo = " after '" .. v.file .. ":" .. v.line .. "'"
end
end
-- assertion failed, break
print("assertion \"" .. string.sub(symbol, #"assertion:" + 1) .. "\" failed" .. lineinfo .. ".")
state:_break()
state:raise("assertion_failed")
end
end
end
function setup()
-- perform setup
add_symbol_hook(assert_handler)
end
MODULE = {
Type = "Debugger",
Name = "Assertion Module",
Version = "1.0",
SDescription = "Checks .ASSERT directives at runtime",
URL = "http://dcputoolcha.in/docs/modules/list/assert.html"
};