private void ExecIterPrep(Instruction i) { DynValue v = m_ValueStack.Pop(); if (v.Type != DataType.Tuple) { v = DynValue.NewTuple(v, DynValue.Nil, DynValue.Nil); } DynValue f = v.Tuple.Length >= 1 ? v.Tuple[0] : DynValue.Nil; DynValue s = v.Tuple.Length >= 2 ? v.Tuple[1] : DynValue.Nil; DynValue var = v.Tuple.Length >= 3 ? v.Tuple[2] : DynValue.Nil; // MoonSharp additions - given f, s, var // 1) if f is not a function and has a __iterator metamethod, call __iterator to get the triplet // 2) if f is a table with no __call metamethod, use a default table iterator if (f.Type != DataType.Function && f.Type != DataType.ClrFunction) { DynValue meta = this.GetMetamethod(f, "__iterator"); if (meta != null && !meta.IsNil()) { v = this.GetScript().Call(meta, f, s, var); f = v.Tuple.Length >= 1 ? v.Tuple[0] : DynValue.Nil; s = v.Tuple.Length >= 2 ? v.Tuple[1] : DynValue.Nil; var = v.Tuple.Length >= 3 ? v.Tuple[2] : DynValue.Nil; m_ValueStack.Push(DynValue.NewTuple(f, s, var)); } else if (f.Type == DataType.Table) { DynValue callmeta = this.GetMetamethod(f, "__call"); if (callmeta == null || callmeta.IsNil()) { m_ValueStack.Push(EnumerableWrapper.ConvertTable(f.Table)); } } } m_ValueStack.Push(DynValue.NewTuple(f, s, var)); }