lobject.h 15 KB

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