Skip to content

Commit

Permalink
Fix foreach ending on tables with type mismatch (#2804)
Browse files Browse the repository at this point in the history
* Fix foreach ending on tables with type mismatch

* Update test
  • Loading branch information
Denneisk authored Oct 30, 2023
1 parent 269b3b9 commit e4d7aaf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
23 changes: 19 additions & 4 deletions data/expression2/tests/regressions/2803.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
## SHOULD_PASS:EXECUTE

local Ran = 0
local TabMixedNonSeq = table(1 = 1, 2 = 2, 3 = 3, 10 = 10, 100 = 100, 2359 = 2359, # 6
4 = "4", 5 = "5", 6 = "6", 11 = "11", 123 = "123", # 5
"s" = "s", "foo" = "bar") # 2

foreach(_:number, _:string = table( 123 = "foo" )) {
Ran = 1
local TabMixedSeq = table(1 = 1, 2 = 2, 3 = 3, 7 = 7, 9 = 9, 11 = 11, # 6
4 = "4", 5 = "5", 6 = "6", 8 = "8", 10 = "10", # 5
"s" = "s", "foo" = "bar") # 2


local I = 0
foreach(_:number, _:number = TabMixedNonSeq) {
I = I + 1
}

assert(I == 6)

I = 0
foreach(_:number, _:string = TabMixedSeq) {
I = I + 1
}

assert(Ran)
assert(I == 5)
18 changes: 10 additions & 8 deletions lua/entities/gmod_wire_expression2/core/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1162,17 +1162,19 @@ registerCallback( "postinit", function()

local next = next
local function itern(tbl, i)
local key, value = next(tbl.n, i)
if tbl.ntypes[key] == id then
return key, value
end
local value
repeat
i, value = next(tbl.n, i)
until tbl.ntypes[i] == id or value == nil
return i, value
end

local function iters(tbl, i)
local key, value = next(tbl.s, i)
if tbl.stypes[key] == id then
return key, value
end
local value
repeat
i, value = next(tbl.s, i)
until tbl.stypes[i] == id or value == nil
return i, value
end

registerOperator("iter", "s" .. id .. "=t", "", function(state, table)
Expand Down

0 comments on commit e4d7aaf

Please sign in to comment.