lobject.h 20 KB

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