Skip to content

Commit 467210d

Browse files
committed
Return a closure without arguments in gfind
1 parent 2348fd0 commit 467210d

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

_glua-tests/issues.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,18 @@ function test()
490490
assert(b == nil)
491491
end
492492
test()
493+
494+
-- issue #462
495+
function test()
496+
local x = string.gmatch("asdf", "a")
497+
assert(x() == "a", "check gmatch callback")
498+
499+
local expected = {
500+
"a",
501+
"c",
502+
}
503+
for i in string.gmatch("abc", "[ac]") do
504+
assert(i == table.remove(expected, 1), "check gmatch inside loop")
505+
end
506+
end
507+
test()

stringlib.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func OpenString(L *LState) int {
1414
//_, ok := L.G.builtinMts[int(LTString)]
1515
//if !ok {
1616
mod = L.RegisterModule(StringLibName, strFuncs).(*LTable)
17-
gmatch := L.NewClosure(strGmatch, L.NewFunction(strGmatchIter))
17+
gmatch := L.NewFunction(strGmatch)
1818
mod.RawSetString("gmatch", gmatch)
1919
mod.RawSetString("gfind", gmatch)
2020
mod.RawSetString("__index", mod)
@@ -299,7 +299,7 @@ type strMatchData struct {
299299
}
300300

301301
func strGmatchIter(L *LState) int {
302-
md := L.CheckUserData(1).Value.(*strMatchData)
302+
md := L.CheckUserData(UpvalueIndex(1)).Value.(*strMatchData)
303303
str := md.str
304304
matches := md.matches
305305
idx := md.pos
@@ -331,11 +331,11 @@ func strGmatch(L *LState) int {
331331
if err != nil {
332332
L.RaiseError(err.Error())
333333
}
334-
L.Push(L.Get(UpvalueIndex(1)))
335334
ud := L.NewUserData()
336335
ud.Value = &strMatchData{str, 0, mds}
337-
L.Push(ud)
338-
return 2
336+
f := L.NewClosure(strGmatchIter, ud)
337+
L.Push(f)
338+
return 1
339339
}
340340

341341
func strLen(L *LState) int {

0 commit comments

Comments
 (0)