示例#1
0
        public static LuaTable GetControlMetaTable()
        {
            if (controlMetaTable == null)
            {
                controlMetaTable = new LuaTable();

                controlMetaTable.SetNameValue("__index", new LuaFunction((values) =>
                {
                    LuaUserdata control = values[0] as LuaUserdata;
                    Type type           = control.Value.GetType();

                    LuaString member = values[1] as LuaString;
                    if (member != null)
                    {
                        return(GetMemberValue(control.Value, type, member.Text));
                    }

                    LuaNumber index = values[1] as LuaNumber;
                    if (index != null)
                    {
                        return(GetIndexerValue(control.Value, type, index.Number));
                    }

                    return(LuaNil.Nil);
                }));

                controlMetaTable.SetNameValue("__newindex", new LuaFunction((values) =>
                {
                    LuaUserdata control = values[0] as LuaUserdata;
                    LuaString member    = values[1] as LuaString;
                    LuaValue value      = values[2];

                    Type type = control.Value.GetType();
                    SetMemberValue(control.Value, type, member.Text, value.Value);
                    return(null);
                }));
            }

            return(controlMetaTable);
        }
示例#2
0
        /// <summary>
        /// A REPL (Read, Eval, Print, Loop function)
        /// </summary>
        /// <param name="args">Application startup args</param>
        public static void REPL(string[] args)
        {
            // Create global variables
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            GlobalEnvironment = LuaRuntime.CreateGlobalEnviroment();
            PrintBanner();
            // how to handle errors
            #if DEBUG
            GlobalEnvironment.SetNameValue("DEBUG", LuaBoolean.True);
            #else
            GlobalEnvironment.SetNameValue("DEBUG", LuaBoolean.False);
            #endif

            Prompt = "> ";
            // load startup scripts
            LoadFiles();

            // check command line args
            if (args.Length > 0)
            {
                string file = args[0];
                if (File.Exists(file))
                {
                    try
                    {
                        GlobalEnvironment.SetNameValue("_WORKDIR", new LuaString(Path.GetDirectoryName(file)));
                        if (file.EndsWith(".out") || file.EndsWith(".luac") || file.EndsWith(".sluac"))
                        {
                            Chunk c = Serializer.Deserialize(file) as Chunk;
                            c.Enviroment = GlobalEnvironment;
                            bool isBreak;
                            c.Execute(GlobalEnvironment, out isBreak).ToString();
                        }
                        else
                        {
                            LuaRuntime.RunFile(file, GlobalEnvironment);
                        }
                        // it loaded successfully
                        if (args.Length > 1) // possibly interactive mode after
                        {
                            Console.WriteLine("Loaded file '" + Path.GetFileName(file) + "'");
                        }
                    }
                    catch (Exception error)
                    {
                        Console.WriteLine(error.Message);
                    }
                    //Console.ReadKey(true);
                    //return;
                    // instead, go to interactive
                }
                else
                {
                    Console.WriteLine(file + " not found.");
                }
            }

            // check for interactive mode
            foreach (string arg in args)
            {
                if (arg.ToUpper() == "-I")
                {
                    GoInteractive = true;
                }
            }

            if (args.Length == 0)
            {
                GoInteractive = true;
            }

            if (GoInteractive)
            {
                while (true)
                {
                    Console.Write(Prompt);
                    string line = Console.ReadLine();

                    if (line == "quit" || line == "exit" || line == "bye")
                    {
                        break;
                    }
                    else
                    {
                        try
                        {
                            LuaValue v = Run(line, GlobalEnvironment);
                            if (v == null)
                            {
                                Console.WriteLine("=> [no returned value]");
                            }
                            else
                            {
                                Console.WriteLine("=> " + v.ToString());
                            }
                        }
                        catch (Parser.ParserException error)
                        {
                            // create spacing
                            StringBuilder sb = new StringBuilder();
                            for (int i = 0; i < Prompt.Length; i++)
                            {
                                sb.Append(" ");
                            }
                            for (int i = 0; i < error.FirstErrorIndex; i++)
                            {
                                sb.Append(" ");
                            }

                            Console.WriteLine(sb.ToString() + "^");
                            Console.WriteLine("Error: " + error.Message);
                            GlobalEnvironment.SetNameValue("LASTERROR", ObjectToLua.ToLuaValue(error));
                        }
                        catch (Exception error)
                        {
                            if (((LuaBoolean)GlobalEnvironment.GetValue(GlobalEnvironment.GetKey("DEBUG"))) == LuaBoolean.True)
                            {
                                Console.WriteLine(error.ToString());
                            }
                            else
                            {
                                Console.WriteLine("Error: " + error.Message);
                            }
                            GlobalEnvironment.SetNameValue("LASTERROR", ObjectToLua.ToLuaValue(error));
                        }
                    }
                }
            }
        }
