1 // 2 // Copyright (c) 2010-2022 Antmicro 3 // Copyright (c) 2011-2015 Realtime Embedded 4 // 5 // This file is licensed under the MIT License. 6 // Full license text is available in 'licenses/MIT.txt'. 7 // 8 using System; 9 using Antmicro.Renode.Core; 10 using Antmicro.Renode.Exceptions; 11 using Antmicro.Renode.Logging; 12 using Antmicro.Renode.Peripherals.CPU; 13 using Antmicro.Migrant.Hooks; 14 using Microsoft.Scripting.Hosting; 15 using Antmicro.Migrant; 16 17 namespace Antmicro.Renode.Hooks 18 { 19 public sealed class UserStatePythonEngine : PythonEngine 20 { UserStatePythonEngine(IMachine machine, string script)21 public UserStatePythonEngine(IMachine machine, string script) 22 { 23 Script = script; 24 Machine = machine; 25 26 InnerInit(); 27 28 Hook = state => 29 { 30 Scope.SetVariable("state", state); 31 Execute(code, error => 32 { 33 Logger.Log(LogLevel.Error, "Python runtime error: {0}", error); 34 }); 35 }; 36 } 37 38 [PostDeserialization] InnerInit()39 private void InnerInit() 40 { 41 Scope.SetVariable(Core.Machine.MachineKeyword, Machine); 42 Scope.SetVariable("self", Machine); 43 var source = Engine.CreateScriptSourceFromString(Script); 44 code = Compile(source); 45 } 46 47 public Action<string> Hook { get; private set; } 48 49 [Transient] 50 private CompiledCode code; 51 private readonly string Script; 52 private readonly IMachine Machine; 53 } 54 } 55 56