前言
这是一篇关于lua源码分析的杂谈,里面混入各种知识
正文
static的作用,一个局部变量的创建是在栈区,当出栈的时候,局部变量就销毁了,而使用static 关键字修饰,可以改变变量的生命周期,也就是不会销毁,但是不会改变作用域。使用static修饰全局变量的时候,会使它的作用域变小,也就是外部不可以链接
remalloc函数支持缩容解析
luaL_openlibs1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31extern void luaL_openlibs (lua_State *L) {
const luaL_Reg *lib;
/* "require" functions from 'loadedlibs' and set results to global table */
for (lib = loadedlibs; lib->func; lib++) {
luaL_requiref(L, lib->name, lib->func, 1);
lua_pop(L, 1); /* remove lib */
}
}
// modname = _G
// openf = luaopen_base
extern void luaL_requiref (lua_State *L, const char *modname,
lua_CFunction openf, int glb) {
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
lua_getfield(L, -1, modname); /* LOADED[modname] */
if (!lua_toboolean(L, -1)) { /* package not already loaded? */
lua_pop(L, 1); /* remove field */
lua_pushcfunction(L, openf);
lua_pushstring(L, modname); /* argument to open function */
lua_call(L, 1, 1); /* call 'openf' to open module */
lua_pushvalue(L, -1); /* make copy of module (call result) */
lua_setfield(L, -3, modname); /* LOADED[modname] = module */
}
lua_remove(L, -2); /* remove LOADED table */
if (glb) {
lua_pushvalue(L, -1); /* copy of module */
lua_setglobal(L, modname); /* _G[modname] = module */
}
}
lua_setfield(L, -2, "_G"); 其实会把 最后又会把l->top -2,其实又回到起点函数
lua_call其实就是new了一个ci,如果想得到一个返回值其实就是ci->top + wanted就是函数执行的返回值了lua数据类型,lua的
tvalue就包括所有类型1
2
3
4
5
6
7
8
9
10
11/*
** Union of all Lua values
*/
typedef union Value {
GCObject *gc; /* collectable objects */
void *p; /* light userdata */
int b; /* booleans */
lua_CFunction f; /* light C functions */
lua_Integer i; /* integer numbers */
lua_Number n; /* float numbers */
} Value;gcobject类型如下,这是需要垃圾回收的类型1
2
3
4
5struct GCObject {
GCObject *next;
lu_byte tt;
lu_byte marked;
};