Ejemplo n.º 1
0
 public IrcScriptEngine()
 {
     Engine = new Lua (
         integerType: LuaIntegerType.Int32,
         floatType: LuaFloatType.Float
     );
     LuaEnvironment = Engine.CreateEnvironment<LuaGlobal> ();
 }
Ejemplo n.º 2
0
        }         // ctor

        internal LuaChunk LuaRequire(LuaGlobal global, string sModName)
        {
            if (String.IsNullOrEmpty(sModName))
            {
                return(null);
            }

            string   sFileName;
            DateTime dtStamp;

            if (LuaRequireFindFile(sModName, out sFileName, out dtStamp))
            {
                lock (packageLock)
                {
                    WeakReference rc;
                    LuaChunk      c;
                    string        sCacheId = sFileName + ";" + dtStamp.ToString("o");

                    // is the modul loaded
                    if (loadedModuls == null ||
                        !loadedModuls.TryGetValue(sCacheId, out rc) ||
                        !rc.IsAlive)
                    {
                        // compile the modul
                        c = global.Lua.CompileChunk(sFileName, compileOptions);

                        // Update Cache
                        if (loadedModuls == null)
                        {
                            loadedModuls = new Dictionary <string, WeakReference>();
                        }
                        loadedModuls[sCacheId] = new WeakReference(c);
                    }
                    else
                    {
                        c = (LuaChunk)rc.Target;
                    }

                    return(c);
                }
            }
            else
            {
                return(null);
            }
        }         // func LuaRequire
Ejemplo n.º 3
0
		} // ctor

		internal LuaChunk LuaRequire(LuaGlobal global, string sModName)
		{
			if (String.IsNullOrEmpty(sModName))
				return null;

			string sFileName;
			DateTime dtStamp;
			if (LuaRequireFindFile(sModName, out sFileName, out dtStamp))
			{
				lock (packageLock)
				{
					WeakReference rc;
					LuaChunk c;
					string sCacheId = sFileName + ";" + dtStamp.ToString("o");

					// is the modul loaded
					if (loadedModuls == null ||
						!loadedModuls.TryGetValue(sCacheId, out rc) ||
						!rc.IsAlive)
					{
						// compile the modul
						c = global.Lua.CompileChunk(sFileName, compileOptions);

						// Update Cache
						if (loadedModuls == null)
							loadedModuls = new Dictionary<string, WeakReference>();
						loadedModuls[sCacheId] = new WeakReference(c);
					}
					else
						c = (LuaChunk)rc.Target;

					return c;
				}
			}
			else
				return null;
		} // func LuaRequire
Ejemplo n.º 4
0
        public LuaInstance()
        {
            lua = new Lua();
            packages = new HashSet<string>();

            try
            {
                global = lua.CreateEnvironment();
                global["package.path"] = LuaConfig.PackagePath;

                LuaAerospike.LoadLibrary(this);
                LuaStream.LoadLibrary(this);

                Assembly assembly = Assembly.GetExecutingAssembly();
                LoadSystemPackage(assembly, "aslib");
                LoadSystemPackage(assembly, "stream_ops");
                LoadSystemPackage(assembly, "aerospike");
            }
            catch (Exception)
            {
                lua.Dispose();
                throw;
            }
        }
Ejemplo n.º 5
0
		/// <summary></summary>
		/// <param name="global"></param>
		public LuaLibraryPackage(LuaGlobal global)
		{
			this.loaded = new LuaLoadedTable(global);
			this.path = CurrentDirectoryPathVariable;
		} // ctor
Ejemplo n.º 6
0
        } // ctor

        public void Dispose()
        {
            chunk  = null;
            global = null;
        } // proc Dispose
Ejemplo n.º 7
0
 public LuaTask(LuaChunk chunk)
 {
     this.chunk  = chunk;
     this.global = new LuaGlobal(LuaTaskFactory.Lua);
 } // ctor
Ejemplo n.º 8
0
 } // ctor
 
 public void Dispose()
 {
   chunk = null;
   global = null;
 } // proc Dispose
Ejemplo n.º 9
0
 public LuaTask(LuaChunk chunk)
 {
   this.chunk = chunk;
   this.global = new LuaGlobal(LuaTaskFactory.Lua);
 } // ctor
