数据结构
lua的内存结构最主要有三大块,lua_State、 CallInfo、 lua_TValue。
- lua_State里面的 stack (栈)是主要的内存结构,类型是 lua_TValue;
- lua_TValue 主要是Value,是一个 uion,存的内容根据 lua_TValue.tt_ 标记;
- CallInfo 用于记录函数调用信息:作用的栈区间,返回数量,调用链表。
1 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 31 32 33 34 35 36 37 38 39 40 41
| typedef union lua_Value { void * p; int b; lua_Integer i; lua_Number n; lua_CFunction f; } Value;
typedef struct lua_TValue { Value value_; int tt_; } TValue;
typedef TValue* StkId;
struct CallInfo { StkId func; StkId top; int nresult; int callstatus; struct CallInfo* next; struct CallInfo* previous; };
typedef struct lua_State { StkId stack; StkId stack_last; StkId top; int stack_size; struct lua_longjmp * errorjmp; int status; struct lua_State * next; struct lua_State * previous; struct CallInfo base_ci; struct CallInfo* ci; struct global_State* l_G; ptrdiff_t errorfunc; int ncalls; } lua_State;
|
主要操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| struct lua_State* lua_newstate(lua_Alloc alloc, void* ud); void lua_close(struct lua_State* L);
void increase_top(struct lua_State* L); void lua_pushinteger(struct lua_State* L, int integer);
void lua_settop(struct lua_State* L, int idx); int lua_gettop(struct lua_State* L); void lua_pop(struct lua_State* L);
lua_Integer lua_tointegerx(struct lua_State* L, int idx, int* isnum);
static struct CallInfo* next_ci(struct lua_State* L, StkId func, int nresult);
|