internal static int LoadFunction(Script script, SourceCode source, ByteCode bytecode, bool usesGlobalEnv) { ScriptLoadingContext lcontext = CreateLoadingContext(script, source); try { FunctionDefinitionExpression fnx; using (script.PerformanceStats.StartStopwatch(Diagnostics.PerformanceCounter.AstCreation)) fnx = new FunctionDefinitionExpression(lcontext, usesGlobalEnv); int beginIp = -1; //var srcref = new SourceRef(source.SourceID); using (script.PerformanceStats.StartStopwatch(Diagnostics.PerformanceCounter.Compilation)) using (bytecode.EnterSource(null)) { bytecode.Emit_Nop(string.Format("Begin function {0}", source.Name)); beginIp = fnx.CompileBody(bytecode, source.Name); bytecode.Emit_Nop(string.Format("End function {0}", source.Name)); } //Debug_DumpByteCode(bytecode, source.SourceID); return beginIp; } catch (SyntaxErrorException ex) { ex.DecorateMessage(script); throw; } }
internal static int LoadChunk(Script script, SourceCode source, ByteCode bytecode) { ScriptLoadingContext lcontext = CreateLoadingContext(script, source); try { Statement stat; using (script.PerformanceStats.StartStopwatch(Diagnostics.PerformanceCounter.AstCreation)) stat = new ChunkStatement(lcontext); int beginIp = -1; //var srcref = new SourceRef(source.SourceID); using (script.PerformanceStats.StartStopwatch(Diagnostics.PerformanceCounter.Compilation)) using (bytecode.EnterSource(null)) { bytecode.Emit_Nop(string.Format("Begin chunk {0}", source.Name)); beginIp = bytecode.GetJumpPointForLastInstruction(); stat.Compile(bytecode); bytecode.Emit_Nop(string.Format("End chunk {0}", source.Name)); } //Debug_DumpByteCode(bytecode, source.SourceID); return beginIp; } catch (SyntaxErrorException ex) { ex.DecorateMessage(script); throw; } }
private static ScriptLoadingContext CreateLoadingContext(Script script, SourceCode source) { return new ScriptLoadingContext(script) { Scope = new BuildTimeScope(), Source = source, Lexer = new Lexer(source.SourceID, source.Code, true) }; }
public void SetSourceCode(SourceCode sourceCode) { Send(xw => { using (xw.Element("source-code")) { xw.Attribute("id", sourceCode.SourceID) .Attribute("name", sourceCode.Name); foreach (string line in sourceCode.Lines) xw.ElementCData("l", EpurateNewLines(line)); } }); }
internal static DynamicExprExpression LoadDynamicExpr(Script script, SourceCode source) { ScriptLoadingContext lcontext = CreateLoadingContext(script, source); try { lcontext.IsDynamicExpression = true; lcontext.Anonymous = true; Expression exp; using (script.PerformanceStats.StartStopwatch(Diagnostics.PerformanceCounter.AstCreation)) exp = Expression.Expr(lcontext); return new DynamicExprExpression(exp, lcontext); } catch (SyntaxErrorException ex) { ex.DecorateMessage(script); throw; } }
/// <summary> /// Loads a Lua/MoonSharp script from a System.IO.Stream. NOTE: This will *NOT* close the stream! /// </summary> /// <param name="stream">The stream containing code.</param> /// <param name="globalTable">The global table to bind to this chunk.</param> /// <param name="codeFriendlyName">Name of the code - used to report errors, etc.</param> /// <returns> /// A DynValue containing a function which will execute the loaded code. /// </returns> public DynValue LoadStream(Stream stream, Table globalTable = null, string codeFriendlyName = null) { this.CheckScriptOwnership(globalTable); Stream codeStream = new UndisposableStream(stream); if (!Processor.IsDumpStream(codeStream)) { using (StreamReader sr = new StreamReader(codeStream)) { string scriptCode = sr.ReadToEnd(); return LoadString(scriptCode, globalTable, codeFriendlyName); } } else { string chunkName = string.Format("{0}", codeFriendlyName ?? "dump_" + m_Sources.Count.ToString()); SourceCode source = new SourceCode(codeFriendlyName ?? chunkName, string.Format("-- This script was decoded from a binary dump - dump_{0}", m_Sources.Count), m_Sources.Count, this); m_Sources.Add(source); bool hasUpvalues; int address = m_MainProcessor.Undump(codeStream, m_Sources.Count - 1, globalTable ?? m_GlobalTable, out hasUpvalues); SignalSourceCodeChange(source); SignalByteCodeChange(); if (hasUpvalues) return MakeClosure(address, globalTable ?? m_GlobalTable); else return MakeClosure(address); } }
/// <summary> /// Loads a string containing a Lua/MoonSharp script. /// </summary> /// <param name="code">The code.</param> /// <param name="globalTable">The global table to bind to this chunk.</param> /// <param name="codeFriendlyName">Name of the code - used to report errors, etc.</param> /// <returns> /// A DynValue containing a function which will execute the loaded code. /// </returns> public DynValue LoadString(string code, Table globalTable = null, string codeFriendlyName = null) { this.CheckScriptOwnership(globalTable); if (code.StartsWith(StringModule.BASE64_DUMP_HEADER)) { code = code.Substring(StringModule.BASE64_DUMP_HEADER.Length); byte[] data = Convert.FromBase64String(code); using (MemoryStream ms = new MemoryStream(data)) return LoadStream(ms, globalTable, codeFriendlyName); } string chunkName = string.Format("{0}", codeFriendlyName ?? "chunk_" + m_Sources.Count.ToString()); SourceCode source = new SourceCode(codeFriendlyName ?? chunkName, code, m_Sources.Count, this); m_Sources.Add(source); int address = Loader_Fast.LoadChunk(this, source, m_ByteCode, globalTable ?? m_GlobalTable); SignalSourceCodeChange(source); SignalByteCodeChange(); return MakeClosure(address); }
private void SignalSourceCodeChange(SourceCode source) { if (m_Debugger != null) { m_Debugger.SetSourceCode(source); } }
/// <summary> /// Loads a string containing a Lua/MoonSharp function. /// </summary> /// <param name="code">The code.</param> /// <param name="globalTable">The global table to bind to this chunk.</param> /// <param name="funcFriendlyName">Name of the function used to report errors, etc.</param> /// <returns> /// A DynValue containing a function which will execute the loaded code. /// </returns> public DynValue LoadFunction(string code, Table globalTable = null, string funcFriendlyName = null) { this.CheckScriptOwnership(globalTable); string chunkName = string.Format("libfunc_{0}", funcFriendlyName ?? m_Sources.Count.ToString()); SourceCode source = new SourceCode(chunkName, code, m_Sources.Count, this); m_Sources.Add(source); int address = Loader_Fast.LoadFunction(this, source, m_ByteCode, globalTable ?? m_GlobalTable); SignalSourceCodeChange(source); SignalByteCodeChange(); return MakeClosure(address); }
internal HashSet<int> ResetBreakPoints(SourceCode src, HashSet<int> lines) { HashSet<int> result = new HashSet<int>(); foreach (SourceRef srf in src.Refs) { if (srf.CannotBreakpoint) continue; srf.Breakpoint = lines.Contains(srf.FromLine); if (srf.Breakpoint) result.Add(srf.FromLine); } return result; }
public void SetSourceCode(SourceCode sourceCode) { }
void IDebugger.SetSourceCode(SourceCode sourceCode) { }
/// <summary> /// Resets the break points for a given file. Supports only line-based breakpoints. /// </summary> /// <param name="src">The source.</param> /// <param name="lines">The lines.</param> /// <returns>The lines for which breakpoints have been set</returns> public HashSet<int> ResetBreakPoints(SourceCode src, HashSet<int> lines) { return m_Processor.ResetBreakPoints(src, lines); }
/// <summary> /// Resets the break points for a given file. Supports only line-based breakpoints. /// </summary> /// <param name="src">The source.</param> /// <param name="lines">The lines.</param> /// <returns>The lines for which breakpoints have been set</returns> public HashSet <int> ResetBreakPoints(SourceCode src, HashSet <int> lines) { return(m_Processor.ResetBreakPoints(src, lines)); }
void IDebugger.SetSourceCode(SourceCode sourceCode) { m_SourcesMap[sourceCode.SourceID] = sourceCode; bool invalidFile = false; string file = m_SourceFinder(sourceCode); if (!string.IsNullOrEmpty(file)) { try { if (!File.Exists(file)) invalidFile = true; } catch { invalidFile = true; } } else { invalidFile = true; } if (invalidFile) { file = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".lua"); File.WriteAllText(file, sourceCode.Code + GetFooterForTempFile()); m_SourcesOverride[sourceCode.SourceID] = file; } else if (file != sourceCode.Name) { m_SourcesOverride[sourceCode.SourceID] = file; } lock (m_Lock) if (Client != null) Client.OnSourceCodeChanged(sourceCode.SourceID); }