Ejemplo n.º 10
0
        public bool Create()
        {
            ScriptEnvironment lastEnvironment = null, oldLastEnvironment = null;

            try
            {
                if (ms_luaState == null)
                {
                    ms_luaState = new Lua();

                    //ms_luaDebug = new LuaStackTraceDebugger();
                    ms_luaDebug = null;

                    if (Resource.Manager.Configuration.ScriptDebug)
                    {
                        ms_luaDebug = new LuaStackTraceDebugger();
                    }

                    ms_luaCompileOptions = new LuaCompileOptions();
                    ms_luaCompileOptions.DebugEngine = ms_luaDebug;

                    ms_initChunks = new []
                    {
                        ms_luaState.CompileChunk("system/MessagePack.lua", ms_luaCompileOptions),
                        ms_luaState.CompileChunk("system/dkjson.lua", ms_luaCompileOptions),
                        ms_luaState.CompileChunk("system/resource_init.lua", ms_luaCompileOptions)
                    };
                }

                m_luaEnvironment = ms_luaState.CreateEnvironment();

                foreach (var func in ms_luaFunctions)
                {
                    //m_luaEnvironment[func.Key] = Delegate.CreateDelegate
                    var parameters = func.Value.GetParameters()
                                    .Select(p => p.ParameterType)
                                    .Concat(new Type[] { func.Value.ReturnType })
                                    .ToArray();

                    var delegType = Expression.GetDelegateType
                    (
                        parameters
                    );

                    var deleg = Delegate.CreateDelegate
                    (
                        delegType,
                        null,
                        func.Value
                    );

                    var expParameters = parameters.Take(parameters.Count() - 1).Select(a => Expression.Parameter(a)).ToArray();

                    var pushedEnvironment = Expression.Variable(typeof(PushedEnvironment));

                    Expression<Func<PushedEnvironment>> preFunc = () => PushEnvironment(this);
                    Expression<Action<PushedEnvironment>> postFunc = env => env.PopEnvironment();

                    Expression body;

                    if (func.Value.ReturnType.Name != "Void")
                    {
                        var retval = Expression.Variable(func.Value.ReturnType);

                        body = Expression.Block
                        (
                            func.Value.ReturnType,
                            new[] { retval, pushedEnvironment },

                            Expression.Assign
                            (
                                pushedEnvironment,
                                Expression.Invoke(preFunc)
                            ),

                            Expression.Assign
                            (
                                retval,
                                Expression.Call(func.Value, expParameters)
                            ),

                            Expression.Invoke(postFunc, pushedEnvironment),

                            retval
                        );
                    }
                    else
                    {
                        body = Expression.Block
                        (
                            func.Value.ReturnType,
                            new[] { pushedEnvironment },

                            Expression.Assign
                            (
                                pushedEnvironment,
                                Expression.Invoke(preFunc)
                            ),

                            Expression.Call(func.Value, expParameters),

                            Expression.Invoke(postFunc, pushedEnvironment)
                        );
                    }

                    var lambda = Expression.Lambda(delegType, body, expParameters);

                    m_luaEnvironment[func.Key] = lambda.Compile();
                }

                InitHandler = null;

                /*m_luaNative = LuaL.LuaLNewState();
                LuaL.LuaLOpenLibs(m_luaNative);

                LuaLib.LuaNewTable(m_luaNative);
                LuaLib.LuaSetGlobal(m_luaNative, "luanet");

                InitHandler = null;

                m_luaState = new NLua.Lua(m_luaNative);*/

                lock (m_luaEnvironment)
                {
                    lastEnvironment = ms_currentEnvironment;
                    ms_currentEnvironment = this;

                    oldLastEnvironment = LastEnvironment;
                    LastEnvironment = lastEnvironment;

                    // load global data files
                    foreach (var chunk in ms_initChunks)
                    {
                        m_luaEnvironment.DoChunk(chunk);
                    }
                }

                return true;
            }
            catch (Exception e)
            {
                this.Log().Error(() => "Error creating script environment for resource " + m_resource.Name + ": " + e.Message, e);

                if (e.InnerException != null)
                {
                    this.Log().Error(() => "Inner exception: " + e.InnerException.Message, e.InnerException);
                }

                PrintLuaStackTrace(e);
            }
            finally
            {
                ms_currentEnvironment = lastEnvironment;
                LastEnvironment = oldLastEnvironment;
            }

            return false;
        }
Ejemplo n.º 11
0
 /// <summary></summary>
 /// <param name="global"></param>
 public LuaLibraryPackage(LuaGlobal global)
 {
     this.loaded = new LuaLoadedTable(global);
     this.path   = CurrentDirectoryPathVariable;
 }         // ctor
