lua学习笔记(2)——内核入口

这篇博客深入探讨了Lua的内核入口,从lua.c的main()函数开始,逐步解析pmain()、handle_script()、doREPL()、dofile()和docall()等关键函数,详细介绍了lua_pcallk()在调用结构中的作用,揭示了Lua执行过程的内部机制。
摘要由CSDN通过智能技术生成

lua.c:595 main()

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

lua.c:553 pmain()

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

lua.c:436 handle_script()

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

lua.c:404 doREPL()

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(
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值