lobject.h 20 KB

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