Inheritance: IDynamicMetaObjectProvider
Exemplo n.º 1
0
        public override void Setup(LuaTable table)
        {
            table.SetConstant("config", ConfigStr);

            table.SetConstant("cpath",
                Environment.GetEnvironmentVariable("LUA_CPATH_5_2") ??
                    Environment.GetEnvironmentVariable("LUA_CPATH") ??
                        String.Join(";", new[]
                        {
                            "!\\?.dll",
                            "!\\loadall.dll",
                            ".\\?.dll"
                        }));

            table.SetConstant("path",
                Environment.GetEnvironmentVariable("LUA_PATH_5_2") ??
                    Environment.GetEnvironmentVariable("LUA_PATH") ??
                        String.Join(";", new[]
                        {
                            "!\\lua\\" + "?.lua",
                            "!\\lua\\" + "?\\init.lua",
                            "!\\" + "?.lua",
                            "!\\" + "?\\init.lua",
                            ".\\?.lua"
                        }));

            table.SetConstant("loaded", new LuaTable(Context));
            table.SetConstant("preload", new LuaTable(Context));
            table.SetConstant("searchers", new LuaTable(Context)); // TODO: fill with searchers

            table.SetConstant("loadlib", (Func<string, string, object>)Loadlib);
            table.SetConstant("searchpath", (Func<string, string, string, string, object>) SearchPath);
        }
Exemplo n.º 2
0
        public static double Time(LuaTable t)
        {
            //if (t == null)
            //{
                return (DateTime.UtcNow - Epoch).TotalSeconds;
            //}

            //throw new NotImplementedException();
        }
Exemplo n.º 3
0
        public override void Setup(LuaTable table)
        {
            table.SetConstant("len", (Func<string, double>) (s => s.Length));
            table.SetConstant("upper", (Func<string, string>) (s => s.ToUpperInvariant()));
            table.SetConstant("lower", (Func<string, string>)(s => s.ToLowerInvariant()));
            table.SetConstant("rep", (Func<string, double, string>) ((s, r) => s.Repeat((int)Math.Round(r, MidpointRounding.ToEven))));

            table.SetConstant("sub", (Func<string, double, double, string>)Subst); // TODO: varargs
            table.SetConstant("char", (Func<double[], string>) Char); // TODO: varargs
            table.SetConstant("byte", (Func<string, double, double, double[]>) Byte); // TODO: varargs
        }
Exemplo n.º 4
0
        public static Varargs IPairs(LuaTable t)
        {
            var length = t.Length();
            Func<double, object> func =
                index =>
                    {
                        index++;
                        return index > length ? null : new Varargs(index, t.GetValue(index));
                    };

            return new Varargs(func, t, 0.0);
        }
Exemplo n.º 5
0
        public LuaTable ImportType(Type type, bool generateNamespaces)
        {
            if (generateNamespaces)
            {
                string[] typeNameParts = type.FullName.Split('.');
                string rootNamespace = typeNameParts.First();
                string typeName = typeNameParts.Last();
                typeNameParts = typeNameParts.Skip(1).Reverse().Skip(1).Reverse().ToArray();

                IDictionary<string,object> current = null;

                var storage = Context.EngineGlobals.Storage as IDictionary<string,object>;

                if (storage.ContainsKey(rootNamespace))
                    current = storage[rootNamespace] as IDictionary<string,object>;
                else
                {
                    current = new LuaTable(Context);
                    storage.AddOrSet(rootNamespace, current);
                }

                if (current == null)
                    throw LuaRuntimeException.Create(Context, "Another variable is obscuring the type's required namespace name ({0})", rootNamespace);

                string soFar = rootNamespace;

                foreach (var part in typeNameParts)
                {
                    if (current == null)
                        throw LuaRuntimeException.Create(Context, "Another variable is obscuring the type's required namespace name ({0}.{1})", soFar, part);

                    else if (!current.ContainsKey(part))
                    {
                        LuaTable newTable = new LuaTable(Context);
                        current.Add(part, newTable);
                        current = newTable;
                    }
                    else
                        current = current[part] as IDictionary<string,object>;

                    soFar += "." + part;
                }

                var typeTable = GetTypeTable(type);
                current.AddOrSet(typeName, typeTable);

                Context.Language.SetTypeMetatable(type, InteropMetatable);
                return typeTable;
            }

            Context.Language.SetTypeMetatable(type, InteropMetatable);
            return GetTypeTable(type);
        }