Ejemplo n.º 12
0
        public static void Main(string[] args)
        {
            int iCurrentArg = 0;

            WriteText(ConsoleColor.Gray, "NeoLua Interactive Command"); Console.WriteLine();
            WriteText(ConsoleColor.DarkGray, "Version ");
            WriteText(ConsoleColor.White, String.Format("{0} ({1})", LuaGlobal.VersionString, Lua.Version));
            WriteText(ConsoleColor.DarkGray, " by neolithos");
            Console.WriteLine();
            Console.WriteLine();
            WriteText(ConsoleColor.DarkGray, "  source code at ");
            WriteText(ConsoleColor.Gray, "http://neolua.codeplex.com");
            Console.WriteLine();
            WriteText(ConsoleColor.DarkGray, "  supported from ");
            WriteText(ConsoleColor.Gray, "http://tecware-gmbh.de");
            Console.WriteLine();
            Console.WriteLine();
            WriteText(ConsoleColor.DarkGray, "  Write ':h' for help.");
            Console.WriteLine();
            Console.WriteLine();

            global = lua.CreateEnvironment <LuaCommandGlobal>();

            // change to the samples directory
#if DEBUG
            string sSamples = Path.GetFullPath(@"..\..\Samples");
            Debug.Listeners.Add(new ConsoleTraceListener());
#else
            string sSamples = Path.GetFullPath("Samples");
#endif
            if (Directory.Exists(sSamples))
            {
                Environment.CurrentDirectory = sSamples;
            }

            while (true)
            {
                string   sLine;
                Commands cmd;

                if (iCurrentArg < args.Length)
                {
                    cmd = ParseArgument(args[iCurrentArg++], out sLine);
                }
                else
                {
                    cmd = InputCommand(out sLine);
                }

                switch (cmd)
                {
                case Commands.List:
                    // list all variables in global
                    WriteText(ConsoleColor.DarkYellow, "List global:");
                    Console.WriteLine();
                    foreach (var c in global)
                    {
                        WriteVariable(c.Key, c.Value);
                    }
                    Console.WriteLine();
                    break;

                case Commands.Load:
                    RunScript(() => File.ReadAllText(Path.GetFullPath(sLine)), Path.GetFileName(sLine));
                    break;

                case Commands.Debug:
                    if (sLine == "trace")
                    {
                        WriteText(ConsoleColor.DarkYellow, "Compile emits traceline code, now."); Console.WriteLine();
                        debugEngine = debugConsole;
                    }
                    else if (sLine == Boolean.TrueString)
                    {
                        WriteText(ConsoleColor.DarkYellow, "Compile emits stack trace information and runtime functions, now."); Console.WriteLine();
                        debugEngine = LuaStackTraceDebugger.Default;
                    }
                    else
                    {
                        WriteText(ConsoleColor.DarkYellow, "Compile creates dynamic functions, now."); Console.WriteLine();
                        debugEngine = null;
                    }
                    Console.WriteLine();
                    break;

                case Commands.Environment:
                    WriteText(ConsoleColor.DarkYellow, "New environment created."); Console.WriteLine();
                    Console.WriteLine();
                    global = lua.CreateEnvironment <LuaCommandGlobal>();
                    break;

                case Commands.Cache:
                    lua.DumpRuleCaches(Console.Out);
                    Console.WriteLine();
                    break;

                case Commands.Help:
                    WriteText(ConsoleColor.DarkYellow, "Commands:"); Console.WriteLine();
                    WriteCommand(":q", "Exit the application.");
                    WriteCommand(":list", "Lists all global variables.");
                    WriteCommand(":load", "Loads the lua-script from a file.");
                    WriteCommand(":debugoff", "Tell the compiler to emit no debug informations.");
                    WriteCommand(":debugon", "Let the compiler emit debug informations.");
                    WriteCommand(":debugtrace", "Let the compiler emit trace line functionality.");
                    WriteCommand(":c", "Clears the current script buffer.");
                    WriteCommand(":env", "Create a fresh environment.");
                    WriteCommand(":cache", "Shows the content of the binder cache.");
                    Console.WriteLine();
                    break;

                case Commands.Run:
                    if (sLine.Length > 0)
                    {
                        RunScript(() => sLine, "line");
                    }
                    break;

                case Commands.Exit:
                    return;
                }
            }
        } // Main
Ejemplo n.º 13
0
			public LuaLoadedTable(LuaGlobal global)
			{
				this.global = global;
			} // ctor
