github.com/dfcfw/lua@v0.0.0-20230325031207-0cc7ffb7b8b9/luar/map_iter.go (about)

     1  //go:build !go1.12
     2  // +build !go1.12
     3  
     4  package luar
     5  
     6  import "github.com/dfcfw/lua"
     7  
     8  func mapCall(L *lua.LState) int {
     9  	ref, _ := check(L, 1)
    10  
    11  	keys := ref.MapKeys()
    12  	i := 0
    13  	fn := func(L *lua.LState) int {
    14  		if i >= len(keys) {
    15  			return 0
    16  		}
    17  		L.Push(New(L, keys[i].Interface()))
    18  		L.Push(New(L, ref.MapIndex(keys[i]).Interface()))
    19  		i++
    20  		return 2
    21  	}
    22  	L.Push(L.NewFunction(fn))
    23  	return 1
    24  }