Пример #1
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));
        }
        public void Initialize()
        {
            string filename, dir;

            if (CommonPackage.TryGetStartupFileAndDirectory(out filename, out dir))
            {
                RemoteScriptFactory.SetCurrentDirectory(dir);
            }
        }
        public override void Restart()
        {
            WriteLine("Remote process has exited, restarting...");
            _factory = CreateFactory();
            Start();

            var changed = AvailableScopesChanged;

            if (changed != null)
            {
                changed(this, EventArgs.Empty);
            }
        }
        public override void Restart()
        {
            WriteLine("Remote process has exited, restarting...");
            _factory = CreateFactory();
            Start();
            _factory.CommandDispatcher = _engine.GetService <PythonService>(_engine).GetLocalCommandDispatcher();

            var changed = AvailableScopesChanged;

            if (changed != null)
            {
                changed(this, EventArgs.Empty);
            }
        }
        public override void Reset()
        {
            // TODO: strange text buffer behavior (race condition?)
            // WriteLine("Remote process has been reset...");
            _factory.Shutdown();

            _factory = CreateFactory();
            Start();

            var changed = AvailableScopesChanged;

            if (changed != null)
            {
                changed(this, EventArgs.Empty);
            }
        }
Пример #6
0
        /// <summary>
        /// Called when we start the remote server
        /// </summary>
        private static int Main(string[] args)
        {
            ApartmentState state;

            if (args.Length != 1 || !Enum.TryParse <ApartmentState>(args[0], out state))
            {
                Console.WriteLine("Expected no arguments");
                return(1);
            }

            try {
                RemoteScriptFactory.RunServer(state);
                return(0);
            } catch (Exception e) {
                Console.WriteLine(e);
                return(2);
            }
        }
Пример #7
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);
            }
        }
 // Constructed via reflection when deserialized from the registry.
 public RemotePythonEvaluator()
 {
     _factory = CreateFactory();
 }
 // Constructed via reflection when deserialized from the registry.
 public RemoteRubyEvaluator()
 {
     _factory = CreateFactory();
 }