Ejemplo n.º 14
0
        public static void Main(string[] args)
        {
            int iCurrentArg = 0;

              WriteText(ConsoleColor.Gray, "NeoLua Interactive Command"); Console.WriteLine();
              WriteText(ConsoleColor.DarkGray, "Version ");
              WriteText(ConsoleColor.White, String.Format("{0} ({1})", LuaGlobal.VersionString, Lua.Version));
              WriteText(ConsoleColor.DarkGray, " by neolithos");
              Console.WriteLine();
              Console.WriteLine();
              WriteText(ConsoleColor.DarkGray, "  source code at ");
              WriteText(ConsoleColor.Gray, "http://neolua.codeplex.com");
              Console.WriteLine();
              WriteText(ConsoleColor.DarkGray, "  supported from ");
              WriteText(ConsoleColor.Gray, "http://tecware-gmbh.de");
              Console.WriteLine();
              Console.WriteLine();
              WriteText(ConsoleColor.DarkGray, "  Write ':h' for help.");
              Console.WriteLine();
              Console.WriteLine();

              global = lua.CreateEnvironment<LuaCommandGlobal>();

              // change to the samples directory
            #if DEBUG
              string sSamples = Path.GetFullPath(@"..\..\Samples");
            #else
              string sSamples = Path.GetFullPath("Samples");
            #endif
              if (Directory.Exists(sSamples))
            Environment.CurrentDirectory = sSamples;

              while (true)
              {
            string sLine;
                Commands cmd;

                if (iCurrentArg < args.Length)
                    cmd = ParseArgument(args[iCurrentArg++], out sLine);
                else
                    cmd = InputCommand(out sLine);

            switch (cmd)
            {
              case Commands.List:
            // list all variables in global
            WriteText(ConsoleColor.DarkYellow, "List global:");
            Console.WriteLine();
            foreach (var c in global)
              WriteVariable(c.Key, c.Value);
            Console.WriteLine();
            break;
              case Commands.Load:
            RunScript(() => File.ReadAllText(Path.GetFullPath(sLine)), Path.GetFileName(sLine));
            break;
              case Commands.Debug:
               if(sLine =="trace")
               {
             WriteText(ConsoleColor.DarkYellow, "Compile emits traceline code, now."); Console.WriteLine();
             debugEngine = debugConsole;
               }
               else if (sLine == Boolean.TrueString)
            {
              WriteText(ConsoleColor.DarkYellow, "Compile emits stack trace information and runtime functions, now."); Console.WriteLine();
                            debugEngine = LuaStackTraceDebugger.Default;
            }
            else
            {
              WriteText(ConsoleColor.DarkYellow, "Compile creates dynamic functions, now."); Console.WriteLine();
              debugEngine = null;
            }
            Console.WriteLine();
            break;
              case Commands.Environment:
            WriteText(ConsoleColor.DarkYellow, "New environment created."); Console.WriteLine();
            Console.WriteLine();
            global = lua.CreateEnvironment<LuaCommandGlobal>();
            break;
                    case Commands.Cache:
                        lua.DumpRuleCaches(Console.Out);
                    Console.WriteLine();
                        break;
              case Commands.Help:
            WriteText(ConsoleColor.DarkYellow, "Commands:"); Console.WriteLine();
            WriteCommand(":q", "Exit the application.");
            WriteCommand(":list", "Lists all global variables.");
            WriteCommand(":load", "Loads the lua-script from a file.");
            WriteCommand(":debugoff", "Tell the compiler to emit no debug informations.");
            WriteCommand(":debugon", "Let the compiler emit debug informations.");
            WriteCommand(":debugtrace", "Let the compiler emit trace line functionality.");
            WriteCommand(":c", "Clears the current script buffer.");
            WriteCommand(":env", "Create a fresh environment.");
            WriteCommand(":cache", "Shows the content of the binder cache.");
            Console.WriteLine();
            break;
              case Commands.Run:
            if (sLine.Length > 0)
              RunScript(() => sLine, "line");
            break;
              case Commands.Exit:
            return;
            }
              }
        }
Ejemplo n.º 15
0
 public LuaLibraryPackage(LuaGlobal global)
 {
     this.loaded = new LuaLoadedTable(global);
     this.path   = ";;";
 }             // ctor
Ejemplo n.º 16
0
 public LuaLoadedTable(LuaGlobal global)
 {
     this.global = global;
 }             // ctor
Ejemplo n.º 17
-1
		private void DoScript(Lua l, LuaGlobal g, string sScript)
		{
			Type type = typeof(RunLuaTests);
			using (Stream src = type.Assembly.GetManifestResourceStream(type, "CompTests." + sScript))
			using (StreamReader sr = new StreamReader(src))
			{
				Console.WriteLine();
				Console.WriteLine(new string('=', 60));
				Console.WriteLine("= " + sScript);
				Console.WriteLine();
				try
				{
					g.DoChunk(l.CompileChunk(sr, sScript, LuaDeskop.StackTraceCompileOptions));
				}
				catch (Exception e)
				{
					if (e is LuaException)
						Console.WriteLine("Line: {0}, Column: {1}", ((LuaException)e).Line, ((LuaException)e).Column);
					Console.WriteLine("StackTrace:");
					Console.WriteLine(LuaExceptionData.GetData(e).StackTrace);
					throw;
				}
			}
		}