/*
** {==================================================================
@@ LUA_NUMBER is the type of numbers in Lua.
** CHANGE the following definitions only if you want to build Lua
** with a number type different from double. You may also need to
** change lua_number2int & lua_number2integer.
** ===================================================================
*/#define LUA_NUMBER_DOUBLE
#define LUA_NUMBER double
Code Snippet 7:
luaconf.h
98
99
/* type of numbers in Lua */typedef LUA_NUMBER lua_Number;
Code Snippet 8:
lua.h
类似的,用 tt 记录类型,用 value 中 lua_Number n 来记录 number 数值。
/*
** Union of all collectable objects
*/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 */
};
Code Snippet 10:
lstate.h
如果仔细观察内部内存的安排,会发现这种方式是非常巧妙的。
gch h p uv th 都是 struct,头部的字段都是 CommonHeader。
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
** Common Header for all collectable objects (in macro form, to be
** included in other objects)
*/#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
/*
** Common header in struct form
*/typedefstruct GCheader {
CommonHeader;
} GCheader;
Code Snippet 11:
lobject.h
338
339
340
341
342
343
344
345
346
347
348
typedefstruct Table {
CommonHeader;
lu_byte flags; /* 1<<p means tagmethod(p) is not present */
lu_byte lsizenode; /* log2 of size of `node' array */struct Table *metatable;
TValue *array; /* array part */
Node *node;
Node *lastfree; /* any free position is before this position */
GCObject *gclist;
int sizearray; /* size of `array' array */
} Table;
/*
** Function Prototypes
*/typedefstruct Proto {
CommonHeader;
TValue *k; /* constants used by the function */
Instruction *code;
struct Proto **p; /* functions defined inside the function */int *lineinfo; /* map from opcodes to source lines */struct LocVar *locvars; /* information about local variables */
TString **upvalues; /* upvalue names */
TString *source;
int sizeupvalues;
int sizek; /* size of `k' */int sizecode;
int sizelineinfo;
int sizep; /* size of `p' */int sizelocvars;
int linedefined;
int lastlinedefined;
GCObject *gclist;
lu_byte nups; /* number of upvalues */
lu_byte numparams;
lu_byte is_vararg;
lu_byte maxstacksize;
} Proto;
/*
** `per thread' state
*/struct lua_State {
CommonHeader;
lu_byte status;
StkId top; /* first free slot in the stack */
StkId base; /* base of current function */
global_State *l_G;
CallInfo *ci; /* call info for current function */const Instruction *savedpc; /* `savedpc' of current function */
StkId stack_last; /* last free slot in the stack */
StkId stack; /* stack base */
CallInfo *end_ci; /* points after end of ci array*/
CallInfo *base_ci; /* array of CallInfo's */int stacksize;
int size_ci; /* size of array `base_ci' */unsignedshort nCcalls; /* number of nested C calls */unsignedshort baseCcalls; /* nested C calls when resuming coroutine */
lu_byte hookmask;
lu_byte allowhook;
int basehookcount;
int hookcount;
lua_Hook hook;
TValue l_gt; /* table of globals */
TValue env; /* temporary place for environments */
GCObject *openupval; /* list of open upvalues in this stack */
GCObject *gclist;
struct lua_longjmp *errorJmp; /* current error recover point */
ptrdiff_t errfunc; /* current error handling function (stack index) */
};