public override LuaValue Evaluate(LuaValue baseValue, LuaTable enviroment) { LuaFunction function = baseValue as LuaFunction; if (function != null) { if (function.Function.Method.DeclaringType.FullName == "SharpLua.Library.BaseLib" && (function.Function.Method.Name == "loadstring" || function.Function.Method.Name == "dofile")) { if (this.Args.String != null) { return(function.Function.Invoke(new LuaValue[] { this.Args.String.Evaluate(enviroment), enviroment })); } else { return(function.Function.Invoke(new LuaValue[] { this.Args.ArgList[0].Evaluate(enviroment), enviroment })); } } if (this.Args.Table != null) { return(function.Function.Invoke(new LuaValue[] { this.Args.Table.Evaluate(enviroment) })); } else if (this.Args.String != null) { return(function.Function.Invoke(new LuaValue[] { this.Args.String.Evaluate(enviroment) })); } else { List <LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment)); return(function.Function.Invoke(LuaMultiValue.UnWrapLuaValues(args.ToArray()))); } } else if ((baseValue as LuaTable) != null) { List <LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment)); args.Insert(0, baseValue); return(((baseValue as LuaTable).MetaTable.GetValue("__call") as LuaFunction).Invoke(args.ToArray())); } else if ((baseValue as LuaClass) != null) { LuaClass c = baseValue as LuaClass; List <LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment)); //args.Insert(0, new LuaString(this.Method); if (c.Self.MetaTable == null) { c.GenerateMetaTable(); } return((c.Self.MetaTable.GetValue("__call") as LuaFunction).Invoke(args.ToArray())); } else if ((baseValue as LuaUserdata) != null) { List <LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment)); LuaUserdata u = baseValue as LuaUserdata; if (u.MetaTable != null) { if (u.MetaTable.GetValue("__call") != null) { return(ObjectToLua.ToLuaValue((u.MetaTable.GetValue("__call") as LuaFunction).Invoke(args.ToArray()))); } else { throw new NotImplementedException(); } } else { throw new NotImplementedException(); } } else { throw new Exception("Invoke function call on non function value."); } }
public override LuaValue Evaluate(LuaValue baseValue, LuaTable enviroment) { LuaValue value = null; try { LuaValue.GetKeyValue(baseValue, new LuaString(this.Method)); } catch (Exception) { } LuaFunction function = value as LuaFunction; if (function != null) { if (this.Args.Table != null) { return(function.Function.Invoke(new LuaValue[] { baseValue, this.Args.Table.Evaluate(enviroment) })); } else if (this.Args.String != null) { return(function.Function.Invoke(new LuaValue[] { baseValue, this.Args.String.Evaluate(enviroment) })); } else { List <LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment)); args.Insert(0, baseValue); return(function.Function.Invoke(args.ToArray())); } } // method call on table would be like _G:script() else if ((baseValue as LuaTable) != null) { List <LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment)); return(((baseValue as LuaTable).MetaTable.GetValue("__call") as LuaFunction).Invoke(args.ToArray())); } else if ((baseValue as LuaClass) != null) { LuaClass c = baseValue as LuaClass; List <LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment)); args.Insert(0, new LuaString(this.Method)); if (c.Self.MetaTable == null) { c.GenerateMetaTable(); } return((c.Self.MetaTable.GetValue("__call") as LuaFunction).Invoke(args.ToArray())); } else if ((baseValue as LuaUserdata) != null) { List <LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment)); LuaUserdata obj = baseValue as LuaUserdata; object o = obj.Value; if (obj.MetaTable != null) { if (obj.MetaTable.GetValue(this.Method) != null) { LuaValue o2 = obj.MetaTable.GetValue(this.Method); if ((o2 as LuaFunction) != null) { return((o2 as LuaFunction).Invoke(args.ToArray())); } else if ((o2 as LuaTable) != null) { throw new NotImplementedException(); // TODO } } } return(ObjectToLua.ToLuaValue(o.GetType().GetMethod(this.Method, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Invoke(o, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, args.ToArray(), CultureInfo.CurrentCulture))); } else { throw new Exception("Invoke method call on non function value."); } }
/// <summary> /// Sets the assignment /// </summary> /// <param name="baseValue"></param> /// <param name="key"></param> /// <param name="value"></param> private static void SetKeyValue(LuaValue baseValue, LuaValue key, LuaValue value) { LuaValue newIndex = LuaNil.Nil; LuaTable table = baseValue as LuaTable; if (table != null) { if (table.MetaTable != null) { newIndex = table.MetaTable.GetValue("__newindex"); // to be finished at the end of this method } if (newIndex == LuaNil.Nil) { table.SetKeyValue(key, value); return; } } else if ((baseValue as LuaClass) != null) { LuaClass c = baseValue as LuaClass; // null checks (mainly for debugging) if (c.Self.MetaTable == null) { c.GenerateMetaTable(); } //throw new Exception("Class metatable is nil!"); newIndex = c.Self.MetaTable.GetValue("__newindex"); if (newIndex == LuaNil.Nil) { c.Self.SetKeyValue(key, value); } else { (newIndex as LuaFunction).Invoke(new LuaValue[] { baseValue, key, value }); } return; } else { LuaUserdata userdata = baseValue as LuaUserdata; if (userdata != null) { if (userdata.MetaTable != null) { newIndex = userdata.MetaTable.GetValue("__newindex"); } if (newIndex == LuaNil.Nil) { throw new Exception("Assign field of userdata without __newindex defined."); } } } LuaFunction func = newIndex as LuaFunction; if (func != null) { func.Invoke(new LuaValue[] { baseValue, key, value }); } else { SetKeyValue(newIndex, key, value); } }