lobject.h 21 KB

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