Exemplo n.º 6
0
        public override void Setup(LuaTable table)
        {
            table.SetConstant("time", (Func<LuaTable, double>)Time );
            table.SetConstant("difftime", (Func<double, double, double>) ((t2, t1) => t2 - t1));

            //table.SetConstant("date", (Func<object, object>)Date); // TODO

            table.SetConstant("exit", (Action<double>)(e => Environment.Exit((int)e)));
            table.SetConstant("getenv", (Func<string, string>) Environment.GetEnvironmentVariable);

            table.SetConstant("remove", (Func<string, object>) Delete);
            table.SetConstant("rename", (Func<string, string, object>) Rename);
        }
Exemplo n.º 7
0
        public override void Setup(LuaTable table)
        {
            const double Math_Tau = 2.0 * Math.PI; // http://tauday.com

            table.SetConstant("huge", Double.MaxValue);

            // Basic operations
            table.SetConstant("abs", (Func<double, double>)Math.Abs);
            table.SetConstant("mod", (Func<double, double, double>) ((a, b) => a%b));
            table.SetConstant("modf", (Func<double, double, Varargs>)((a, b) =>
            {
                long r;
                long q = Math.DivRem((long) a, (long) b, out r);
                return new Varargs(q, r);
            }));
            table.SetConstant("floor", (Func<double, double>) Math.Floor);
            table.SetConstant("ceil", (Func<double, double>) Math.Ceiling);
            table.SetConstant("min", (Func<double, double, double>) Math.Min);
            table.SetConstant("max", (Func<double, double, double>) Math.Max);

            // Exponetial and logarithmic
            table.SetConstant("sqrt", (Func<double, double>) Math.Sqrt);
            table.SetConstant("pow", (Func<double, double, double>) Math.Pow);
            table.SetConstant("exp", (Func<double, double>) Math.Exp);
            table.SetConstant("log", (Func<double, double>) Math.Log);
            table.SetConstant("log10", (Func<double, double>) Math.Log10);

            // Trigonometrical
            table.SetConstant("pi", Math.PI);
            table.SetConstant("tau", Math_Tau);
            table.SetConstant("deg", (Func<double, double>)(r => r * 360.0 / Math_Tau));
            table.SetConstant("rad", (Func<double, double>)(d => d / 360.0 * Math_Tau));
            table.SetConstant("cos", (Func<double, double>) Math.Cos);
            table.SetConstant("sin", (Func<double, double>) Math.Sin);
            table.SetConstant("tan", (Func<double, double>)Math.Tan);
            table.SetConstant("acos", (Func<double, double>)Math.Acos);
            table.SetConstant("asin", (Func<double, double>)Math.Asin);
            table.SetConstant("atan", (Func<double, double>)Math.Atan);
            table.SetConstant("atan2", (Func<double, double, double>)Math.Atan2);

            // Splitting on powers of 2
            //table.SetConstant("frexp", (Func<double, double>) Math.??);
            //table.SetConstant("ldexp", (Func<double, double, double>) Math.??);

            // Pseudo-random numbers
            table.SetConstant("randomseed", (Func<double, double>)(x => { rand = new Random((int)x); return rand.NextDouble(); }));
            table.SetConstant("random", (Func<double, double, double>)((min,max) => { return (double)rand.Next((int)min,(int)max); })); // overloaded
        }
Exemplo n.º 8
0
        public LuaTable ImportType(Type type, bool generateNamespaces)
        {
            if (generateNamespaces)
            {
                string[] typeNameParts = type.FullName.Split('.');
                string rootNamespace = typeNameParts.First();
                string typeName = typeNameParts.Last();
                typeNameParts = typeNameParts.Skip(1).Reverse().Skip(1).Reverse().ToArray();

                LuaTable current = null;

                if (Context.Globals.HasValue(rootNamespace))
                    current = Context.Globals.GetValue(rootNamespace) as LuaTable;
                else
                {
                    current = new LuaTable(Context);
                    Context.Globals.SetConstant(rootNamespace, current);
                }

                if (current == null)
                    throw new LuaRuntimeException(Context, "Another variable is obscuring the type's required namespace name ({0})", rootNamespace);

                string soFar = rootNamespace;

                foreach (var part in typeNameParts)
                {
                    if (current == null)
                        throw new LuaRuntimeException(Context, "Another variable is obscuring the type's required namespace name ({0}.{1})", soFar, part);

                    else if (!current.HasValue(part))
                    {
                        LuaTable newTable = new LuaTable(Context);
                        current.SetConstant(part, newTable);
                        current = newTable;
                    }
                    else
                        current = current.GetValue(part) as LuaTable;

                    soFar += "." + part;
                }

                var typeTable = GetTypeTable(type);
                current.SetConstant(typeName, typeTable);
                return typeTable;
            }

            return GetTypeTable(type);
        }
