Exemplo n.º 1
0
        /**
         * This is badly named - really it sets up the
         * global context and then executes the file given
         * on within that context.
         */
        public void ReadJsFile(string in_filename)
        {
            commonjs.SetRequireStack(in_filename);

            // string pathpart = Path.GetDirectoryName( in_filename );
            // path = Directory.GetCurrentDirectory() + pathpart;
            // log.Trace( "path of js file: " + path );

            var emit =
                Utils.createHostFunction <Action <IronJS.BoxedValue> >(
                    ctx.Environment, (obj) => {
                Console.WriteLine(IronJS.TypeConverter.ToString(obj));
            }
                    );

            ctx.SetGlobal <IronJS.FunctionObject>("puts", emit);

            var require =
                Utils.createHostFunction <Func <string, IronJS.CommonObject> >(
                    ctx.Environment, (obj) => {
                return(Require(obj));
            }
                    );

            ctx.SetGlobal <IronJS.FunctionObject>("require", require);

            // Forms the `net" namespace
            ctx.SetGlobal <IronJS.CommonObject>("net", netObj);

            // Forms the `http" namespace
            // TODO: re-add after httpserver.cs is converted to IJS 0.2
            // ctx.SetGlobal<IronJS.CommonObject>( "http", httpObj );

            ctx.ExecuteFile(in_filename);
        }
Exemplo n.º 2
0
        protected override TestResult ExecuteTest(IronJS.Hosting.CSharp.Context ctx, string test)
        {
            var errors = new StringBuilder();

            Action <string> appendError = err =>
            {
                if (errors.Length > 0)
                {
                    errors.AppendLine();
                }

                errors.Append(err);
            };

            var score = string.Empty;

            Action <string, string> notifyResult = (name, result) => { };
            Action <string, string> notifyError  = (name, error) => appendError(name + ": " + error);
            Action <string>         notifyScore  = s => score = s;

            ctx.SetGlobal("NotifyResult", IronJS.Native.Utils.CreateFunction(ctx.Environment, 2, notifyResult));
            ctx.SetGlobal("NotifyError", IronJS.Native.Utils.CreateFunction(ctx.Environment, 2, notifyError));
            ctx.SetGlobal("NotifyScore", IronJS.Native.Utils.CreateFunction(ctx.Environment, 1, notifyScore));

            try
            {
                ctx.ExecuteFile(test);
                ctx.Execute(@"BenchmarkSuite.RunSuites({ NotifyResult: NotifyResult,
                                                         NotifyError: NotifyError,
                                                         NotifyScore: NotifyScore });");
            }
            catch (Exception ex)
            {
                appendError("Exception: " + ex.GetBaseException().Message);
            }

            if (errors.Length > 0)
            {
                return(new TestResult {
                    Error = errors.ToString()
                });
            }
            else
            {
                return(new TestResult {
                    Score = score
                });
            }
        }
        public bool Evaluate(string conditionScript, IWorkflowRuntime runtime)
        {
            if (conditionScript == null)
                throw new ArgumentNullException("conditionScript");
            if (runtime == null)
                throw new ArgumentNullException("runtime");

            var context = new IronJS.Hosting.CSharp.Context();
            context.SetGlobal("input", runtime.Input);
            foreach (var providerRuntimeResult in runtime.ProviderResults)
            {
                if (providerRuntimeResult.ProviderStatus == EWorkflowProviderRuntimeStatus.Success)
                    context.SetGlobal(providerRuntimeResult.ProviderName, providerRuntimeResult.Result);
            }
            conditionScript = "(function(){ " + conditionScript + "})()";
            return context.Execute<bool>(conditionScript);
        }
Exemplo n.º 4
0
        private static IronJS.Hosting.CSharp.Context CreateContext(string libPath, Action <string> errorAction)
        {
            var ctx = new IronJS.Hosting.CSharp.Context();

            Action <string> failAction    = error => { throw new Exception(error); };
            Action <string> printAction   = message => Debug.WriteLine(message);
            Action <string> includeAction = file => ctx.Execute(GetInclude(libPath, file));

            var errorFunc   = IronJS.Native.Utils.CreateFunction(ctx.Environment, 1, errorAction);
            var failFunc    = IronJS.Native.Utils.CreateFunction(ctx.Environment, 1, failAction);
            var printFunc   = IronJS.Native.Utils.CreateFunction(ctx.Environment, 1, printAction);
            var includeFunc = IronJS.Native.Utils.CreateFunction(ctx.Environment, 1, includeAction);

            ctx.SetGlobal("$FAIL", failFunc);
            ctx.SetGlobal("ERROR", errorFunc);
            ctx.SetGlobal("$ERROR", errorFunc);
            ctx.SetGlobal("$PRINT", printFunc);
            ctx.SetGlobal("$INCLUDE", includeFunc);
            return(ctx);
        }
