Exemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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.º 6
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.º 7
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.º 8
0
 public override void Setup(LuaTable table)
 {
     table.SetConstant("assert", (Func<object, object, object[], Varargs>)Assert);
     table.SetConstant("collectgarbage", (Action<string, string>)CollectGarbage);
     table.SetConstant("dofile", (Func<string, object>)DoFile);
     table.SetConstant("error", (Action<object, object>)Error);
     table.SetValue("_ENV", table);
     table.SetConstant("_G", table);
     table.SetConstant("getfenv", (Func<object, object>)GetFEnv);
     table.SetConstant("getmetatable", (Func<object, object>)GetMetatable);
     table.SetConstant("ipairs", (Func<LuaTable, Varargs>)IPairs);
     table.SetConstant("load", (Func<Delegate, string, Varargs>)Load);
     table.SetConstant("loadfile", (Func<string, Varargs>)LoadFile);
     table.SetConstant("loadstring", (Func<string, string, Varargs>)LoadString);
     table.SetConstant("next", (Func<LuaTable, object, Varargs>)Next);
     table.SetConstant("pairs", (Func<LuaTable, Varargs>)Pairs);
     table.SetConstant("pcall", (Func<Delegate, object[], Varargs>)PCall);
     table.SetConstant("print", (Action<object[]>)Print);
     table.SetConstant("rawequal", (Func<object, object, bool>)RawEqual);
     table.SetConstant("rawget", (Func<LuaTable, object, object>)RawGet);
     table.SetConstant("rawset", (Func<LuaTable, object, object, object>)RawSet);
     table.SetConstant("select", (Func<object, object[], Varargs>)Select);
     table.SetConstant("setfenv", (Func<object, LuaTable, object>)SetFEnv);
     table.SetConstant("setmetatable", (Func<LuaTable, LuaTable, LuaTable>)SetMetatable);
     table.SetConstant("tonumber", (Func<object, object, object>)ToNumber);
     table.SetConstant("tostring", (Func<object, object>)ToStringEx);
     table.SetConstant("type", (Func<object, string>)Type);
     table.SetConstant("unpack", (Func<LuaTable, object, object, Varargs>)Unpack);
     table.SetConstant("_VERSION", Constant.LUA_VERSION);
     table.SetConstant("xpcall", (Func<Delegate, Delegate, Varargs>)XPCall);
 }
Exemplo n.º 9
0
        private LuaTable GetTypeTable(Type type)
        {
            if (type != null)
            {
                var table = new LuaTable(Context);
                table.SetConstant("__clrtype", type);
                table.Metatable = GenerateMetatable();

                Context.SetTypeMetatable(type, table.Metatable);

                return table;
            }
            return null;
        }
Exemplo n.º 10
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
        }