Lua学习---lua源代码的分析顺序
2011-12-18 20:48
387 查看
Recommended reading order:
lmathlib.c, lstrlib.c: get familiar with the external C API. Don't bother with the pattern matcher though. Just the easy functions.
lapi.c: Check how the API is implemented internally. Only skim this to get a feeling for the code. Cross-reference to lua.h and luaconf.h as needed.
lobject.h: tagged values and object representation. skim through this first. you'll want to keep a window with this file open all the time.
lstate.h: state objects. ditto.
lopcodes.h: bytecode instruction format and opcode definitions. easy.
lvm.c: scroll down to luaV_execute, the main interpreter loop. see how all of the instructions are implemented. skip the details for now. reread later.
ldo.c: calls, stacks, exceptions, coroutines. tough read.
lstring.c: string interning. cute, huh?
ltable.c: hash tables and arrays. tricky code.
ltm.c: metamethod handling, reread all of lvm.c now.
You may want to reread lapi.c now.
ldebug.c: surprise waiting for you. abstract interpretation is used to find object names for tracebacks. does bytecode verification, too.
lparser.c, lcode.c: recursive descent parser, targetting a register-based VM. start from chunk() and work your way through. read the expression parser and the code generator parts last.
lgc.c: incremental garbage collector. take your time.
Read all the other files as you see references to them. Don't let your stack
英文比较简单,我就不翻译了。
今天已经读了 lmathlib.c, lstrlib.c, lapi.c这三个文件。
对于lmathlib.c和lstrllib.c 这个两个文件主要是熟悉如何扩展Lua,一般使用过Lua的人都对这个会有所了解。只是简单的读一读、熟悉代码风格,不用花太多时间(我只看了20分钟)
lapi.c这里就开始有点难度了, 主要是你这里有一些平时没有接触过的东西。阅读这个文件可以结合手册中第三章C API来进行。
由于lapi.c涉及到很多的宏和结构,建议装一个vim + ctags或Visual studio 2010做cross references。
以下是一些关于lapi.c笔记:
TValue:由Value 和tt 组成, tt为类型标识,等于LUA_TNIL的呢个 Value是一个联合体,
根据tt的值不同使用不同的值,lua中所有的值、对象,都是由这个结构储存。以下是Value联合体的定义:
typedef union {
GCObject *gc;
void *p;
lua_Number n;
int b;
} Value;
GCObject:里面的内容比较复杂,从字面上理解是指就是gc会收集的对象,
可以理解为Lua中的基本对象。包括Thread,Table,UserData,String都是存储在这个对象。
void* p:就是指针 对应的内容就是api中的lightuserdata,这个可以从lua_pushlightuserdata
和lua_touserdata中得到证明。由此,可以知道lightuserdata和number, integer有相同的行为
index2adr函数:将栈索引转换成StkId,伪索引(GLOBAL Index, REGISTRY Index等)
也是在这里处理。几乎所有API都需要调用这个。
StkId:字面意思为栈ID,实际为TValue*,使用地址作为一标识。可以想象,
lua_State中的栈是一个TValue数组。
阅读lapi.c的时间也不需要太长,不需要太深入,遇到与虚拟机有关的函数,直接step over就可以了。
--------------------------------------------------------------------------------------------------------
Lua 源代码阅读(2)
作者:future0906 发布时间:May 16, 2011 分类:
这次的分析主要是lobject.h文件和lstate.h文件。
lobject.h充满了关于lua 对象如何在C中实现的线索。如果你对这十分好奇,可以详细读一下。
lstate.h 中重要结构体有三个lua_State,global_State和GCObject。
Lua中用到大量C的联合体和结构体组合来巧妙的组织代码。
GCheader 结构体 是所有GC可以收集的对象的基础属性。这里类似于C++中的多态(所有的Lua对象在C里面都是用这个结构体存储)。
GCheader {
CommonHeader;
}
GCObject结构体,TString, Udata, Closure, Table, Proto, UpVal, lua_State,都包含一个CommonHeader
union GCObject {
GCheader gch;
union TString ts;
union Udata u;
union Closure cl;
struct Table h;
struct Proto p;
struct UpVal uv;
struct lua_State th; /* thread */
};
这是一个非常重要的技巧,可以用C来实现多态。
另外,可以留意到的是,上述GCObject包含的类型在加上基本类型(NIL, BOOLEAN, LIGHTUSERDATA, NUMBER)Lua中的所有类型。
TString:从字面上就是字符串
Udata: 用户数据
Closure: 闭包数据(如何实现?)
Table: 表格
UpVal: 函数外局部变量
lua_State: lua状态对象
所有的对象值都保存为TValue结构体
typedef struct lua_TValue {
TValuefields;
} TValue;
#define TValuefields Value value; int tt
根据tt的值不同value会有不同的含义,这个算多态的表现
这就是LUA基本类型的编号,与 CommonHeader中的tt对应
#define LUA_TNONE (-1)
#define LUA_TNIL 0
#define LUA_TBOOLEAN 1
#define LUA_TLIGHTUSERDATA 2
#define LUA_TNUMBER 3
#define LUA_TSTRING 4
#define LUA_TTABLE 5
#define LUA_TFUNCTION 6
#define LUA_TUSERDATA 7
#define LUA_TTHREAD 8
每个Lua虚拟机实例都包含一个global_state和一个lua_State
global_state对于C API来说是不可见,每个lua_State都有一个指向global_state的指针
global_state包含gc信息等。和global_state一起创建的lua_State称为MainThread。
lua_State中的stack由stack_init初始化(申请内存等),可以由luaE_newthread和f_luaopen调用,初始栈的大小为20*2 +5
lmathlib.c, lstrlib.c: get familiar with the external C API. Don't bother with the pattern matcher though. Just the easy functions.
lapi.c: Check how the API is implemented internally. Only skim this to get a feeling for the code. Cross-reference to lua.h and luaconf.h as needed.
lobject.h: tagged values and object representation. skim through this first. you'll want to keep a window with this file open all the time.
lstate.h: state objects. ditto.
lopcodes.h: bytecode instruction format and opcode definitions. easy.
lvm.c: scroll down to luaV_execute, the main interpreter loop. see how all of the instructions are implemented. skip the details for now. reread later.
ldo.c: calls, stacks, exceptions, coroutines. tough read.
lstring.c: string interning. cute, huh?
ltable.c: hash tables and arrays. tricky code.
ltm.c: metamethod handling, reread all of lvm.c now.
You may want to reread lapi.c now.
ldebug.c: surprise waiting for you. abstract interpretation is used to find object names for tracebacks. does bytecode verification, too.
lparser.c, lcode.c: recursive descent parser, targetting a register-based VM. start from chunk() and work your way through. read the expression parser and the code generator parts last.
lgc.c: incremental garbage collector. take your time.
Read all the other files as you see references to them. Don't let your stack
英文比较简单,我就不翻译了。
今天已经读了 lmathlib.c, lstrlib.c, lapi.c这三个文件。
对于lmathlib.c和lstrllib.c 这个两个文件主要是熟悉如何扩展Lua,一般使用过Lua的人都对这个会有所了解。只是简单的读一读、熟悉代码风格,不用花太多时间(我只看了20分钟)
lapi.c这里就开始有点难度了, 主要是你这里有一些平时没有接触过的东西。阅读这个文件可以结合手册中第三章C API来进行。
由于lapi.c涉及到很多的宏和结构,建议装一个vim + ctags或Visual studio 2010做cross references。
以下是一些关于lapi.c笔记:
TValue:由Value 和tt 组成, tt为类型标识,等于LUA_TNIL的呢个 Value是一个联合体,
根据tt的值不同使用不同的值,lua中所有的值、对象,都是由这个结构储存。以下是Value联合体的定义:
typedef union {
GCObject *gc;
void *p;
lua_Number n;
int b;
} Value;
GCObject:里面的内容比较复杂,从字面上理解是指就是gc会收集的对象,
可以理解为Lua中的基本对象。包括Thread,Table,UserData,String都是存储在这个对象。
void* p:就是指针 对应的内容就是api中的lightuserdata,这个可以从lua_pushlightuserdata
和lua_touserdata中得到证明。由此,可以知道lightuserdata和number, integer有相同的行为
index2adr函数:将栈索引转换成StkId,伪索引(GLOBAL Index, REGISTRY Index等)
也是在这里处理。几乎所有API都需要调用这个。
StkId:字面意思为栈ID,实际为TValue*,使用地址作为一标识。可以想象,
lua_State中的栈是一个TValue数组。
阅读lapi.c的时间也不需要太长,不需要太深入,遇到与虚拟机有关的函数,直接step over就可以了。
--------------------------------------------------------------------------------------------------------
Lua 源代码阅读(2)
作者:future0906 发布时间:May 16, 2011 分类:
这次的分析主要是lobject.h文件和lstate.h文件。
lobject.h充满了关于lua 对象如何在C中实现的线索。如果你对这十分好奇,可以详细读一下。
lstate.h 中重要结构体有三个lua_State,global_State和GCObject。
Lua中用到大量C的联合体和结构体组合来巧妙的组织代码。
GCheader 结构体 是所有GC可以收集的对象的基础属性。这里类似于C++中的多态(所有的Lua对象在C里面都是用这个结构体存储)。
GCheader {
CommonHeader;
}
GCObject结构体,TString, Udata, Closure, Table, Proto, UpVal, lua_State,都包含一个CommonHeader
union GCObject {
GCheader gch;
union TString ts;
union Udata u;
union Closure cl;
struct Table h;
struct Proto p;
struct UpVal uv;
struct lua_State th; /* thread */
};
这是一个非常重要的技巧,可以用C来实现多态。
另外,可以留意到的是,上述GCObject包含的类型在加上基本类型(NIL, BOOLEAN, LIGHTUSERDATA, NUMBER)Lua中的所有类型。
TString:从字面上就是字符串
Udata: 用户数据
Closure: 闭包数据(如何实现?)
Table: 表格
UpVal: 函数外局部变量
lua_State: lua状态对象
所有的对象值都保存为TValue结构体
typedef struct lua_TValue {
TValuefields;
} TValue;
#define TValuefields Value value; int tt
根据tt的值不同value会有不同的含义,这个算多态的表现
这就是LUA基本类型的编号,与 CommonHeader中的tt对应
#define LUA_TNONE (-1)
#define LUA_TNIL 0
#define LUA_TBOOLEAN 1
#define LUA_TLIGHTUSERDATA 2
#define LUA_TNUMBER 3
#define LUA_TSTRING 4
#define LUA_TTABLE 5
#define LUA_TFUNCTION 6
#define LUA_TUSERDATA 7
#define LUA_TTHREAD 8
每个Lua虚拟机实例都包含一个global_state和一个lua_State
global_state对于C API来说是不可见,每个lua_State都有一个指向global_state的指针
global_state包含gc信息等。和global_state一起创建的lua_State称为MainThread。
lua_State中的stack由stack_init初始化(申请内存等),可以由luaE_newthread和f_luaopen调用,初始栈的大小为20*2 +5
相关文章推荐
- 关于LUA源代码分析学习教程
- BT源代码学习心得(十三):客户端源代码分析(对等客户的连接建立及其握手协议)
- 韩顺平_轻松搞定网页设计(html+css+javascript)_第26讲_js函数调用过程内存分析_js函数细节_学习笔记_源代码图解_PPT文档整理
- java 静态块和构造块等执行顺序分析学习
- 【学习笔记】Cocos2d-x Lua脚本引擎之HelloLua分析笔记
- java 静态块和构造块等执行顺序分析学习
- BT源代码学习心得(八):跟踪服务器(Tracker)的代码分析(用户请求的实际处理) 转自CSDN:gushenghua的专栏
- 学习Android闹钟源代码(二)-桌面widget类分析
- BT源代码学习心得(十四):客户端源代码分析(对等客户连接中的阻塞管理)
- BT源代码学习心得(十二):客户端源代码分析(从开始到连接建立阶段)
- Proxy源代码分析--谈谈如何学习linux网络编程
- BT源代码学习心得(十五):客户端源代码分析(下载过程中的块选取策略)
- 从源代码上分析ListView的addHeaderView和setAdapter的调用顺序
- 伸展树的学习(二):源代码分析
- BT源代码学习心得(六):跟踪服务器(Tracker)的代码分析(初始化)
- BT源代码学习心得(十一):客户端源代码分析(存储管理) 转自CSDN:gushenghua的专栏
- Swoole源代码学习记录(十五)——Timer模块分析
- 韩顺平_轻松搞定网页设计(html+css+javascript)_第30讲_类和对象细节_创建对象的几种方式_js对象内存分析_学习笔记_源代码图解_PPT文档整理
- Cocos2d-x学习笔记(20)(TestCpp源代码分析-4)
- [lua源码学习笔记]词法语法分析