protected override ILuaMultiValue InvokeInternal(ILuaMultiValue args) { ILuaValue obj = args[0]; Stream s; int start = 0; if (obj.ValueType == LuaValueType.Table) { s = ((ILuaTable)obj).GetItemRaw(_stream) as Stream; if (s == null) { throw new ArgumentException("First argument to io.read must be a file-stream or a file path, make sure to use file:read."); } start = 1; } else if (obj.GetValue() is Stream) { s = obj.GetValue() as Stream; start = 1; } else { s = _input; start = 0; } int[] a = _parse(args.Skip(start), "io.read"); return(_read(a, new StreamReader(s), Environment)); }
protected override ILuaMultiValue _invokeInternal(ILuaMultiValue args) { if (args.Count < 2) { throw new ArgumentException("Expecting at least two arguments to function 'overload'."); } ILuaValue meth = args[0]; ILuaValue obj = args[1]; if (meth.ValueType != LuaValueType.Function) { throw new ArgumentException("First argument to function 'overload' must be a method."); } if (obj.ValueType != LuaValueType.Number || ((double)obj.GetValue() % 1 != 0)) { throw new ArgumentException( "Second argument to function 'overload' must be an integer."); } int i = Convert.ToInt32((double)obj.GetValue()); return(meth.Invoke(null, false, i, _environment.Runtime.CreateMultiValue(args.Skip(2).ToArray()))); }
protected override ILuaMultiValue _invokeInternal(ILuaMultiValue args) { ILuaValue obj = args[0]; bool close; Stream s; int start; string oString = obj.GetValue() as string; if (oString != null) { if (oString[0] != '*') { s = File.OpenRead(oString); close = true; start = 1; } else { s = _input; close = false; start = 0; } } else if (obj.ValueType == LuaValueType.Table) { s = ((ILuaTable)obj).GetItemRaw(_stream) as Stream; if (s == null) { throw new ArgumentException("First argument to io.lines must be a file-stream or a " + "file path, make sure to use file:lines."); } close = false; start = 1; } else if (obj.GetValue() is Stream st) { s = st; close = false; start = 1; } else { s = _input; close = false; start = 0; } int[] a = _parse(args.Skip(start), "io.lines"); return(_environment.Runtime.CreateMultiValue(new LinesHelper(_environment, close, s, a))); }
static ILuaMultiValue _getStream(ILuaValue file, ILuaEnvironment E, out Stream s) { s = null; if (file == LuaNil.Nil) { if (_output == null) { return(E.Runtime.CreateMultiValueFromObj(null, "No default output file set.")); } s = _output; } else { if (file.ValueType == LuaValueType.Table) { s = ((ILuaTable)file).GetItemRaw(_stream).GetValue() as Stream; if (s == null) { return(E.Runtime.CreateMultiValueFromObj(null, "Specified argument is not a valid file stream.")); } } else if (file.ValueType == LuaValueType.UserData) { s = file.GetValue() as Stream; } else { return(E.Runtime.CreateMultiValueFromObj(null, "Specified argument is not a valid file stream.")); } } return(null); }
protected override ILuaMultiValue InvokeInternal(ILuaMultiValue args) { ILuaValue obj = args[0]; if (obj != LuaNil.Nil) { if (obj.ValueType == LuaValueType.String) { Stream s = File.OpenRead(obj.GetValue() as string); _output = s; } else if (obj.ValueType == LuaValueType.Table) { Stream s = ((ILuaTable)obj).GetItemRaw(_stream) as Stream; if (s == null) { throw new InvalidOperationException("First argument to function 'io.output' must be a file-stream or a string path."); } _output = s; } else if (obj is Stream) { _output = obj as Stream; } else { throw new InvalidOperationException("First argument to function 'io.output' must be a file-stream or a string path."); } } return(Environment.Runtime.CreateMultiValue(_CreateFile(_output, Environment))); }
public override void Assign(ILuaValue value) { LuaType type = value as LuaType; if (type != null) { if (Type == null) { Type = type.Type; } else { throw new InvalidOperationException(string.Format(Resources.MemberHasType, Name)); } } else { if (Default != null) { throw new InvalidOperationException(string.Format(Resources.MemberHasDefault, Name)); } if (Type == null) { object temp = value == null ? value : value.GetValue(); Type = temp == null ? typeof(object) : temp.GetType(); } Default = value; } }
public override void SetIndex(ILuaValue index, ILuaValue value) { string name = index.GetValue() as string; int overload = -1; if (name == null) { throw new InvalidOperationException(string.Format(Resources.BadIndexType, "class definition", "string")); } // find if name is defining an overload. if (name.Contains('`')) { if (!int.TryParse(name.Substring(name.IndexOf('`') + 1), out overload)) { throw new InvalidOperationException(Resources.OnlyNumbersInOverload); } name = name.Substring(0, name.IndexOf('`')); } // find the members with the given name. var members = type.GetMembers(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance).Where(m => m.Name == name).ToArray(); if (members == null || members.Length == 0) { throw new InvalidOperationException(string.Format(Resources.NoMemberFound, type, name)); } if (members.Length > 1 && overload == -1) { throw new AmbiguousMatchException(string.Format(Resources.NoMemberFound, type, name)); } if (overload != -1 && overload >= members.Length) { throw new InvalidOperationException(Resources.OverloadOutOfRange); } // set the backing parrent object. name = type.FullName + "." + name; if (members[0].MemberType == MemberTypes.Method) { if (value.ValueType != LuaValueType.Function) { throw new InvalidOperationException(string.Format(Resources.MustBeFunction, name)); } Item item = parent.CreateItem(name, new[] { members[overload == -1 ? 0 : overload] }); item.Assign(value); parent._items.Add(item); } else if (members[0].MemberType == MemberTypes.Property) { Item item = parent.CreateItem(name, members); item.Assign(value); parent._items.Add(item); } }
public virtual IEnumerable <ILuaMultiValue> GenericLoop(ILuaEnvironment env, ILuaMultiValue args) { // TODO: Replace this. if (args == null) { throw new ArgumentNullException(nameof(args)); } if (env == null) { throw new ArgumentNullException(nameof(env)); } ILuaValue target = args[0]; object temp = target.GetValue(); if (temp is IEnumerable <ILuaMultiValue> enumT) { foreach (var item in enumT) { yield return(item); } } else if (temp is IEnumerable en) { foreach (var item in en) { yield return(new LuaMultiValue(CreateValue(item))); } } else if (target.ValueType == LuaValueType.Function) { ILuaValue s = args[1]; ILuaValue var = args[2]; while (true) { var ret = target.Invoke(LuaNil.Nil, false, -1, CreateMultiValue(s, var)); if (ret == null || ret[0] == null || ret[0] == LuaNil.Nil) { yield break; } var = ret[0]; yield return(ret); } } else { throw new InvalidOperationException( $"Cannot enumerate over an object of type '{args[0]}'."); } }
protected override ILuaMultiValue InvokeInternal(ILuaMultiValue args) { if (args.Count < 1) { throw new ArgumentException("Expecting at least one argument to function 'load'."); } ILuaValue ld = args[0]; string chunk; if (ld.ValueType == LuaValueType.Function) { chunk = ""; while (true) { var ret = ld.Invoke(LuaNil.Nil, false, -1, Environment.Runtime.CreateMultiValue()); if (ret[0].ValueType == LuaValueType.String) { if (string.IsNullOrEmpty(ret[0].GetValue() as string)) { break; } else { chunk += ret[0].GetValue() as string; } } else { break; } } } else if (ld.ValueType == LuaValueType.String) { chunk = ld.GetValue() as string; } else { throw new ArgumentException("First argument to 'load' must be a string or a method."); } try { return(Environment.Runtime.CreateMultiValue(Environment.CodeCompiler.Compile(Environment, PlainParser.Parse(Environment.Parser, chunk, null), null))); } catch (Exception e) { return(Environment.Runtime.CreateMultiValueFromObj(null, e.Message)); } }
protected override ILuaMultiValue InvokeInternal(ILuaMultiValue args) { ILuaValue obj = args[0]; if (obj.GetValue() is Stream) { return(Environment.Runtime.CreateMultiValueFromObj("file")); } else if (obj.ValueType == LuaValueType.Table) { Stream s = ((ILuaTable)obj).GetItemRaw(_stream) as Stream; return(Environment.Runtime.CreateMultiValueFromObj((s == null ? null : "file"))); } else { return(LuaMultiValue.Empty); } }
public override ILuaValue GetIndex(ILuaValue index) { string n = index.GetValue() as string; if (n == null) { throw new InvalidOperationException(string.Format(Resources.BadIndexType, "class definition", "string")); } foreach (var item in Interfaces) { if (item.Name == n) { return(new LuaClassItem(this, item)); } } throw new InvalidOperationException(string.Format(Resources.DoesNotImplement, Name, n)); }
public gsubHelper(ILuaValue value, int max) { count_ = 0; max_ = max; if (value.ValueType == LuaValueType.String) { string_ = (string)value.GetValue(); } else if (value.ValueType == LuaValueType.Table) { table_ = (ILuaTable)value; } else if (value.ValueType == LuaValueType.Function) { method_ = value; } else { throw new ArgumentException("Third argument to function 'string.gsub' must be a string, table, or function."); } }
public gsubHelper(ILuaValue value, int max) { count_ = 0; max_ = max; if (value.ValueType == LuaValueType.String) string_ = (string)value.GetValue(); else if (value.ValueType == LuaValueType.Table) table_ = (ILuaTable)value; else if (value.ValueType == LuaValueType.Function) method_ = value; else throw new ArgumentException("Third argument to function 'string.gsub' must be a string, table, or function."); }
static ILuaMultiValue _getStream(ILuaValue file, ILuaEnvironment E, out Stream s) { s = null; if (file == LuaNil.Nil) { if (_output == null) return E.Runtime.CreateMultiValueFromObj(null, "No default output file set."); s = _output; } else { if (file.ValueType == LuaValueType.Table) { s = ((ILuaTable)file).GetItemRaw(_stream).GetValue() as Stream; if (s == null) return E.Runtime.CreateMultiValueFromObj(null, "Specified argument is not a valid file stream."); } else if (file.ValueType == LuaValueType.UserData) { s = file.GetValue() as Stream; } else return E.Runtime.CreateMultiValueFromObj(null, "Specified argument is not a valid file stream."); } return null; }
public override void SetIndex(ILuaValue index, ILuaValue value) { var name = index.GetValue() as string; if (_created != null || name == null) { return; } // If this is the contructor, assign it. if (name == "__ctor") { if (value.ValueType == LuaValueType.Function) { _ctor = value; } else { throw new InvalidOperationException(Resources.CtorMustBeFunc); } return; } // Try to find an existing element. Item i = _items.Where(ii => ii.Name == name).FirstOrDefault(); if (i == null) { // Check for the member in the base class. if (BaseType != null) { var mems = BaseType.GetMember(name); if (mems != null && mems.Length > 0) { i = CreateItem(name, mems); } } // Check for the member in interfaces. if (i == null) { Type inter = null; MemberInfo[] mems = null; foreach (var item in Interfaces) { var temp = item.GetMember(name); if (temp != null && temp.Length > 0) { if (mems != null) { mems = null; break; } inter = item; mems = temp; } } if (mems != null) { i = CreateItem(name, mems); } } // If still not found, create a new field. if (i == null) { i = new FieldItem(name); } _items.Add(i); } i.Assign(value); }