lobject.h 21 KB

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