示例#3
0
        /// <summary>
        /// Creates a global environment with all the base modules registered and
        /// some default values set.
        /// </summary>
        /// <returns></returns>
        public static LuaTable CreateGlobalEnviroment(bool createBaseLib       = true,
                                                      bool createStringLib     = true,
                                                      bool createTableLib      = true,
                                                      bool createOSLib         = true,
                                                      bool createIOLib         = true,
                                                      bool createFileLib       = true,
                                                      bool createMathLib       = true,
                                                      bool createScriptLib     = true,
                                                      bool createWinFormsLib   = true,
                                                      bool createConsoleLib    = true,
                                                      bool createCoroutineLib  = true,
                                                      bool createPackageLib    = true,
                                                      bool createClassLib      = true,
                                                      bool createFileSystemLib = true)
        {
            LuaTable global = new LuaTable();

            // Register Lua Modules

            if (createBaseLib)
            {
                BaseLib.RegisterFunctions(global);
            }
            if (createStringLib)
            {
                StringLib.RegisterModule(global);
            }
            if (createTableLib)
            {
                TableLib.RegisterModule(global);
            }
            if (createIOLib)
            {
                IOLib.RegisterModule(global);
            }
            if (createFileLib)
            {
                FileLib.RegisterModule(global);
            }
            if (createMathLib)
            {
                MathLib.RegisterModule(global);
            }
            if (createOSLib)
            {
                OSLib.RegisterModule(global);
            }
            if (createScriptLib)
            {
                ScriptLib.RegisterModule(global);
            }
            //if (createWinFormsLib)
            //    WinFormLib.RegisterModule(global);
            if (createConsoleLib)
            {
                ConsoleLib.RegisterModule(global);
            }
            if (createCoroutineLib)
            {
                CoroutineLib.RegisterModule(global);
            }
            if (createPackageLib)
            {
                PackageLib.RegisterModule(global);
            }
            if (createClassLib)
            {
                ClassLib.RegisterModule(global);
            }
            if (createFileSystemLib)
            {
                FileSystemLib.RegisterModule(global);
            }

            //global.SetNameValue("_WORKDIR", new LuaString(Application.StartupPath + "\\"));
            global.SetNameValue("_VERSION", new LuaString("Sharp Lua 1.1"));
            global.SetNameValue("_G", global);

            if (createPackageLib)
            {
                // set package.preload table
                LuaTable preload = (LuaTable)(global.GetValue("package") as LuaTable).GetValue("preload");
                if (createStringLib)
                {
                    preload.SetNameValue("string", (LuaTable)global.GetValue("string"));
                }
                if (createTableLib)
                {
                    preload.SetNameValue("table", (LuaTable)global.GetValue("table"));
                }
                if (createIOLib)
                {
                    preload.SetNameValue("io", (LuaTable)global.GetValue("io"));
                }
                if (createFileLib)
                {
                    preload.SetNameValue("file", (LuaTable)global.GetValue("file"));
                }
                if (createMathLib)
                {
                    preload.SetNameValue("math", (LuaTable)global.GetValue("math"));
                }
                if (createOSLib)
                {
                    preload.SetNameValue("os", (LuaTable)global.GetValue("os"));
                }
                if (createScriptLib)
                {
                    preload.SetNameValue("script", (LuaTable)global.GetValue("script"));
                }
                //if (createWinFormsLib)
                //    preload.SetNameValue("WinForms", (LuaTable) global.GetValue("WinForms"));
                if (createConsoleLib)
                {
                    preload.SetNameValue("console", (LuaTable)global.GetValue("console"));
                }
                if (createCoroutineLib)
                {
                    preload.SetNameValue("coroutine", (LuaTable)global.GetValue("coroutine"));
                }
                if (createPackageLib) // wait a second...
                {
                    preload.SetNameValue("package", (LuaTable)global.GetValue("package"));
                }
                if (createClassLib)
                {
                    preload.SetNameValue("class", (LuaTable)global.GetValue("class"));
                }
                if (createFileSystemLib)
                {
                    preload.SetNameValue("filesystem", (LuaTable)global.GetValue("filesystem"));
                }
            }
            if (createFileSystemLib)
            {
                FileSystemLib.currentDir = global.GetValue("_WORKDIR").ToString();
            }

            GlobalEnvironment = global;
            return(global);
        }
示例#4
0
        public static LuaTable GetControlMetaTable(bool isStatic = false, Type classType = null)
        {
            LuaTable controlMetaTable = null;

            if (isStatic == false && instanceControlMetaTable == null || isStatic == true && controlMetaTables.ContainsKey(classType) == false)
            {
                controlMetaTable = new LuaTable();

                controlMetaTable.SetNameValue("__index", new LuaFunction((values) =>
                {
                    bool isStaticClass  = false;
                    LuaUserdata control = values[0] as LuaUserdata;
                    Type type;
                    if (control.MetaTable.ContainsKey(new LuaString("staticClassType")))
                    {
                        isStaticClass = true;
                        type          = values[0].MetaTable.GetValue("staticClassType").Value as Type;
                    }
                    else
                    {
                        type = control.Value.GetType();
                    }

                    LuaString member = values[1] as LuaString;
                    if (member != null)
                    {
                        return(GetMemberValue((isStaticClass == false ? control.Value : null), type, member.Text));
                    }

                    LuaNumber index = values[1] as LuaNumber;
                    if (index != null)
                    {
                        return(GetIndexerValue((isStaticClass == false ? control.Value : null), type, index.Number));
                    }

                    return(LuaNil.Nil);
                }));

                controlMetaTable.SetNameValue("__newindex", new LuaFunction((values) =>
                {
                    bool isStaticClass  = false;
                    LuaUserdata control = values[0] as LuaUserdata;
                    Type type;
                    if (control.MetaTable.ContainsKey(new LuaString("staticClassType")))
                    {
                        isStaticClass = true;
                        type          = values[0].MetaTable.GetValue("staticClassType").Value as Type;
                    }
                    else
                    {
                        type = control.Value.GetType();
                    }

                    LuaString member = values[1] as LuaString;
                    LuaValue value   = values[2];

                    SetMemberValue((isStaticClass == false ? control.Value : null), type, member.Text, value.Value);
                    return(null);
                }));

                if (isStatic == false)
                {
                    instanceControlMetaTable = controlMetaTable;
                }
                else
                {
                    controlMetaTables[classType] = controlMetaTable;
                }
            }

            if (isStatic == false)
            {
                return(instanceControlMetaTable);
            }
            else
            {
                return(controlMetaTables[classType]);
            }
        }