Skip to content

Commit 09ee001

Browse files
committed
Squashed commit of the following:
commit 467210d Author: Sebastian Waschik <[email protected]> Date: Thu Dec 14 11:49:50 2023 +0100 Return a closure without arguments in gfind
1 parent 152f322 commit 09ee001

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

_glua-tests/issues.lua

+16-1
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,21 @@ function test()
491491
end
492492
test()
493493

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()
508+
494509
-- issue #304
495510
function test()
496511
local x ={
@@ -508,4 +523,4 @@ function test()
508523
assert(#a == i)
509524
end
510525
end
511-
test()
526+
test()

stringlib.go

+5-5
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)