Skip to content

Commit

Permalink
Merge pull request #7 from cesare-montresor/ScriptEngine
Browse files Browse the repository at this point in the history
Script engine tweaks
  • Loading branch information
cesare-montresor authored Dec 28, 2023
2 parents 2ac76d3 + cbfddf2 commit 6b5d408
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 201 deletions.
2 changes: 1 addition & 1 deletion Razor/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:

[assembly: AssemblyVersion("0.8.2.100")]
[assembly: AssemblyVersion("0.8.2.101")]

//
// In order to sign your assembly you must specify a key to use. Refer to the
Expand Down
71 changes: 53 additions & 18 deletions Razor/RazorEnhanced/EnhancedScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Threading.Tasks;
using System.Management.Instrumentation;
using Microsoft.Scripting.Utils;
using System.Diagnostics.Tracing;

namespace RazorEnhanced
{
Expand All @@ -45,26 +46,31 @@ public class EnhancedScriptService {
internal List<EnhancedScript> ScriptListTabCs() { return ScriptListTab().Where(script => script.Language == ScriptLanguage.CSHARP).ToList(); }
internal List<EnhancedScript> ScriptListTabUos() { return ScriptListTab().Where(script => script.Language == ScriptLanguage.UOSTEAM).ToList(); }

public event Action<EnhancedScript, bool> OnScriptListChanged;

internal bool AddScript(EnhancedScript script)
{
bool result = true;
if (m_ScriptList.ContainsKey(script.Fullpath))
{
m_ScriptList[script.Fullpath] = script;
return true;
}
else
{
return m_ScriptList.TryAdd(script.Fullpath, script);
result = m_ScriptList.TryAdd(script.Fullpath, script);
}
OnScriptListChanged?.Invoke(script, result);
return result;
}
internal bool RemoveScript(EnhancedScript script)
{
bool result = true;
if (m_ScriptList.ContainsKey(script.Fullpath))
{
return m_ScriptList.TryRemove(script.Fullpath, out _);
result = m_ScriptList.TryRemove(script.Fullpath, out _);
}
return true;
OnScriptListChanged?.Invoke(script, !result);
return result;
}

internal EnhancedScript CurrentScript()
Expand Down Expand Up @@ -144,7 +150,11 @@ public class EnhancedScript

private readonly object m_Lock = new object();


public event Action<EnhancedScript, bool> OnLoad;
public event Action<EnhancedScript> OnStart;
public event Action<EnhancedScript> OnStop;
public event Action<EnhancedScript, string> OnError;
public event Action<EnhancedScript, string> OnOutput;

public static ScriptLanguage ExtToLanguage(string extenstion)
{
Expand Down Expand Up @@ -264,15 +274,22 @@ public ScriptItem ToScriptItem()

public bool Load(bool force = false)
{
string content = "";
string content = m_Text ?? "";
try
{
LastModified = File.GetLastWriteTime(Fullpath);
content = File.ReadAllText(Fullpath);
if (this.Exist)
{
LastModified = File.GetLastWriteTime(Fullpath);
content = File.ReadAllText(Fullpath);
}
else {
Misc.SendMessage("file not found:" + this.Filename, 178);
}
}
catch (Exception e)
{
Misc.SendMessage("ERROR:EnhancedScript:Load: " + e.Message, 178);
OnLoad?.Invoke(this, false);
return false;
}

Expand All @@ -282,6 +299,7 @@ public bool Load(bool force = false)
m_Text = content;
InitEngine();
}
OnLoad?.Invoke(this,true);
return true;
}

Expand Down Expand Up @@ -362,7 +380,9 @@ internal void Start()

//m_Run = true;
}
catch { }
catch (Exception e){
OnError.Invoke(this, e.Message);
}
}

