lobject.h 23 KB

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