void insert(ILuaTable table, ILuaValue pos, ILuaValue value = null) { CheckNotNull("table.insert", table); CheckNotNull("table.insert", pos); double i; double len = table.Length().AsDouble() ?? 0; if (value == null) { value = pos; i = len + 1; } else { i = pos.AsDouble() ?? 0; } if (i > len + 1 || i < 1 || i % 1 != 0) { throw new ArgumentException( "Position given to function 'table.insert' is outside valid range."); } for (double d = len; d >= i; d--) { var temp = table.GetItemRaw(E.Runtime.CreateValue(d)); table.SetItemRaw(E.Runtime.CreateValue(d + 1), temp); } table.SetItemRaw(pos, value); }
string concat(ILuaTable table, string sep = null, int i = 1, int j = -1) { CheckNotNull("table.concat", table); int len = (int)(table.Length().AsDouble() ?? 0); if (i >= len) { return(""); } i = normalizeIndex_(len, i); j = normalizeIndex_(len, j); StringBuilder str = new StringBuilder(); for (; i <= j; i++) { ILuaValue temp = table.GetItemRaw(E.Runtime.CreateValue(i)); if (temp.ValueType != LuaValueType.String && temp.ValueType != LuaValueType.Number) { throw new ArgumentException( "Invalid '" + temp.ValueType + "' value for function 'table.concat'."); } if (str.Length > 0) { str.Append(sep); } str.Append(temp); } return(str.ToString()); }
IEnumerable <ILuaValue> unpack(ILuaTable table, int i = 1, int?jOrNull = null) { CheckNotNull("table.unpack", table); int len = (int)(table.Length().AsDouble() ?? 0); int j = jOrNull ?? len; for (; i <= j; i++) { yield return(table.GetItemRaw(E.Runtime.CreateValue(i))); } }
static object[] pairs(ILuaTable table) { ILuaTable meta = table.MetaTable; if (meta != null) { ILuaValue p = meta.GetItemRaw(_pairs); if (p != null && p != LuaNil.Nil) { var ret = p.Invoke(table, true, -1, LuaMultiValue.Empty); return(new object[] { ret.AdjustResults(3) }); } } return(new object[] { (Func <ILuaTable, ILuaValue, object[]>)next, table }); }
static object[] ipairs(ILuaTable table) { ILuaTable meta = table.MetaTable; if (meta != null) { ILuaValue method = meta.GetItemRaw(_ipairs); if (method != null && method != LuaNil.Nil) { var ret = method.Invoke(table, true, -1, LuaMultiValue.Empty); // The runtime will correctly expand the results (because the multi-value // is at the end). return(new object[] { ret.AdjustResults(3) }); } } return(new object[] { (Func <ILuaTable, double, object[]>)_ipairs_itr, table, 0 }); }
static object[] _ipairs_itr(ILuaTable table, double index) { if (index < 0 || index % 1 != 0) { throw new ArgumentException("Second argument to function 'ipairs iterator' must be a positive integer."); } index++; ILuaValue ret = table.GetItemRaw(new LuaNumber(index)); if (ret == null || ret == LuaNil.Nil) { return(new object[0]); } else { return new object[] { index, ret } }; } }
static ILuaValue getmetatable(ILuaValue value) { if (value.ValueType != LuaValueType.Table) { return(LuaNil.Nil); } ILuaTable meta = ((ILuaTable)value).MetaTable; if (meta != null) { ILuaValue method = meta.GetItemRaw(_metamethod); if (method != null && method != LuaNil.Nil) { return(method); } } return(meta); }
ILuaValue remove(ILuaTable table, int?pos = null) { CheckNotNull("table.remove", table); double len = table.Length().AsDouble() ?? 0; pos = pos ?? (int)len; if (pos > len + 1 || pos < 1) { throw new ArgumentException( "Position given to function 'table.remove' is outside valid range."); } ILuaValue prev = LuaNil.Nil; for (double d = len; d >= pos; d--) { ILuaValue ind = E.Runtime.CreateValue(d); ILuaValue temp = table.GetItemRaw(ind); table.SetItemRaw(ind, prev); prev = temp; } return(prev); }
object[] rename(string old, string new_) { if (File.Exists(old)) { try { File.Move(old, new_); return(new object[] { true }); } catch (Exception e) { return(new object[] { null, e.Message, e }); } } else if (Directory.Exists(old)) { try { Directory.Move(old, new_); return(new object[] { true }); } catch (Exception e) { return(new object[] { null, e.Message, e }); } } else { return new object[] { null, "Specified path does not exist." } }; } string setlocale(string name) { try { CultureInfo ci = CultureInfo.GetCultureInfo(name); if (ci == null) { return(null); } Thread.CurrentThread.CurrentCulture = ci; return(ci.Name); } catch (Exception) { return(null); } } double time(object source) { DateTime time; if (source is ILuaTable) { ILuaTable table = source as ILuaTable; int year, month, day, hour, min, sec; Func <string, bool, int> get = (name, req) => { ILuaValue value = table.GetItemRaw(E.Runtime.CreateValue(name)); if (value == null || value.ValueType != LuaValueType.Number) { if (req) { throw new ArgumentException("First argument to function 'os.time' is not a valid time table."); } else { return(0); } } else { return(value.As <int>()); } }; year = get("year", true); month = get("month", true); day = get("day", true); hour = get("hour", false); min = get("min", false); sec = get("sec", false); time = new DateTime(year, month, day, hour, min, sec); } else if (source is DateTime) { time = (DateTime)source; } else if (source is DateTimeOffset) { time = ((DateTimeOffset)source).LocalDateTime; } else if (source != null) { throw new ArgumentException("First argument to function 'os.time' must be a table."); } else { time = DateTime.Now; } return(time.Ticks); } string tmpname() { return(Path.GetTempFileName()); } }
protected override ILuaMultiValue InvokeInternal(ILuaMultiValue args) { Stream s = args[0].GetValue() as Stream; SeekOrigin origin = SeekOrigin.Current; long off = 0; if (s == null) { ILuaTable table = args[0] as ILuaTable; if (table != null) { s = table.GetItemRaw(_stream) as Stream; } if (s == null) { throw new ArgumentException("First real argument to function file:seek must be a file-stream, make sure to use file:seek."); } } if (args.Count > 1) { string str = args[1].GetValue() as string; if (str == "set") { origin = SeekOrigin.Begin; } else if (str == "cur") { origin = SeekOrigin.Current; } else if (str == "end") { origin = SeekOrigin.End; } else { throw new ArgumentException("First argument to function file:seek must be a string."); } if (args.Count > 2) { object obj = args[2].GetValue(); if (obj is double) { off = Convert.ToInt64((double)obj); } else { throw new ArgumentException("Second argument to function file:seek must be a number."); } } } if (!s.CanSeek) { return(Environment.Runtime.CreateMultiValueFromObj(null, "Specified stream cannot be seeked.")); } try { return(Environment.Runtime.CreateMultiValueFromObj(Convert.ToDouble(s.Seek(off, origin)))); } catch (Exception e) { return(Environment.Runtime.CreateMultiValueFromObj(null, e.Message, e)); } }
static object[] _ipairs_itr(ILuaTable table, double index) { if (index < 0 || index % 1 != 0) throw new ArgumentException("Second argument to function 'ipairs iterator' must be a positive integer."); index++; ILuaValue ret = table.GetItemRaw(new LuaNumber(index)); if (ret == null || ret == LuaNil.Nil) return new object[0]; else return new object[] { index, ret }; }
static ILuaValue rawget(ILuaTable table, ILuaValue index) { return table.GetItemRaw(index); }
static ILuaValue rawget(ILuaTable table, ILuaValue index) { return(table.GetItemRaw(index)); }