Example #1
0
        /// <summary>
        /// Creates a function based on an expression. Ex: "x + 1"
        /// </summary>
        public ScriptFunction(IScriptEngineFactory engineFactory, IEnumerable<string> argumentNames, string expression, bool addReturnStatement)
        {
            this.engineFactory = engineFactory;
            this.expression = expression;

            // generate the javascript for the function
            StringBuilder argsDecl = new StringBuilder();

            foreach (string arg in argumentNames)
            {
                if (argsDecl.Length > 0)
                    argsDecl.Append(",");
                argsDecl.Append(arg);
            }

            if(addReturnStatement)
                functionBody = "(" + argsDecl + "){return (" + expression + ");}";
            else
                functionBody = "(" + argsDecl + "){ " + expression + " }";

            // assign a unique name to this function
            lock (functionNames)
            {
                if (!functionNames.TryGetValue(functionBody, out functionName))
                {
                    functionName = "_func_" + functionNames.Count;
                    functionNames.Add(functionBody, functionName);
                }
            }

            // Verify the syntax of the function. This will also register the function with the current script engine.
            EnsureCompiled(engineFactory.GetScriptEngine());
        }
Example #2
0
 public void RegisterEngineName(string name, IScriptEngineFactory factory)
 {
     if (name == null || factory == null)
     {
         throw new NullReferenceException();
     }
     nameAssociations[name] = factory;//.Put(name, factory);
 }
Example #3
0
 public override IScriptEngineFactory GetFactory()
 {
     lock (this)
     {
         if (scriptEngineFactory == null)
         {
             scriptEngineFactory = new JuelScriptEngineFactory();
         }
     }
     return(scriptEngineFactory);
 }
Example #4
0
        /// <summary>
        /// Creates a function based on an expression. Ex: "x + 1"
        /// </summary>
        public ScriptFunction(IScriptEngineFactory engineFactory, IEnumerable <string> argumentNames, string expression, bool addReturnStatement)
        {
            this.engineFactory = engineFactory;
            this.expression    = expression;

            // generate the javascript for the function
            StringBuilder argsDecl = new StringBuilder();

            foreach (string arg in argumentNames)
            {
                if (argsDecl.Length > 0)
                {
                    argsDecl.Append(",");
                }
                argsDecl.Append(arg);
            }

            if (addReturnStatement)
            {
                functionBody = "(" + argsDecl + "){return (" + expression + ");}";
            }
            else
            {
                functionBody = "(" + argsDecl + "){ " + expression + " }";
            }

            // assign a unique name to this function
            lock (functionNames)
            {
                if (!functionNames.TryGetValue(functionBody, out functionName))
                {
                    functionName = "_func_" + functionNames.Count;
                    functionNames.Add(functionBody, functionName);
                }
            }

            // Verify the syntax of the function. This will also register the function with the current script engine.
            EnsureCompiled(engineFactory.GetScriptEngine());
        }
Example #5
0
 public JuelScriptEngine(IScriptEngineFactory scriptEngineFactory)
 {
     this.scriptEngineFactory = scriptEngineFactory;
     // Resolve the ExpressionFactory
     expressionFactory = ExpressionFactoryResolver.ResolveExpressionFactory();
 }
Example #6
0
 /// <summary>
 /// Creates a function based on an expression. Ex: "x + 1"
 /// </summary>
 public ScriptFunction(IScriptEngineFactory engineFactory, IEnumerable <string> argumentNames, string expression)
     : this(engineFactory, argumentNames, expression, true)
 {
 }
 public ScriptEngineHost(IScriptEngineFactory engineFactory)
 {
     _engineFactory = engineFactory;
 }
