我们知道,lua是动态语言,也就是说lua中的变量没有类型,只有lua中的值才有类型,lua中的值带有自己的类型
首先,我们先看下Lua中最基本数据结构TValuefields:
/*
** Tagged Values. This is the basic representation of values in Lua,
** an actual value plus a tag with its type.
*/
#define TValuefields Value value_; int tt_
这里的int tt_就是这个value值的类型,value中包含所有种类型的值:
union Value {
GCObject *gc; /* collectable objects */
void *p; /* light userdata */
int b; /* booleans */
lua_CFunction f; /* light C functions */
numfield /* numbers */
};
我们可以看到,int b是布尔类型的, numfiled是表示数字类型, p是light userdata类型
所以当这些值赋值的时候,会修改TValuefields中的类型tt,以及给相应的类型赋值。
那gc是做什么用的呢? gc主要保存的是lua需要自己回收的一些类型,也就是说,Value中的其他几种变量是不用lua回收机制的。<