private void __Run(string code, CSLE.CLS_Content content) { var tokens = env.tokenParser.Parse(code); var expr = env.Expr_CompilerToken(tokens); env.Expr_Execute(expr, content); }
private void button10_Click(object sender, EventArgs e) { listLog.Items.Clear(); if (compilerResult == null) { button2_Click(sender, e); } if (compilerResult != null) { CSLE.ICLS_Expression exp = compilerResult; CSLE.CLS_Content.Value returnvalue = new CSLE.CLS_Content.Value(); //try { CSLE.CLS_Content content = this.scriptService.CreateContent(); returnvalue = exp.ComputeValue(content); } //catch (Exception err) //{ // Log_Error("执行错误" + err.ToString()); //} if (returnvalue.type != null) { Log("result=<" + returnvalue.type.Name + ">" + returnvalue.value); } else { Log("result=<unknown>" + returnvalue.value); } } }
void Run(string code, bool useTry) { if (!builded) { Log_Error("Build 失败无法运行"); } CSLE.CLS_Content content = env.CreateContent();//创建一个上下文,出错以后可以用此上下文捕获信息 if (useTry) { try { __Run(code, content); } catch (Exception err) { string errValue = content.DumpValue(); string errStack = content.DumpStack(null); string errSystem = "SystemError:\n" + err.ToString(); MessageBox.Show(errValue + errStack + errSystem); } } else { __Run(code, content); } }
//--------------------------------------------------------------------- public object doFile(string file_name) { string code; mMapFile.TryGetValue(file_name, out code); if (code == null) { EbLog.Error("EbScriptMgr.runScript() Error! 读取文件失败,File=" + file_name); return(null); } if (mContent == null) { mContent = mEnvironment.CreateContent(); } try { var tokens = mEnvironment.tokenParser.Parse(code); var expr = mEnvironment.Expr_CompilerToken(tokens); return(mEnvironment.Expr_Execute(expr, mContent)); } catch (Exception ec) { EbLog.Error("EbScriptMgr.runScript() Error!"); EbLog.Error(ec.ToString()); EbLog.Error(mContent.DumpValue()); EbLog.Error(mContent.DumpStack(null)); } return(null); }
//--------------------------------------------------------------------- public void setValue(string name, object v) { if (mContent == null) { mContent = mEnvironment.CreateContent(); } mContent.DefineAndSet(name, v.GetType(), v); }
private void button9_Click(object sender, EventArgs e) { listLog.Items.Clear(); if (compilerResult == null) { button2_Click(sender, e); } if (compilerResult != null) { DateTime t1 = DateTime.Now; CSLE.CLS_Content.Value returnvalue = new CSLE.CLS_Content.Value(); int count = 1000; for (int i = 0; i < count; i++) { CSLE.ICLS_Expression exp = compilerResult; try { CSLE.CLS_Content content = this.scriptService.CreateContent(); returnvalue = exp.ComputeValue(content); } catch (Exception err) { Log_Error("执行错误" + err.ToString()); return; } } DateTime t2 = DateTime.Now; Log("C#Lite count=" + count + "time:" + (t2 - t1).TotalSeconds); if (returnvalue.type != null) { Log("result=<" + returnvalue.type.Name + ">" + returnvalue.value); } else { Log("result=<unknown>" + returnvalue.value); } } }
private void button3_Click(object sender, EventArgs e) { listLog.Items.Clear(); if (compilerResult == null) { button2_Click(sender, e); } if (compilerResult != null) { CSLE.ICLS_Expression exp = compilerResult; CSLE.CLS_Content.Value returnvalue = new CSLE.CLS_Content.Value(); CSLE.CLS_Content content = this.scriptService.CreateContent(); try { returnvalue = exp.ComputeValue(content); } catch (Exception err) { string contentValue = content.DumpValue(); string contentStack = content.DumpStack(null); string systemError = "SystemError:\n" + err.ToString(); MessageBox.Show(contentValue + "\n" + contentStack + "\n" + systemError); Log_Error("执行错误" + err.ToString() + ":" + contentStack + "\n" + contentValue); } if (returnvalue == null) { Log("result=<none>"); } else if (null != returnvalue.type) { Log("result=<" + returnvalue.type.Name + ">" + returnvalue.value); } else { Log("result=<unknown>" + returnvalue.value); } } }
private void Profile(int count) { string code = richTextBox_Code.Text; try { string codelite = "return " + code; var tokens = this.scriptService.ParserToken(code); compilerResult = this.scriptService.Expr_CompilerToken(tokens, true); compilerResult = this.scriptService.Expr_Optimize(compilerResult); if (compilerResult != null) { CSLE.CLS_Content.Value result = null; DateTime t = DateTime.Now; for (int i = 0; i < count; i++) { CSLE.CLS_Content content = this.scriptService.CreateContent(); //compilerResult = compiler.Compiler(code); CSLE.ICLS_Expression exp = compilerResult; result = exp.ComputeValue(content); } DateTime t2 = DateTime.Now; Log("C#Lite count=" + count + "time:" + (t2 - t).TotalSeconds); if (result.type != null) { Log("result=<" + result.type.Name + ">" + result.value.ToString()); } else { Log("result=null"); } } } catch (Exception err) { Log("C#Lite error"); } string codelua = "function test() \n return " + code + "\nend"; try { int result = 0; var lua = UniLua.LuaAPI.NewState(); var state = lua.L_DoString(codelua); int itop = lua.GetTop(); DateTime t = DateTime.Now; for (int i = 0; i < count; i++) { //lua.SetTop(itop); lua.GetGlobal("test"); // 加载 lua 中定义的一个名叫 foo 的全局函数到堆栈 lua.Call(0, 1); // 调用函数 foo, 指明有2个参数,没有返回值 //lua.SetTop(0); //lua.PCall(0, -1, 0); result = lua.ToInteger(-1); lua.Pop(1); //lua.p(1); } DateTime t2 = DateTime.Now; Log("unilua count=" + count + " time=" + (t2 - t).TotalSeconds); Log("result=<int>" + result); } catch (Exception err) { Log("unilua error."); } try { object ssresult = null; ScriptNET.Script ssc = ScriptNET.Script.CompileExpression(code); DateTime t = DateTime.Now; for (int i = 0; i < count; i++) { ssc.Context = new ScriptNET.Runtime.ScriptContext(); ssresult = ssc.Execute(); } DateTime t2 = DateTime.Now; Log("ssharp count=" + count + " time=" + (t2 - t).TotalSeconds); Log("result=<int>" + ssresult); } catch (Exception err) { Log("ssharp error."); } }
//--------------------------------------------------------------------- public void clearAllValue() { mContent = null; }
System.Reflection.MethodInfo GetMethodSlow(CSLE.CLS_Content content, bool bStatic, string funcname, IList <Type> types, IList <object> _params) { List <object> myparams = new List <object>(_params); if (slowCache == null) { System.Reflection.MethodInfo[] ms = this.type.GetMethods(); slowCache = new Dictionary <string, IList <System.Reflection.MethodInfo> >(); foreach (var m in ms) { string name = m.IsStatic ? "s=" + m.Name : m.Name; if (slowCache.ContainsKey(name) == false) { slowCache[name] = new List <System.Reflection.MethodInfo>(); } slowCache[name].Add(m); } } IList <System.Reflection.MethodInfo> minfo = null; if (slowCache.TryGetValue(bStatic ? "s=" + funcname : funcname, out minfo) == false) { return(null); } foreach (var m in minfo) { bool match = true; var pp = m.GetParameters(); if (pp.Length < types.Count)//参数多出来,不匹配 { match = false; continue; } for (int i = 0; i < pp.Length; i++) { if (i >= types.Count)//参数多出来 { if (!pp[i].IsOptional) { match = false; break; } else { myparams.Add(pp[i].DefaultValue); continue; } } else { if (pp[i].ParameterType == types[i]) { continue; } try { if (types[i] == null && !pp[i].ParameterType.IsValueType) { continue; } myparams[i] = content.environment.GetType(types[i]).ConvertTo(content, _params[i], pp[i].ParameterType); if (myparams[i] == null) { match = false; break; } } catch { match = false; break; } } if (match) { break; } } if (!match) { continue; } else { for (int i = 0; i < myparams.Count; i++) { if (i < _params.Count) { _params[i] = myparams[i]; } else { _params.Add(myparams[i]); } } return(m); } } if (minfo.Count == 1) { return(minfo[0]); } return(null); }