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