/// <summary> /// Check the specified luaCode to see, if there are any errors. /// </summary> /// <param name="luaCode">String with Lua code to check.</param> public static void Check(string luaCode, string luaFileName) { // Create Lua runtime LuaRuntime luaState = new LuaRuntime (); // Check Lua for errors try { // Try to compile the code luaState.CompileString (luaCode, luaFileName); } catch (Exception ex) { // There are errors in the Lua code // So raise an error with the error data Match match = Regex.Match(ex.Message, @"(.*):(\d*):(.*)"); int line = Convert.ToInt32(match.Groups[2].Value); string message = String.Format("{0}, line {1}: {2}", match.Groups[1].Value.Replace("string", "File"), match.Groups[2].Value, match.Groups[3].Value); string error = match.Groups[3].Value; string[] lines = luaCode.Replace("\r","").Split('\n'); string code = lines.Length >= line-1 ? lines[line-1] : null; string before = lines.Length >= line-2 ? lines[line-2] : null; string after = lines.Length >= line ? lines[line] : null; throw new CompilerLuaException(message, line, error, code, before, after); } luaState = null; }
/// <summary> /// Check the specified luaCode to see, if there are any errors. /// </summary> /// <param name="luaCode">String with Lua code to check.</param> public static void Check(string luaCode, string luaFileName) { // Create Lua runtime LuaRuntime luaState = new LuaRuntime(); // Check Lua for errors try { // Try to compile the code luaState.CompileString(luaCode, luaFileName); } catch (Exception ex) { // There are errors in the Lua code // So raise an error with the error data Match match = Regex.Match(ex.Message, @"(.*):(\d*):(.*)"); int line = Convert.ToInt32(match.Groups[2].Value); string message = String.Format("{0}, line {1}: {2}", match.Groups[1].Value.Replace("string", "File"), match.Groups[2].Value, match.Groups[3].Value); string error = match.Groups[3].Value; string[] lines = luaCode.Replace("\r", "").Split('\n'); string code = lines.Length >= line - 1 ? lines[line - 1] : null; string before = lines.Length >= line - 2 ? lines[line - 2] : null; string after = lines.Length >= line ? lines[line] : null; throw new CompilerLuaException(message, line, error, code, before, after); } luaState = null; }
/// <summary> /// Loads a lua chunk into a lua function. /// </summary> /// <param name="chunk">Chunk to load.</param> /// <param name="chunkName">Name of the chunk.</param> /// <returns>LuaFunction that corresponds to the loaded chunk.</returns> public LuaFunction SafeLoadString(byte[] chunk, string chunkName) { lock (_luaState) { try { return(_luaState.CompileString(chunk, chunkName)); } catch (Exception e) { return(HandleException(e, null) as LuaFunction); } } }
private LuaFunction CompileCode(string code) { LuaFunction function; try { function = luaRuntime.CompileString(code); } catch (Exception ex) { throw new BuildException("Error parsing Lua: " + ex.ToString()); } return(function); }
public void Environments() { using (var runtime = new LuaRuntime()) { using (var f = runtime.CompileString("print('hi')")) { using (var tab = runtime.CreateTable()) { f.Environment = tab; } using (var env = f.Environment) { Assert.AreEqual(0, env.Count); } f.Call().Dispose(); } } }