lobject.h 21 KB

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