luaobject.go
func (ti *LuaTableIter) Next(key, value interface{}) bool {
	if ti.lo == nil {
		ti.err = errors.New("empty iterator")
		return false
	}
	L := ti.lo.l

	if ti.iterRef == lua.LUA_NOREF {
		// Must be a table. This requires the Iter() function to set
		// ref=LUA_NOREF.

		// Push table.
		ti.lo.Push()
		defer L.Pop(1)

		if ti.keyRef == lua.LUA_NOREF {
			L.PushNil()
		} else {
			L.RawGeti(lua.LUA_REGISTRYINDEX, ti.keyRef)
		}

		if L.Next(-2) == 0 {
			L.Unref(lua.LUA_REGISTRYINDEX, ti.keyRef)
			return false
		}

	} else {
		L.RawGeti(lua.LUA_REGISTRYINDEX, ti.iterRef)
		ti.lo.Push()

		if ti.keyRef == lua.LUA_NOREF {
			L.PushNil()
		} else {
			L.RawGeti(lua.LUA_REGISTRYINDEX, ti.keyRef)
		}

		err := L.Call(2, 2)
		if err != nil {
			L.Pop(1)
			ti.err = err
			return false
		}
		if L.IsNil(-2) {
			L.Pop(2)
			L.Unref(lua.LUA_REGISTRYINDEX, ti.iterRef)
			return false
		}
	}

	err := LuaToGo(L, -2, key)
	if err != nil {
		ti.err = err
		return false
	}
	if value != nil {
		err = LuaToGo(L, -1, value)
		if err != nil {
			ti.err = err
			return false
		}
	}

	// Drop value, key is now on top.
	L.Pop(1)

	// Replace former key reference with new key.
	L.Unref(lua.LUA_REGISTRYINDEX, ti.keyRef)
	ti.keyRef = L.Ref(lua.LUA_REGISTRYINDEX)
	return true
}
func (ti *LuaTableIter) Next(key, value interface{}) bool {
	if ti.lo == nil {
		ti.err = errors.New("empty iterator")
		return false
	}
	L := ti.lo.l

	if ti.iterRef == lua.LUA_NOREF {
		// Must be a table. This requires the Iter() function to set
		// ref=LUA_NOREF.

		// Push table.
		ti.lo.Push()
		defer L.Pop(1)

		if ti.keyRef == lua.LUA_NOREF {
			L.PushNil()
		} else {
			L.RawGeti(lua.LUA_REGISTRYINDEX, ti.keyRef)
		}

		if L.Next(-2) == 0 {
			L.Unref(lua.LUA_REGISTRYINDEX, ti.keyRef)
			return false
		}

	} else {
		L.RawGeti(lua.LUA_REGISTRYINDEX, ti.iterRef)
		ti.lo.Push()

		if ti.keyRef == lua.LUA_NOREF {
			L.PushNil()
		} else {
			L.RawGeti(lua.LUA_REGISTRYINDEX, ti.keyRef)
		}

		err := L.Call(2, 2)
		if err != nil {
			L.Pop(1)
			ti.err = err
			return false
		}
		if L.IsNil(-2) {
			L.Pop(2)
			L.Unref(lua.LUA_REGISTRYINDEX, ti.iterRef)
			return false
		}
	}

	err := LuaToGo(L, -2, key)
	if err != nil {
		ti.err = err
		return false
	}
	if value != nil {
		err = LuaToGo(L, -1, value)
		if err != nil {
			ti.err = err
			return false
		}
	}

	// Drop value, key is now on top.
	L.Pop(1)

	// Replace former key reference with new key.
	L.Unref(lua.LUA_REGISTRYINDEX, ti.keyRef)
	ti.keyRef = L.Ref(lua.LUA_REGISTRYINDEX)
	return true
}
// Next gets the next key/value pair from the indexable value.
//
// 'value' must be a valid argument for LuaToGo. As a special case, 'value' can
// be nil to make it possible to loop over keys without caring about associated
// values.