This commit is contained in:
2026-05-09 13:50:16 -05:00
parent 610491c1d7
commit 6762c49ed3
2 changed files with 50 additions and 4 deletions

View File

@@ -341,7 +341,7 @@ do TEST "VM: GC strings"
end end
do TEST "VM: arrays" do TEST "VM: arrays"
local vm = VM.new():set_debug(true):load(assemble [[ local vm = VM.new():load(assemble [[
.func 0 .func 0
newa newa
pushi 10 pushi 10
@@ -359,4 +359,42 @@ do TEST "VM: arrays"
assert_eq(vm.heap:size(), 1) assert_eq(vm.heap:size(), 1)
end end
do TEST "VM: arrays GC"
local vm = VM.new():load(assemble [[
.func 0
pushn
newa
pushi 10
seti 0
pop
gc
ret
]]):call(0)
assert_eq(vm.heap:size(), 0)
end
do TEST "VM: GC items (1st level)"
local vm = VM.new():set_debug(true):load(assemble [[
.const
0: "Hello "
1: "world"
.func 0
pushn
newa
pushc 0
pushc 1
sum
seti 0
gc
pop
ret
]]):call(0)
pprint(vm.heap)
print(vm:debug_heap())
assert_eq(vm.heap:size(), 0)
end
-- TODO - arrays gc items
print('End.') print('End.')

View File

@@ -365,7 +365,7 @@ function VM:format_value(v)
if v.type == 'integer' or v.type == 'real' then if v.type == 'integer' or v.type == 'real' then
return tostring(v.value) return tostring(v.value)
elseif v.type == 'string' then elseif v.type == 'string' then
return self_:extract_string(v) return '"' .. self:_extract_string(v) .. '"'
elseif v.type == 'array' then elseif v.type == 'array' then
local array = self:_extract_array(v) local array = self:_extract_array(v)
local tbl = {} local tbl = {}
@@ -376,7 +376,7 @@ function VM:format_value(v)
elseif v.type == 'nil' then elseif v.type == 'nil' then
return 'nil' return 'nil'
else else
print('warning: cannot convert from type ' .. v.type) print('warning: cannot convert from type ' .. tostring(v.type))
return pprint.pformat(v) return pprint.pformat(v)
end end
end end
@@ -396,7 +396,15 @@ end
function VM:debug_heap() function VM:debug_heap()
local ss = { "Heap:\n" } local ss = { "Heap:\n" }
for k,v in pairs(self.heap.items) do for k,v in pairs(self.heap.items) do
table.insert(ss, string.format(" [%X] = %s", k, pprint.pformat(v))) if type(v) == 'string' then
table.insert(ss, string.format(' [%X] = "%s"', k, v))
elseif type(v) == 'table' then
table.insert(ss, string.format(' [%X] = [', k))
local t = {}; for _,vv in ipairs(v) do t[#t+1] = self:format_value(vv) end
table.insert(ss, table.concat(t, ", ") .. ']')
else
error('Unsupported type in heap')
end
table.insert(ss, "\n") table.insert(ss, "\n")
end end
return table.concat(ss) return table.concat(ss)