示例#1
0
文件: Script.cs 项目: yonifra/roslyn
        /// <summary>
        /// Creates a delegate that will execute this script when invoked.
        /// </summary>
        public ScriptRunner CreateDelegate(CancellationToken cancellationToken = default(CancellationToken))
        {
            var executor = this.GetAggregateScriptExecutor(cancellationToken);

            return((globals) =>
            {
                var executionState = ScriptExecutionState.Create(globals);
                return executor(executionState);
            });
        }
示例#2
0
        private static Dictionary <string, ScriptVariable> CreateVariableMap(ScriptExecutionState executionState)
        {
            var map = new Dictionary <string, ScriptVariable>();

            for (int i = 0; i < executionState.Count; i++)
            {
                var state = executionState[i];
                if (state != null)
                {
                    AddVaraibles(map, state);
                }
            }

            return(map);
        }
示例#3
0
文件: Script.cs 项目: yonifra/roslyn
 ///<summary>
 /// Continue running script from the point after the intermediate state was produced.
 ///</summary>
 private bool TryRunFrom(ScriptState state, out ScriptExecutionState executionState, out object value)
 {
     if (state.Script == this)
     {
         value          = state.ReturnValue;
         executionState = state.ExecutionState.FreezeAndClone();
         return(true);
     }
     else if (_previous != null && _previous.TryRunFrom(state, out executionState, out value))
     {
         var executor = this.GetExecutor(CancellationToken.None);
         value = executionState.RunSubmission(executor);
         return(true);
     }
     else
     {
         // couldn't find starting point to continue running from.
         value          = null;
         executionState = null;
         return(false);
     }
 }
示例#4
0
文件: Script.cs 项目: yonifra/roslyn
        /// <summary>
        /// Runs this script.
        /// </summary>
        /// <param name="globals">An object instance whose members can be accessed by the script as global variables,
        /// or a <see cref="ScriptState"/> instance that was the output from a previously run script.</param>
        /// <returns>A <see cref="ScriptState"/> that represents the state after running the script, including all declared variables and return value.</returns>
        public ScriptState Run(object globals = null)
        {
            var state = globals as ScriptState;

            if (state != null)
            {
                if (state.Script == this)
                {
                    // this state is already the output of running this script.
                    return(state);
                }
                else if (this.Previous == null)
                {
                    // if this script is unbound (no previous script) then run this script bound to the state's script
                    return(this.WithPrevious(state.Script).Run(state));
                }
                else
                {
                    // attempt to run script forward from the point after the specified state was computed.
                    ScriptExecutionState executionState;
                    object value;
                    if (this.TryRunFrom(state, out executionState, out value))
                    {
                        return(new ScriptState(executionState, value, this));
                    }
                    else
                    {
                        throw new InvalidOperationException(CommonScriptingResources.StartingStateIncompatible);
                    }
                }
            }
            else
            {
                if (_globalsType != null && globals == null)
                {
                    throw new ArgumentNullException("globals");
                }
                else if (globals != null && _globalsType != null)
                {
                    var runtimeType = globals.GetType();
                    if (!_globalsType.IsAssignableFrom(runtimeType))
                    {
                        throw new ArgumentException(string.Format(CommonScriptingResources.GlobalsNotAssignable, runtimeType, _globalsType));
                    }
                }

                // make sure we are running from a script with matching globals type
                if (globals != null && _globalsType == null)
                {
                    return(this.WithGlobalsType(globals.GetType()).Run(globals));
                }

                // run this script from the start with the specified globals
                var executionState = ScriptExecutionState.Create(globals);
                if (this.Previous == null)
                {
                    // only single submission, so just execute it directly.
                    var executor = this.GetExecutor(CancellationToken.None);
                    var value    = executionState.RunSubmission(executor);
                    return(new ScriptState(executionState, value, this));
                }
                else
                {
                    // otherwise run the aggregate script.
                    var executor = this.GetAggregateScriptExecutor(CancellationToken.None);
                    var value    = executor(executionState);
                    return(new ScriptState(executionState, value, this));
                }
            }
        }
示例#5
0
 internal ScriptVariables(ScriptExecutionState executionState)
 {
     _map = CreateVariableMap(executionState);
 }
示例#6
0
 internal ScriptState(ScriptExecutionState executionState, object value, Script script)
 {
     _executionState = executionState;
     _value          = value;
     _script         = script;
 }