Beispiel #1
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
        }