Exemplo n.º 9
0
 public static Varargs Next(LuaTable table, object index = null)
 {
     return table.Next(index);
 }
Exemplo n.º 10
0
 public static Expr Call(LuaTable context, DynamicMetaObject target, DynamicMetaObject[] args)
 {
     return Call(Expr.Property(Expr.Constant(context, typeof(LuaTable)), "Context"), target, args);
 }
Exemplo n.º 11
0
 public override void Setup(LuaTable table)
 {
     table.SetValue("assert", (Func<bool, object, object[], Varargs>)Assert);
     table.SetValue("collectgarbage", (Action<string, string>)CollectGarbage);
     table.SetValue("dofile", (Func<string, object>)DoFile);
     table.SetValue("error", (Action<string, object>)Error);
     table.SetValue("_G", table);
     table.SetValue("getfenv", (Func<object, object>)GetFEnv);
     table.SetValue("getmetatable", (Func<object, object>)GetMetatable);
     table.SetValue("ipairs", (Func<LuaTable, Varargs>)IPairs);
     table.SetValue("load", (Func<Delegate, string, Varargs>)Load);
     table.SetValue("loadfile", (Func<string, Varargs>)LoadFile);
     table.SetValue("loadstring", (Func<string, string, Varargs>)LoadString);
     table.SetValue("next", (Func<LuaTable, object, Varargs>)Next);
     table.SetValue("pairs", (Func<LuaTable, Varargs>)Pairs);
     table.SetValue("pcall", (Func<Delegate, object[], Varargs>)PCall);
     table.SetValue("print", (Action<object[]>)Print);
     table.SetValue("rawequal", (Func<object, object, bool>)RawEqual);
     table.SetValue("rawget", (Func<LuaTable, object, object>)RawGet);
     table.SetValue("rawset", (Func<LuaTable, object, object, object>)RawSet);
     table.SetValue("select", (Func<object, object[], Varargs>)Select);
     table.SetValue("setfenv", (Func<object, LuaTable, object>)SetFEnv);
     table.SetValue("setmetatable", (Func<LuaTable, LuaTable, LuaTable>)SetMetatable);
     table.SetValue("tonumber", (Func<string, object, object>)ToNumber);
     table.SetValue("tostring", (Func<object, object>)ToString);
     table.SetValue("type", (Func<object, string>)Type);
     table.SetValue("unpack", (Func<LuaTable, object, object, Varargs>)Unpack);
     table.SetValue("_VERSION", Constant.LUA_VERSION);
     table.SetValue("xpcall", (Func<Delegate, Delegate, Varargs>)XPCall);
 }
Exemplo n.º 12
0
 public object SetFEnv(object f, LuaTable table)
 {
     throw new LuaRuntimeException(ExceptionMessage.FUNCTION_NOT_IMPLEMENTED);
 }
Exemplo n.º 13
0
 internal static DynamicMetaObject CreateDMO(LuaTable table, string message = null, params object[] args)
 {
     return CreateDMO(Expression.Property(Expression.Constant(table, typeof(LuaTable)), "Context"), message, args);
 }
Exemplo n.º 14
0
        public object SetFEnv(object f, LuaTable table)
        {
            if (f is double)
            {
                //stack level
                var function = Context.GetFunction(Convert.ToInt32(f) - 1);
                Context.SetFunctionEnvironment(function.ExecScope, table);
            }
            else if (f is Delegate)
                Context.SetFunctionEnvironment(f as Delegate, table);
            else
                throw LuaRuntimeException.Create(Context, ExceptionMessage.FUNCTION_NOT_IMPLEMENTED);

            return table;
        }
Exemplo n.º 15
0
 public object RawGet(LuaTable table, object index)
 {
     return table.GetValue(index);
 }
Exemplo n.º 16
0
        public Varargs Unpack(LuaTable list, object i = null, object j = null)
        {
            var listLength = list.Length();

            var startIndex = ConvertToNumber(i, 2, 1.0);
            var length = ConvertToNumber(j, 3, listLength);

            if (startIndex < 1)
                return Varargs.Empty;
            length = Math.Min(length, listLength - startIndex + 1);

            var array = new object[(int)length];
            var arrayIndex = 0;
            for (var k = startIndex; k < startIndex + length; k++)
                array[arrayIndex++] = list.GetValue(k);

            return new Varargs(array);
        }
