2
0

lobject.h 24 KB

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