diff --git a/lua-temp/tests.lua b/lua-temp/tests.lua index ca81f94..e04aafc 100755 --- a/lua-temp/tests.lua +++ b/lua-temp/tests.lua @@ -341,7 +341,7 @@ do TEST "VM: GC strings" end do TEST "VM: arrays" - local vm = VM.new():set_debug(true):load(assemble [[ + local vm = VM.new():load(assemble [[ .func 0 newa pushi 10 @@ -359,4 +359,42 @@ do TEST "VM: arrays" assert_eq(vm.heap:size(), 1) 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.') diff --git a/lua-temp/tyche-vm.lua b/lua-temp/tyche-vm.lua index 1057613..92458a8 100644 --- a/lua-temp/tyche-vm.lua +++ b/lua-temp/tyche-vm.lua @@ -365,7 +365,7 @@ function VM:format_value(v) if v.type == 'integer' or v.type == 'real' then return tostring(v.value) elseif v.type == 'string' then - return self_:extract_string(v) + return '"' .. self:_extract_string(v) .. '"' elseif v.type == 'array' then local array = self:_extract_array(v) local tbl = {} @@ -376,7 +376,7 @@ function VM:format_value(v) elseif v.type == 'nil' then return 'nil' else - print('warning: cannot convert from type ' .. v.type) + print('warning: cannot convert from type ' .. tostring(v.type)) return pprint.pformat(v) end end @@ -396,7 +396,15 @@ end function VM:debug_heap() local ss = { "Heap:\n" } 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") end return table.concat(ss)