Example #8
0
        /// <summary>
        /// スクリプトを利用して、DependencyInjectionコンテナを生成して
        /// コンポーネントを登録し、そのインスタンスを返す
        /// </summary>
        /// <returns>生成されたDependencyInjectionコンテナ</returns>
        /// <exception cref="CompileErrorException">
        /// スクリプトのコンパイルエラーが発生した場合はこの例外を投げる
        /// </exception>
        /// <exception cref="FactoryNotFoundException">
        /// DependencyInjectionコンテナのファクトリーメソッドがスクリプトから
        /// みつからない場合はこの例外を投げる
        /// </exception>
        /// <remarks>
        /// <p>スクリプトにはC#、VBScriptとJScriptが利用できます。スクリプト上の
        /// ComponentContainerFactoryAttribute属性をもちIComponentContainerインターフェイス
        /// と互換性のある値を返すStaticな関数が、DependencyInjectionコンテナを生成し
        /// 依存関係が設定したものを返すファクトリーメソッドとなります。スクリプト上に
        /// このファクトリーメソッドが存在しない場合はFactoryNotFoundException例外を
        /// 投げます。以下にスクリプトの例をあげます。</p>
        /// <code lang="C#">
        /// using System;
        /// using Kodama.DependencyInjection.Container;
        /// using Kodama.DependencyInjection.Factory;
        /// using HogeHoge;
        ///
        /// public class ComponentContainerBuilder
        /// {
        ///     [ComponentContainerFactory]
        ///     public IMutableComponentContainer Build()
        ///     {
        ///         IMutableComponentContainer container = new ComponentContainerImpl();
        ///         container.Register(typeof(ClassA));
        ///         container.Register(typeof(ClassB));
        ///         return container;
        ///     }
        /// }
        /// </code>
        /// </remarks>
        public IComponentContainer Build()
        {
            IScriptEngineFactory[] factories = new IScriptEngineFactory[]
            { new CSharpScriptEngineFactory(), new JScriptEngineFactory(), new VBScriptEngineFactory() };
            foreach (IScriptEngineFactory factory in factories)
            {
                if (!factory.CanCompile(scriptPath))
                {
                    continue;
                }

                IScriptEngine engine = factory.CreateFromFile(scriptPath);

                engine.AddReference(Assembly.GetExecutingAssembly().Location);
                foreach (string assemblyName in assemblyNames)
                {
                    engine.AddReference(assemblyName);
                }

                try
                {
                    engine.Compile();
                }
                finally
                {
                    engine.Close();
                }

                foreach (Type type in engine.ScriptAssembly.GetTypes())
                {
                    foreach (MethodInfo mi in type.GetMethods())
                    {
                        if (!mi.IsStatic)
                        {
                            continue;
                        }
                        if (mi.GetParameters().Length != 0)
                        {
                            continue;
                        }
                        if (!typeof(IComponentContainer).IsAssignableFrom(mi.ReturnType))
                        {
                            continue;
                        }
                        if (!Attribute.IsDefined(mi, typeof(ComponentContainerFactoryAttribute)))
                        {
                            continue;
                        }

                        IComponentContainer container = (IComponentContainer)mi.Invoke(null, null);

                        engine.Close();

                        return(container);
                    }
                }

                engine.Close();

                throw new FactoryNotFoundException();
            }

            throw new NotSupportedScriptException();
        }
Example #9
0
 public virtual ScriptingEngines AddScriptEngineFactory(IScriptEngineFactory scriptEngineFactory)
 {
     scriptEngineResolver.AddScriptEngineFactory(scriptEngineFactory);
     return(this);
 }
