Exemplo n.º 1
0
        } // func create

        #endregion

        #region -- Resume -----------------------------------------------------------------

        /// <summary>Starts the execution of the next part of the thread.</summary>
        /// <param name="args">Arguments, that will pass to the function-part</param>
        /// <returns>Returns the <c>LuaThread</c> as an IAsyncResult.</returns>
        public IAsyncResult BeginResume(object[] args)
        {
            // Lock the resume to only one pair Begin/End
            lock (this)
            {
                if (lockResume != null)
                {
                    throw new InvalidOperationException();
                }

                lockResume = new object();
                Monitor.Enter(lockResume);
            }

            // set parameters
            currentArguments = args;

            // start the coroutine, if not startet
            if (arExecute == null)
            {
                evYield = new ManualResetEventSlim(false);

                arExecute = procExecute.BeginInvoke(EndExecuteDelegate, null);
            }
            else if (evResume != null)
            {
                evYield.Reset();
                evResume.Set(); // resume the coroutine
            }

            return(this);
        } // proc resumeAsync
Exemplo n.º 2
0
        }         // ctor

        /// <summary>Gibt die Daten frei</summary>
        public void Dispose()
        {
            currentArguments = null;
            currentArguments = null;
            if (taskCancelExecute != null)
            {
                if (!taskCancelExecute.IsCancellationRequested)
                {
                    taskCancelExecute.Cancel();
                }
                taskCancelExecute.Dispose();
                taskCancelExecute = null;
            }
            if (evYield != null)
            {
                evYield.Dispose();
                evYield = null;
            }
            if (evResume != null)
            {
                evResume.Set();
                evResume.Dispose();
                evResume = null;
            }
        }         // proc Dispose
Exemplo n.º 3
0
        }         // func Yield

        #endregion

        #region -- ExecuteDelegate ----------------------------------------------------

        private void ExecuteDelegate()
        {
            evResume = new ManualResetEventSlim(false);

            Task.Yield();             // force background thread

            // add this thread to the thread-pool
            lock (luaThreads)
                luaThreads.Add(this);

            try
            {
                yield(new LuaResult(Lua.RtInvoke(target, currentArguments.Values)));
            }
            finally
            {
                // remove the thread from the pool
                lock (luaThreads)
                    luaThreads.Remove(this);

                taskExecute = null;
                taskCancelExecute.Dispose();
                taskCancelExecute = null;

                // dispose the events
                currentYield = LuaResult.Empty;
                evResume.Dispose();
                evResume = null;
                evYield.Set();
            }
        }         // proc ExecuteDelegate
Exemplo n.º 4
0
        /// <summary>Starts the execution of the next part of the thread.</summary>
        /// <param name="args">Arguments, that will pass to the function-part</param>
        /// <returns>Returns the <c>LuaThread</c> as an IAsyncResult.</returns>
        public IAsyncResult BeginResume(object[] args)
        {
            // Lock the resume to only one pair Begin/End
            lock (lockThread)
            {
                if (lockResume != null)
                {
                    throw new InvalidOperationException();
                }

                lockResume = new object();
                Monitor.Enter(lockResume);
            }

            // set parameters
            currentArguments = args;

            // start the coroutine, if not startet
            if (taskExecute == null)
            {
                evYield = new ManualResetEventSlim(false);

                taskCancelExecute = new CancellationTokenSource();
                taskExecute       = Task.Factory.StartNew(ExecuteDelegate, taskCancelExecute.Token);
            }
            else if (evResume != null)
            {
                evYield.Reset();
                evResume.Set();                 // resume the coroutine
            }

            return(this);
        }         // proc resumeAsync
Exemplo n.º 5
0
        } // proc Dispose

        /// <summary></summary>
        /// <returns></returns>
        public LuaResult close()
        {
            LuaResult r = LuaResult.Empty;

            if (process != null)
            {
                r = new LuaResult(process.ExitCode);
            }
            Dispose();
            return(r);
        } // func close
Exemplo n.º 6
0
        } // ctor

        /// <summary>Gibt die Daten frei</summary>
        public void Dispose()
        {
            currentArguments = null;
            currentArguments = null;
            if (evYield != null)
            {
                evYield.Dispose();
                evYield = null;
            }
            if (evResume != null)
            {
                evResume.Set();
                evResume.Dispose();
                evResume = null;
            }
        } // proc Dispose
