lobject.h 21 KB

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