luaL_newstate(); //create new stack L
lua_pushcfunction(L, &pmain); //push pmin into L
lua_pushinteger(L, argc); //push argc into L
lua_pushlightuserdata(L, argv); //push argv into L
status = lua_pcall(L, 2, 1, 0); //call top function from L
result = lua_toboolean(L, -1); //get function result form L
report(L, status); //print error from L when result not ok
lua_close(L); //clear all in L
int argc = (int)lua_tointeger(L, 1); //get argc from L
char **argv = (char **)lua_touserdata(L, 2); //get argv from L
int args = collectargs(argv, &script); //decode argv to args(mask with [error,-i,-v,-e,-E])
args == has_error //when argv has error
args & has_v //-v to show lua version
args & has_E //-E to ignore env vars(use std lib)
runargs(L, argv, script) //check -e and -l
handle_script(L, argv + script) //run script from L
args & has_i //-i to do read-eval-print loop
lua_stdin_is_tty() //run from shell
dofile(L, NULL) //run from file
lua_pushboolean(L, 1) //write ok/error to L
const char *fname = argv[0]; //script file name
status = luaL_loadfile(L, fname); //read file and push into L
int n = pushargs(L); //push arguments to script in L
status = docall(L, n, LUA_MULTRET); //run code from L
const char *oldprogname = progname; //backup progname
progname = NULL; //no progname in interactive mode
while ((status = loadline(L)) != -1) { //read input while not EOF
if (status == LUA_OK)
status = docall(L, 0, LUA_MULTRET); //run code from L
if (status == LUA_OK) l_print(L); //print result
else report(L, status); //print error info
}
lua_settop(L, 0); //clear L
lua_writeline(); //print '\n'
progname = oldprogname; //recover progname
luaL_loadfile(L,name)->luaL_loadfilexj(L,name,NULL) //read from file and push into L.(simple and skip)
dochunk(L, luaL_loadfile(L, name)->docall(L,0,0) //run code from L
docall(L,narg,nres)->lua_pcall(L,narg,nres,base)->lua_pcallk(L,narg,nres,base,0,NULL)
struct CallS c;
int status;
ptrdiff_t func;
lua_lock(L); //multi-thread api to acquire lock for L
api_check(k == NULL || !isLua(L->ci), //defined as assert(e && msg), when e = false, interrupt
"cannot use continuations inside hooks");
api_checknelems(L, nargs+1);
api_check(L->status == LUA_OK, "cannot do calls on non-normal thread");
checkresults(L, nargs, nresults);
if (errfunc == 0)
func = 0;
else {
StkId o = index2addr(L, errfunc); //convert index to address in L
api_checkstackindex(errfunc, o); //check stkid
func = savestack(L, o); //error function
}
c.func = L->top - (nargs+1); //the function need to call in stack
if (k == NULL || L->nny > 0) { //status is not protected
c.nresults = nresults;
status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); //call c.func in protected
}
else { //status is protected by 'resume'
CallInfo *ci = L->ci;
ci->u.c.k = k; //save details to ci,start.
ci->u.c.ctx = ctx;
ci->extra = savestack(L, c.func);
ci->u.c.old_errfunc = L->errfunc; //backup error function
L->errfunc = func;
setoah(ci->callstatus, L->allowhook);
ci->callstatus |= CIST_YPCALL; //save details to ci,end.
luaD_call(L, c.func, nresults, 1); //call c.call
ci->callstatus &= ~CIST_YPCALL;
L->errfunc = ci->u.c.old_errfunc; //recovery error function
status = LUA_OK;
}
adjustresults(L, nresults);
lua_unlock(L); //multi-thread api to release lock for L
return status;
main->pmain
pmain->handle_script->luaL_loadfile->luaL_loadfilex->docall
pmain->doREPL->loop(docall)
pmain->dofile->luaL_loadfilexj->docall
docall->lua_pcall->lua_pcallk
lua_pcallk->luaD_pcall
lua_pcallk->luaD_call