Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ExpressionScript{TResult}"/> class.
        /// </summary>
        /// <param name="scriptContext">
        ///   The <see cref="ScriptContext"/> to use when executing the script. The script context is used to isolate re-motion modules from each other.
        /// </param>
        /// <param name="scriptLanguageType">The script language to use for the script.</param>
        /// <param name="scriptText">
        ///   The source code of the script. This must be an expression in the language defined by <paramref name="scriptLanguageType"/>.
        /// </param>
        /// <param name="scriptEnvironment">
        ///   The <see cref="ScriptEnvironment"/> defining the variables and imported symbols the script has access to.
        /// </param>
        public ExpressionScript(
            ScriptContext scriptContext,
            ScriptLanguageType scriptLanguageType,
            string scriptText,
            ScriptEnvironment scriptEnvironment)
            : base(
                ArgumentUtility.CheckNotNull("scriptContext", scriptContext),
                scriptLanguageType,
                ArgumentUtility.CheckNotNullOrEmpty("scriptText", scriptText))
        {
            ArgumentUtility.CheckNotNull("scriptEnvironment", scriptEnvironment);

            _scriptEnvironment = scriptEnvironment;

            var engine = ScriptingHost.GetScriptEngine(scriptLanguageType);

            _scriptSource = engine.CreateScriptSourceFromString(scriptText, SourceCodeKind.Expression);
        }
        /// <summary>
        /// Imports the passed symbols from the given namespace in the given assembly into the <see cref="ScriptEnvironment"/>.
        /// </summary>
        /// <param name="assembly">Partial name of the assembly to import from (e.g. "Remotion")</param>
        /// <param name="nameSpace">Namespace name in assembly to import from (e.g. "Remotion.Diagnostics.ToText")</param>
        /// <param name="symbols">array of symbol names to import (e.g. "To", "ToTextBuilder", ...)</param>
        public void Import(string assembly, string nameSpace, params string[] symbols)
        {
            ArgumentUtility.CheckNotNullOrEmpty("assembly", assembly);
            ArgumentUtility.CheckNotNullOrEmpty("nameSpace", nameSpace);
            ArgumentUtility.CheckNotNullOrEmpty("symbols", symbols);

            string scriptText = @"
import clr
clr.AddReferenceByPartialName('" + assembly + "')" +
                                @"
from " + nameSpace + " import " + string.Join(",", symbols);

            const ScriptLanguageType scriptLanguageType = ScriptLanguageType.Python;
            var engine       = ScriptingHost.GetScriptEngine(scriptLanguageType);
            var scriptSource = engine.CreateScriptSourceFromString(scriptText, SourceCodeKind.Statements);

            scriptSource.Execute(_scriptScope);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new script instance. This immediately executes the given
        /// <paramref name="scriptText"/>, and stores a delegate to the <paramref name="scriptFunctionName" />, which is run when <see cref="Execute"/>
        /// is called.
        /// </summary>
        /// <param name="scriptContext">
        ///   The <see cref="ScriptContext"/> to use when executing the script. The script context is used to isolate re-motion modules from each other.
        /// </param>
        /// <param name="scriptLanguageType">The script language to use for the script.</param>
        /// <param name="scriptText">
        ///   The source code of the script. This must be source code matching the language defined by <paramref name="scriptLanguageType"/>. The script
        ///   is immediately executed, i.e., all global statements are immediately run. After that, a delegate defining the main function (defined by
        ///   <paramref name="scriptFunctionName"/>) is stored for later use. When <see cref="Execute"/> is invoked, that function is run and its result
        ///   returned.
        /// </param>
        /// <param name="scriptEnvironment">
        ///   The <see cref="ScriptEnvironment"/> defining the variables and imported symbols the script has access to.
        /// </param>
        /// <param name="scriptFunctionName">
        ///   The name of the script's main function. This function must be defined by <paramref name="scriptText"/>, and it is invoked when the
        ///   script is executed.
        /// </param>
        public ScriptFunction(
            ScriptContext scriptContext,
            ScriptLanguageType scriptLanguageType,
            string scriptText,
            ScriptEnvironment scriptEnvironment,
            string scriptFunctionName)
            : base(
                ArgumentUtility.CheckNotNull("scriptContext", scriptContext),
                scriptLanguageType,
                ArgumentUtility.CheckNotNullOrEmpty("scriptText", scriptText))
        {
            ArgumentUtility.CheckNotNull("scriptEnvironment", scriptEnvironment);
            ArgumentUtility.CheckNotNullOrEmpty("scriptFunctionName", scriptFunctionName);

            var engine       = ScriptingHost.GetScriptEngine(scriptLanguageType);
            var scriptSource = engine.CreateScriptSourceFromString(scriptText, SourceCodeKind.Statements);

            // Immediately execute the script. This will cause all function definitions to be stored as variables. We can then extract the script function
            // and store it as a delegate.
            scriptSource.Execute(scriptEnvironment.ScriptScope);

            _func = scriptEnvironment.ScriptScope.GetVariable <Func <TResult> > (scriptFunctionName);
        }
 /// <summary>
 /// <see cref="ScriptEnvironment"/> factory method.
 /// </summary>
 public static ScriptEnvironment Create()
 {
     return(new ScriptEnvironment(ScriptingHost.GetScriptEngine(ScriptLanguageType.Python).CreateScope()));
 }