Shopify/go-lua

output difference between clua and go-lua

rphigh2017 opened this issue · 1 comments

local set = {}

function set:new(list)
local this = {}
setmetatable(this, self)
self.__index = self
for _,v in ipairs(list) do this[v] = true end
return this
end

set.__add = function (l, r)
local this = set:new{}
for k in pairs(l) do this[k] = true end
for k in pairs(r) do this[k] = true end
return this
end

set.__mul = function (l, r)
local this = set:new{}
for k in pairs(l) do this[k] = r[k] end
return this
end

set.__tostring = function (f)
local this = {}
for k in pairs(f) do this[#this+1] = k end
return '{' .. table.concat(this, ', ') .. '}'
end

A,B,C = set:new{2,5,7}, set:new{3,9,27}, set:new{2,3,16}
print(A)
print(B)
print(C) -- expected: {2, 3, 16}; but {2, 3}

I think what may be happening here is the VM's calls to next aren't using keys from the table's map after iterating over array indices. Something like this fails, for example:

func TestPairsSplit(t *testing.T) {
	testString(t, `
	local t = {}
	-- first two keys go into array
	t[1] = true
	t[2] = true
	-- next key forced into map instead of array since it's non-sequential
	t[16] = true
	-- next key inserted into array
	t[3] = true

	local keys = {}
	for k, v in pairs(t) do
		keys[#keys + 1] = k
	end

	table.sort(keys)
	assert(keys[1] == 1, 'got ' .. tostring(keys[1]) .. '; want 1')
	assert(keys[2] == 2, 'got ' .. tostring(keys[2]) .. '; want 2')
	assert(keys[3] == 3, 'got ' .. tostring(keys[3]) .. '; want 3')
	assert(keys[4] == 16, 'got ' .. tostring(keys[4]) .. '; want 16')
	`)
}