Exemplo n.º 7
0
        } // func lines

        /// <summary></summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public LuaResult close(LuaFile file = null)
        {
            if (file != null)
            {
                return(file.close());
            }
            else if (defaultOutput != null)
            {
                LuaResult r = defaultOutput.close();
                defaultOutput = null;
                return(r);
            }
            else
            {
                return(null);
            }
        } // proc close
Exemplo n.º 8
0
            }             // func GetTargetDynamicCall

            public override DynamicMetaObject BindConvert(ConvertBinder binder)
            {
                BindingRestrictions r = BindingRestrictions.GetTypeRestriction(Expression, typeof(LuaResult));
                LuaResult           v = (LuaResult)Value;

                if (binder.Type == typeof(LuaResult))
                {
                    return(new DynamicMetaObject(Expression.Convert(this.Expression, binder.ReturnType), r));
                }
                else if (binder.Type == typeof(object[]))
                {
                    return(new DynamicMetaObject(GetValuesExpression(), r, v.result));
                }
                else
                {
                    return(new DynamicMetaObject(Expression.Dynamic(binder, binder.Type, GetFirstResultExpression()), r));
                }
            }             // func BindConvert
Exemplo n.º 9
0
        } // proc ExecuteDelegate

        private void EndExecuteDelegate(IAsyncResult ar)
        {
            try
            {
                procExecute.EndInvoke(ar);
            }
            catch
            {
            }

            // remove the thread from the pool
            lock (luaThreads)
                luaThreads.Remove(this);

            iCoroutineThreadId = -1;

            // dispose the events
            currentYield = LuaResult.Empty;
            evResume.Dispose();
            evResume = null;
            evYield.Set();
        } // proc EndExecuteDelegate
Exemplo n.º 10
0
        }         // func ParseArgument

        private static void RunScript(Func <string> code, string sName)
        {
            try
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();

                // compile chunk
                LuaChunk c = lua.CompileChunk(code(), sName, new LuaCompileOptions()
                {
                    DebugEngine = debugEngine
                });

                string sCompileTime = String.Format("{0:N0} ms", sw.ElapsedMilliseconds);
                sw.Reset();
                sw.Start();

                // run chunk
                LuaResult r        = global.DoChunk(c);
                string    sRunTime = String.Format("{0:N0} ms", sw.ElapsedMilliseconds);

                string sSize;
                if (c.Size < 0)
                {
                    sSize = "unknown";
                }
                else if (c.Size == 0)
                {
                    sSize = String.Empty;
                }
                else
                {
                    sSize = c.Size.ToString("N0") + " byte";
                }

                // start with a new line
                if (Console.CursorLeft > 0)
                {
                    Console.WriteLine();
                }

                // print result
                if (r.Count > 0)
                {
                    for (int i = 0; i < r.Count; i++)
                    {
                        WriteVariable(i, r[i]);
                    }
                }

                // print summary
                const string csCompile = "==> compile: ";
                const string csRuntime = " run: ";

                Console.CursorLeft = Console.WindowWidth - csCompile.Length - (sSize.Length > 0 ? sSize.Length + 3 : 0) - sCompileTime.Length - csRuntime.Length - sRunTime.Length - 1;
                WriteText(ConsoleColor.DarkGreen, csCompile);
                WriteText(ConsoleColor.Green, sCompileTime);
                if (sSize.Length > 0)
                {
                    WriteText(ConsoleColor.DarkGreen, " [");
                    WriteText(ConsoleColor.Green, sSize);
                    WriteText(ConsoleColor.DarkGreen, "]");
                }
                WriteText(ConsoleColor.DarkGreen, csRuntime);
                WriteText(ConsoleColor.Green, sRunTime);
                Console.WriteLine();
            }
            catch (LuaParseException e)
            {
                WriteText(ConsoleColor.DarkRed, String.Format("Parse error at line {0:N0} (column: {1:N0}):", e.Line, e.Column));
                Console.WriteLine();
                WriteText(ConsoleColor.DarkRed, "  " + e.Message);
                Console.WriteLine();
            }
            catch (Exception e)
            {
                Exception ex = e is TargetInvocationException ? e.InnerException : e;
                WriteException(ex);
            }
        } // proc RunScript