lobject.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. ** $Id: lobject.h $
  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_TUPVAL LUA_NUMTAGS /* upvalues */
  15. #define LUA_TPROTO (LUA_NUMTAGS+1) /* function prototypes */
  16. /*
  17. ** number of all possible tags (including LUA_TNONE)
  18. */
  19. #define LUA_TOTALTAGS (LUA_TPROTO + 2)
  20. /*
  21. ** tags for Tagged Values have the following use of bits:
  22. ** bits 0-3: actual tag (a LUA_T* value)
  23. ** bits 4-5: variant bits
  24. ** bit 6: whether value is collectable
  25. */
  26. /*
  27. ** Union of all Lua values
  28. */
  29. typedef union Value {
  30. struct GCObject *gc; /* collectable objects */
  31. void *p; /* light userdata */
  32. int b; /* booleans */
  33. lua_CFunction f; /* light C functions */
  34. lua_Integer i; /* integer numbers */
  35. lua_Number n; /* float numbers */
  36. } Value;
  37. /*
  38. ** Tagged Values. This is the basic representation of values in Lua:
  39. ** an actual value plus a tag with its type.
  40. */
  41. #define TValuefields Value value_; lu_byte tt_
  42. typedef struct TValue {
  43. TValuefields;
  44. } TValue;
  45. #define val_(o) ((o)->value_)
  46. #define valraw(o) (&val_(o))
  47. /* raw type tag of a TValue */
  48. #define rawtt(o) ((o)->tt_)
  49. /* tag with no variants (bits 0-3) */
  50. #define novariant(t) ((t) & 0x0F)
  51. /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
  52. #define withvariant(t) ((t) & 0x3F)
  53. #define ttypetag(o) withvariant(rawtt(o))
  54. /* type of a TValue */
  55. #define ttype(o) (novariant(rawtt(o)))
  56. /* Macros to test type */
  57. #define checktag(o,t) (rawtt(o) == (t))
  58. #define checktype(o,t) (ttype(o) == (t))
  59. /* Macros for internal tests */
  60. #define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt)
  61. #define checkliveness(L,obj) \
  62. lua_longassert(!iscollectable(obj) || \
  63. (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))
  64. /* Macros to set values */
  65. #define settt_(o,t) ((o)->tt_=(t))
  66. #define setobj(L,obj1,obj2) \
  67. { TValue *io1=(obj1); const TValue *io2=(obj2); \
  68. io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
  69. (void)L; checkliveness(L,io1); lua_assert(!isreallyempty(io1)); }
  70. /*
  71. ** different types of assignments, according to destination
  72. */
  73. /* from stack to stack */
  74. #define setobjs2s(L,o1,o2) setobj(L,s2v(o1),s2v(o2))
  75. /* to stack (not from same stack) */
  76. #define setobj2s(L,o1,o2) setobj(L,s2v(o1),o2)
  77. /* from table to same table */
  78. #define setobjt2t setobj
  79. /* to new object */
  80. #define setobj2n setobj
  81. /* to table */
  82. #define setobj2t setobj
  83. typedef union StackValue {
  84. TValue val;
  85. } StackValue;
  86. typedef StackValue *StkId; /* index to stack elements */
  87. /* convert a 'StackValue' to a 'TValue' */
  88. #define s2v(o) (&(o)->val)
  89. /*
  90. ** {==================================================================
  91. ** Nil
  92. ** ===================================================================
  93. */
  94. /* macro to test for (any kind of) nil */
  95. #define ttisnil(v) checktype((v), LUA_TNIL)
  96. /* macro to test for a "pure" nil */
  97. #define ttisstrictnil(o) checktag((o), LUA_TNIL)
  98. #define setnilvalue(obj) settt_(obj, LUA_TNIL)
  99. /*
  100. ** Variant tag, used only in tables to signal an empty slot
  101. ** (which might be different from a slot containing nil)
  102. */
  103. #define LUA_TEMPTY (LUA_TNIL | (1 << 4))
  104. /*
  105. ** Variant used only in the value returned for a key not found in a
  106. ** table (absent key).
  107. */
  108. #define LUA_TABSTKEY (LUA_TNIL | (2 << 4))
  109. #define isabstkey(v) checktag((v), LUA_TABSTKEY)
  110. /*
  111. ** macro to detect non-standard nils (used only in assertions)
  112. */
  113. #define isreallyempty(v) (ttisnil(v) && !ttisstrictnil(v))
  114. /*
  115. ** By default, entries with any kind of nil are considered empty.
  116. ** (In any definition, values associated with absent keys must also
  117. ** be accepted as empty.)
  118. */
  119. #define isempty(v) ttisnil(v)
  120. /* macro defining a value corresponding to an absent key */
  121. #define ABSTKEYCONSTANT {NULL}, LUA_TABSTKEY
  122. /* mark an entry as empty */
  123. #define setempty(v) settt_(v, LUA_TEMPTY)
  124. /* }================================================================== */
  125. /*
  126. ** {==================================================================
  127. ** Booleans
  128. ** ===================================================================
  129. */
  130. #define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
  131. #define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
  132. #define bvalueraw(v) ((v).b)
  133. #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
  134. #define setbvalue(obj,x) \
  135. { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
  136. /* }================================================================== */
  137. /*
  138. ** {==================================================================
  139. ** Threads
  140. ** ===================================================================
  141. */
  142. #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
  143. #define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc))
  144. #define setthvalue(L,obj,x) \
  145. { TValue *io = (obj); lua_State *x_ = (x); \
  146. val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \
  147. checkliveness(L,io); }
  148. #define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t)
  149. /* }================================================================== */
  150. /*
  151. ** {==================================================================
  152. ** Collectable Objects
  153. ** ===================================================================
  154. */
  155. /*
  156. ** Common Header for all collectable objects (in macro form, to be
  157. ** included in other objects)
  158. */
  159. #define CommonHeader struct GCObject *next; lu_byte tt; lu_byte marked
  160. /* Common type for all collectable objects */
  161. typedef struct GCObject {
  162. CommonHeader;
  163. } GCObject;
  164. /* Bit mark for collectable types */
  165. #define BIT_ISCOLLECTABLE (1 << 6)
  166. #define iscollectable(o) (rawtt(o) & BIT_ISCOLLECTABLE)
  167. /* mark a tag as collectable */
  168. #define ctb(t) ((t) | BIT_ISCOLLECTABLE)
  169. #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
  170. #define gcvalueraw(v) ((v).gc)
  171. #define setgcovalue(L,obj,x) \
  172. { TValue *io = (obj); GCObject *i_g=(x); \
  173. val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); }
  174. /* }================================================================== */
  175. /*
  176. ** {==================================================================
  177. ** Numbers
  178. ** ===================================================================
  179. */
  180. /* Variant tags for numbers */
  181. #define LUA_TNUMFLT (LUA_TNUMBER | (1 << 4)) /* float numbers */
  182. #define LUA_TNUMINT (LUA_TNUMBER | (2 << 4)) /* integer numbers */
  183. #define ttisnumber(o) checktype((o), LUA_TNUMBER)
  184. #define ttisfloat(o) checktag((o), LUA_TNUMFLT)
  185. #define ttisinteger(o) checktag((o), LUA_TNUMINT)
  186. #define nvalue(o) check_exp(ttisnumber(o), \
  187. (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
  188. #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
  189. #define ivalue(o) check_exp(ttisinteger(o), val_(o).i)
  190. #define fltvalueraw(v) ((v).n)
  191. #define ivalueraw(v) ((v).i)
  192. #define setfltvalue(obj,x) \
  193. { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
  194. #define chgfltvalue(obj,x) \
  195. { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
  196. #define setivalue(obj,x) \
  197. { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); }
  198. #define chgivalue(obj,x) \
  199. { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
  200. /* }================================================================== */
  201. /*
  202. ** {==================================================================
  203. ** Strings
  204. ** ===================================================================
  205. */
  206. /* Variant tags for strings */
  207. #define LUA_TSHRSTR (LUA_TSTRING | (1 << 4)) /* short strings */
  208. #define LUA_TLNGSTR (LUA_TSTRING | (2 << 4)) /* long strings */
  209. #define ttisstring(o) checktype((o), LUA_TSTRING)
  210. #define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR))
  211. #define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR))
  212. #define tsvalueraw(v) (gco2ts((v).gc))
  213. #define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
  214. #define setsvalue(L,obj,x) \
  215. { TValue *io = (obj); TString *x_ = (x); \
  216. val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
  217. checkliveness(L,io); }
  218. /* set a string to the stack */
  219. #define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s)
  220. /* set a string to a new object */
  221. #define setsvalue2n setsvalue
  222. /*
  223. ** Header for string value; string bytes follow the end of this structure
  224. ** (aligned according to 'UTString'; see next).
  225. */
  226. typedef struct TString {
  227. CommonHeader;
  228. lu_byte extra; /* reserved words for short strings; "has hash" for longs */
  229. lu_byte shrlen; /* length for short strings */
  230. unsigned int hash;
  231. union {
  232. size_t lnglen; /* length for long strings */
  233. struct TString *hnext; /* linked list for hash table */
  234. } u;
  235. } TString;
  236. /*
  237. ** Get the actual string (array of bytes) from a 'TString'.
  238. ** (Access to 'extra' ensures that value is really a 'TString'.)
  239. */
  240. #define getstr(ts) \
  241. check_exp(sizeof((ts)->extra), cast_charp((ts)) + sizeof(TString))
  242. /* get the actual string (array of bytes) from a Lua value */
  243. #define svalue(o) getstr(tsvalue(o))
  244. /* get string length from 'TString *s' */
  245. #define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen)
  246. /* get string length from 'TValue *o' */
  247. #define vslen(o) tsslen(tsvalue(o))
  248. /* }================================================================== */
  249. /*
  250. ** {==================================================================
  251. ** Userdata
  252. ** ===================================================================
  253. */
  254. #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
  255. #define ttisfulluserdata(o) checktype((o), LUA_TUSERDATA)
  256. #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
  257. #define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
  258. #define pvalueraw(v) ((v).p)
  259. #define setpvalue(obj,x) \
  260. { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
  261. #define setuvalue(L,obj,x) \
  262. { TValue *io = (obj); Udata *x_ = (x); \
  263. val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \
  264. checkliveness(L,io); }
  265. /* Ensures that addresses after this type are always fully aligned. */
  266. typedef union UValue {
  267. TValue uv;
  268. LUAI_MAXALIGN; /* ensures maximum alignment for udata bytes */
  269. } UValue;
  270. /*
  271. ** Header for userdata with user values;
  272. ** memory area follows the end of this structure.
  273. */
  274. typedef struct Udata {
  275. CommonHeader;
  276. unsigned short nuvalue; /* number of user values */
  277. size_t len; /* number of bytes */
  278. struct Table *metatable;
  279. GCObject *gclist;
  280. UValue uv[1]; /* user values */
  281. } Udata;
  282. /*
  283. ** Header for userdata with no user values. These userdata do not need
  284. ** to be gray during GC, and therefore do not need a 'gclist' field.
  285. ** To simplify, the code always use 'Udata' for both kinds of userdata,
  286. ** making sure it never accesses 'gclist' on userdata with no user values.
  287. ** This structure here is used only to compute the correct size for
  288. ** this representation. (The 'bindata' field in its end ensures correct
  289. ** alignment for binary data following this header.)
  290. */
  291. typedef struct Udata0 {
  292. CommonHeader;
  293. unsigned short nuvalue; /* number of user values */
  294. size_t len; /* number of bytes */
  295. struct Table *metatable;
  296. union {LUAI_MAXALIGN;} bindata;
  297. } Udata0;
  298. /* compute the offset of the memory area of a userdata */
  299. #define udatamemoffset(nuv) \
  300. ((nuv) == 0 ? offsetof(Udata0, bindata) \
  301. : offsetof(Udata, uv) + (sizeof(UValue) * (nuv)))
  302. /* get the address of the memory block inside 'Udata' */
  303. #define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue))
  304. /* compute the size of a userdata */
  305. #define sizeudata(nuv,nb) (udatamemoffset(nuv) + (nb))
  306. /* }================================================================== */
  307. /*
  308. ** {==================================================================
  309. ** Prototypes
  310. ** ===================================================================
  311. */
  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 (register) */
  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. ** Associates the absolute line source for a given instruction ('pc').
  331. ** The array 'lineinfo' gives, for each instruction, the difference in
  332. ** lines from the previous instruction. When that difference does not
  333. ** fit into a byte, Lua saves the absolute line for that instruction.
  334. ** (Lua also saves the absolute line periodically, to speed up the
  335. ** computation of a line number: we can use binary search in the
  336. ** absolute-line array, but we must traverse the 'lineinfo' array
  337. ** linearly to compute a line.)
  338. */
  339. typedef struct AbsLineInfo {
  340. int pc;
  341. int line;
  342. } AbsLineInfo;
  343. /*
  344. ** Function Prototypes
  345. */
  346. typedef struct Proto {
  347. CommonHeader;
  348. lu_byte numparams; /* number of fixed (named) parameters */
  349. lu_byte is_vararg;
  350. lu_byte maxstacksize; /* number of registers needed by this function */
  351. int sizeupvalues; /* size of 'upvalues' */
  352. int sizek; /* size of 'k' */
  353. int sizecode;
  354. int sizelineinfo;
  355. int sizep; /* size of 'p' */
  356. int sizelocvars;
  357. int sizeabslineinfo; /* size of 'abslineinfo' */
  358. int linedefined; /* debug information */
  359. int lastlinedefined; /* debug information */
  360. TValue *k; /* constants used by the function */
  361. Instruction *code; /* opcodes */
  362. struct Proto **p; /* functions defined inside the function */
  363. Upvaldesc *upvalues; /* upvalue information */
  364. ls_byte *lineinfo; /* information about source lines (debug information) */
  365. AbsLineInfo *abslineinfo; /* idem */
  366. LocVar *locvars; /* information about local variables (debug information) */
  367. TString *source; /* used for debug information */
  368. GCObject *gclist;
  369. } Proto;
  370. /* }================================================================== */
  371. /*
  372. ** {==================================================================
  373. ** Closures
  374. ** ===================================================================
  375. */
  376. /* Variant tags for functions */
  377. #define LUA_TLCL (LUA_TFUNCTION | (1 << 4)) /* Lua closure */
  378. #define LUA_TLCF (LUA_TFUNCTION | (2 << 4)) /* light C function */
  379. #define LUA_TCCL (LUA_TFUNCTION | (3 << 4)) /* C closure */
  380. #define ttisfunction(o) checktype(o, LUA_TFUNCTION)
  381. #define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_TLCL)
  382. #define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
  383. #define ttislcf(o) checktag((o), LUA_TLCF)
  384. #define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
  385. #define isLfunction(o) ttisLclosure(o)
  386. #define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc))
  387. #define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
  388. #define fvalue(o) check_exp(ttislcf(o), val_(o).f)
  389. #define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
  390. #define fvalueraw(v) ((v).f)
  391. #define setclLvalue(L,obj,x) \
  392. { TValue *io = (obj); LClosure *x_ = (x); \
  393. val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \
  394. checkliveness(L,io); }
  395. #define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl)
  396. #define setfvalue(obj,x) \
  397. { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
  398. #define setclCvalue(L,obj,x) \
  399. { TValue *io = (obj); CClosure *x_ = (x); \
  400. val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \
  401. checkliveness(L,io); }
  402. /*
  403. ** Upvalues for Lua closures
  404. */
  405. typedef struct UpVal {
  406. CommonHeader;
  407. TValue *v; /* points to stack or to its own value */
  408. union {
  409. struct { /* (when open) */
  410. struct UpVal *next; /* linked list */
  411. struct UpVal **previous;
  412. } open;
  413. TValue value; /* the value (when closed) */
  414. } u;
  415. } UpVal;
  416. /* variant for "To Be Closed" upvalues */
  417. #define LUA_TUPVALTBC (LUA_TUPVAL | (1 << 4))
  418. #define ClosureHeader \
  419. CommonHeader; lu_byte nupvalues; GCObject *gclist
  420. typedef struct CClosure {
  421. ClosureHeader;
  422. lua_CFunction f;
  423. TValue upvalue[1]; /* list of upvalues */
  424. } CClosure;
  425. typedef struct LClosure {
  426. ClosureHeader;
  427. struct Proto *p;
  428. UpVal *upvals[1]; /* list of upvalues */
  429. } LClosure;
  430. typedef union Closure {
  431. CClosure c;
  432. LClosure l;
  433. } Closure;
  434. #define getproto(o) (clLvalue(o)->p)
  435. /* }================================================================== */
  436. /*
  437. ** {==================================================================
  438. ** Tables
  439. ** ===================================================================
  440. */
  441. #define ttistable(o) checktag((o), ctb(LUA_TTABLE))
  442. #define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc))
  443. #define sethvalue(L,obj,x) \
  444. { TValue *io = (obj); Table *x_ = (x); \
  445. val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \
  446. checkliveness(L,io); }
  447. #define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h)
  448. /*
  449. ** Nodes for Hash tables: A pack of two TValue's (key-value pairs)
  450. ** plus a 'next' field to link colliding entries. The distribution
  451. ** of the key's fields ('key_tt' and 'key_val') not forming a proper
  452. ** 'TValue' allows for a smaller size for 'Node' both in 4-byte
  453. ** and 8-byte alignments.
  454. */
  455. typedef union Node {
  456. struct NodeKey {
  457. TValuefields; /* fields for value */
  458. lu_byte key_tt; /* key type */
  459. int next; /* for chaining */
  460. Value key_val; /* key value */
  461. } u;
  462. TValue i_val; /* direct access to node's value as a proper 'TValue' */
  463. } Node;
  464. /* copy a value into a key */
  465. #define setnodekey(L,node,obj) \
  466. { Node *n_=(node); const TValue *io_=(obj); \
  467. n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \
  468. (void)L; checkliveness(L,io_); }
  469. /* copy a value from a key */
  470. #define getnodekey(L,obj,node) \
  471. { TValue *io_=(obj); const Node *n_=(node); \
  472. io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \
  473. (void)L; checkliveness(L,io_); }
  474. /*
  475. ** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the
  476. ** real size of 'array'. Otherwise, the real size of 'array' is the
  477. ** smallest power of two not smaller than 'alimit' (or zero iff 'alimit'
  478. ** is zero); 'alimit' is then used as a hint for #t.
  479. */
  480. #define BITRAS (1 << 7)
  481. #define isrealasize(t) (!((t)->marked & BITRAS))
  482. #define setrealasize(t) ((t)->marked &= cast_byte(~BITRAS))
  483. #define setnorealasize(t) ((t)->marked |= BITRAS)
  484. typedef struct Table {
  485. CommonHeader;
  486. lu_byte flags; /* 1<<p means tagmethod(p) is not present */
  487. lu_byte lsizenode; /* log2 of size of 'node' array */
  488. unsigned int alimit; /* "limit" of 'array' array */
  489. TValue *array; /* array part */
  490. Node *node;
  491. Node *lastfree; /* any free position is before this position */
  492. struct Table *metatable;
  493. GCObject *gclist;
  494. } Table;
  495. /*
  496. ** Macros to manipulate keys inserted in nodes
  497. */
  498. #define keytt(node) ((node)->u.key_tt)
  499. #define keyval(node) ((node)->u.key_val)
  500. #define keyisnil(node) (keytt(node) == LUA_TNIL)
  501. #define keyisinteger(node) (keytt(node) == LUA_TNUMINT)
  502. #define keyival(node) (keyval(node).i)
  503. #define keyisshrstr(node) (keytt(node) == ctb(LUA_TSHRSTR))
  504. #define keystrval(node) (gco2ts(keyval(node).gc))
  505. #define setnilkey(node) (keytt(node) = LUA_TNIL)
  506. #define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE)
  507. #define gckey(n) (keyval(n).gc)
  508. #define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL)
  509. /*
  510. ** Use a "nil table" to mark dead keys in a table. Those keys serve
  511. ** to keep space for removed entries, which may still be part of
  512. ** chains. Note that the 'keytt' does not have the BIT_ISCOLLECTABLE
  513. ** set, so these values are considered not collectable and are different
  514. ** from any valid value.
  515. */
  516. #define setdeadkey(n) (keytt(n) = LUA_TTABLE, gckey(n) = NULL)
  517. /* }================================================================== */
  518. /*
  519. ** 'module' operation for hashing (size is always a power of 2)
  520. */
  521. #define lmod(s,size) \
  522. (check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1)))))
  523. #define twoto(x) (1<<(x))
  524. #define sizenode(t) (twoto((t)->lsizenode))
  525. /* size of buffer for 'luaO_utf8esc' function */
  526. #define UTF8BUFFSZ 8
  527. LUAI_FUNC int luaO_int2fb (unsigned int x);
  528. LUAI_FUNC int luaO_fb2int (int x);
  529. LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
  530. LUAI_FUNC int luaO_ceillog2 (unsigned int x);
  531. LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1,
  532. const TValue *p2, TValue *res);
  533. LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
  534. const TValue *p2, StkId res);
  535. LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
  536. LUAI_FUNC int luaO_hexavalue (int c);
  537. LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj);
  538. LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
  539. va_list argp);
  540. LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  541. LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen);
  542. #endif