您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. ** $Id: lobject.h,v 2.70 2012/05/11 14:10:50 roberto Exp $
  3. ** Type definitions for Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #ifndef lobject_h
  7. #define lobject_h
  8. #include <stdarg.h>
  9. #include "llimits.h"
  10. #include "lua.h"
  11. /*
  12. ** Extra tags for non-values
  13. */
  14. #define LUA_TPROTO LUA_NUMTAGS
  15. #define LUA_TUPVAL (LUA_NUMTAGS+1)
  16. #define LUA_TDEADKEY (LUA_NUMTAGS+2)
  17. /*
  18. ** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
  19. */
  20. #define LUA_TOTALTAGS (LUA_TUPVAL+2)
  21. /*
  22. ** tags for Tagged Values have the following use of bits:
  23. ** bits 0-3: actual tag (a LUA_T* value)
  24. ** bits 4-5: variant bits
  25. ** bit 6: whether value is collectable
  26. */
  27. #define VARBITS (3 << 4)
  28. /*
  29. ** LUA_TFUNCTION variants:
  30. ** 0 - Lua function
  31. ** 1 - light C function
  32. ** 2 - regular C function (closure)
  33. */
  34. /* Variant tags for functions */
  35. #define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */
  36. #define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */
  37. #define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */
  38. /*
  39. ** LUA_TSTRING variants */
  40. #define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */
  41. #define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */
  42. /* Bit mark for collectable types */
  43. #define BIT_ISCOLLECTABLE (1 << 6)
  44. /* mark a tag as collectable */
  45. #define ctb(t) ((t) | BIT_ISCOLLECTABLE)
  46. /*
  47. ** Union of all collectable objects
  48. */
  49. typedef union GCObject GCObject;
  50. /*
  51. ** Common Header for all collectable objects (in macro form, to be
  52. ** included in other objects)
  53. */
  54. #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
  55. /*
  56. ** Common header in struct form
  57. */
  58. typedef struct GCheader {
  59. CommonHeader;
  60. } GCheader;
  61. /*
  62. ** Union of all Lua values
  63. */
  64. typedef union Value Value;
  65. #define numfield lua_Number n; /* numbers */
  66. /*
  67. ** Tagged Values. This is the basic representation of values in Lua,
  68. ** an actual value plus a tag with its type.
  69. */
  70. #define TValuefields Value value_; int tt_
  71. typedef struct lua_TValue TValue;
  72. /* macro defining a nil value */
  73. #define NILCONSTANT {NULL}, LUA_TNIL
  74. #define val_(o) ((o)->value_)
  75. #define num_(o) (val_(o).n)
  76. /* raw type tag of a TValue */
  77. #define rttype(o) ((o)->tt_)
  78. /* tag with no variants (bits 0-3) */
  79. #define novariant(x) ((x) & 0x0F)
  80. /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
  81. #define ttype(o) (rttype(o) & 0x3F)
  82. /* type tag of a TValue with no variants (bits 0-3) */
  83. #define ttypenv(o) (novariant(rttype(o)))
  84. /* Macros to test type */
  85. #define checktag(o,t) (rttype(o) == (t))
  86. #define checktype(o,t) (ttypenv(o) == (t))
  87. #define ttisnumber(o) checktag((o), LUA_TNUMBER)
  88. #define ttisnil(o) checktag((o), LUA_TNIL)
  89. #define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
  90. #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
  91. #define ttisstring(o) checktype((o), LUA_TSTRING)
  92. #define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR))
  93. #define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR))
  94. #define ttistable(o) checktag((o), ctb(LUA_TTABLE))
  95. #define ttisfunction(o) checktype(o, LUA_TFUNCTION)
  96. #define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION)
  97. #define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
  98. #define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
  99. #define ttislcf(o) checktag((o), LUA_TLCF)
  100. #define ttisuserdata(o) checktag((o), ctb(LUA_TUSERDATA))
  101. #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
  102. #define ttisdeadkey(o) checktag((o), LUA_TDEADKEY)
  103. #define ttisequal(o1,o2) (rttype(o1) == rttype(o2))
  104. /* Macros to access values */
  105. #define nvalue(o) check_exp(ttisnumber(o), num_(o))
  106. #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
  107. #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
  108. #define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts)
  109. #define tsvalue(o) (&rawtsvalue(o)->tsv)
  110. #define rawuvalue(o) check_exp(ttisuserdata(o), &val_(o).gc->u)
  111. #define uvalue(o) (&rawuvalue(o)->uv)
  112. #define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl)
  113. #define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l)
  114. #define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c)
  115. #define fvalue(o) check_exp(ttislcf(o), val_(o).f)
  116. #define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h)
  117. #define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
  118. #define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th)
  119. /* a dead value may get the 'gc' field, but cannot access its contents */
  120. #define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
  121. #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
  122. #define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE)
  123. /* Macros for internal tests */
  124. #define righttt(obj) (ttype(obj) == gcvalue(obj)->gch.tt)
  125. #define checkliveness(g,obj) \
  126. lua_longassert(!iscollectable(obj) || \
  127. (righttt(obj) && !isdead(g,gcvalue(obj))))
  128. /* Macros to set values */
  129. #define settt_(o,t) ((o)->tt_=(t))
  130. #define setnvalue(obj,x) \
  131. { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); }
  132. #define changenvalue(o,x) check_exp(ttisnumber(o), num_(o)=(x))
  133. #define setnilvalue(obj) settt_(obj, LUA_TNIL)
  134. #define setfvalue(obj,x) \
  135. { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
  136. #define setpvalue(obj,x) \
  137. { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
  138. #define setbvalue(obj,x) \
  139. { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
  140. #define setgcovalue(L,obj,x) \
  141. { TValue *io=(obj); GCObject *i_g=(x); \
  142. val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); }
  143. #define setsvalue(L,obj,x) \
  144. { TValue *io=(obj); \
  145. TString *x_ = (x); \
  146. val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \
  147. checkliveness(G(L),io); }
  148. #define setuvalue(L,obj,x) \
  149. { TValue *io=(obj); \
  150. val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \
  151. checkliveness(G(L),io); }
  152. #define setthvalue(L,obj,x) \
  153. { TValue *io=(obj); \
  154. val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \
  155. checkliveness(G(L),io); }
  156. #define setclLvalue(L,obj,x) \
  157. { TValue *io=(obj); \
  158. val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \
  159. checkliveness(G(L),io); }
  160. #define setclCvalue(L,obj,x) \
  161. { TValue *io=(obj); \
  162. val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \
  163. checkliveness(G(L),io); }
  164. #define sethvalue(L,obj,x) \
  165. { TValue *io=(obj); \
  166. val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \
  167. checkliveness(G(L),io); }
  168. #define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
  169. #define setobj(L,obj1,obj2) \
  170. { const TValue *io2=(obj2); TValue *io1=(obj1); \
  171. io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
  172. checkliveness(G(L),io1); }
  173. /*
  174. ** different types of assignments, according to destination
  175. */
  176. /* from stack to (same) stack */
  177. #define setobjs2s setobj
  178. /* to stack (not from same stack) */
  179. #define setobj2s setobj
  180. #define setsvalue2s setsvalue
  181. #define sethvalue2s sethvalue
  182. #define setptvalue2s setptvalue
  183. /* from table to same table */
  184. #define setobjt2t setobj
  185. /* to table */
  186. #define setobj2t setobj
  187. /* to new object */
  188. #define setobj2n setobj
  189. #define setsvalue2n setsvalue
  190. /* check whether a number is valid (useful only for NaN trick) */
  191. #define luai_checknum(L,o,c) { /* empty */ }
  192. /*
  193. ** {======================================================
  194. ** NaN Trick
  195. ** =======================================================
  196. */
  197. #if defined(LUA_NANTRICK)
  198. /*
  199. ** numbers are represented in the 'd_' field. All other values have the
  200. ** value (NNMARK | tag) in 'tt__'. A number with such pattern would be
  201. ** a "signaled NaN", which is never generated by regular operations by
  202. ** the CPU (nor by 'strtod')
  203. */
  204. /* allows for external implementation for part of the trick */
  205. #if !defined(NNMARK) /* { */
  206. #if !defined(LUA_IEEEENDIAN)
  207. #error option 'LUA_NANTRICK' needs 'LUA_IEEEENDIAN'
  208. #endif
  209. #define NNMARK 0x7FF7A500
  210. #define NNMASK 0x7FFFFF00
  211. #undef TValuefields
  212. #undef NILCONSTANT
  213. #if (LUA_IEEEENDIAN == 0) /* { */
  214. /* little endian */
  215. #define TValuefields \
  216. union { struct { Value v__; int tt__; } i; double d__; } u
  217. #define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}}
  218. /* field-access macros */
  219. #define v_(o) ((o)->u.i.v__)
  220. #define d_(o) ((o)->u.d__)
  221. #define tt_(o) ((o)->u.i.tt__)
  222. #else /* }{ */
  223. /* big endian */
  224. #define TValuefields \
  225. union { struct { int tt__; Value v__; } i; double d__; } u
  226. #define NILCONSTANT {{tag2tt(LUA_TNIL), {NULL}}}
  227. /* field-access macros */
  228. #define v_(o) ((o)->u.i.v__)
  229. #define d_(o) ((o)->u.d__)
  230. #define tt_(o) ((o)->u.i.tt__)
  231. #endif /* } */
  232. #endif /* } */
  233. /* correspondence with standard representation */
  234. #undef val_
  235. #define val_(o) v_(o)
  236. #undef num_
  237. #define num_(o) d_(o)
  238. #undef numfield
  239. #define numfield /* no such field; numbers are the entire struct */
  240. /* basic check to distinguish numbers from non-numbers */
  241. #undef ttisnumber
  242. #define ttisnumber(o) ((tt_(o) & NNMASK) != NNMARK)
  243. #define tag2tt(t) (NNMARK | (t))
  244. #undef rttype
  245. #define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff)
  246. #undef settt_
  247. #define settt_(o,t) (tt_(o) = tag2tt(t))
  248. #undef setnvalue
  249. #define setnvalue(obj,x) \
  250. { TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); }
  251. #undef setobj
  252. #define setobj(L,obj1,obj2) \
  253. { const TValue *o2_=(obj2); TValue *o1_=(obj1); \
  254. o1_->u = o2_->u; \
  255. checkliveness(G(L),o1_); }
  256. /*
  257. ** these redefinitions are not mandatory, but these forms are more efficient
  258. */
  259. #undef checktag
  260. #undef checktype
  261. #define checktag(o,t) (tt_(o) == tag2tt(t))
  262. #define checktype(o,t) (ctb(tt_(o) | VARBITS) == ctb(tag2tt(t) | VARBITS))
  263. #undef ttisequal
  264. #define ttisequal(o1,o2) \
  265. (ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2)))
  266. #undef luai_checknum
  267. #define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; }
  268. #endif
  269. /* }====================================================== */
  270. /*
  271. ** {======================================================
  272. ** types and prototypes
  273. ** =======================================================
  274. */
  275. union Value {
  276. GCObject *gc; /* collectable objects */
  277. void *p; /* light userdata */
  278. int b; /* booleans */
  279. lua_CFunction f; /* light C functions */
  280. numfield /* numbers */
  281. };
  282. struct lua_TValue {
  283. TValuefields;
  284. };
  285. typedef TValue *StkId; /* index to stack elements */
  286. /*
  287. ** Header for string value; string bytes follow the end of this structure
  288. */
  289. typedef union TString {
  290. L_Umaxalign dummy; /* ensures maximum alignment for strings */
  291. struct {
  292. CommonHeader;
  293. lu_byte extra; /* reserved words for short strings; "has hash" for longs */
  294. unsigned int hash;
  295. size_t len; /* number of characters in string */
  296. } tsv;
  297. } TString;
  298. /* get the actual string (array of bytes) from a TString */
  299. #define getstr(ts) cast(const char *, (ts) + 1)
  300. /* get the actual string (array of bytes) from a Lua value */
  301. #define svalue(o) getstr(rawtsvalue(o))
  302. /*
  303. ** Header for userdata; memory area follows the end of this structure
  304. */
  305. typedef union Udata {
  306. L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
  307. struct {
  308. CommonHeader;
  309. struct Table *metatable;
  310. struct Table *env;
  311. size_t len; /* number of bytes */
  312. } uv;
  313. } Udata;
  314. /*
  315. ** Description of an upvalue for function prototypes
  316. */
  317. typedef struct Upvaldesc {
  318. TString *name; /* upvalue name (for debug information) */
  319. lu_byte instack; /* whether it is in stack */
  320. lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
  321. } Upvaldesc;
  322. /*
  323. ** Description of a local variable for function prototypes
  324. ** (used for debug information)
  325. */
  326. typedef struct LocVar {
  327. TString *varname;
  328. int startpc; /* first point where variable is active */
  329. int endpc; /* first point where variable is dead */
  330. } LocVar;
  331. /*
  332. ** Function Prototypes
  333. */
  334. typedef struct Proto {
  335. CommonHeader;
  336. TValue *k; /* constants used by the function */
  337. Instruction *code;
  338. struct Proto **p; /* functions defined inside the function */
  339. int *lineinfo; /* map from opcodes to source lines (debug information) */
  340. LocVar *locvars; /* information about local variables (debug information) */
  341. Upvaldesc *upvalues; /* upvalue information */
  342. union Closure *cache; /* last created closure with this prototype */
  343. TString *source; /* used for debug information */
  344. int sizeupvalues; /* size of 'upvalues' */
  345. int sizek; /* size of `k' */
  346. int sizecode;
  347. int sizelineinfo;
  348. int sizep; /* size of `p' */
  349. int sizelocvars;
  350. int linedefined;
  351. int lastlinedefined;
  352. GCObject *gclist;
  353. lu_byte numparams; /* number of fixed parameters */
  354. lu_byte is_vararg;
  355. lu_byte maxstacksize; /* maximum stack used by this function */
  356. } Proto;
  357. /*
  358. ** Lua Upvalues
  359. */
  360. typedef struct UpVal {
  361. CommonHeader;
  362. TValue *v; /* points to stack or to its own value */
  363. union {
  364. TValue value; /* the value (when closed) */
  365. struct { /* double linked list (when open) */
  366. struct UpVal *prev;
  367. struct UpVal *next;
  368. } l;
  369. } u;
  370. } UpVal;
  371. /*
  372. ** Closures
  373. */
  374. #define ClosureHeader \
  375. CommonHeader; lu_byte nupvalues; GCObject *gclist
  376. typedef struct CClosure {
  377. ClosureHeader;
  378. lua_CFunction f;
  379. TValue upvalue[1]; /* list of upvalues */
  380. } CClosure;
  381. typedef struct LClosure {
  382. ClosureHeader;
  383. struct Proto *p;
  384. UpVal *upvals[1]; /* list of upvalues */
  385. } LClosure;
  386. typedef union Closure {
  387. CClosure c;
  388. LClosure l;
  389. } Closure;
  390. #define isLfunction(o) ttisLclosure(o)
  391. #define getproto(o) (clLvalue(o)->p)
  392. /*
  393. ** Tables
  394. */
  395. typedef union TKey {
  396. struct {
  397. TValuefields;
  398. struct Node *next; /* for chaining */
  399. } nk;
  400. TValue tvk;
  401. } TKey;
  402. typedef struct Node {
  403. TValue i_val;
  404. TKey i_key;
  405. } Node;
  406. typedef struct Table {
  407. CommonHeader;
  408. lu_byte flags; /* 1<<p means tagmethod(p) is not present */
  409. lu_byte lsizenode; /* log2 of size of `node' array */
  410. struct Table *metatable;
  411. TValue *array; /* array part */
  412. Node *node;
  413. Node *lastfree; /* any free position is before this position */
  414. GCObject *gclist;
  415. int sizearray; /* size of `array' array */
  416. } Table;
  417. /*
  418. ** `module' operation for hashing (size is always a power of 2)
  419. */
  420. #define lmod(s,size) \
  421. (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
  422. #define twoto(x) (1<<(x))
  423. #define sizenode(t) (twoto((t)->lsizenode))
  424. /*
  425. ** (address of) a fixed nil value
  426. */
  427. #define luaO_nilobject (&luaO_nilobject_)
  428. LUAI_DDEC const TValue luaO_nilobject_;
  429. LUAI_FUNC int luaO_int2fb (unsigned int x);
  430. LUAI_FUNC int luaO_fb2int (int x);
  431. LUAI_FUNC int luaO_ceillog2 (unsigned int x);
  432. LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
  433. LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
  434. LUAI_FUNC int luaO_hexavalue (int c);
  435. LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
  436. va_list argp);
  437. LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  438. LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
  439. #endif