lua5 3資料結構

2021-07-25 11:18:36 字數 2932 閱讀 2958

由於lua版本迭代之間,資料結構和函式都會部分變化

以下是我分析的lua.h中的資訊,以便確定版本

#define lua_version_major	"5"

#define lua_version_minor "3"

#define lua_version_num 503

#define lua_version_release "3"

#define commonheader	gcobject *next; lu_byte tt; lu_byte marked

/*** union of all collectable objects (only for conversions)

*/union gcunion ;

##主要位於lobject.h中,部分位於lstate.h

以下和機器有關,比如在32位(64位)機器

#define lua_int32	int

#define luai_umem size_t

#define luai_mem ptrdiff_t

以下通用定義

#define lua_number_double

#define lua_number double

typedef unsigned char lu_byte;

typedef tvalue *stkid; /* index to stack elements */

typedef struct lua_tvalue tvalue;

typedef int (*lua_cfunction) (lua_state *l);

typedef unsigned lua_int32 lu_int32;

typedef luai_umem lu_mem;

typedef luai_mem l_mem;

typedef lu_int32 instruction;

lua_tvalue 位於lobject.h

//儲存值和型別

#define tvaluefields value value_; int tt_

struct lua_tvalue ;

/*

** union of all lua values

*/typedef union value value;

#define commonheader   gcobject *next; lu_byte tt; lu_byte marked

struct gcobject ;

/*

** header for userdata; memory area follows the end of this structure

** (aligned according to 'uudata'; see next).

*/typedef struct udata udata;

/*** ensures that address after this type is always fully aligned.

*/typedef union uudata uudata;

string

/*

** header for string value; string bytes follow the end of this structure

** (aligned according to 'utstring'; see next).

*/typedef struct tstring u;

} tstring;

/*** ensures that address after this type is always fully aligned.

*/typedef union utstring utstring;

//閉包相關

#define closureheader commonheader; lu_byte nupvalues; gcobject *gclist

typedef struct cclosure cclosure;

typedef struct lclosure lclosure;

typedef union closure closure;

//表相關

typedef union tkey nk;

tvalue tvk;

} tkey;

typedef struct node node;

typedef struct table table;

/*

** description of an upvalue for function prototypes

*/typedef struct upvaldesc upvaldesc;

/*** description of a local variable for function prototypes

** (used for debug information)

*/typedef struct locvar locvar;

/*** function prototypes

*/typedef struct proto proto;

//上值

typedef struct upval l;

} u;

} upval;

lua5 3異常機制

lua本質上利用c函式來操作lua虛擬機器。lua虛擬機器對於c來說只是在堆上的記憶體物件。lua有自己的執行物件 協程 每個協程有自己的呼叫棧。比如下面的函式 function add x,y return x y end如果x或y不能進行加法操作,在呼叫中就會產生異常。產生了異常,虛擬機器需要對...

Lua5 3遇到的坑

attempt to call a nil value global unpack 在lua 5.2和5.3的版本中,全域性的unpack函式已經被移除了,改為table.unpack,所以如果用到的第三方庫或者源 使用了unpack方法,可以在 前面加上 local unpack unpack o...

lua 5 3開發除錯環境搭建

目錄 5.1 以後就沒有很好的lua ide可用了,luastudio不錯但是小貴,發現有位仁兄寫了個 visual studio code的lua除錯外掛程式,試了一下還不錯 外掛程式原始碼 安裝lua 5.3 debug 外掛程式,在vscode中按ctrl p,輸入 ext install l...