public object[] Execute(Executable exe, string pkgname, string entryName, object[] paramToExec = null, int retValueCount = 0) { Execute(exe); // 找到包入口 var func = exe.GetFuncByName(new ObjectName(pkgname, entryName)) as ValuePhoFunc; if (func == null) { throw new RuntimeException("unknown start package name: " + pkgname); } // 参数转到栈上 ObjectListToDataStack(paramToExec); var rtpkg = GetRuntimePackageByName(pkgname); var argCount = paramToExec != null ? paramToExec.Length:0; ExecuteFunc(exe, rtpkg, func, argCount, retValueCount); if (retValueCount > 0) { var retValue = new object[retValueCount]; for (int i = 0; i < retValueCount; i++) { retValue[i] = Convertor.ValueToNativeValue(DataStack.Get(-(i + 1))); } return(retValue); } return(null); }
public override bool Visit(Value iter, DataStack ds) { var con = Raw as IContainer; if (con == null) { throw new RuntimeException("Expect 'IContainer'"); } ValueIterator iterObj = null; if (iter.Equals(Value.Nil)) { iterObj = con.GetIterator(); } else { iterObj = iter as ValueIterator; if (iterObj == null) { throw new RuntimeException("Expect 'Iterator"); } iterObj.Next(); } return(iterObj.Iterate(ds)); }
internal void MoveArgStack2Local(int argCount) { // 将栈转为被调用函数的寄存器 for (int i = 0; i < argCount; i++) { var arg = DataStack.Get(-i - 1); LocalReg.Set(argCount - i - 1, arg); } }
// k, v, iter = ITER( x, iter ) internal override bool Iterate(DataStack ds) { if (_data.Current == null) { return(false); } ds.Push(this); ds.Push(_data.Current); ds.Push(new ValueInteger32(_index)); return(true); }
// k, v, iter = ITER( x, iter ) internal override bool Iterate(DataStack ds) { if (_data.Current.Equals(nullKV)) { return(false); } ds.Push(this); var kv = _data.Current; ds.Push(kv.Value); ds.Push(kv.Key); return(true); }
internal void ExecuteFunc(Executable exe, RuntimePackage rtpkg, ValuePhoFunc func, int argCount, int retValueCount) { if (func.Commands.Count == 0) { return; } if (ShowDebugInfo) { Logger.DebugLine(string.Format("============ Run '{0}' ============", func.Name)); } rtpkg.Reg.SetCount(func.RegCount); EnterFrame(func); CurrFrame.ReceiverCount = retValueCount; // 数据栈转寄存器 if (argCount > 0) { MoveArgStack2Local(argCount); _dataStack.Clear(); } int currSrcLine = 0; _state = State.Running; while (true) { var cmd = CurrFrame.GetCurrCommand(); if (cmd == null) { break; } if (ShowDebugInfo) { Logger.DebugLine("{0}|{1}", cmd.CodePos, exe.QuerySourceLine(cmd.CodePos)); Logger.DebugLine("---------------------"); Logger.DebugLine("{0,5} {1,2}| {2} {3}", _currFrame.Func.Name, _currFrame.PC, cmd.Op.ToString(), _insset.InstructToString(cmd)); } // 源码行有变化时 if (currSrcLine == 0 || currSrcLine != cmd.CodePos.Line) { if (currSrcLine != 0) { CallHook(DebugHook.SourceLine); } currSrcLine = cmd.CodePos.Line; } // 每条指令执行前 CallHook(DebugHook.AssemblyLine); if (_insset.ExecCode(this, cmd)) { if (_currFrame == null) { break; } _currFrame.PC++; } // 打印执行完后的信息 if (ShowDebugInfo) { rtpkg.Reg.DebugPrint(); // 寄存器信息 LocalReg.DebugPrint(); // 数据栈信息 DataStack.DebugPrint(); Logger.DebugLine(""); } } if (ShowDebugInfo) { Logger.DebugLine("============ VM End ============"); } }
internal virtual bool Iterate(DataStack ds) { return(false); }
public virtual bool Visit(Value iter, DataStack ds) { return(false); }