private void AsyncStart()
Expand All @@ -374,11 +394,17 @@ private void AsyncStart()
try
{
//EventManager.Instance.Unsubscribe(m_Thread);
OnStart?.Invoke(this);
m_ScriptEngine.Run();
OnStop?.Invoke(this);
}
catch (ThreadAbortException ex) {
OnStop?.Invoke(this);
return;
}
catch (ThreadAbortException ex) { return; }
catch (Exception ex)
{
OnError?.Invoke(this, ex.Message);
Misc.SendMessage($"EnhancedScript:AsyncStart:{ex.GetType()}:\n{ex.Message}", 138);
}

Expand Down Expand Up @@ -789,14 +815,23 @@ private bool RunPython()
{
pyEngine.Engine.SetTrace(pyTraceback);
}
if (m_StderrWriter != null)
{
pyEngine.SetStderr(m_StderrWriter);
}
if (m_StdoutWriter != null)
{
pyEngine.SetStdout(m_StdoutWriter);
}

pyEngine.SetStderr(
(string message) => {
Misc.SendMessage(message,178);
if (m_StderrWriter == null) return;
m_StderrWriter.Invoke(message);
}
);

pyEngine.SetStdout(
(string message) => {
Misc.SendMessage(message);
if (m_StdoutWriter == null) return;
m_StdoutWriter.Invoke(message);
}
);

return pyEngine.Execute();
} catch (Exception ex)
{
Expand Down
35 changes: 23 additions & 12 deletions Razor/RazorEnhanced/PythonEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@ namespace RazorEnhanced
// --------------------------------------------------------- PythonEngine --------------------------------------------------------
public class PythonEngine
{


public Dictionary<string, object> Modules;

public static ScriptRuntime Runtime;
public ScriptEngine Engine { get; }
public ScriptScope Scope { get; set; }
public String Text { get; set; }
public String FilePath { get; set; }
public ScriptSource Source { get; set; }
public CompiledCode Compiled { get; set; }
public PythonCompilerOptions CompilerOptions { get; set; }



public PythonEngine() {
var runtime = IronPython.Hosting.Python.CreateRuntime();
Engine = IronPython.Hosting.Python.GetEngine(runtime);
if (Runtime == null) {
Runtime = IronPython.Hosting.Python.CreateRuntime();
}
Engine = IronPython.Hosting.Python.GetEngine(Runtime);

//Paths for IronPython 3.4
var paths = new List<string>();
Expand Down Expand Up @@ -86,14 +93,10 @@ public PythonEngine() {

//Setup builtin modules and scope
foreach (var module in Modules) {
Engine.Runtime.Globals.SetVariable(module.Key, module.Value);
Runtime.Globals.SetVariable(module.Key, module.Value);
Engine.GetBuiltinModule().SetVariable(module.Key, module.Value);
}
Scope = Engine.CreateScope();

CompilerOptions = (PythonCompilerOptions)Engine.GetCompilerOptions(Scope);
CompilerOptions.ModuleName = "__main__";
CompilerOptions.Module |= ModuleOptions.Initialize;

}

~PythonEngine() {
Expand Down Expand Up @@ -156,17 +159,25 @@ public bool Load(String text, String path = null)

//COMPILE with OPTIONS
//PythonCompilerOptions in order to initialize Python modules correctly, without it the Python env is half broken
Scope = Engine.CreateScope();

CompilerOptions = (PythonCompilerOptions)Engine.GetCompilerOptions(Scope);
CompilerOptions.ModuleName = "__main__";
CompilerOptions.Module |= ModuleOptions.Initialize;
CompilerOptions.Optimized = true;

Compiled = Source.Compile(CompilerOptions);
if (Compiled == null) { return false; }

Scope = Engine.CreateScope();
return true;
}
public bool Execute() {
//EXECUTE
if (Source == null) { return false; }
if (Compiled == null) { return false; }
if (Scope == null) { return false; }
if (Scope == null) { return false; }
else if (Compiled == null) { return false; }
else if (Source == null) { return false; }



Journal journal = Modules["Journal"] as Journal;
journal.Active = true;
Expand Down
Loading

0 comments on commit 6b5d408

Please sign in to comment.