/// <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)); }
/// <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) { 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)); }
/// <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) { if (!m_isAlive) { throw new InvalidOperationException("Attempting to loadfunction on a dead Script"); } 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 != null || m_GlobalTable != null); SignalSourceCodeChange(source); SignalByteCodeChange(); return(MakeClosure(address, globalTable ?? m_GlobalTable)); }
/// <summary> /// Creates a new dynamic expression. /// </summary> /// <param name="code">The code of the expression.</param> /// <returns></returns> public DynamicExpression CreateDynamicExpression(string code) { DynamicExprExpression dee = Loader_Fast.LoadDynamicExpr(this, new SourceCode("__dynamic", code, -1, this)); return(new DynamicExpression(this, code, dee)); }