第3章 lua中函数调用的方法
前面,以及分析了lua中定义一个函数的方法,现在总结如下:
1、 将函数名作为局部变量存在局部变量表里,并在栈上开辟一个寄存器空间,在运行期,将新建一个closure,并存在已保留的寄存器里;
前面,以及分析了lua中定义一个函数的方法,现在总结如下:
1、 将函数名作为局部变量存在局部变量表里,并在栈上开辟一个寄存器空间,在运行期,将新建一个closure,并存在已保留的寄存器里;
首先,解释一下,为什么题目叫做:条件编译。其实很简单,现在这一章要分析的是,if、while、repeat、for语句。这些语句有个什么特点呢?那就是,都要有条件判断。根据条件判断的结果,以决定是否执行,该如何执行。我不知道该怎么称呼这样的语句,就一致称其为:条件编译。因为这一章的重点是研究,条件对于中间码的生成造成的影响,以及如何生成中间码以实现这种条件判断。 首先分析if then elseif then else then end句型。 这个很简单,实际上是lua处理的很简单。 当lua遇到了if这个关键字的时候,就开始调用ifstat(),首先,这是进入一个块。这个块这个东西和函数是相同的重要,因为它关系到一个变量的作用域的问题。块由block()函数处理。不过在此之前,要注意一个重要的事情,那就是条件判断语句。lua是怎么处理条件判断的呢? 首先,lua会跳过if关键字,然后,会进入一个十分常用的函数:cond(),这个函数还是比较复杂的,我现在要再看一遍这个函数,还是心有余悸的。 cond()这个函数比较短,我还是贴出来吧:
前面研究了局部变量的定义,下面就研究下,lua中局部函数定义的方法。
其中局部函数定义的语法是: local function FuncName (parlist) chunk END 首先,lua会检测到local function这两个关键字,知道后面是在定义局部函数,lua会跳过这两个关键字,分别用llex_next()和testnext()。testnext()和checknext()这两个函数的区别是,checknext()是期望后面是某个token,是的话就读取,如果不是的话,就会报错,而testnext()是检查后面是不是某个token,如果是就读取,不是的话也不会报错。 当跳过了local function这两个关键字。就到了局部函数函数名的地方了。这时,lua会把这个局部函数当作一个局部变量,为这个函数名字注册一个局部变量名。其中就用到了前面注册局部变量时用到的函数 newlocalvar()。这个函数在局部变量数字里增加一项,记录这个函数名。这里的记录并没有检查是否已经存在了这个局部变量。但是使用的时候,lua会从头开始遍历查找,也就是说第一次注册的这个局部变量会被使用。 如前所说,在语法解析的过程中,有一个数据结构非常重要,那就是expdesc,这个数据结构代表一个表达式。在局部函数定义的时候,会用到2个这样的数据结构,这里分别称为v和b。其中v代表这个函数,b代表body,也就是函数体。当解析到这里的时候,会初始化v,将其类型赋值为VLOCAL,v->k = VLOCAL,并在里面记录当前空闲寄存器的位置,v->u.s.info = fs->freereg。 接下来就是将空闲寄存器指针加一,将局部变量的个数加一。 于是,就进入解析函数体的部分了,body()。 首先,新建了一个FuncState数据结构,通过open_func()函数,这个数据结构就是我们一直看到的ls->fs这个fs。这个数据结构是在语法解析时用到的,代表一个函数,它有一个函数头,叫Proto,每个函数都以一个这样的头。每个函数记录自己的中间码,它存在于这个函数头Proto里面,具体就是ls->fs->f->code[]数组里。在语法解析的过程中,整个lua程序就当作了一个函数,也就是第一个FuncState,也就是第一个ls->fs,之后,遇到的每个定义的函数,都会新建一个FuncState,并链在ls->fs上,也就是,所有的ls->fs链成一个链表。而ls->fs就是当前解析到的函数。 新建了这个函数结构FuncState后,便将其及其常量数组压入栈中。 之后便是解析参数列表和函数体了。现在先不管这些,因为先从整体对解析函数有个了解。假设我们的函数是这个样子: local function FuncName () END; 也就是说,没有参数表,也没有函数体的一个最简单的函数。 当lua解析这个函数的时候,如前所说,FuncName会被注册进局部变量表中,并且会新建一个FuncState数据结构,将其链在ls->fs上,并当作当前函数。 当lua检测到函数定义的语法都正确,也就是参数表包含在小括号()内,函数体以END结束,通过了检测,就会执行close_func()函数。 这个函数比较有意思,它里面包含了一些比较有趣的函数。其中第一个有趣的函数叫做removevar(),看字面意思是去除变量。是什么意思呢?是这么个意思:函数定义结束后,函数内部的变量是外部不看见的,所以,要从可见的地方去掉。这个函数就是处理这个情况的。它的做法很简单,就是把每个这个函数内的局部变量的endpc标记为当前pc。也就是说,到当前位置,当前代码以后,局部变量是不可见的。这里的可见度,作用域是与指令联系起来的,也就是说,从某条指令开始,局部变量可见,到某条指令结束,局部变量不可见。于是,便从当前可见域里remove了那个函数的局部变量。 然后会通过luaK_ret()函数,生成一条OP_RETURN指令。 最后,将这个函数从ls中踢掉,也就是ls->fs = fs->prev。到这里,这个函数算解析完了,但是,这个函数生成的指令码是在这个函数结构fs里面,现在踢掉了,当要调用的时候该怎么调用呢? 这是最后一个疑问,不过, body并没有结束,还有最后一个函数pushclosure()。看了这个函数,我们就会发现,其实,lua并没有把这个函数踢掉,而是把它保存在他的父函数里面。这里是这么回事:每个函数里面都可以定义函数,这个函数名首先会作为局部变量名保存在父函数的局部变量表里,当作其父函数的一个局部变量。然后,这个函数的结构FuncState会被保存在其父函数的内部函数数组里,也就是每个函数结构的头结构里面,fs->f,都会有一个函数头数组,fs->f->p[],其中包含着在这个函数内部定义的函数。 pushclosure()首先就会做这件事,也就是将函数结构保存在其父函数结构的fs->f->p[]里面。 然后,就会生成一条指令,OP_CLOSURE,说明这里定义了一个函数。这条指令是做什么的呢? 别忘了刚开始我们说的,一个局部函数定义,和局部变量定义是同样的,在栈里会保留一个空槽(寄存器),但是,那个寄存器里到底存的是什么呢?这个就要留在运行时回答了。当lua虚拟机运行到OP_CLOSURE的时候,就会新建一个Closure,并用这个Closure初始化那个预留的寄存器,也就是那个局部函数。 而刚开始的那两个expdesc数据结构,其中之一b,也就是那个代表body的,就是用来储存这个OP_CLOSURE指令了。而那个v,其v->u.s.info存的是这个局部函数所存在的寄存器位置。这里,已经生成了一个OP_CLOSURE指令,但是,这条指令所执行时,生成的Closure存在栈的哪里呢?也就是,应该放在那个局部函数所对应的那个寄存器里。下面就是要完成这个操作的函数:luaK_storevar()。就把当初保存在e里面的寄存器位置保存在了那条指令OP_CLOSURE里面了。 这就是新建了一个局部函数,其实和局部变量差不多。写到现在,发现文章的架构太乱了,算是草稿吧,以后再改。 ———————————————— 版权声明:本文为CSDN博主「haxixi_keli」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/haxixi_keli/article/details/3210446
Lua是边进行语法分析,边词法分析。其中,词法分析的模块是:llex。其对外暴露的接口是:llex_next()。并且,在整个语法分析、词法分析的过程中,只有一个唯一的全局实例:llex_state。在词法分析的过程中,lua会处理以下几种情况:
由于安全原因,tcp的445端口被网络运营商封闭,造成无法访问公网的samba服务,即使打开了腾讯的安全组策略中的端口也无济于事
修改访问端口
/*
** $Id: llex.c,v 2.20.1.2 2009/11/23 14:58:22 roberto Exp $
** Lexical Analyzer
** See Copyright Notice in lua.h
*/
#include <ctype.h>
#include <locale.h>
#include <string.h>
#define llex_c
#define LUA_CORE
#include "lua.h"
#include "ldo.h"
#include "llex.h"
#include "lobject.h"
#include "lparser.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "lzio.h"
#define next(ls) (ls->current = zgetc(ls->z))
#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
/* ORDER RESERVED */
const char *const luaX_tokens [] = {
"and", "break", "do", "else", "elseif",
"end", "false", "for", "function", "if",
"in", "local", "nil", "not", "or", "repeat",
"return", "then", "true", "until", "while",
"..", "...", "==", ">=", "<=", "~=",
"<number>", "<name>", "<string>", "<eof>",
NULL
};
#define save_and_next(ls) (save(ls, ls->current), next(ls))
/* 将c存到ls->buff中 */
static void save (LexState *ls, int c) {
Mbuffer *b = ls->buff;
if (b->n + 1 > b->buffsize) {
size_t newsize;
if (b->buffsize >= MAX_SIZET/2)
luaX_lexerror(ls, "lexical element too long", 0);
newsize = b->buffsize * 2;
luaZ_resizebuffer(ls->L, b, newsize);
}
b->buffer[b->n++] = cast(char, c);
}
/* 构建出关键字 */
void luaX_init (lua_State *L) {
int i;
for (i=0; i<NUM_RESERVED; i++) {
TString *ts = luaS_new(L, luaX_tokens[i]);
luaS_fix(ts); /* reserved words are never collected */
lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN);
ts->tsv.reserved = cast_byte(i+1); /* reserved word */
}
}
#define MAXSRC 80
const char *luaX_token2str (LexState *ls, int token) {
if (token < FIRST_RESERVED) {
lua_assert(token == cast(unsigned char, token));
return (iscntrl(token)) ? luaO_pushfstring(ls->L, "char(%d)", token) :
luaO_pushfstring(ls->L, "%c", token);
}
else
return luaX_tokens[token-FIRST_RESERVED];
}
static const char *txtToken (LexState *ls, int token) {
switch (token) {
case TK_NAME:
case TK_STRING:
case TK_NUMBER:
save(ls, '\0');
return luaZ_buffer(ls->buff);
default:
return luaX_token2str(ls, token);
}
}
void luaX_lexerror (LexState *ls, const char *msg, int token) {
char buff[MAXSRC];
luaO_chunkid(buff, getstr(ls->source), MAXSRC);
msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
if (token)
luaO_pushfstring(ls->L, "%s near " LUA_QS, msg, txtToken(ls, token));
luaD_throw(ls->L, LUA_ERRSYNTAX);
}
void luaX_syntaxerror (LexState *ls, const char *msg) {
luaX_lexerror(ls, msg, ls->t.token);
}
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
lua_State *L = ls->L;
TString *ts = luaS_newlstr(L, str, l);
TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */
if (ttisnil(o)) {
setbvalue(o, 1); /* make sure `str' will not be collected */
luaC_checkGC(L);
}
return ts;
}
/* 读到单个换行符\n,\r,再多读一个字符,看是否是双字符的换行\n\r,\r\n */
static void inclinenumber (LexState *ls) {
int old = ls->current;
lua_assert(currIsNewline(ls)); /* 当前是换行符 */
/* 往后再读一个字符,看看不是不是\n\r和\r\n这种双字符的换行符(仅算一次换行) */
next(ls); /* skip `\n' or `\r' */
if (currIsNewline(ls) && ls->current != old)
next(ls); /* skip `\n\r' or `\r\n' */
if (++ls->linenumber >= MAX_INT)
luaX_syntaxerror(ls, "chunk has too many lines");
}
/* 初始化LexState,初始化ls->buf,从ZIO读取第一个字符 */
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) {
ls->decpoint = '.';
ls->L = L;
ls->lookahead.token = TK_EOS; /* no look-ahead token */
ls->z = z;
ls->fs = NULL; /* 尚未开始编译函数,这里置NULL */
ls->linenumber = 1; /* 当前在第一行,亲 */
ls->lastline = 1;
ls->source = source;
/* 申请属于ls->buff的私有buff */
luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
next(ls); /* read first char */
}
/*
** =======================================================
** LEXICAL ANALYZER
** =======================================================
*/
/* ls->current和set字符集中的某个字符匹配吗 */
static int check_next (LexState *ls, const char *set) {
if (!strchr(set, ls->current))
return 0;
save_and_next(ls);
return 1;
}
/* 将ls->buff中的from字符替换层to字符 */
static void buffreplace (LexState *ls, char from, char to) {
size_t n = luaZ_bufflen(ls->buff);
char *p = luaZ_buffer(ls->buff);
while (n--)
if (p[n] == from) p[n] = to;
}
/* 数字字符串转换为数字时失败,尝试更换成本地区的数字字符小数点后再次尝试转换 */
static void trydecpoint (LexState *ls, SemInfo *seminfo) {
/* format error: try to update decimal point separator */
struct lconv *cv = localeconv();
char old = ls->decpoint;
ls->decpoint = (cv ? cv->decimal_point[0] : '.');
buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */
if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
/* format error with correct decimal point: no more options */
buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
luaX_lexerror(ls, "malformed number", TK_NUMBER);
}
}
/* LUA_NUMBER
** .123这种 123或者科学计数法形式(1.99714E+13)的数字
*/
static void read_numeral (LexState *ls, SemInfo *seminfo) {
lua_assert(isdigit(ls->current)); /* 属于 [0,9] 集合?*/
/* 读取第一部分 1.99714 */
do {
save_and_next(ls);
} while (isdigit(ls->current) || ls->current == '.');
/* 读取第二部分 E+ */
if (check_next(ls, "Ee")) /* `E'? */
check_next(ls, "+-"); /* optional exponent sign */
/* 读取第三部分 13 */
while (isalnum(ls->current) || ls->current == '_') /* 这里的_不太明白其含义 */
save_and_next(ls);
/* 主动补\0,关闭字符串 */
save(ls, '\0');
/* 不同国家不同的小数点 */
buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
trydecpoint(ls, seminfo); /* try to update decimal point separator */
}
/* 尝试读取多行字符串的开头或结尾 --[{=}[ 或者 ]{=}] */
static int skip_sep (LexState *ls) {
int count = 0;
int s = ls->current;
lua_assert(s == '[' || s == ']');
save_and_next(ls);
while (ls->current == '=') {
save_and_next(ls);
count++;
}
return (ls->current == s) ? count : (-count) - 1;
}
/* 读取一个长字符串和"结尾控制符"(长注释或String的token)(同时更新linenumber) */
static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
int cont = 0;
(void)(cont); /* avoid warnings when `cont' is not used */
save_and_next(ls); /* skip 2nd `[' */
if (currIsNewline(ls)) /* string starts with a newline? */
inclinenumber(ls); /* skip it */
for (;;) {
switch (ls->current) {
case EOZ:
luaX_lexerror(ls, (seminfo) ? "unfinished long string" :
"unfinished long comment", TK_EOS);
break; /* to avoid warnings */
#if defined(LUA_COMPAT_LSTR)
case '[': {
if (skip_sep(ls) == sep) {
save_and_next(ls); /* skip 2nd `[' */
cont++;
#if LUA_COMPAT_LSTR == 1
if (sep == 0)
luaX_lexerror(ls, "nesting of [[...]] is deprecated", '[');
#endif
}
break;
}
#endif
case ']': {
if (skip_sep(ls) == sep) {
save_and_next(ls); /* skip 2nd `]' */
#if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2
cont--;
if (sep == 0 && cont >= 0) break;
#endif
goto endloop;
}
break;
}
case '\n':
case '\r': {
save(ls, '\n');
inclinenumber(ls); /* 注释的业务中也不能忘了linenumber */
if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
break;
}
default: {
if (seminfo) save_and_next(ls);
else next(ls);
}
}
} endloop:
/* seminfo为null是注释逻辑,注释不用管里面的具体String,否则是一个正常的String的token,需将String保存 */
if (seminfo) {
/* 2+sep:[{=}[ */
seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),
luaZ_bufflen(ls->buff) - 2*(2 + sep));
}
}
/* 读一个"字符串"或'字符串'格式的字符串 */
static void read_string (LexState *ls, int del, SemInfo *seminfo) {
save_and_next(ls);
while (ls->current != del) {
switch (ls->current) {
case EOZ:
luaX_lexerror(ls, "unfinished string", TK_EOS);
continue; /* to avoid warnings */
case '\n':
case '\r':
luaX_lexerror(ls, "unfinished string", TK_STRING);
continue; /* to avoid warnings */
case '\\': { /* 可能的转移序列 */
int c;
next(ls); /* do not save the `\' */
switch (ls->current) {
case 'a': c = '\a'; break;
case 'b': c = '\b'; break;
case 'f': c = '\f'; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case 'v': c = '\v'; break;
/* 本身就是一个换行符 */
case '\n': /* go through */
case '\r': save(ls, '\n'); inclinenumber(ls); continue;
case EOZ: continue; /* will raise an error next loop */
default: {
if (!isdigit(ls->current))
save_and_next(ls); /* handles \\, \", \', and \? */
else { /* \xxx */
int i = 0;
c = 0;
do {
c = 10*c + (ls->current-'0');
next(ls);
} while (++i<3 && isdigit(ls->current));
if (c > UCHAR_MAX)
luaX_lexerror(ls, "escape sequence too large", TK_STRING);
save(ls, c);
}
continue;
}
}
save(ls, c);
next(ls);
continue;
}
default:
save_and_next(ls);
}
}
save_and_next(ls); /* skip delimiter(分隔符) */
seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
luaZ_bufflen(ls->buff) - 2);
}
/* 从读取下一个字符token
** 从ZIO读取下一个token到seminfo,并返回TokenType(中途可能用到lx->buff)
*/
static int llex (LexState *ls, SemInfo *seminfo) {
luaZ_resetbuffer(ls->buff);
for (;;) {
switch (ls->current) {
case '\n':
case '\r': {
inclinenumber(ls);
continue;
}
case '-': {
next(ls);
/* 单独的 - */
if (ls->current != '-') return '-';
/* else is a comment */
next(ls); /* 单行OR多行注释? */
if (ls->current == '[') {
int sep = skip_sep(ls);
luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
if (sep >= 0) {
read_long_string(ls, NULL, sep); /* long comment */
luaZ_resetbuffer(ls->buff);
continue;
}
}
/* else short comment */
while (!currIsNewline(ls) && ls->current != EOZ)
next(ls);
continue;
}
case '[': { /* 长字符串: [{=}[ String ]{=}] */
int sep = skip_sep(ls);
if (sep >= 0) { /* 多行字符串开头 [{=}[ */
read_long_string(ls, seminfo, sep);
return TK_STRING;
}
else if (sep == -1) return '['; /* [,others这样的开头 */
else luaX_lexerror(ls, "invalid long string delimiter", TK_STRING); /* [={=},others这样的开头 */
}
case '=': {
next(ls);
if (ls->current != '=') return '=';
else { next(ls); return TK_EQ; }
}
case '<': {
next(ls);
if (ls->current != '=') return '<';
else { next(ls); return TK_LE; }
}
case '>': {
next(ls);
if (ls->current != '=') return '>';
else { next(ls); return TK_GE; }
}
case '~': {
next(ls);
if (ls->current != '=') return '~';
else { next(ls); return TK_NE; }
}
/* 短字符串 */
case '"':
case '\'': {
read_string(ls, ls->current, seminfo);
return TK_STRING;
}
/* 看这个符号的解析,是一个深度优先的解析示例 */
case '.': {
save_and_next(ls);
if (check_next(ls, ".")) {
if (check_next(ls, "."))
return TK_DOTS; /* ... */
else return TK_CONCAT; /* .. */
}
else if (!isdigit(ls->current)) return '.';
else {
read_numeral(ls, seminfo);
return TK_NUMBER;
}
}
case EOZ: {
return TK_EOS;
}
default: {
if (isspace(ls->current)) {
lua_assert(!currIsNewline(ls)); /* 换行符在前面就被解析掉了,这里不能再是换行符了,否则就重复了 */
next(ls);
continue;
}
else if (isdigit(ls->current)) {
read_numeral(ls, seminfo);
return TK_NUMBER;
}
else if (isalpha(ls->current) || ls->current == '_') { /* 标识符 */
/* identifier or reserved word */
TString *ts;
do {
save_and_next(ls);
} while (isalnum(ls->current) || ls->current == '_'); /* 这里和上面的有一点差别 */
ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
luaZ_bufflen(ls->buff));
/* 关键或保留字符串 */
if (ts->tsv.reserved > 0) /* reserved word? */
return ts->tsv.reserved - 1 + FIRST_RESERVED;
else {
seminfo->ts = ts;
return TK_NAME;
}
}
else {
int c = ls->current;
next(ls);
return c; /* single-char tokens (+ - / ...) */
}
}
}
}
}
void luaX_next (LexState *ls) {
ls->lastline = ls->linenumber
/* 已有一个准备好的lookhead'token,则取出来用 */
if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
ls->t = ls->lookahead; /* use this one */
ls->lookahead.token = TK_EOS; /* and discharge it */
}
else /* 读取一个token */
ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
}
/* 当前lookhead'token已过期,重新准备lookhead'token */
void luaX_lookahead (LexState *ls) {
lua_assert(ls->lookahead.token == TK_EOS);
ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
}
/*
** $Id: ldo.c,v 2.38.1.4 2012/01/18 02:27:10 roberto Exp $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
#include <setjmp.h>
#include <stdlib.h>
#include <string.h>
#define ldo_c
#define LUA_CORE
#include "lua.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lparser.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#include "lundump.h"
#include "lvm.h"
#include "lzio.h"
/*
** {======================================================
** Error-recovery functions
** =======================================================
*/
/* chain list of long jump buffers */
struct lua_longjmp {
struct lua_longjmp *previous;
luai_jmpbuf b;
volatile int status; /* error code */
};
void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
switch (errcode) {
case LUA_ERRMEM: {
setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG));
break;
}
case LUA_ERRERR: {
setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
break;
}
case LUA_ERRSYNTAX:
case LUA_ERRRUN: {
setobjs2s(L, oldtop, L->top - 1); /* error message on current top */
break;
}
}
/* 这里结合 luaD_pcall 来一起来看 */
L->top = oldtop + 1; /* correct top */
}
/* 空闲的callInfo过多时,尝试压缩其空间 */
static void restore_stack_limit (lua_State *L) {
lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
if (L->size_ci > LUAI_MAXCALLS) { /* there was an overflow? */
int inuse = cast_int(L->ci - L->base_ci);
if (inuse + 1 < LUAI_MAXCALLS) /* can `undo' overflow? */
luaD_reallocCI(L, LUAI_MAXCALLS);
}
}
/* 回滚stack到初始状态!!!! */
static void resetstack (lua_State *L, int status) {
/* 这一下彻底回滚了 */
L->ci = L->base_ci;
L->base = L->ci->base;
luaF_close(L, L->base); /* close eventual pending closures */
luaD_seterrorobj(L, status, L->base);
L->nCcalls = L->baseCcalls;
L->allowhook = 1;
restore_stack_limit(L);
L->errfunc = 0;
L->errorJmp = NULL;
}
/* 尝试调用异常处理函数
** 主要在luaG_errormsg中被间接调用
*/
void luaD_throw (lua_State *L, int errcode) {
if (L->errorJmp) {
L->errorJmp->status = errcode; /* !!! 跳出去之前设置status */
LUAI_THROW(L, L->errorJmp); /* 正式跳出 */
}
else { /* 没有设置errHdl,调用panic后退出进程 */
L->status = cast_byte(errcode); /* 无jump点了,在这里设置L的状态,有则由上层业务处理 */
if (G(L)->panic) {
resetstack(L, errcode); /* 这里对stack进行收尾 */
lua_unlock(L);
G(L)->panic(L);
}
exit(EXIT_FAILURE);
}
}
/* 保护模式下(longjump)调用C函数
** 但发生错误,则调用了L->errfunc后(若设置了),后走到这里而不是直接退出进程
**
** RETURN:执行流的执行结果,没有同步到L->status中(由上层调用决定是否同步)
*/
int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
struct lua_longjmp lj;
lj.status = 0;
lj.previous = L->errorJmp; /* chain new error handler */
L->errorJmp = &lj;
LUAI_TRY(L, &lj,
(*f)(L, ud);
);
L->errorJmp = lj.previous; /* restore old error handler */
return lj.status; /* luaD_throw()中更新了status */
}
/* }====================================================== */
/* stack移动后更新upvalues,ci-list和L->base */
static void correctstack (lua_State *L, TValue *oldstack) {
CallInfo *ci;
GCObject *up;
L->top = (L->top - oldstack) + L->stack;
for (up = L->openupval; up != NULL; up = up->gch.next)
gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;
for (ci = L->base_ci; ci <= L->ci; ci++) {
ci->top = (ci->top - oldstack) + L->stack;
ci->base = (ci->base - oldstack) + L->stack;
ci->func = (ci->func - oldstack) + L->stack;
}
/* L->ci不用调整哈 */
L->base = (L->base - oldstack) + L->stack;
}
/* 重新调整stack的大小 */
void luaD_reallocstack (lua_State *L, int newsize) {
TValue *oldstack = L->stack;
int realsize = newsize + 1 + EXTRA_STACK;
lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1); /* 和stack_init()函数对应 */
luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue);
L->stacksize = realsize;
L->stack_last = L->stack+newsize;
correctstack(L, oldstack);
}
/* 调整callInfo链的大小 */
void luaD_reallocCI (lua_State *L, int newsize) {
CallInfo *oldci = L->base_ci;
luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
L->size_ci = newsize;
L->ci = (L->ci - oldci) + L->base_ci;
L->end_ci = L->base_ci + L->size_ci - 1;
}
void luaD_growstack (lua_State *L, int n) {
if (n <= L->stacksize) /* double size is enough? */
luaD_reallocstack(L, 2*L->stacksize);
else
luaD_reallocstack(L, L->stacksize + n);
}
static CallInfo *growCI (lua_State *L) {
if (L->size_ci > LUAI_MAXCALLS) /* overflow while handling overflow? 嵌套调用层次太深了,直接报错,方便用户检查调用情况 */
luaD_throw(L, LUA_ERRERR);
else {
luaD_reallocCI(L, 2*L->size_ci); /* 简单粗暴,直接扩大一倍 */
if (L->size_ci > LUAI_MAXCALLS)
luaG_runerror(L, "stack overflow");
}
return ++L->ci;
}
static StkId callrethooks (lua_State *L, StkId firstResult) {
ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
luaD_callhook(L, LUA_HOOKRET, -1);
if (f_isLua(L->ci)) { /* Lua function? */
while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */
luaD_callhook(L, LUA_HOOKTAILRET, -1);
}
return restorestack(L, fr);
}
/* 调用钩子函数 */
void luaD_callhook (lua_State *L, int event, int line) {
lua_Hook hook = L->hook;
if (hook && L->allowhook) {
ptrdiff_t top = savestack(L, L->top);
ptrdiff_t ci_top = savestack(L, L->ci->top);
lua_Debug ar;
ar.event = event;
ar.currentline = line;
if (event == LUA_HOOKTAILRET)
ar.i_ci = 0; /* tail call; no debug information about it */
else
ar.i_ci = cast_int(L->ci - L->base_ci);
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
L->ci->top = L->top + LUA_MINSTACK;
lua_assert(L->ci->top <= L->stack_last);
L->allowhook = 0; /* cannot call hooks inside a hook */
lua_unlock(L);
(*hook)(L, &ar); /* 正式调用钩子函数 */
lua_lock(L);
lua_assert(!L->allowhook);
/* !!!! 现场需恢复,别忘了,亲 */
L->allowhook = 1;
L->ci->top = restorestack(L, ci_top);
L->top = restorestack(L, top);
}
}
/*
**补齐固定形参(若实际传入的参数不够)
**将传给固定形参的值mv到top之上且纠正top
**将剩下(若还有剩下)的参数留给变参...
*/
static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
int i;
int nfixargs = p->numparams;
Table *htab = NULL;
StkId base, fixed;
/* 传入的参数数量不够填补fixed参数的,直接补nil:至少得把fixed形参需要的个数补齐 */
for (; actual < nfixargs; ++actual)
setnilvalue(L->top++);
#if defined(LUA_COMPAT_VARARG) /* 将留给...的参数信息打包到额外的arg表中 */
if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */
int nvar = actual - nfixargs; /* number of extra arguments */
lua_assert(p->is_vararg & VARARG_HASARG);
luaC_checkGC(L);
luaD_checkstack(L, p->maxstacksize);
htab = luaH_new(L, nvar, 1); /* create `arg' table */
for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */
setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i);
/* store counter in field `n' */
setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
}
#endif
/* move fixed parameters to final position */
fixed = L->top - actual; /* first fixed argument */
base = L->top; /* final position of first argument */
/* 从第一个参数开始移动其值到被调函数的fixed‘arg域,直到给所有的fixed'arg赋值为止
** 如果还剩下多余的参数,则直接保留下来(留给变参...),无需移动
*/
for (i=0; i<nfixargs; i++) {
setobjs2s(L, L->top++, fixed+i); /* !!!!这里移动了top指针 */
setnilvalue(fixed+i);
}
/* add `arg' parameter */
if (htab) {
sethvalue(L, L->top++, htab);
lua_assert(iswhite(obj2gco(htab)));
}
return base;
}
/* 直接看代码 */
static StkId tryfuncTM (lua_State *L, StkId func) {
const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
StkId p;
ptrdiff_t funcr = savestack(L, func);
if (!ttisfunction(tm))
luaG_typeerror(L, func, "call");
/* Open a hole inside the stack at `func' */
for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
incr_top(L);
func = restorestack(L, funcr); /* previous call may change stack */
setobj2s(L, func, tm); /* tag method is the new function to be called */
return func;
}
#define inc_ci(L) \
((L->ci == L->end_ci) ? growCI(L) : \
(condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci))
/* 先做调用前的准备工作,后进入函数调用(for C,not Lua)
** nresults:-1返回所有的返回值
** 0:不要返回值
** 1:期待一个返回值
*/
int luaD_precall (lua_State *L, StkId func, int nresults) {
LClosure *cl;
ptrdiff_t funcr; /* 当前调用函数的pc距离stack栈底的偏移量 */
if (!ttisfunction(func)) /* `func' is not a function? */
func = tryfuncTM(L, func); /* check the `function' tag method */
/* 随着新的调用产生,ci链/stack可能因为增长而移动位置
** 故不能记住绝地位置而记住相对位置,后面根据此值最终确定ci->func
*/
funcr = savestack(L, func);
cl = &clvalue(func)->l;
L->ci->savedpc = L->savedpc; /* 正式调用前,存档L->savedpc至L->ci->savedpc */
if (!cl->isC) { /* Lua function? prepare its call */
CallInfo *ci;
StkId st, base;
Proto *p = cl->p;
luaD_checkstack(L, p->maxstacksize);
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 */
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 = inc_ci(L); /* now `enter' new function */
ci->func = func;
L->base = ci->base = base;
/* 这里可以推导出L->base---->L->top之间的区域都是ci的私有栈空间(lua,c均如此) */
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;
/* 新的函数的私有栈空间直接补nil(参数的区域除外) */
for (st = L->top; st < ci->top; st++)
setnilvalue(st);
/* 最后调整L->top使其指向本次ci的栈顶,对于Lua函数而言L->Base---->(L->Base+L->maxstacksize)之间都是我私有的了,且是有效的
** C由于L->top是动态变化的,故而L->top的值被设置为传入参数后栈顶的位置,后面会因为push等函数而动态变化-
*/
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;
}
else { /* if is a C function, call it */
CallInfo *ci;
int n;
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
/* 填充新的CallInfo */
ci = inc_ci(L); /* now `enter' new function */
ci->func = restorestack(L, funcr);
L->base = ci->base = ci->func + 1; /* C函数没有Lua函数的变参问题,所以这里无需adjust_varargs() */
/* "OP_CALL指令"已经将L->top指向了最后一个传入参数的上方 */
ci->top = L->top + LUA_MINSTACK; /* 这里和上面luaD_checkstack呼应 */
lua_assert(ci->top <= L->stack_last);
ci->nresults = nresults;
if (L->hookmask & LUA_MASKCALL)
luaD_callhook(L, LUA_HOOKCALL, -1);
lua_unlock(L);
// L->top已经在lvm中准备好了(call和vararg指令)
n = (*curr_func(L)->c.f)(L); /* do the actual call */
lua_lock(L);
if (n < 0) /* yielding, co调用yield,co.yeild运行完毕了,co.yeild还不能释放ci-list信息,需等到母thread调用resume,将控制权转移到co,再在co.resume中luaD_poscall()才释放 */
return PCRYIELD;
else {
luaD_poscall(L, L->top - n); /* 调整子C函数的返回值到指定位置并适配母函数的wanted(results) */
return PCRC;
}
}
}
/* 函数调用结束后,处理实际返回值和期待返回值的匹配问题
** 也处理ci链的嵌套逻辑(本层ci结束往后退一层)
**
** 即处理C函数调用,也处理Lua函数执行结束即将返回这两种情况
** 没有检测C函数说返回了n个参数,当实际上没有返回那么多参数的情况
** RETURNS: wanted.cnt: 0:返回多个参数,1:返回0个,2:返回1个。。。
*/
int luaD_poscall (lua_State *L, StkId firstResult) {
StkId res;
int wanted, i;
CallInfo *ci;
if (L->hookmask & LUA_MASKRET)
firstResult = callrethooks(L, firstResult);
ci = L->ci--;
res = ci->func; /* res == final position of 1st result */
wanted = ci->nresults;
L->base = (ci - 1)->base; /* restore base */
L->savedpc = (ci - 1)->savedpc; /* restore savedpc */
/* move results to correct place */
for (i = wanted; i != 0 && firstResult < L->top; i--) /* 这个判断即处理非尾调用,又处理了尾调用 */
setobjs2s(L, res++, firstResult++); /* wanted根据实际返回数量赋值 */
while (i-- > 0)
setnilvalue(res++); /* local a, b, c = funcA(...), 针对 funcA的返回值不够则补nil */
/*
** L->top恢复到最后一个返回参数在stack的位置,这里和调用函数之前,
** 将L->top设置到最后一个传入参数在stack的位置相呼应了!!!
**
** 最终将L->top恢复到ci->top是由“OP_CALL”指令负责
*/
L->top = res;
return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */
}
/*
** Call a function (C or Lua). The function to be called is at *func.
** The arguments are on the stack, right after the function.
** When returns, all the results are on the stack, starting at the original
** function position.
*/
void luaD_call (lua_State *L, StkId func, int nResults) {
if (++L->nCcalls >= LUAI_MAXCCALLS) { /* 调用层次太深,进入抛出异常 */
if (L->nCcalls == LUAI_MAXCCALLS)
luaG_runerror(L, "C stack overflow");
else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
}
if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */
luaV_execute(L, 1); /* call it, 这里的1是真的妙啊 */
L->nCcalls--;
luaC_checkGC(L);
}
/* 协程co开始执行co.resume 母thread在lbaselib.auxresume()中交出CPU,等待子co返回 */
static void resume (lua_State *L, void *ud) {
StkId firstArg = cast(StkId, ud); /* 没有传参时firstArg指向top,下面的firstArg>L->base还是成立 */
CallInfo *ci = L->ci;
if (L->status == 0) { /* start coroutine? */
lua_assert(ci == L->base_ci); /* 尚未有任何调用链ci生成(或co已运行完毕) */
lua_assert(firstArg > L->base); /* 至少还有个参数(是co.fun),意味着不是co运行完毕的状态,运行完毕后不能调用本函数了,co.fun都没有了,ci也是空的,ro不知道该怎么运行了不是 */
/* 若是崭新的co第一次开始运行resume,则会生成相应的ci(co.initFun),再运行起来和普通的c.main中构建一个thread后第一次运行是一样的 */
if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA)
return;
} else { /* resuming from previous yield */
lua_assert(L->status == LUA_YIELD); /* 非YEILD状态,不能调用resume */
L->status = 0; /* switch back status */
if (!f_isLua(ci)) { /* `common' yield? ci这里指向的是baselib.yield */
/* finish interrupted execution of `OP_CALL' */
lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL ||
GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL);
if (luaD_poscall(L, firstArg)) /* complete it... 结束上述说的baselib.yield的调用流程 */
L->top = L->ci->top; /* and correct top if not multiple results,如果是 multiple results则由跟在后面的vararg或者setlist来调整L->top(他们还需要用到L->top来确定传入参数的个数呢,所以这里不能将其恢复到L->ci->top,) */
}
else /* yielded inside a hook: just continue its execution */
L->base = L->ci->base;
}
luaV_execute(L, cast_int(L->ci - L->base_ci)); /* 这里的nexeccalls值得好好推导一下 */
}
static int resume_error (lua_State *L, const char *msg) {
L->top = L->ci->base;
setsvalue2s(L, L->top, luaS_new(L, msg));
incr_top(L);
lua_unlock(L);
return LUA_ERRRUN;
}
LUA_API int lua_resume (lua_State *L, int nargs) {
int status;
lua_lock(L);
if (L->status != LUA_YIELD && (L->status != 0 || L->ci != L->base_ci))
return resume_error(L, "cannot resume non-suspended coroutine");
if (L->nCcalls >= LUAI_MAXCCALLS)
return resume_error(L, "C stack overflow");
luai_userstateresume(L, nargs);
lua_assert(L->errfunc == 0);
L->baseCcalls = ++L->nCcalls;
/* 必须protected状态下call,不然协程出错,整个进程都会被关闭
** 本函数还没有为co生成ci链,resume中会生成co的ci调用链(如果是第一次resume)
*/
status = luaD_rawrunprotected(L, resume, L->top - nargs);
if (status != 0) { /* error? */
L->status = cast_byte(status); /* mark thread as `dead' */
luaD_seterrorobj(L, status, L->top);
L->ci->top = L->top; /* 上面压入了errMsg这里更新下top */
}
else {
lua_assert(L->nCcalls == L->baseCcalls);
status = L->status; /* coroutinue运行中出让则为 LUA_YIELD */
}
--L->nCcalls;
lua_unlock(L);
return status;
}
LUA_API int lua_yield (lua_State *L, int nresults) {
luai_userstateyield(L, nresults);
lua_lock(L);
if (L->nCcalls > L->baseCcalls)
luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
L->base = L->top - nresults; /* protect stack slots below */
L->status = LUA_YIELD;
lua_unlock(L);
return -1; /* note:这是一个特殊的值,用于标识从yield返回 */
}
/* old_top 指向被调用函数slot
** KEYCODE
*/
int luaD_pcall (lua_State *L, Pfunc func, void *u,
ptrdiff_t old_top, ptrdiff_t ef) {
int status;
unsigned short oldnCcalls = L->nCcalls;
/* 存档当前的ci,以便发生错误恢复时使用 */
ptrdiff_t old_ci = saveci(L, L->ci); /* 这里只能记住offset而不是绝对地址(call过程中ci可能会调整!) */
lu_byte old_allowhooks = L->allowhook;
ptrdiff_t old_errfunc = L->errfunc;
L->errfunc = ef;
status = luaD_rawrunprotected(L, func, u);
/* 发生了错误,回滚到存档时刻 */
if (status != 0) { /* an error occurred? */
StkId oldtop = restorestack(L, old_top);
luaF_close(L, oldtop); /* close eventual pending closures */
luaD_seterrorobj(L, status, oldtop); /* 顺带correct了top */
L->nCcalls = oldnCcalls;
L->ci = restoreci(L, old_ci);
L->base = L->ci->base;
L->savedpc = L->ci->savedpc;
L->allowhook = old_allowhooks;
restore_stack_limit(L);
}
L->errfunc = old_errfunc;
return status;
}
/*
** Execute a protected parser.
*/
struct SParser { /* data to `f_parser' */
ZIO *z;
Mbuffer buff; /* buffer to be used by the scanner */
const char *name;
};
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);
cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L))); /* 新生成的clouse的env直接来自gobal'table而不是上层函数的env */
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);
incr_top(L);
}
int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {
struct SParser p;
int status;
p.z = z; p.name = name;
luaZ_initbuffer(L, &p.buff);
status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
luaZ_freebuffer(L, &p.buff);
return status;
}
Lua 5.1 Reference Manual by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
Copyright © 2006–2012 Lua.org, PUC-Rio. Freely available under the terms of the Lua license.
contents · index · other versions · english · português · español · deutsch 1 – Introduction Lua is an extension programming language designed to support general procedural programming with data description facilities. It also offers good support for object-oriented programming, functional programming, and data-driven programming. Lua is intended to be used as a powerful, light-weight scripting language for any program that needs one. Lua is implemented as a library, written in clean C (that is, in the common subset of ANSI C and C++).