lobject.h 22 KB

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