File tree 2 files changed +21
-6
lines changed
2 files changed +21
-6
lines changed Original file line number Diff line number Diff line change @@ -491,6 +491,21 @@ function test()
491
491
end
492
492
test ()
493
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 ()
508
+
494
509
-- issue #304
495
510
function test ()
496
511
local x = {
@@ -508,4 +523,4 @@ function test()
508
523
assert (# a == i )
509
524
end
510
525
end
511
- test ()
526
+ test ()
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ func OpenString(L *LState) int {
14
14
//_, ok := L.G.builtinMts[int(LTString)]
15
15
//if !ok {
16
16
mod = L .RegisterModule (StringLibName , strFuncs ).(* LTable )
17
- gmatch := L .NewClosure ( strGmatch , L . NewFunction (strGmatchIter ) )
17
+ gmatch := L .NewFunction (strGmatch )
18
18
mod .RawSetString ("gmatch" , gmatch )
19
19
mod .RawSetString ("gfind" , gmatch )
20
20
mod .RawSetString ("__index" , mod )
@@ -299,7 +299,7 @@ type strMatchData struct {
299
299
}
300
300
301
301
func strGmatchIter (L * LState ) int {
302
- md := L .CheckUserData (1 ).Value .(* strMatchData )
302
+ md := L .CheckUserData (UpvalueIndex ( 1 ) ).Value .(* strMatchData )
303
303
str := md .str
304
304
matches := md .matches
305
305
idx := md .pos
@@ -331,11 +331,11 @@ func strGmatch(L *LState) int {
331
331
if err != nil {
332
332
L .RaiseError (err .Error ())
333
333
}
334
- L .Push (L .Get (UpvalueIndex (1 )))
335
334
ud := L .NewUserData ()
336
335
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
339
339
}
340
340
341
341
func strLen (L * LState ) int {
You can’t perform that action at this time.
0 commit comments