protected override ScriptRuntime /*!*/ CreateRuntime()
        {
            var setup = new ScriptRuntimeSetup();

            setup.AddRubySetup();
            return(_factory.CreateRuntime(setup));
        }
示例#2
0
        protected override ScriptRuntime /*!*/ CreateRuntime()
        {
            string        root              = IronRubyToolsPackage.Instance.IronRubyBinPath;
            string        configPath        = IronRubyToolsPackage.Instance.IronRubyExecutable + ".config";
            LanguageSetup existingRubySetup = null;
            LanguageSetup rubySetup         = Ruby.CreateRubySetup();

            if (File.Exists(configPath))
            {
                try {
                    existingRubySetup = GetSetupByName(ScriptRuntimeSetup.ReadConfiguration(configPath), "IronRuby");
                } catch {
                    // TODO: report the error
                }
            }

            if (existingRubySetup != null)
            {
                var options = new RubyOptions(existingRubySetup.Options);
                rubySetup.Options["StandardLibraryPath"] = NormalizePaths(root, new[] { options.StandardLibraryPath })[0];
                rubySetup.Options["RequiredPaths"]       = NormalizePaths(root, options.RequirePaths);
                rubySetup.Options["SearchPaths"]         = NormalizePaths(root, options.SearchPaths);
            }

            var runtimeSetup = new ScriptRuntimeSetup();

            runtimeSetup.LanguageSetups.Add(rubySetup);
            return(RemoteScriptFactory.CreateRuntime(runtimeSetup));
        }
        protected override ScriptEngine MakeEngine(Stream stream, TextWriter writer, TextReader reader)
        {
            _factory.SetConsoleOut(writer);
            _factory.SetConsoleError(writer);
            _factory.SetConsoleIn(reader);

            var runtime = (ScriptRuntime)_factory.CreateRuntime(Python.CreateRuntimeSetup(GetOptions()));
            var res     = runtime.GetEngine("Python");

            InitializeEngine(stream, writer, res);
            return(res);
        }
示例#4
0
        public void Scenario_RemoteScriptFactory()
        {
            using (var factory = RemotePythonEvaluator.CreateFactory()) {
                var          runtime   = (ScriptRuntime)factory.CreateRuntime(Python.CreateRuntimeSetup(new Dictionary <string, object>()));
                StringWriter writer    = new StringWriter();
                StringWriter errWriter = new StringWriter();

                runtime.IO.SetOutput(Stream.Null, writer);
                factory.SetConsoleOut(writer);
                factory.SetConsoleError(errWriter);

                // verify print goes to the correct output
                var engine = runtime.GetEngine("Python");
                engine.Execute("print 'hello'");
                var builder = writer.GetStringBuilder();

                AreEqual(builder.ToString(), "hello\r\n");
                builder.Clear();

                // verify Console.WriteLine is redirected
                engine.Execute("import System\nSystem.Console.WriteLine('hello')\n");
                AreEqual(builder.ToString(), "hello\r\n");
                builder.Clear();

                // verify Console.Error.WriteLine is redirected to stderr
                var errBuilder = errWriter.GetStringBuilder();
                engine.Execute("import System\nSystem.Console.Error.WriteLine('hello')\n");
                AreEqual(errBuilder.ToString(), "hello\r\n");
                errBuilder.Clear();

                // raise an exception, should be propagated back
                try {
                    engine.Execute("import System\nraise System.ArgumentException()\n");
                    AreEqual(true, false);
                } catch (ArgumentException) {
                }

                /*
                 * // verify that all code runs on the same thread
                 * var scope = engine.CreateScope();
                 * engine.Execute("import System");
                 *
                 *
                 * List<object> res = new List<object>();
                 * for (int i = 0; i < 100; i++) {
                 *  ThreadPool.QueueUserWorkItem(
                 *      (x) => {
                 *          object value = engine.Execute("System.Threading.Thread.CurrentThread.ManagedThreadId", scope);
                 *          lock (res) {
                 *              res.Add(value);
                 *          }
                 *  });
                 * }
                 *
                 * while (res.Count != 100) {
                 *  Thread.Sleep(100);
                 * }
                 *
                 * for (int i = 1; i < res.Count; i++) {
                 *  if (!res[i - 1].Equals(res[i])) {
                 *      throw new Exception("running on multiple threads");
                 *  }
                 * }*/

                // create a long running work item, execute it, and then make sure we can continue to execute work items.
                ThreadPool.QueueUserWorkItem(x => {
                    engine.Execute("while True: pass");
                });
                Thread.Sleep(1000);
                factory.Abort();

                AreEqual(engine.Execute("42"), 42);
            }

            // check starting on an MTA thread
            using (var factory = new RemoteScriptFactory(ApartmentState.MTA)) {
                var runtime = (ScriptRuntime)factory.CreateRuntime(Python.CreateRuntimeSetup(new Dictionary <string, object>()));
                var engine  = runtime.GetEngine("Python");
                AreEqual(engine.Execute("import System\nSystem.Threading.Thread.CurrentThread.ApartmentState == System.Threading.ApartmentState.MTA"), true);
            }

            // check starting on an STA thread
            using (var factory = new RemoteScriptFactory(ApartmentState.STA)) {
                var runtime = (ScriptRuntime)factory.CreateRuntime(Python.CreateRuntimeSetup(new Dictionary <string, object>()));
                var engine  = runtime.GetEngine("Python");
                AreEqual(engine.Execute("import System\nSystem.Threading.Thread.CurrentThread.ApartmentState == System.Threading.ApartmentState.STA"), true);
            }
        }