private MSAst.Expression AddModulePublishing(MSAst.Expression body) { if (IsModule) { PythonCompilerOptions pco = _compilerContext.Options as PythonCompilerOptions; string moduleName = ModuleName; if ((pco.Module & ModuleOptions.Initialize) != 0) { var tmp = Ast.Variable(typeof(object), "$originalModule"); // TODO: Should be try/fault body = Ast.Block( new[] { tmp }, AstUtils.Try( Ast.Assign(tmp, Ast.Call(AstMethods.PublishModule, LocalContext, Ast.Constant(moduleName))), body ).Catch( typeof(Exception), Ast.Call(AstMethods.RemoveModule, LocalContext, Ast.Constant(moduleName), tmp), Ast.Rethrow(body.Type) ) ); } } return(body); }
/// <summary> /// Creates a new PythonAst without a body. ParsingFinished should be called afterwards to set /// the body. /// </summary> public PythonAst(bool isModule, ModuleOptions languageFeatures, bool printExpressions, CompilerContext context) { _isModule = isModule; _printExpressions = printExpressions; _languageFeatures = languageFeatures; _mode = ((PythonCompilerOptions)context.Options).CompilationMode ?? GetCompilationMode(context); _compilerContext = context; FuncCodeExpr = _functionCode; PythonCompilerOptions pco = context.Options as PythonCompilerOptions; Debug.Assert(pco != null); string name; if (!context.SourceUnit.HasPath || (pco.Module & ModuleOptions.ExecOrEvalCode) != 0) { name = "<module>"; } else { name = context.SourceUnit.Path; } _name = name; Debug.Assert(_name != null); PythonOptions po = ((PythonContext)context.SourceUnit.LanguageContext).PythonOptions; if (po.EnableProfiler && _mode != CompilationMode.ToDisk) { _profiler = Profiler.GetProfiler(PyContext); } _document = context.SourceUnit.Document ?? Ast.SymbolDocument(name, PyContext.LanguageGuid, PyContext.VendorGuid); }
public override ScriptCode MakeScriptCode(PythonAst ast) { PythonCompilerOptions pco = ast.CompilerContext.Options as PythonCompilerOptions; var code = (MSAst.Expression <Func <CodeContext /*!*/, FunctionCode /*!*/, object> >)ast.Reduce(); return(new PythonSavableScriptCode(code, ast.SourceUnit, ast.GetNames(), pco.ModuleName)); }
/*Dalamar: BEGIN*/ public void Execute(String text) { if (text == null) { return; } ScriptSource m_Source = this.engine.CreateScriptSourceFromString(text); if (m_Source == null) { return; } //EXECUTE //USE: PythonCompilerOptions in order to initialize Python modules correctly, without it the Python env is half broken PythonCompilerOptions pco = (PythonCompilerOptions)this.engine.GetCompilerOptions(this.scope); pco.ModuleName = "__main__"; pco.Module |= ModuleOptions.Initialize; CompiledCode compiled = m_Source.Compile(pco); compiled.Execute(this.scope); //DONT USE: Execute directly, unless you are not planning to import external modules. //m_Source.Execute(m_Scope); }
public override ScriptCode MakeScriptCode(PythonAst ast) { PythonCompilerOptions pco = ast.CompilerContext.Options as PythonCompilerOptions; // reduce to LightLambda then to Lambda var code = (MSAst.Expression <LookupCompilationDelegate>)ast.Reduce().Reduce(); return(new PythonSavableScriptCode(code, ast.SourceUnit, ast.GetNames(), pco.ModuleName)); }
void CreateTokenizer(SourceUnit source) { PythonCompilerSink sink = new PythonCompilerSink(); PythonCompilerOptions options = new PythonCompilerOptions(); tokenizer = new Tokenizer(sink, options); tokenizer.Initialize(source); }
private static Compiler.CompilationMode GetCompilationMode(CompilerContext context) { PythonCompilerOptions options = (PythonCompilerOptions)context.Options; if ((options.Module & ModuleOptions.ExecOrEvalCode) != 0) { return(CompilationMode.Lookup); } PythonContext pc = ((PythonContext)context.SourceUnit.LanguageContext); return(((pc.PythonOptions.Optimize || options.Optimized) && !pc.PythonOptions.LightweightScopes) ? CompilationMode.Uncollectable : CompilationMode.Collectable); }
/// <summary> /// Parses a single interactive command and executes it. /// /// Returns null if successful and execution should continue, or the appropiate exit code. /// </summary> private int?RunOneInteraction() { bool continueInteraction; string s = ReadStatement(out continueInteraction); if (continueInteraction == false) { PythonContext.DispatchCommand(null); // Notify dispatcher that we're done return(0); } if (String.IsNullOrEmpty(s)) { // Is it an empty line? Console.Write(String.Empty, Style.Out); return(null); } SourceUnit su = Language.CreateSnippet(s, "<stdin>", SourceCodeKind.InteractiveCode); PythonCompilerOptions pco = (PythonCompilerOptions)Language.GetCompilerOptions(Scope); pco.Module |= ModuleOptions.ExecOrEvalCode; Action action = delegate() { try { su.Compile(pco, ErrorSink).Run(Scope); } catch (Exception e) { if (e is SystemExitException) { throw; } // Need to handle exceptions in the delegate so that they're not wrapped // in a TargetInvocationException UnhandledException(e); } }; try { PythonContext.DispatchCommand(action); } catch (SystemExitException sx) { object dummy; return(sx.GetExitCode(out dummy)); } return(null); }
public void run(bool withLeak) { //set up script environment Dictionary <String, Object> options = new Dictionary <string, object>(); options["LightweightScopes"] = true; ScriptEngine engine = Python.CreateEngine(options); PythonCompilerOptions pco = (PythonCompilerOptions)engine.GetCompilerOptions(); pco.Module &= ~ModuleOptions.Optimized; engine.SetSearchPaths(new string[] { @"C:\Program Files\IronPython 2.7\Lib" }); ScriptRuntime runtime = engine.Runtime; ScriptScope scope = runtime.CreateScope(); var source = engine.CreateScriptSourceFromString( withLeak ? scriptWithLeak : scriptWithoutLeak ); var comped = source.Compile(); comped.Execute(scope); runtime.Shutdown(); }
/// <summary> /// Runs a script with a different language inside the LoliScript. /// </summary> /// <param name="script">The script as a string with linebreaks</param> /// <param name="language">The language of the script</param> /// <param name="outputs">The variables that should be extracted from the script's scope and set into the BotData local variables</param> /// <param name="data">The BotData needed for variable replacement</param> private void RunScript(string script, ScriptingLanguage language, string outputs, BotData data) { // Set the console output to stringwriter var sw = new StringWriter(); Console.SetOut(sw); Console.SetError(sw); // Parse variables to get out List <string> outVarList = new List <string>(); if (outputs != "") { try { outVarList = outputs.Split(',').Select(x => x.Trim()).ToList(); } catch { } } var start = DateTime.Now; try { switch (language) { case ScriptingLanguage.JavaScript: // Redefine log() function var jsengine = new Engine().SetValue("log", new Action <object>(Console.WriteLine)); // Add in all the variables foreach (var variable in data.Variables.All) { try { jsengine.SetValue(variable.Name, variable.Value); } catch { } } // Execute JS jsengine.Execute(script); // Print results to log data.Log(new LogEntry($"DEBUG LOG: {sw.ToString()}", Colors.White)); // Get variables out data.Log(new LogEntry($"Parsing {outVarList.Count} variables", Colors.White)); foreach (var name in outVarList) { try { // Add it to the variables and print info var value = jsengine.Global.GetProperty(name).Value; var isArray = value.IsArray(); if (isArray) { data.Variables.Set(new CVar(name, CVar.VarType.List, value.TryCast <List <string> >())); } else { data.Variables.Set(new CVar(name, CVar.VarType.Single, value.ToString())); } data.Log(new LogEntry($"SET VARIABLE {name} WITH VALUE {value.ToString()}", Colors.Yellow)); } catch { data.Log(new LogEntry($"COULD NOT FIND VARIABLE {name}", Colors.Tomato)); } } // Print other info if (jsengine.GetCompletionValue() != null) { data.Log(new LogEntry($"Completion value: {jsengine.GetCompletionValue()}", Colors.White)); } break; case ScriptingLanguage.IronPython: // Initialize the engine var runtime = Python.CreateRuntime(); var pyengine = runtime.GetEngine("py"); PythonCompilerOptions pco = (PythonCompilerOptions)pyengine.GetCompilerOptions(); pco.Module &= ~ModuleOptions.Optimized; //var pyengine = Python.CreateEngine(); var scope = pyengine.CreateScope(); var code = pyengine.CreateScriptSourceFromString(script); // Add in all the variables foreach (var variable in data.Variables.All) { try { scope.SetVariable(variable.Name, variable.Value); } catch { } } // Execute it var result = code.Execute(scope); //var result = pyengine.Execute(script, scope); // Print the logs data.Log(new LogEntry($"DEBUG LOG: {sw.ToString()}", Colors.White)); // Get variables out data.Log(new LogEntry($"Parsing {outVarList.Count} variables", Colors.White)); foreach (var name in outVarList) { try { // Add it to the variables and print info var value = scope.GetVariable(name); data.Variables.Set(new CVar(name, (value.GetType() == typeof(string[])) ? CVar.VarType.List : CVar.VarType.Single, value.ToString())); data.Log(new LogEntry($"SET VARIABLE {name} WITH VALUE {value}", Colors.Yellow)); } catch { data.Log(new LogEntry($"COULD NOT FIND VARIABLE {name}", Colors.Tomato)); } } // Print other info if (result != null) { data.Log(new LogEntry($"Completion value: {result}", Colors.White)); } break; default: break; } data.Log(new LogEntry($"Execution completed in {(DateTime.Now - start).TotalSeconds} seconds", Colors.GreenYellow)); } catch (Exception e) { data.Log(new LogEntry($"[ERROR] INFO: {e.Message}", Colors.White)); } finally { var standardOutput = new StreamWriter(Console.OpenStandardOutput()); var standardError = new StreamWriter(Console.OpenStandardError()); standardOutput.AutoFlush = true; standardError.AutoFlush = true; Console.SetOut(standardOutput); Console.SetError(standardError); } }
/// <summary> /// Runs a script with a different language inside the LoliScript. /// </summary> /// <param name="script">The script as a string with linebreaks</param> /// <param name="language">The language of the script</param> /// <param name="outputs">The variables that should be extracted from the script's scope and set into the BotData local variables</param> /// <param name="data">The BotData needed for variable replacement</param> private void RunScript(string script, ScriptingLanguage language, string outputs, BotData data, string jsFilePath = "") { // Set the console output to stringwriter var sw = new StringWriter(); Console.SetOut(sw); Console.SetError(sw); jsFilePath = BlockBase.ReplaceValues(jsFilePath, data); if (jsFilePath != string.Empty && File.Exists(jsFilePath)) { script += File.ReadAllText(jsFilePath); } // Parse variables to get out List <string> outVarList = new List <string>(); if (outputs != string.Empty) { try { outVarList = outputs.Split(',').Select(x => x.Trim()).ToList(); } catch { } } var start = DateTime.Now; try { switch (language) { case ScriptingLanguage.JavaScript: var taskExecuteJs = Task.Run(() => { object jsRetValue = null; bool disposeEngine = false; JavascriptContext context = null; if (disposeEngine = data.JsEngine == null) { context = new JavascriptContext(); JsEngine.SetParameter(context, data); } else { context = data.JsEngine.GetOrCreateEngine(data); } // Execute JS try { jsRetValue = context.Run(script); } catch (JavascriptException ex) { throw new Exception($"Executing js error {ex.Message}\nline: {ex.Line}\nStart column:{ex.StartColumn}"); } // Print results to log data.Log(new LogEntry($"DEBUG LOG: {sw}", Colors.White)); // Get variables out data.Log(new LogEntry($"Parsing {outVarList.Count} variables", Colors.White)); foreach (var name in outVarList) { try { //Add it to the variables and print info var value = context.GetParameter(name); var isArray = value.GetType().IsArray || value is string[] || value.GetType().Name.Contains("Dictionary"); if (isArray) { try { data.Variables.Set(new CVar(name, CVar.VarType.List, (List <string>)value)); } catch (Exception ex) { data.Log(new LogEntry("[SET VARS] ERROR: " + ex.Message, Colors.Yellow)); } } else { data.Variables.Set(new CVar(name, CVar.VarType.Single, value.ToString())); } data.Log(new LogEntry($"SET VARIABLE {name} WITH VALUE {value}", Colors.Yellow)); } catch { data.Log(new LogEntry($"COULD NOT FIND VARIABLE {name}", Colors.Tomato)); } } if (disposeEngine) { context.Dispose(); } //Print other info if (jsRetValue != null && !jsRetValue.GetType().Name.Contains("Dictionary")) { data.Log(new LogEntry($"Return value: {jsRetValue}", Colors.White)); } }); taskExecuteJs.Wait(); taskExecuteJs.Dispose(); break; case ScriptingLanguage.IronPython: // Initialize the engine var runtime = Python.CreateRuntime(); var pyengine = runtime.GetEngine("py"); PythonCompilerOptions pco = (PythonCompilerOptions)pyengine.GetCompilerOptions(); pco.Module &= ~ModuleOptions.Optimized; //var pyengine = Python.CreateEngine(); var scope = pyengine.CreateScope(); var code = pyengine.CreateScriptSourceFromString(script); // Add in all the variables foreach (var variable in data.Variables.All) { try { scope.SetVariable(variable.Name, variable.Value); } catch { } } // Execute it var result = code.Execute(scope); //var result = pyengine.Execute(script, scope); // Print the logs data.Log(new LogEntry($"DEBUG LOG: {sw}", Colors.White)); // Get variables out data.Log(new LogEntry($"Parsing {outVarList.Count} variables", Colors.White)); foreach (var name in outVarList) { try { // Add it to the variables and print info var value = scope.GetVariable(name); data.Variables.Set(new CVar(name, (value.GetType() == typeof(string[])) ? CVar.VarType.List : CVar.VarType.Single, value.ToString())); data.Log(new LogEntry($"SET VARIABLE {name} WITH VALUE {value}", Colors.Yellow)); } catch { data.Log(new LogEntry($"COULD NOT FIND VARIABLE {name}", Colors.Tomato)); } } // Print other info if (result != null) { data.Log(new LogEntry($"Completion value: {result}", Colors.White)); } break; default: break; } data.Log(new LogEntry($"Execution completed in {(DateTime.Now - start).TotalSeconds} seconds", Colors.GreenYellow)); } catch (Exception e) { data.Log(new LogEntry($"[ERROR] INFO: {e.Message}", Colors.White)); } finally { var standardOutput = new StreamWriter(Console.OpenStandardOutput()); var standardError = new StreamWriter(Console.OpenStandardError()); standardOutput.AutoFlush = true; standardError.AutoFlush = true; Console.SetOut(standardOutput); Console.SetError(standardError); } }
private void AsyncStart() { if (World.Player == null) { return; } try { string fullpath = Settings.GetFullPathForScript(m_Filename); string ext = Path.GetExtension(fullpath); if (ext.Equals(".cs", StringComparison.InvariantCultureIgnoreCase)) { CSharpEngine csharpEngine = CSharpEngine.Instance; bool compileErrors = csharpEngine.CompileFromFile(fullpath, true, out StringBuilder compileMessages, out Assembly assembly); if (compileMessages.Length > 0) { Misc.SendMessage(compileMessages.ToString()); } if (compileErrors == true) { Stop(); return; } csharpEngine.Execute(assembly); } else if (ext.Equals(".uos", StringComparison.InvariantCultureIgnoreCase)) { // Using // only will be deprecated instead of //UOS var text = System.IO.File.ReadAllLines(fullpath); if ((text[0].Substring(0, 2) == "//") && text[0].Length < 5) { string message = "WARNING: // header for UOS scripts is going to be deprecated. Please use //UOS instead"; Misc.SendMessage(message); } UOSteamEngine uosteam = UOSteamEngine.Instance; uosteam.Execute(fullpath); } else { DateTime lastModified = System.IO.File.GetLastWriteTime(fullpath); if (FileChangeDate < lastModified) { ReadText(fullpath); FileChangeDate = System.IO.File.GetLastWriteTime(fullpath); Create(null); } /*Dalamar: BEGIN "fix python env" */ //EXECUTION OF THE SCRIPT //Refactoring option, the whole block can be replaced by: // //m_pe.Execute(m_Text); m_Source = m_Engine.CreateScriptSourceFromString(m_Text); // "+": USE PythonCompilerOptions in order to initialize Python modules correctly, without it the Python env is half broken PythonCompilerOptions pco = (PythonCompilerOptions)m_Engine.GetCompilerOptions(m_Scope); pco.ModuleName = "__main__"; pco.Module |= ModuleOptions.Initialize; CompiledCode compiled = m_Source.Compile(pco); Journal journal = m_Engine.Runtime.Globals.GetVariable("Journal") as Journal; journal.Active = true; compiled.Execute(m_Scope); journal.Active = false; // "-": DONT execute directly, unless you are not planning to import external modules. //m_Source.Execute(m_Scope); /*Dalamar: END*/ } } catch (IronPython.Runtime.Exceptions.SystemExitException) { Stop(); // sys.exit - terminate the thread } catch (Exception ex) { if (ex is System.Threading.ThreadAbortException) { return; } string display_error = ex.Message; if (m_Engine != null) { display_error = m_Engine.GetService <ExceptionOperations>().FormatException(ex); } SendMessageScriptError("ERROR " + m_Filename + ":" + display_error.Replace("\n", " | ")); if (ScriptErrorLog) // enabled log of error { StringBuilder log = new StringBuilder(); log.Append(Environment.NewLine + "============================ START REPORT ============================ " + Environment.NewLine); DateTime dt = DateTime.Now; log.Append("---> Time: " + String.Format("{0:F}", dt) + Environment.NewLine); log.Append(Environment.NewLine); if (ex is SyntaxErrorException) { SyntaxErrorException se = ex as SyntaxErrorException; log.Append("----> Syntax Error:" + Environment.NewLine); log.Append("-> LINE: " + se.Line + Environment.NewLine); log.Append("-> COLUMN: " + se.Column + Environment.NewLine); log.Append("-> SEVERITY: " + se.Severity + Environment.NewLine); log.Append("-> MESSAGE: " + se.Message + Environment.NewLine); } else { log.Append("----> Generic Error:" + Environment.NewLine); log.Append(display_error); } log.Append(Environment.NewLine); log.Append("============================ END REPORT ============================ "); log.Append(Environment.NewLine); try // For prevent crash in case of file are busy or inaccessible { File.AppendAllText(Assistant.Engine.RootPath + "\\" + m_Filename + ".ERROR", log.ToString()); } catch { } log.Clear(); } } }
private void AsyncStart() { if (World.Player == null) { return; } try { string fullpath = Path.Combine(Assistant.Engine.RootPath, "Scripts", m_Filename); string ext = Path.GetExtension(fullpath); if (ext.Equals(".uos", StringComparison.InvariantCultureIgnoreCase)) { UOSteamEngine uosteam = new UOSteamEngine(); uosteam.Execute(fullpath); } else { DateTime lastModified = System.IO.File.GetLastWriteTime(fullpath); if (FileChangeDate < lastModified) { ReadText(); FileChangeDate = System.IO.File.GetLastWriteTime(fullpath); Create(null); } /*Dalamar: BEGIN "fix python env" */ //EXECUTION OF THE SCRIPT //Refactoring option, the whole block can be replaced by: // //m_pe.Execute(m_Text); m_Source = m_Engine.CreateScriptSourceFromString(m_Text); // "+": USE PythonCompilerOptions in order to initialize Python modules correctly, without it the Python env is half broken PythonCompilerOptions pco = (PythonCompilerOptions)m_Engine.GetCompilerOptions(m_Scope); pco.ModuleName = "__main__"; pco.Module |= ModuleOptions.Initialize; CompiledCode compiled = m_Source.Compile(pco); compiled.Execute(m_Scope); // "-": DONT execute directly, unless you are not planning to import external modules. //m_Source.Execute(m_Scope); /*Dalamar: END*/ } } catch (IronPython.Runtime.Exceptions.SystemExitException ex) { Stop(); // sys.exit - terminate the thread } catch (Exception ex) { if (ex is System.Threading.ThreadAbortException) { return; } string display_error = m_Engine.GetService <ExceptionOperations>().FormatException(ex); SendMessageScriptError("ERROR " + m_Filename + ":" + display_error.Replace("\n", " | ")); if (ScriptErrorLog) // enabled log of error { StringBuilder log = new StringBuilder(); log.Append(Environment.NewLine + "============================ START REPORT ============================ " + Environment.NewLine); DateTime dt = DateTime.Now; log.Append("---> Time: " + String.Format("{0:F}", dt) + Environment.NewLine); log.Append(Environment.NewLine); if (ex is SyntaxErrorException) { SyntaxErrorException se = ex as SyntaxErrorException; log.Append("----> Syntax Error:" + Environment.NewLine); log.Append("-> LINE: " + se.Line + Environment.NewLine); log.Append("-> COLUMN: " + se.Column + Environment.NewLine); log.Append("-> SEVERITY: " + se.Severity + Environment.NewLine); log.Append("-> MESSAGE: " + se.Message + Environment.NewLine); } else { log.Append("----> Generic Error:" + Environment.NewLine); ExceptionOperations eo = m_Engine.GetService <ExceptionOperations>(); string error = eo.FormatException(ex); log.Append(error); } log.Append(Environment.NewLine); log.Append("============================ END REPORT ============================ "); log.Append(Environment.NewLine); try // For prevent crash in case of file are busy or inaccessible { File.AppendAllText(Assistant.Engine.RootPath + "\\" + m_Filename + ".ERROR", log.ToString()); } catch { } log.Clear(); } } }