diff options
| author | chai <chaifix@163.com> | 2020-10-01 14:35:21 +0800 |
|---|---|---|
| committer | chai <chaifix@163.com> | 2020-10-01 14:35:21 +0800 |
| commit | dc0e292e76a00fb43ad7fbdd6d65a560927ce981 (patch) | |
| tree | 199b64a68eed6909733ab2b93cbe52c1e655f477 /src/lua51 | |
| parent | 63cb4fbbb961da133c68865845eaf22d9b876700 (diff) | |
*gc
Diffstat (limited to 'src/lua51')
| -rw-r--r-- | src/lua51/ldo.c | 2 | ||||
| -rw-r--r-- | src/lua51/lfunc.c | 2 | ||||
| -rw-r--r-- | src/lua51/lobject.h | 2 | ||||
| -rw-r--r-- | src/lua51/lvm.c | 2 | ||||
| -rw-r--r-- | src/lua51/misc.c | 47 | ||||
| -rw-r--r-- | src/lua51/misc.h | 11 |
6 files changed, 62 insertions, 4 deletions
diff --git a/src/lua51/ldo.c b/src/lua51/ldo.c index 2311d41..736697d 100644 --- a/src/lua51/ldo.c +++ b/src/lua51/ldo.c @@ -533,7 +533,7 @@ static void f_parser (lua_State *L, void *ud) { //c 编译之后会得到Proto* tf, 所有的信息,包括常量表、upvalue、字节码、local变量数量等等都在里面 - cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L))); + cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L))); // 初始情况下函数原型的环境表是全局表 cl->l.p = tf; //c 设置proto = tf //c 新建nups个closed upvalue for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */ diff --git a/src/lua51/lfunc.c b/src/lua51/lfunc.c index 5835295..1f0890e 100644 --- a/src/lua51/lfunc.c +++ b/src/lua51/lfunc.c @@ -37,7 +37,7 @@ Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) { Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); luaC_link(L, obj2gco(c), LUA_TFUNCTION); //c 加入GC链表 c->l.isC = 0; //c not c closure - c->l.env = e; //c 设置这个闭包的环境 + c->l.env = e; //c 设置这个闭包的环境表 c->l.nupvalues = cast_byte(nelems); while (nelems--) c->l.upvals[nelems] = NULL; return c; diff --git a/src/lua51/lobject.h b/src/lua51/lobject.h index 5d6df6c..157fb37 100644 --- a/src/lua51/lobject.h +++ b/src/lua51/lobject.h @@ -356,7 +356,7 @@ typedef struct LClosure { lu_byte isC; //c is c closure lu_byte nupvalues; //c number of upvalues GCObject *gclist; //c gclist? - struct Table *env //c 这个闭包的环境,在luaF_newLclosure设置 + struct Table *env //c 这个闭包的环境表,在luaF_newLclosure设置 */ struct Proto *p; // lua闭包的函数原型 UpVal *upvals[1]; // lua闭包的upvalue diff --git a/src/lua51/lvm.c b/src/lua51/lvm.c index 8733b07..374dcd3 100644 --- a/src/lua51/lvm.c +++ b/src/lua51/lvm.c @@ -739,7 +739,7 @@ reentry: /* entry point */ p = cl->p->p[GETARG_Bx(i)]; //c 根据索引得到函数原型地址 //c lua的每个函数会记录这个函数的信息:参数个数、使用到的stack slot数目、upvalue数目、局部变量数目、常数表、局部函数 nup = p->nups; //c number of upvalues, - //c 创建闭包new closure + //c 创建闭包并设置环境 ncl = luaF_newLclosure(L, nup, cl->env); ncl->l.p = p; // 设置函数原型 /* //c 一个闭包的创建如下 diff --git a/src/lua51/misc.c b/src/lua51/misc.c new file mode 100644 index 0000000..557a05c --- /dev/null +++ b/src/lua51/misc.c @@ -0,0 +1,47 @@ +#include "misc.h" +#include <stdarg.h> +#include <string.h> + +void log(const char* fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + + int i = 0; + while (fmt[i] != '\0') + { + if (fmt[i] != '%') + { + printf("%c", fmt[i]); + i++; + + continue; + } + + fmt++; // 跳过% + switch (fmt[i]) + { + case 'c': // 得到一个字符 + char cc; + cc = (char)va_arg(ap, int); + printf("%c", cc); + break; + case 'd': // 得到一个整数 + int dd; + dd = (int)va_arg(ap, int); + printf("%d", dd); + break; + case 's': // 得到一个字符串 + char *ss; + ss = va_arg(ap, char*); + printf("%s", ss); + break; + } + + va_end(ap); + } + + printf("\n"); +} + + diff --git a/src/lua51/misc.h b/src/lua51/misc.h new file mode 100644 index 0000000..be3fa92 --- /dev/null +++ b/src/lua51/misc.h @@ -0,0 +1,11 @@ +#ifndef MISC_H +#define MISC_H + +// 调试用代码 + +#include <stdio.h> +#include <stdarg.h> + +void log(const char* formmat, ...); + +#endif
\ No newline at end of file |
