关于一些语法包装的问题不涉及(可见前面某篇),主要对堆栈排列上的分析。
一路跟调到static void f_parser (lua_State *L, void *ud)函数,堆栈记录如下:
+ L->top 0x003950e8:当前指针
+ L->stack 0x003950a8
+ L->base 0x003950b8
+ L->ci 0x00398e08
跟调到Proto *luaY_parser函数中。该函数主要进行了语法解析,FuncState的建立,最后返回结构体
Proto。里面具体怎么解析语法等我们都不需要关系,我们只需要关注哪些结果被压入了堆栈。
Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {
struct LexState lexstate;
struct FuncState funcstate;
lexstate.buff = buff;
/*
不涉及堆栈,只是设置lexstate的一些参数
*/
luaX_setinput(L, &lexstate, z, luaS_new(L, name));
/*
会在top上压入2个值,Proto(表示函数属性)和一张表。
L->top排列如下:
L->top 0x003950e8:funcstate->h
L->top 0x003950f8:luaF_newproto(L)
*/
open_func(&lexstate, &funcstate);
funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */
luaX_next(&lexstate); /* read first token */
chunk(&lexstate);
check(&lexstate, TK_EOS);
/*
将之前压入的2个值退出,堆栈恢复为
+ L->top 0x003950e8
*/
close_func(&lexstate);
lua_assert(funcstate.prev == NULL);
lua_assert(funcstate.f->nups == 0);
lua_assert(lexstate.fs == NULL);
return funcstate.f;
}
return上层函数f_parser,继续跟入:
static void f_parser (lua_State *L, void *ud) {
int i;
Proto *tf;
Closure *cl;
struct SParser *p = cast(struct SParser *, ud);
int c = luaZ_lookahead(p->z);
luaC_checkGC(L);
tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z,
&p->buff, p->name);
/*
根据返回回来的函数属性新建一个closure,tf->nups表示多少个upvalues。
并且将tf设置回cl,初始化好upvalues值等
*/
cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));
cl->l.p = tf;
for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */
cl->l.upvals[i] = luaF_newupval(L);
setclvalue(L, L->top, cl);
/*
改变了L->top
L->top 0x003950e8:cl,
cl地址为 0x00473780
*/
incr_top(L);
}
一路返回,断点到最初的luaL_loadfile中,该函数最后执行了一句lua_remove(L, fnameindex);
清除了一些参数(不影响到子调用的压栈),现在top里面排列如下:
+ L->top 0x003950d8:cl包
+ L->top 0x003950e8:当前指针
+ L->stack 0x003950a8
+ L->base 0x003950b8
+ L->ci 0x00398e08
根据宏
#define luaL_dofile(L, fn) \
(luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
接下来调用lua_pcall函数:
LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
struct CallS c;
/*
nargs为0,这段地址算出来为0x003950d8,
也就是我们上文创建的cl包
*/
c.func = L->top - (nargs+1); /* function to be called */
c.nresults = nresults;
/*
savestack算的是c.func和L->stack的差值。
0x003950d8-0x003950a8=48
*/
status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
adjustresults(L, nresults);
lua_unlock(L);
return status;
}
跟入luaD_pcall->luaD_rawrunprotected->f_call->luaD_call->luaD_precall
int luaD_precall (lua_State *L, StkId func, int nresults) {
LClosure *cl;
ptrdiff_t funcr;
if (!ttisfunction(func)) /* `func' is not a function? */
func = tryfuncTM(L, func); /* check the `function' tag method */
/*
savestack算的是c.func和L->stack的差值。
0x003950d8-0x003950a8=48
*/
funcr = savestack(L, func);
cl = &clvalue(func)->l;
L->ci->savedpc = L->savedpc;
if (!cl->isC) { /* Lua function? prepare its call */
CallInfo *ci;
StkId st, base;
//取出函数属性
Proto *p = cl->p;
luaD_checkstack(L, p->maxstacksize);
//??为什么不直接用func呢?
func = restorestack(L, funcr);
if (!p->is_vararg) { /* no varargs? */
base = func + 1;
if (L->top > base + p->numparams)
L->top = base + p->numparams;
}
else { /* vararg function */
/*
因为不带参数,所以base返回L->top,姑且是这个值
*/
int nargs = cast_int(L->top - func) - 1;
base = adjust_varargs(L, p, nargs);
func = restorestack(L, funcr); /* previous call may change the stack */
}
/*
设置好ci的堆栈
ci->func=0x003950d8
ci->base=0x003950e8
ci->top= 0x00395108
L->savedpc=0x00473860 编译出来的虚拟指令
*/
ci = inc_ci(L); /* now `enter' new function */
ci->func = func;
L->base = ci->base = base;
ci->top = L->base + p->maxstacksize;
lua_assert(ci->top <= L->stack_last);
L->savedpc = p->code; /* starting point */
ci->tailcalls = 0;
ci->nresults = nresults;
/*
清空ci需要的堆栈
*/
for (st = L->top; st < ci->top; st++)
setnilvalue(st);
/*
L->top=0x00395108
*/
L->top = ci->top;
if (L->hookmask & LUA_MASKCALL) {
L->savedpc++; /* hooks assume 'pc' is already incremented */
luaD_callhook(L, LUA_HOOKCALL, -1);
L->savedpc--; /* correct 'pc' */
}
return PCRLUA;
}
}
跟入执行luaV_execute,根据 L->savedpc获取执行码
先获取环境表
case OP_GETGLOBAL: {
TValue g;
TValue *rb = KBx(i);
sethvalue(L, &g, cl->env);
lua_assert(ttisstring(rb));
Protect(luaV_gettable(L, &g, rb, ra));
continue;
}
再
case OP_LOADK: {
setobj2s(L, ra, KBx(i));
continue;
}
前面两个的具体意义都可以先不管,最后的调用.如果ci里有多个函数需要执行,则返回值为
PCRC,从下一指令的ci设置完毕,即可马上调用
case OP_CALL: {
int b = GETARG_B(i);
int nresults = GETARG_C(i) - 1;
if (b != 0) L->top = ra+b; /* else previous instruction set top */
L->savedpc = pc;
switch (luaD_precall(L, ra, nresults)) {
case PCRLUA: {
nexeccalls++;
goto reentry; /* restart luaV_execute over new Lua function */
}
case PCRC: {
/* it was a C function (`precall' called it); adjust results */
if (nresults >= 0) L->top = L->ci->top;
base = L->base;
continue;
}
default: {
return; /* yield */
}
}
}
跟调到最后的luaD_precall.上文已经分析过luaD_precall,不同的是if (!cl->isC)的判断。上文中
为lua function,此次为C function
int luaD_precall (lua_State *L, StkId func, int nresults) {
LClosure *cl;
ptrdiff_t funcr;
if (!ttisfunction(func)) /* `func' is not a function? */
func = tryfuncTM(L, func); /* check the `function' tag method */
funcr = savestack(L, func);
cl = &clvalue(func)->l;
L->ci->savedpc = L->savedpc;
if (!cl->isC) { /* Lua function? prepare its call */
}
else { /* if is a C function, call it */
CallInfo *ci;
int n;
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
ci = inc_ci(L); /* now `enter' new function */
/*
设置好ci的环境。要执行的起始栈地址为要执行的函数+1,栈顶为L->top+LUA_MINSTACK
ci->func:0x003950e8
L->base:0x003950f8
ci->top:0x00395248
*/
ci->func = restorestack(L, funcr);
L->base = ci->base = ci->func + 1;
ci->top = L->top + LUA_MINSTACK;
lua_assert(ci->top <= L->stack_last);
ci->nresults = nresults;
if (L->hookmask & LUA_MASKCALL)
luaD_callhook(L, LUA_HOOKCALL, -1);
lua_unlock(L);
/*
执行到最终的函数
*/
n = (*curr_func(L)->c.f)(L); /* do the actual call */
lua_lock(L);
if (n < 0) /* yielding? */
return PCRYIELD;
else {
/*
整理CI的下一次行为.默认结尾时压入一条return的虚拟指令
*/
luaD_poscall(L, L->top - n);
return PCRC;
}
}
}
分享到:
相关推荐
Lua源码分析Lua源码分析Lua源码分析Lua源码分析Lua源码分析Lua源码分析Lua源码分析Lua源码分析
所有版本LUA源码 lua-5.3.5 lua-5.3.4 lua-5.3.3 lua-5.3.2 lua-5.3.1 lua-5.3.0 lua-5.2.4 lua-5.2.3 lua-5.2.2 lua-5.2.1 lua-5.2.0 lua-5.1.5 lua-5.1.4 lua-5.1.3 lua-5.1.2 lua-5.1.1 lua-5.1 lua-5.0.3 lua-...
这个是lua的源码分析,lua的源码写的非常好,整个设计的结构非常巧妙,不管是对于学习lua本身还是c语言的数据结构都非常有帮助。
第一章概览由甮由源文件划分 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮 甮
里面有风云 大神写的对lua 的源码剖析。还有lua5.2源码。
Lua 源码赏析.pdf, 其中有着非常棒的lua源代码资源可供学习。下载文件为该书的百度云连接地址。
当然 Lua 5.0 我也读过,4.0 和 3.2 则读的不多。 最近有一点空闲,想续写我那本 Lua 源码欣赏。按我心里的计划,还有大约 6 章。虚拟机、字节码持久化、C API 、解释器、GC、库函数。 新添了一章关于虚拟机的,...
Lua CJSON 为 Lua 语言提供高性能的 JSON 解析器和编码器,其性能比纯 Lua 库要高 10 到 20 倍。Lua CJSON 完全支持 UTF-8 ,无需依赖其他非 Lua/LuaJIT 的相关包。
Lua被设计成很容易和传统的C/C++整合的语言。这种语言的二元性带来了极大的好处。Lua是一个小巧而简单的语言,因为Lua不致力于做C语言已经做得很好的领域,比如:性能、底层操作以及与第三方软件的接口。Lua依赖于C...
做了那么多 Lua 脚本破解,我们来尝试写一个不能被破解的加密。 所谓不能被破解,并不是真正不能被破解,只是在没有密码的情况下很难破解。
云风新作-----lua源码导读。目前网上最好的lua源代码阅读教程。帮助你快速理解lua实现原理。
云风写的Lua源码欣赏,基于lua5.2版本源代码解析,对于初初学习lua语言的有很不错的帮助
a utf-8 support module for Lua and LuaJIT 源码地址:https://github.com/starwing/luautf8 编译后可用的库: Linux版:lua-utf8.so Windows版:lua-utf8.dll(若是用在openresty中,openresty版本需使用32位版本...
天龙八部全套源码 控制台 lua脚本 测试模块 可以用于学习,有很高的参考价值
lua源码欣赏 lua源代码分析,来之云风!
redis-lua 是 Redis 的 Lua 语言的客户端开发包。 示例代码: require 'redis' local redis = Redis.connect('127.0.0.1', 6379) local response = redis:ping() -- true redis:set('usr:nrk', 10) redis:set('usr...
讲解lua语言的实现源代码,由于lua是基于C语言实现,所以需要有一定C语言基础。 请配合《the implementation of Lua5.0中文版》一起阅读。 http://download.csdn.net/detail/havesnag/5071833
用sublime调试lua,lua中需要 require 'cjson',使用cjson.dll报错,可以将该cjson.lua直接放入同一目录下。
Lua代码性能分析 Lua代码性能分析