Exemple #1
0
        private static RekoEventsAPI Evaluate(
            TextWriter outputWriter,
            DecompilerEventListener eventListener,
            IConfigurationService cfgSvc,
            IFileSystemService fsSvc,
            string script,
            string filename)
        {
            var engine = CreateEngine(outputWriter);

            outputWriter.WriteLine($"Evaluating {filename}");
            var pythonAPI = new PythonAPI(cfgSvc, fsSvc, engine);
            var eventsAPI = new RekoEventsAPI(engine);
            var scope     = CreateRekoVariable(engine, pythonAPI, eventsAPI);
            var src       = engine.CreateScriptSourceFromString(script, filename);

            try
            {
                src.Execute(scope);
            }
            catch (Exception ex)
            {
                var scriptError = CreateError(
                    filename,
                    ex,
                    "An error occurred while evaluating the Python script.",
                    engine);
                eventListener.Error(scriptError);
                DumpPythonStack(outputWriter, ex, engine);
                return(new RekoEventsAPI(engine));
            }
            return(eventsAPI);
        }
Exemple #2
0
        private static ScriptScope CreateRekoVariable(
            ScriptEngine engine,
            PythonAPI pythonAPI,
            RekoEventsAPI eventsAPI)
        {
            var scope       = engine.CreateScope();
            var rekoWrapper = pythonAPI.CreateRekoWrapper(eventsAPI);

            scope.SetVariable("reko", rekoWrapper);
            return(scope);
        }
Exemple #3
0
        public PythonModule(
            IServiceProvider services, string filename, byte[] bytes)
            : base(services, filename, bytes)
        {
            this.eventListener = services.RequireService <DecompilerEventListener>();
            this.engine        = IronPython.Hosting.Python.CreateEngine();
            // $TODO: Redirect script output to Reko Console tab
            engine.Runtime.IO.RedirectToConsole();
            this.pythonAPI = new PythonAPI(services, engine);
            var stream = new MemoryStream(bytes);

            using var rdr  = new StreamReader(stream);
            this.eventsAPI = Evaluate(rdr.ReadToEnd(), filename);
        }
Exemple #4
0
        public PythonModule(
            IServiceProvider services, string filename, byte[] bytes)
            : base(services, filename, bytes)
        {
            this.eventListener = services.RequireService <DecompilerEventListener>();
            this.engine        = IronPython.Hosting.Python.CreateEngine();
            var outputService = services.RequireService <IOutputService>();

            this.outputWriter = outputService.EnsureOutputSource("Scripting");
            RedirectConsoleOutput(outputWriter, engine);
            this.pythonAPI = new PythonAPI(services, engine);
            var stream = new MemoryStream(bytes);

            using var rdr  = new StreamReader(stream);
            this.eventsAPI = Evaluate(rdr.ReadToEnd(), filename);
        }
Exemple #5
0
        public override void FireEvent(ScriptEvent @event, Program program)
        {
            var eventsAPI = this.eventsAPI;
            var engine    = eventsAPI.Engine;

            try
            {
                var pythonAPI      = new PythonAPI(cfgSvc, fsSvc, engine);
                var programAPI     = new RekoProgramAPI(program);
                var programWrapper = pythonAPI.CreateProgramWrapper(
                    programAPI);
                eventsAPI.FireEvent(@event, programWrapper);
            }
            catch (Exception ex)
            {
                var scriptError = CreateError(
                    Location.FilesystemPath,
                    ex,
                    "An error occurred while running the Python script.",
                    engine);
                eventListener.Error(scriptError);
                DumpPythonStack(outputWriter, ex, engine);
            }
        }