Lines Matching refs:L

28 static void lstop (lua_State *L, lua_Debug *ar) {  in lstop()  argument
30 lua_sethook(L, NULL, 0, 0); in lstop()
31 luaL_error(L, "interrupted!"); in lstop()
65 static int report (lua_State *L, int status) { in report() argument
66 if (status && !lua_isnil(L, -1)) { in report()
67 const char *msg = lua_tostring(L, -1); in report()
70 lua_pop(L, 1); in report()
76 static int traceback (lua_State *L) { in traceback() argument
77 if (!lua_isstring(L, 1)) /* 'message' not a string? */ in traceback()
79 lua_getfield(L, LUA_GLOBALSINDEX, "debug"); in traceback()
80 if (!lua_istable(L, -1)) { in traceback()
81 lua_pop(L, 1); in traceback()
84 lua_getfield(L, -1, "traceback"); in traceback()
85 if (!lua_isfunction(L, -1)) { in traceback()
86 lua_pop(L, 2); in traceback()
89 lua_pushvalue(L, 1); /* pass error message */ in traceback()
90 lua_pushinteger(L, 2); /* skip this function and traceback */ in traceback()
91 lua_call(L, 2, 1); /* call debug.traceback */ in traceback()
96 static int docall (lua_State *L, int narg, int clear) { in docall() argument
98 int base = lua_gettop(L) - narg; /* function index */ in docall()
99 lua_pushcfunction(L, traceback); /* push traceback function */ in docall()
100 lua_insert(L, base); /* put it under chunk and args */ in docall()
102 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); in docall()
104 lua_remove(L, base); /* remove traceback function */ in docall()
106 if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0); in docall()
116 static int getargs (lua_State *L, char **argv, int n) { in getargs() argument
122 luaL_checkstack(L, narg + 3, "too many arguments to script"); in getargs()
124 lua_pushstring(L, argv[i]); in getargs()
125 lua_createtable(L, narg, n + 1); in getargs()
127 lua_pushstring(L, argv[i]); in getargs()
128 lua_rawseti(L, -2, i - n); in getargs()
134 static int dofile (lua_State *L, const char *name) { in dofile() argument
135 int status = luaL_loadfile(L, name) || docall(L, 0, 1); in dofile()
136 return report(L, status); in dofile()
140 static int dostring (lua_State *L, const char *s, const char *name) { in dostring() argument
141 int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1); in dostring()
142 return report(L, status); in dostring()
146 static int dolibrary (lua_State *L, const char *name) { in dolibrary() argument
147 lua_getglobal(L, "require"); in dolibrary()
148 lua_pushstring(L, name); in dolibrary()
149 return report(L, docall(L, 1, 1)); in dolibrary()
153 static const char *get_prompt (lua_State *L, int firstline) { in get_prompt() argument
155 lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2"); in get_prompt()
156 p = lua_tostring(L, -1); in get_prompt()
158 lua_pop(L, 1); /* remove global */ in get_prompt()
163 static int incomplete (lua_State *L, int status) { in incomplete() argument
166 const char *msg = lua_tolstring(L, -1, &lmsg); in incomplete()
169 lua_pop(L, 1); in incomplete()
177 static int pushline (lua_State *L, int firstline) { in pushline() argument
181 const char *prmt = get_prompt(L, firstline); in pushline()
182 if (lua_readline(L, b, prmt) == 0) in pushline()
188 lua_pushfstring(L, "return %s", b+1); /* change it to `return' */ in pushline()
190 lua_pushstring(L, b); in pushline()
191 lua_freeline(L, b); in pushline()
196 static int loadline (lua_State *L) { in loadline() argument
198 lua_settop(L, 0); in loadline()
199 if (!pushline(L, 1)) in loadline()
202 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); in loadline()
203 if (!incomplete(L, status)) break; /* cannot try to add lines? */ in loadline()
204 if (!pushline(L, 0)) /* no more input? */ in loadline()
206 lua_pushliteral(L, "\n"); /* add a new line... */ in loadline()
207 lua_insert(L, -2); /* ...between the two lines */ in loadline()
208 lua_concat(L, 3); /* join them */ in loadline()
210 lua_saveline(L, 1); in loadline()
211 lua_remove(L, 1); /* remove line */ in loadline()
216 static void dotty (lua_State *L) { in dotty() argument
220 while ((status = loadline(L)) != -1) { in dotty()
221 if (status == 0) status = docall(L, 0, 0); in dotty()
222 report(L, status); in dotty()
223 if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */ in dotty()
224 lua_getglobal(L, "print"); in dotty()
225 lua_insert(L, 1); in dotty()
226 if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0) in dotty()
227 l_message(progname, lua_pushfstring(L, in dotty()
229 lua_tostring(L, -1))); in dotty()
232 lua_settop(L, 0); /* clear stack */ in dotty()
239 static int handle_script (lua_State *L, char **argv, int n) { in handle_script() argument
242 int narg = getargs(L, argv, n); /* collect arguments */ in handle_script()
243 lua_setglobal(L, "arg"); in handle_script()
247 status = luaL_loadfile(L, fname); in handle_script()
248 lua_insert(L, -(narg+1)); in handle_script()
250 status = docall(L, narg, 0); in handle_script()
252 lua_pop(L, narg); in handle_script()
253 return report(L, status); in handle_script()
294 static int runargs (lua_State *L, char **argv, int n) { in runargs() argument
304 if (dostring(L, chunk, "=(command line)") != 0) in runargs()
312 if (dolibrary(L, filename)) in runargs()
323 static int handle_luainit (lua_State *L) { in handle_luainit() argument
327 return dofile(L, init+1); in handle_luainit()
329 return dostring(L, init, "=" LUA_INIT); in handle_luainit()
340 static int pmain (lua_State *L) { in pmain() argument
341 struct Smain *s = (struct Smain *)lua_touserdata(L, 1); in pmain()
345 globalL = L; in pmain()
347 lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ in pmain()
348 luaL_openlibs(L); /* open libraries */ in pmain()
349 lua_gc(L, LUA_GCRESTART, 0); in pmain()
350 s->status = handle_luainit(L); in pmain()
359 s->status = runargs(L, argv, (script > 0) ? script : s->argc); in pmain()
362 s->status = handle_script(L, argv, script); in pmain()
365 dotty(L); in pmain()
369 dotty(L); in pmain()
371 else dofile(L, NULL); /* executes stdin as a file */ in pmain()
380 lua_State *L = lua_open(); /* create state */ in main() local
381 if (L == NULL) { in main()
387 status = lua_cpcall(L, &pmain, &s); in main()
388 report(L, status); in main()
389 lua_close(L); in main()