Esempio n. 1
0
        public bool Step()
        {
            if (quit)
            {
                return(false);
            }

            CurrentInstruction = Environment.Delegate.instructions[Environment.ProgramCounter];

#if DEBUG
            Environment.AddHistoryEntry(CurrentInstruction);
#endif

            try
            {
                CurrentInstruction.Execute(Environment);
            }
            catch (ScriptException e)
            {
                HandleException(e, Environment);
            }
            catch (TargetInvocationException e)
            {
                HandleException(new ScriptException(CurrentInstruction, e.InnerException), Environment);
            }
            catch (Exception e)
            {
                HandleException(new ScriptException(CurrentInstruction, e), Environment);
            }

            if (Environment.ProgramCounter < -1 ||
                Environment.ProgramCounter >= Environment.Delegate.instructions.Length)
            {
                throw new ScriptException(CurrentInstruction, new IndexOutOfRangeException());
            }

            Environment.ProgramCounter++;

            if (Environment.IsReturning ||
                Environment.ProgramCounter >= Environment.Delegate.instructions.Length)
            {
                quit = true;
                return(false);
            }
            return(true);
        }
Esempio n. 2
0
        public object DynamicInvoke(bool keepAsync = DEFAULT_KEEP_ASYNC, params object[] args)
        {
#if OLD_METHOD
            var env = new ScriptEnvironment(this, args);
            while (!env.IsReturning &&
                   env.ProgramCounter < instructions.Length)
            {
                var inst = instructions[env.ProgramCounter];
#if DEBUG
                env.AddHistoryEntry(inst);
#endif
                try
                {
                    inst.Execute(env);
                }
                catch (ScriptException e)
                {
                    HandleException(e, env);
                }
                catch (TargetInvocationException e)
                {
                    HandleException(new ScriptException(inst, e.InnerException), env);
                }
                catch (Exception e)
                {
                    HandleException(new ScriptException(inst, e), env);
                }
                if (env.ProgramCounter < -1 ||
                    env.ProgramCounter >= instructions.Length)
                {
                    throw new ScriptException(inst, new IndexOutOfRangeException());
                }
                env.ProgramCounter++;
            }
            return(env.ReturnValue);
#else
            var mgr = CreateExecutionManager(args);
            mgr.KeepAsync             = keepAsync;
            mgr.ScriptExecutionAdded += (sender, exec) =>
            {
                var thread = new Thread(() =>
                {
                    try
                    {
                        while (exec.Step())
                        {
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e);
                    }
                });
                thread.Start();
            };

            try
            {
                while (mgr.MainExecution.Step())
                {
                }
            }
            catch (Exception e)
            {
                mgr.Quit();
                throw e;
            }
            mgr.Quit();

            return(mgr.MainExecution.Environment.ReturnValue);
#endif
        }