Skip to content

Commit

Permalink
Bugfix in returning keys in pairs().
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcuth committed Nov 13, 2014
1 parent e285bf4 commit b20928d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
13 changes: 12 additions & 1 deletion test/scripts/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,23 @@ t = { nil, nil, 123 }
a = ''

for i, v in pairs(t) do
a = a..i..':'..v..';'
a = a..i..':'..v..';'
end

assertTrue (a == '3:123;', 'pairs() should iterate over numeric table items')


t = {}
t[10] = {}
t[15] = {}
s = ''

for i in pairs(t) do
s = s..i..';'
end

assertTrue (s == '10;15;', 'pairs() should return correct numeric keys')




Expand Down
43 changes: 43 additions & 0 deletions test/scripts/test-no-require.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,7 @@ assertTrue (b == '[1=2][2=4][3=8]', 'ipairs() should iterate over table items [1

-- pairs


local a, b = "", {foo=1}
b["bar"] = "Hello",
table.insert(b, 123)
Expand All @@ -1425,6 +1426,48 @@ end
assertTrue (#a == #'1:123;bar:Hello;foo:1;', 'pairs() should iterate over table items [2]') -- Have to compare lengths because order is arbitrary


local t = {
[0] = "zero",
[1] = "one",
[-1] = "negative",
foo = "string",
[0.5] = "half"
}
local r = {}

for i, v in pairs(t) do
r[v] = true
end

assertTrue (r.zero, 'pairs() should iterate over zero key')
assertTrue (r.one, 'pairs() should iterate over positive integer keys')
assertTrue (r.negative, 'pairs() should iterate over negative keys')
assertTrue (r.string, 'pairs() should iterate over string keys')
assertTrue (r.half, 'pairs() should iterate over non-integer numberic keys')


t = { nil, nil, 123 }
a = ''

for i, v in pairs(t) do
a = a..i..':'..v..';'
end

assertTrue (a == '3:123;', 'pairs() should iterate over numeric table items')


t = {}
t[10] = {}
t[15] = {}
s = ''

for i in pairs(t) do
s = s..i..';'
end

assertTrue (s == '10;15;', 'pairs() should return correct numeric keys')




-- pcall
Expand Down
2 changes: 1 addition & 1 deletion vm/src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@

if (found) {
while ((key = keys[i]) !== void 0 && (value = numValues[key]) === void 0) i++;
if (value !== void 0) return [i >>= 0, value];
if (value !== void 0) return [key >>= 0, value];
}

} else {
Expand Down

0 comments on commit b20928d

Please sign in to comment.