/// <summary> /// Creates a function. /// </summary> /// <param name="source"></param> /// <param name="status"></param> /// <param name="factory"></param> /// <returns>The new function. If an error is thrown while creating the function, null is returned /// and status is set accordingly (if applicable).</returns> public static LuaFunction CreateFunction(string source, ref LuaStatus status, Lua factory) { var state = factory.State; var global = (LuaTable)factory.GetObjectFromPath("_G"); var globalKeysBefore = (ICollection <object>)global.Keys; // compiles and pushes to the stack as a function // if theres an error then it pushes that error to the stack // we need to clean that up if that happens status = state.LoadString(source); if (status != LuaStatus.OK) { state.Pop(1); return(null); } status = state.PCall(0, 0, 0); // run the function on the stack, ie the chunk if (status != LuaStatus.OK) { return(null); } var globalKeysAfter = (ICollection <object>)global.Keys; var difference = Enumerable.Except(globalKeysAfter, globalKeysBefore); Console.WriteLine(difference.Count()); var first = difference.First(); Console.WriteLine(first); return((LuaFunction)global[first]); }
public void CompileAndRunString() { state = new Lua(); LuaStatus result = state.LoadString("x=1"); stackDump(state); state.Call(0, 0); stackDump(state); result = state.LoadString("1"); stackDump(state); state.PCall(0, 0, 0); stackDump(state); result = state.LoadString("print(x)"); // stdout?? stackDump(state); state.Call(0, 0); stackDump(state); state.Close(); }
static LuaStatus dochunk(ILuaState L, LuaStatus status) { if (status == LuaStatus.OK) { status = docall(L, 0, 0); } return(report(L, status)); }
/* ** Check whether 'status' is not OK and, if so, prints the error ** message on the top of the stack. It assumes that the error object ** is a string, as it was either generated by Lua or by 'msghandler'. */ static LuaStatus report(ILuaState L, LuaStatus status) { if (status != LuaStatus.OK) { String msg = L.ToString(-1); l_message(L, progname, msg); L.Pop(1); /* remove message */ } return(status); }
public void ResumeAcceptsNull() { using (var lua = new Lua()) { lua.LoadString("hello = 1"); LuaStatus result = lua.Resume(null, 0); Assert.AreEqual(LuaStatus.OK, result); } }
public LuaState(bool loadLibs = true) { lua_State = CApi.luaL_newstate(); if (loadLibs) { CApi.luaL_openlibs(lua_State); } Status = LuaStatus.Ok; }
public void LuaScript_smoketest() { state = new Lua(); LuaStatus result = state.LoadFile(GetScriptPath("smoketest")); Assert.AreEqual(LuaStatus.OK, result); result = state.PCall(0, -1, 0); Assert.AreEqual(LuaStatus.OK, result); state.Close(); }
/* * Calls the object as a function with the provided arguments and * casting returned values to the types in returnTypes before returning * them in an array */ internal object[] CallFunction(object function, object[] args, Type[] returnTypes) { int nArgs = 0; int oldTop = luaState.GetTop(); if (!luaState.CheckStack(args.Length + 6)) { throw new LuaException("Lua stack overflow"); } translator.Push(luaState, function); if (args.Length > 0) { nArgs = args.Length; for (int i = 0; i < args.Length; i++) { translator.Push(luaState, args[i]); } } executing = true; try { int errfunction = 0; if (UseTraceback) { errfunction = PushDebugTraceback(luaState, nArgs); oldTop++; } LuaStatus error = luaState.PCall(nArgs, -1, errfunction); if (error != LuaStatus.OK) { ThrowExceptionFromError(oldTop); } } finally { executing = false; } if (returnTypes != null) { return(translator.PopValues(luaState, oldTop, returnTypes)); } return(translator.PopValues(luaState, oldTop)); }
/* ** Check whether 'status' signals a syntax error and the error ** message at the top of the stack ends with the above mark for ** incomplete statements. */ static int incomplete(ILuaState L, LuaStatus status) { if (status == LuaStatus.ErrSyntax) { int lmsg; String msg = L.ToLString(-1, out lmsg); //if (lmsg >= marklen && String.Compare(msg + lmsg - marklen, EOFMARK) == 0) if (lmsg >= marklen && msg.EndsWith(EOFMARK)) { L.Pop(1); return(1); } } return(0); /* else... */ }
/* ** Interface to 'lua_pcall', which sets appropriate message function ** and C-signal handler. Used to run all chunks. */ static LuaStatus docall(ILuaState L, int narg, int nres) { LuaStatus status = 0; int base_ = L.GetTop() - narg; /* function index */ L.PushFunction(msghandler); /* push message handler */ L.Insert(base_); /* put it under function and args */ globalL = L; /* to be available to 'laction' */ Console.CancelKeyPress += Console_CancelKeyPress; //signal(SIGINT, laction); /* set C-signal handler */ status = L.PCall(narg, nres, base_); //signal(SIGINT, SIG_DFL); /* reset C-signal handler */ Console.CancelKeyPress -= Console_CancelKeyPress; L.Remove(base_); /* remove message handler from the stack */ return(status); }
private void UpdateLua(string source) { Status = LuaState.State.LoadString(source); if (Status == LuaStatus.OK) { Status = LuaState.State.PCall(0, -1, 0); } if (Status != LuaStatus.OK) { OnLuaError(this, LuaState); } else { OnLuaChanged(this, LuaState); } }
/* ** Read multiple lines until a complete Lua statement */ static LuaStatus multiline(ILuaState L) { for (; ;) { /* repeat until gets a complete statement */ int len; String line = L.ToLString(1, out len); /* get what it has */ LuaStatus status = L.LoadBuffer(line, len, "=stdin"); /* try it */ if (0 == incomplete(L, status) || 0 == pushline(L, false)) { lua_saveline(L, line); /* keep history */ return(status); /* cannot or should not try to add continuation line */ } L.PushLiteral("\n"); /* add newline... */ L.Insert(-2); /* ...between the two lines */ L.Concat(3); /* join them */ } }
void AssertString(string chunk, Lua state) { string error = string.Empty; LuaStatus result = state.LoadString(chunk); if (result != LuaStatus.OK) { error = state.ToString(1, false); } Assert.True(result == LuaStatus.OK, "Fail loading string: " + chunk + "ERROR:" + error); result = state.PCall(0, -1, 0); if (result != 0) { error = state.ToString(1, false); } Assert.True(result == 0, "Fail calling chunk: " + chunk + " ERROR: " + error); }
void AssertFile(string path) { string error = string.Empty; LuaStatus result = state.LoadFile(path); if (result != LuaStatus.OK) { error = state.ToString(1); } Assert.True(result == LuaStatus.OK, "Fail loading file: " + path + "ERROR:" + error); result = state.PCall(0, -1, 0); if (result != LuaStatus.OK) { error = state.ToString(1); } Assert.True(result == LuaStatus.OK, "Fail calling file: " + path + " ERROR: " + error); }
public void PanicError() // PANIC: unprotected error in call to Lua API (attempt to call a string value) { state = new Lua(); LuaFunction oldPanic = state.AtPanic(MyPanicFunc); stackDump(state); LuaStatus result = state.LoadString("1"); stackDump(state); try { state.Call(0, 0); Assert.Fail("Shoudln't go so far"); } catch (MyPanicException ex) { } stackDump(state); state.Close(); }
static LuaStatus dochunk(ILuaState L, LuaStatus status) { if (status == LuaStatus.OK) status = docall(L, 0, 0); return report(L, status); }
/* ** Check whether 'status' is not OK and, if so, prints the error ** message on the top of the stack. It assumes that the error object ** is a string, as it was either generated by Lua or by 'msghandler'. */ static LuaStatus report(ILuaState L, LuaStatus status) { if (status != LuaStatus.OK) { String msg = L.ToString(-1); l_message(L, progname, msg); L.Pop(1); /* remove message */ } return status; }
/// <summary> /// Creates a function given source and sets status ref. /// </summary> public static LuaFunction CreateFunction(string source, ref LuaStatus status) => CreateFunction(source, ref status, defaultLuaFactory);
/// <summary> /// Creates a function from whatever is in the GuiElementTextBase and sets status ref. /// </summary> public static LuaFunction CreateFunctionFromTextField( GuiElementTextBase field, ref LuaStatus status ) => CreateFunction(field.GetText(), ref status);
/* ** Check whether 'status' signals a syntax error and the error ** message at the top of the stack ends with the above mark for ** incomplete statements. */ static int incomplete(ILuaState L, LuaStatus status) { if (status == LuaStatus.ErrSyntax) { int lmsg; String msg = L.ToLString(-1, out lmsg); //if (lmsg >= marklen && String.Compare(msg + lmsg - marklen, EOFMARK) == 0) if (lmsg >= marklen && msg.EndsWith(EOFMARK)) { L.Pop(1); return 1; } } return 0; /* else... */ }