Exemplo n.º 17
0
        private object InteropGetMethod(object target, string methodName)
        {
            if (target is LuaTable)
            {
                var type = (target as LuaTable).GetValue("__clrtype") as Type;
                var methods = type.GetMethods(BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance)
                    .Where(x => x.Name.Equals(methodName)).ToArray();
                if (methods.Length > 0)
                {
                    var methodTable = new LuaTable(Context);
                    methodTable.SetConstant("__target", null);
                    methodTable.SetConstant("__clrtype", type);
                    methodTable.SetConstant("__method", methodName);
                    foreach (var method in methods)
                        methodTable.SetConstant(method, method);
                    methodTable.Metatable = GenerateMethodMetaTable();

                    return methodTable;
                }

            }
            else
            {
                var type = target.GetType();

                var methods = type.GetMethods(BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance)
                    .Where(x => x.Name.Equals(methodName)).ToArray();
                if (methods.Length > 0)
                {
                    var methodTable = new LuaTable(Context);
                    methodTable.SetConstant("__target", target);
                    methodTable.SetConstant("__clrtype", type);
                    methodTable.SetConstant("__method", methodName);
                    foreach (var method in methods)
                        methodTable.SetConstant(method, method);
                    methodTable.Metatable = GenerateMethodMetaTable();

                    return methodTable;
                }
            }
            return null;
        }
Exemplo n.º 18
0
 public InteropLibrary(CodeContext context, params Type[] types)
     : base(context)
 {
     InteropMetatable = GenerateMetatable(context);
 }
Exemplo n.º 19
0
 private LuaTable GenerateMethodMetaTable()
 {
     var table = new LuaTable(Context);
     table.SetConstant(Constant.CALL_METAMETHOD, (Func<object, Varargs, object>)MethodInteropCall);
     table.SetConstant(Constant.TOSTRING_METAFIELD, (Func<LuaTable, string>)MethodTableToString);
     return table;
 }
Exemplo n.º 20
0
        internal LuaTable GetTypeTable(Type type)
        {
            if (type != null)
            {
                var table = new LuaTable(Context);
                table.SetConstant("__clrtype", type);
                table.Metatable = InteropMetatable;

                return table;
            }
            return null;
        }
Exemplo n.º 21
0
        internal LuaTable GenerateMetatable(CodeContext context)
        {
            LuaTable table = new LuaTable(context);
            table.SetConstant(Constant.INDEX_METAMETHOD, (Func<object, object, object>)InteropIndex);
            table.SetConstant(Constant.NEWINDEX_METAMETHOD, (Func<object, object, object, object>)InteropNewIndex);
            table.SetConstant(Constant.CALL_METAMETHOD, (Func<object, object[], object>)InteropCall);
            table.SetConstant(Constant.CONCAT_METAMETHOD, (Func<string, object, string>)Concat);
            table.SetConstant(Constant.TOSTRING_METAFIELD, (Func<object, string>)ToString);
            table.SetConstant(Constant.LENGTH_METAMETHOD, (Func<object, object>)InteropLength);

            return table;
        }
Exemplo n.º 22
0
        public Varargs Next(LuaTable table, object index = null)
        {
            if (table == null)
                throw LuaRuntimeException.Create(Context, ExceptionMessage.INVOKE_BAD_ARGUMENT_GOT, "next", "table", "nil");

            return table.Next(index);
        }
Exemplo n.º 23
0
 public Varargs Pairs(LuaTable t)
 {
     return new Varargs((Func<LuaTable, object, Varargs>)Next, t, null);
 }
Exemplo n.º 24
0
 public static Expr NewIndex(LuaTable context, DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject value)
 {
     return NewIndex(Expr.Property(Expr.Constant(context, typeof(LuaTable)), "Context"), target, indexes, value);
 }
Exemplo n.º 25
0
 public LuaTable RawSet(LuaTable table, object index, object value)
 {
     table.SetValue(index, value);
     return table;
 }
Exemplo n.º 26
0
 public override void Setup(LuaTable table)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 27
0
        public LuaTable SetMetatable(LuaTable table, LuaTable metatable)
        {
            if (table.Metatable != null && table.Metatable.GetValue(Constant.METATABLE_METAFIELD) != null)
                throw LuaRuntimeException.Create(Context, ExceptionMessage.PROTECTED_METATABLE);

            table.Metatable = metatable;
            return table;
        }
Exemplo n.º 28
0
 internal static DynamicMetaObject CreateDMO(LuaTable table, string message= null, Exception inner = null)
 {
     return CreateDMO(Expression.Property(Expression.Constant(table, typeof(LuaTable)), "Context"), message, inner);
 }
Exemplo n.º 29
0
 public abstract void Setup(LuaTable table);
Exemplo n.º 30
0
 private string MethodTableToString(LuaTable table)
 {
     return string.Format("{0}.{1}(...)", (table.GetValue("__clrtype") as Type).FullName, table.GetValue("__method"));
 }