Example #10
0
        private static void Main(string[] args)
        {
            // コマンドライン引数をパースする
            string scriptPath = null;
            string configPath = null;

            for (int i = 0; i < args.Length; ++i)
            {
                if ((string.Compare(args[i], "-s", true) == 0) && (i + 1 < args.Length))
                {
                    scriptPath = args[i + 1];
                }
                else if ((string.Compare(args[i], "-c", true) == 0) && (i + 1 < args.Length))
                {
                    configPath = args[i + 1];
                }
            }

            // スクリプトの指定がない場合はエラー
            if (scriptPath == null)
            {
                MessageBox.Show("Please specify a script to set a command line argument.");
                return;
            }

            // XML設定ファイルをパースする
            referenceAssembly reference = null;

            if (configPath != null)
            {
                Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream
                                    ("Kodama.BootLoader.Schema.AssemblyConfig.xsd");
                XmlSchemaCollection schema = new XmlSchemaCollection();
                schema.Add(null, new XmlTextReader(stream));

                XmlValidatingReader reader = new XmlValidatingReader(new XmlTextReader(configPath));
                reader.ValidationType = ValidationType.Schema;
                reader.Schemas.Add(schema);

                XmlSerializer serializer = new XmlSerializer(typeof(referenceAssembly));

                try
                {
                    reference = (referenceAssembly)serializer.Deserialize(reader);
                }
                finally
                {
                    reader.Close();
                }
            }

            // スクリプトエンジンを作成し、スクリプトを呼び出す
            IScriptEngineFactory[] factories = new IScriptEngineFactory[]
            { new CSharpScriptEngineFactory(), new JScriptEngineFactory(), new VBScriptEngineFactory() };
            foreach (IScriptEngineFactory factory in factories)
            {
                // サポートしているフォーマットかチェックする
                if (!factory.CanCompile(scriptPath))
                {
                    continue;
                }

                // スクリプトエンジンの作成
                IScriptEngine engine = factory.CreateFromFile(scriptPath);

                // XMLファイルに書かれていたアセンブリの参照を設定する
                if (reference != null)
                {
                    foreach (referenceAssemblyAssembly assembly in reference.Items)
                    {
                        engine.AddReference(assembly.name);
                    }
                }

                // コンパイルする
                try
                {
                    engine.Compile();
                }
                catch (CompileErrorException e)
                {
                    StringBuilder errors = new StringBuilder(10000);
                    foreach (CompileErrorInfo cei in e.GetCompileErrorInfos())
                    {
                        errors.Append("--------------------------------" + "\n");
                        errors.Append("SourceName  : " + cei.SourceName + "\n");
                        errors.Append("Description : " + cei.Description + "\n");
                        errors.Append("Line        : " + cei.ErrorLine + "\n");
                        errors.Append("Code        : " + cei.ErrorText + "\n");
                        errors.Append("--------------------------------" + "\n");
                    }
                    engine.Close();
                    MessageBox.Show("The error occurred in compile of the script.\n" + errors.ToString());
                    return;
                }

                // スクリプトを実行する
                try
                {
                    engine.Run();
                }
                catch (ScriptEntryPointNotFoundException)
                {
                    engine.Close();
                    MessageBox.Show("An entry point is not found in the script.");
                }

                // エンジンを閉じる
                engine.Close();

                return;
            }

            MessageBox.Show("It is the format of the script which is not supported.");
        }
 static ExtendedObjectExpressionFormatter()
 {
     _scriptEngineFactory = new ScriptEngineFactory();
 }
Example #12
0
        static Func <object, object> CreateScriptFunction(string argName1, string expression, IScriptEngineFactory factory, bool addReturnStatement)
        {
            ScriptFunction function  = new ScriptFunction(factory, new string[] { argName1 }, expression, addReturnStatement);
            Marshaler      marshaler = new Marshaler(factory.GetScriptEngine());

            return((a) => function.Evaluate(new object[] { a }, marshaler));
        }
Example #13
0
 /// <summary>
 /// Creates a function based on an expression. Ex: "x + 1"
 /// </summary>
 public ScriptFunction(IScriptEngineFactory engineFactory, IEnumerable<string> argumentNames, string expression)
     : this(engineFactory, argumentNames, expression, true)
 {
 }
Example #14
0
 public virtual void AddScriptEngineFactory(IScriptEngineFactory scriptEngineFactory)
 {
     scriptEngineManager.RegisterEngineName(scriptEngineFactory.EngineName, scriptEngineFactory);
 }
Example #15
0
        public IScriptEngine GetEngineByName(string shortName)
        {
            if (shortName == null)
            {
                throw new NullReferenceException();                   // NullPointerException();
            }
            //look for registered name first
            Object obj = nameAssociations.ContainsKey(shortName) ? nameAssociations[shortName] : null;

            if (null != obj)
            {
                IScriptEngineFactory spi = (IScriptEngineFactory)obj;
                try
                {
                    IScriptEngine engine = spi.GetScriptEngine();
                    engine.SetBindings(GetBindings(), ScriptContext_Fields.GLOBAL_SCOPE);
                    return(engine);
                }
                catch (System.Exception exp)
                {
                    throw exp;
                    //if (DEBUG) exp.printStackTrace();
                }
            }

            foreach (IScriptEngineFactory spi in engineSpis)
            {
                IList <String> names = null;
                try
                {
                    names = spi.Names;
                }
                catch (System.Exception exp)
                {
                    throw exp;
                    //if (DEBUG) exp.printStackTrace();
                }

                if (names != null)
                {
                    foreach (String name in names)
                    {
                        if (shortName.Equals(name))
                        {
                            try
                            {
                                IScriptEngine engine = spi.GetScriptEngine();
                                engine.SetBindings(GetBindings(), ScriptContext_Fields.GLOBAL_SCOPE);
                                return(engine);
                            }
                            catch (System.Exception exp)
                            {
                                throw exp;
                                //if (DEBUG) exp.printStackTrace();
                            }
                        }
                    }
                }
            }

            return(null);
        }