[Lua源码阅读] 理解Lua的object
2012-08-15 10:24
453 查看
Lua的object定义在lobject.h文件中。Lua中总有有9种数据类型:nil, boolean, lightuserdata, number, string, table, userdata, thread, function,用数字0-8来表示:
除了以上9中基本数据类型之外(他们是对外的基本类型),Lua还定义了一些类型的变种:
TValue的内部结构图如下:
int tt_是lua对象的具体类型,value_才是具体存储的值,对于一般的类型,可以直接表示,而对于一些特殊的类型,譬如user data/thread/string,得用GCObject union来表示。
#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
除了以上9中基本数据类型之外(他们是对外的基本类型),Lua还定义了一些类型的变种:
/* ** tags for Tagged Values have the following use of bits: ** bits 0-3: actual tag (a LUA_T* value) ** bits 4-5: variant bits ** bit 6: whether value is collectable */ #define VARBITS (3 << 4) /* ** LUA_TFUNCTION variants: ** 0 - Lua function ** 1 - light C function ** 2 - regular C function (closure) */ /* Variant tags for functions */ #define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */ #define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */ #define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */ /* ** LUA_TSTRING variants */ #define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */ #define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */
TValue的内部结构图如下:
int tt_是lua对象的具体类型,value_才是具体存储的值,对于一般的类型,可以直接表示,而对于一些特殊的类型,譬如user data/thread/string,得用GCObject union来表示。
相关文章推荐
- kubernetes源码之watch包until.go阅读理解四
- [SDS阅读理解/5]源码中的函数/2
- JDK部分源码阅读与理解
- Python: matplotlib模块 源码阅读&理解
- lua源码阅读(4)---表
- skynet 源码阅读笔记 bootstrap.lua
- FutureTask源码阅读与理解
- [SDS阅读理解/7]源码中的函数/4
- 阅读Lua源码
- Spark源码阅读笔记之BlockObjectWriter
- jdk之object源码理解
- 源码阅读笔记:java.lang.Object
- lua源码阅读(3)----字符串
- Lua源码阅读顺序推荐
- Lua源码建议阅读顺序
- [SDS阅读理解/9]源码中的函数/6
- 阅读ISE源码过程中重新对单例模式的理解
- 深入理解Lua的全局变量_G以及源码实现
- lua源码阅读(5)-lua_State
- Lua源码阅读四——lua虚拟机指令系统