1 //
2 // Copyright (c) 2010-2024 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.Logging;
10 using Antmicro.Renode.Peripherals.CPU;
11 using Antmicro.Migrant.Hooks;
12 using Microsoft.Scripting.Hosting;
13 using Antmicro.Migrant;
14 
15 namespace Antmicro.Renode.Hooks
16 {
17     public sealed class PSCIPythonEngine : PythonEngine
18     {
PSCIPythonEngine(ICPUWithPSCI cpu, string script, ulong functionIdentifier)19         public PSCIPythonEngine(ICPUWithPSCI cpu, string script, ulong functionIdentifier)
20         {
21             this.script = script;
22             this.cpu = cpu;
23 
24             InnerInit();
25 
26             Hook = () =>
27             {
28                 Scope.SetVariable("self", cpu);
29                 Scope.SetVariable("function_id", functionIdentifier);
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", cpu);
41             var source = Engine.CreateScriptSourceFromString(script);
42             code = Compile(source);
43         }
44 
45         public Action Hook { get; }
46 
47         [Transient]
48         private CompiledCode code;
49         private readonly string script;
50         private readonly ICPUWithPSCI cpu;
51     }
52 }
53 
54