Lua5.3 VM 分析(一)字节码运行
luaV_execute 是Lua VM 执行一段字节码的入口。Lua VM 就是一个状态机,从当前调用栈上次运行点开始解释字节码指令,直到下一个 C 边界跳出点(可以是函数执行完毕,也可以是一次协程 yield 操作)。
Lua 函数部分
typedef struct CallInfo {
StkId func; /* function index in the stack */
StkId top; /* top for this function */
struct CallInfo *previous, *next; /* dynamic call link */
union {
struct { /* only for Lua functions */
StkId base; /* base for this function */
const Instruction *savedpc;
} l;
struct { /* only for C functions */
lua_KFunction k; /* continuation in case of yields */
ptrdiff_t old_errfunc;
lua_KContext ctx; /* context info. in case of yields */
} c;
} u;
ptrdiff_t extra;
short nresults; /* expected number of results from this function */
lu_byte callstatus;
} CallInfo;
savepc域 保存着指向当前指令的指针;base 域保存着当前函数的数据栈栈底指针。
每