Exemplo n.º 5
0
        private static IronJS.Hosting.CSharp.Context CreateContext(Action<string> errorAction)
        {
            var ctx = new IronJS.Hosting.CSharp.Context();

            Action<string> failAction = error => Assert.Fail(error);
            Action<string> printAction = message => Trace.WriteLine(message);
            Action<string> includeAction = file => ctx.Execute(GetInclude(file));

            var errorFunc = IronJS.Native.Utils.CreateFunction(ctx.Environment, 1, errorAction);
            var failFunc = IronJS.Native.Utils.CreateFunction(ctx.Environment, 1, failAction);
            var printFunc = IronJS.Native.Utils.CreateFunction(ctx.Environment, 1, printAction);
            var includeFunc = IronJS.Native.Utils.CreateFunction(ctx.Environment, 1, includeAction);

            ctx.SetGlobal("$FAIL", failFunc);
            ctx.SetGlobal("ERROR", errorFunc);
            ctx.SetGlobal("$ERROR", errorFunc);
            ctx.SetGlobal("$PRINT", printFunc);
            ctx.SetGlobal("$INCLUDE", includeFunc);

            return ctx;
        }
Exemplo n.º 6
0
        private static IronJS.Hosting.CSharp.Context CreateContext()
        {
            var ctx = new IronJS.Hosting.CSharp.Context();

            Func<BoxedValue, bool> fnExists = f => f.IsFunction;
            var fnExistsFunc = IronJS.Native.Utils.CreateFunction(ctx.Environment, 1, fnExists);
            ctx.SetGlobal("fnExists", fnExistsFunc);

            ctx.Execute("var currentTest; var ES5Harness = { registerTest: function (test) { currentTest = test; } };");

            return ctx;
        }
Exemplo n.º 7
0
        private static IronJS.Hosting.CSharp.Context CreateContext()
        {
            var ctx = new IronJS.Hosting.CSharp.Context();

            Func <BoxedValue, bool> fnExists = f => f.IsFunction;
            var fnExistsFunc = IronJS.Native.Utils.CreateFunction(ctx.Environment, 1, fnExists);

            ctx.SetGlobal("fnExists", fnExistsFunc);

            ctx.Execute("var currentTest; var ES5Harness = { registerTest: function (test) { currentTest = test; } };");

            return(ctx);
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            var context = new IronJS.Hosting.CSharp.Context();

            var writeLine = IronJS.Native.Utils.createHostFunction<Action<string>>(context.Environment,
                x => {
                    Console.WriteLine("Console: " + x);
                });

            context.SetGlobal("WriteLine", writeLine);

            var setTitle = IronJS.Native.Utils.createHostFunction<Action<string>>(context.Environment,
                x =>
                {
                    Console.Title = x;
                });

            context.SetGlobal("SetTitle", setTitle);

            Console.WriteLine("Type a JS expression below and press enter. (Blank line to quit.)");

            Console.Write("js-console> ");
            string command = Console.ReadLine();

            while (command != "" && command != "exit")
            {
                try
                {
                    Console.WriteLine(context.Execute(command));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Oops. Got an error: \n" + ex.Message);
                }
                Console.Write("\njs-console> ");
                command = Console.ReadLine();
            }
        }
        public bool Evaluate(string conditionScript, IWorkflowRuntime runtime)
        {
            if (conditionScript == null)
            {
                throw new ArgumentNullException("conditionScript");
            }
            if (runtime == null)
            {
                throw new ArgumentNullException("runtime");
            }

            var context = new IronJS.Hosting.CSharp.Context();

            context.SetGlobal("input", runtime.Input);
            foreach (var providerRuntimeResult in runtime.ProviderResults)
            {
                if (providerRuntimeResult.ProviderStatus == EWorkflowProviderRuntimeStatus.Success)
                {
                    context.SetGlobal(providerRuntimeResult.ProviderName, providerRuntimeResult.Result);
                }
            }
            conditionScript = "(function(){ " + conditionScript + "})()";
            return(context.Execute <bool>(conditionScript));
        }
        void test2()
        {
            var v = new IronJS.Hosting.CSharp.Context();

            MyClass myClass = new MyClass() { msg = "C#实例" };

              //   将实例映射到JS变量
            v.SetGlobal("myClass", myClass);

             //    创建JS方法
            v.Execute("function sayHello(){ return {msg:'Hello ,' + myClass.msg};}");

               //  通过JS代码调用JS方法返回值
            dynamic dy = v.Execute("sayHello();");

             Console.WriteLine(dy.msg);
        }
Exemplo n.º 11
0
        void test2()
        {
            var v = new IronJS.Hosting.CSharp.Context();

            MyClass myClass = new MyClass()
            {
                msg = "C#实例"
            };

            //   将实例映射到JS变量
            v.SetGlobal("myClass", myClass);

            //    创建JS方法
            v.Execute("function sayHello(){ return {msg:'Hello ,' + myClass.msg};}");

            //  通过JS代码调用JS方法返回值
            dynamic dy = v.Execute("sayHello();");

            Console.WriteLine(dy.msg);
        }