lobject.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*
  2. ** $Id: lobject.h,v 2.121 2017/06/01 20:24:05 roberto Exp roberto $
  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 tags for non-values
  13. */
  14. #define LUA_TUPVAL LUA_NUMTAGS /* upvalues */
  15. #define LUA_TPROTO (LUA_NUMTAGS+1) /* function prototypes */
  16. #define LUA_TDEADKEY (LUA_NUMTAGS+2) /* removed keys in tables */
  17. /*
  18. ** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
  19. */
  20. #define LUA_TOTALTAGS (LUA_TPROTO + 2)
  21. /*
  22. ** tags for Tagged Values have the following use of bits:
  23. ** bits 0-3: actual tag (a LUA_T* value)
  24. ** bits 4-5: variant bits
  25. ** bit 6: whether value is collectable
  26. */
  27. /*
  28. ** LUA_TFUNCTION variants:
  29. ** 0 - Lua function
  30. ** 1 - light C function
  31. ** 2 - regular C function (closure)
  32. */
  33. /* Variant tags for functions */
  34. #define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */
  35. #define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */
  36. #define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */
  37. /* Variant tags for strings */
  38. #define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */
  39. #define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */
  40. /* Variant tags for numbers */
  41. #define LUA_TNUMFLT (LUA_TNUMBER | (0 << 4)) /* float numbers */
  42. #define LUA_TNUMINT (LUA_TNUMBER | (1 << 4)) /* integer numbers */
  43. /* Bit mark for collectable types */
  44. #define BIT_ISCOLLECTABLE (1 << 6)
  45. /* mark a tag as collectable */
  46. #define ctb(t) ((t) | BIT_ISCOLLECTABLE)
  47. /*
  48. ** Common type for all collectable objects
  49. */
  50. typedef struct GCObject GCObject;
  51. /*
  52. ** Common Header for all collectable objects (in macro form, to be
  53. ** included in other objects)
  54. */
  55. #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
  56. /*
  57. ** Common type has only the common header
  58. */
  59. struct GCObject {
  60. CommonHeader;
  61. };
  62. /*
  63. ** Tagged Values. This is the basic representation of values in Lua,
  64. ** an actual value plus a tag with its type.
  65. */
  66. /*
  67. ** Union of all Lua values
  68. */
  69. typedef union Value {
  70. GCObject *gc; /* collectable objects */
  71. void *p; /* light userdata */
  72. int b; /* booleans */
  73. lua_CFunction f; /* light C functions */
  74. lua_Integer i; /* integer numbers */
  75. lua_Number n; /* float numbers */
  76. } Value;
  77. #define TValuefields Value value_; lu_byte tt_
  78. typedef struct lua_TValue {
  79. TValuefields;
  80. } TValue;
  81. /* macro defining a nil value */
  82. #define NILCONSTANT {NULL}, LUA_TNIL
  83. #define val_(o) ((o)->value_)
  84. #define valraw(o) (&val_(o))
  85. /* raw type tag of a TValue */
  86. #define rttype(o) ((o)->tt_)
  87. /* tag with no variants (bits 0-3) */
  88. #define novariant(x) ((x) & 0x0F)
  89. /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
  90. #define ttyperaw(t) ((t) & 0x3F)
  91. #define ttype(o) ttyperaw(rttype(o))
  92. /* type tag of a TValue with no variants (bits 0-3) */
  93. #define ttnov(o) (novariant(rttype(o)))
  94. /* Macros to test type */
  95. #define checktag(o,t) (rttype(o) == (t))
  96. #define checktype(o,t) (ttnov(o) == (t))
  97. #define ttisnumber(o) checktype((o), LUA_TNUMBER)
  98. #define ttisfloat(o) checktag((o), LUA_TNUMFLT)
  99. #define ttisinteger(o) checktag((o), LUA_TNUMINT)
  100. #define ttisnil(o) checktag((o), LUA_TNIL)
  101. #define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
  102. #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
  103. #define ttisstring(o) checktype((o), LUA_TSTRING)
  104. #define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR))
  105. #define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR))
  106. #define ttistable(o) checktag((o), ctb(LUA_TTABLE))
  107. #define ttisfunction(o) checktype(o, LUA_TFUNCTION)
  108. #define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION)
  109. #define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
  110. #define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
  111. #define ttislcf(o) checktag((o), LUA_TLCF)
  112. #define ttisfulluserdata(o) checktag((o), ctb(LUA_TUSERDATA))
  113. #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
  114. /*
  115. ** Macros to access unstructured values (may come both from
  116. ** 'TValue's and table keys)
  117. */
  118. #define ivalueraw(v) ((v).i)
  119. #define fltvalueraw(v) ((v).n)
  120. #define gcvalueraw(v) ((v).gc)
  121. #define pvalueraw(v) ((v).p)
  122. #define tsvalueraw(v) (gco2ts((v).gc))
  123. #define fvalueraw(v) ((v).f)
  124. #define bvalueraw(v) ((v).b)
  125. /* Macros to access values */
  126. #define ivalue(o) check_exp(ttisinteger(o), val_(o).i)
  127. #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
  128. #define nvalue(o) check_exp(ttisnumber(o), \
  129. (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
  130. #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
  131. #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
  132. #define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
  133. #define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
  134. #define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc))
  135. #define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
  136. #define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
  137. #define fvalue(o) check_exp(ttislcf(o), val_(o).f)
  138. #define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc))
  139. #define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
  140. #define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc))
  141. #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
  142. #define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE)
  143. /*
  144. ** Protected access to objects in values
  145. */
  146. #define gcvalueN(o) (iscollectable(o) ? gcvalue(o) : NULL)
  147. /* Macros for internal tests */
  148. #define righttt(obj) (ttype(obj) == gcvalue(obj)->tt)
  149. #define checkliveness(L,obj) \
  150. lua_longassert(!iscollectable(obj) || \
  151. (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))
  152. /* Macros to set values */
  153. #define settt_(o,t) ((o)->tt_=(t))
  154. #define setfltvalue(obj,x) \
  155. { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
  156. #define chgfltvalue(obj,x) \
  157. { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
  158. #define setivalue(obj,x) \
  159. { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); }
  160. #define chgivalue(obj,x) \
  161. { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
  162. #define setnilvalue(obj) settt_(obj, LUA_TNIL)
  163. #define setfvalue(obj,x) \
  164. { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
  165. #define setpvalue(obj,x) \
  166. { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
  167. #define setbvalue(obj,x) \
  168. { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
  169. #define setgcovalue(L,obj,x) \
  170. { TValue *io = (obj); GCObject *i_g=(x); \
  171. val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); }
  172. #define setsvalue(L,obj,x) \
  173. { TValue *io = (obj); TString *x_ = (x); \
  174. val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
  175. checkliveness(L,io); }
  176. #define setuvalue(L,obj,x) \
  177. { TValue *io = (obj); Udata *x_ = (x); \
  178. val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \
  179. checkliveness(L,io); }
  180. #define setthvalue(L,obj,x) \
  181. { TValue *io = (obj); lua_State *x_ = (x); \
  182. val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \
  183. checkliveness(L,io); }
  184. #define setclLvalue(L,obj,x) \
  185. { TValue *io = (obj); LClosure *x_ = (x); \
  186. val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \
  187. checkliveness(L,io); }
  188. #define setclCvalue(L,obj,x) \
  189. { TValue *io = (obj); CClosure *x_ = (x); \
  190. val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \
  191. checkliveness(L,io); }
  192. #define sethvalue(L,obj,x) \
  193. { TValue *io = (obj); Table *x_ = (x); \
  194. val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \
  195. checkliveness(L,io); }
  196. #define setobj(L,obj1,obj2) \
  197. { TValue *io1=(obj1); const TValue *io2=(obj2); \
  198. io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
  199. (void)L; checkliveness(L,io1); }
  200. /*
  201. ** different types of assignments, according to destination
  202. */
  203. /* from stack to (same) stack */
  204. #define setobjs2s setobj
  205. /* to stack (not from same stack) */
  206. #define setobj2s setobj
  207. #define setsvalue2s setsvalue
  208. #define sethvalue2s sethvalue
  209. #define setptvalue2s setptvalue
  210. /* from table to same table */
  211. #define setobjt2t setobj
  212. /* to new object */
  213. #define setobj2n setobj
  214. #define setsvalue2n setsvalue
  215. /* to table */
  216. #define setobj2t setobj
  217. /*
  218. ** {======================================================
  219. ** types and prototypes
  220. ** =======================================================
  221. */
  222. typedef TValue *StkId; /* index to stack elements */
  223. /*
  224. ** Header for string value; string bytes follow the end of this structure
  225. ** (aligned according to 'UTString'; see next).
  226. */
  227. typedef struct TString {
  228. CommonHeader;
  229. lu_byte extra; /* reserved words for short strings; "has hash" for longs */
  230. lu_byte shrlen; /* length for short strings */
  231. unsigned int hash;
  232. union {
  233. size_t lnglen; /* length for long strings */
  234. struct TString *hnext; /* linked list for hash table */
  235. } u;
  236. } TString;
  237. /*
  238. ** Ensures that address after this type is always fully aligned.
  239. */
  240. typedef union UTString {
  241. LUAI_MAXALIGN; /* ensures maximum alignment for strings */
  242. TString tsv;
  243. } UTString;
  244. /*
  245. ** Get the actual string (array of bytes) from a 'TString'.
  246. ** (Access to 'extra' ensures that value is really a 'TString'.)
  247. */
  248. #define getstr(ts) \
  249. check_exp(sizeof((ts)->extra), cast(char *, (ts)) + sizeof(UTString))
  250. /* get the actual string (array of bytes) from a Lua value */
  251. #define svalue(o) getstr(tsvalue(o))
  252. /* get string length from 'TString *s' */
  253. #define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen)
  254. /* get string length from 'TValue *o' */
  255. #define vslen(o) tsslen(tsvalue(o))
  256. /*
  257. ** Header for userdata; memory area follows the end of this structure
  258. ** (aligned according to 'UUdata'; see next).
  259. */
  260. typedef struct Udata {
  261. CommonHeader;
  262. lu_byte ttuv_; /* user value's tag */
  263. struct Table *metatable;
  264. size_t len; /* number of bytes */
  265. union Value user_; /* user value */
  266. } Udata;
  267. /*
  268. ** Ensures that address after this type is always fully aligned.
  269. */
  270. typedef union UUdata {
  271. LUAI_MAXALIGN; /* ensures maximum alignment for 'local' udata */
  272. Udata uv;
  273. } UUdata;
  274. /*
  275. ** Get the address of memory block inside 'Udata'.
  276. ** (Access to 'ttuv_' ensures that value is really a 'Udata'.)
  277. */
  278. #define getudatamem(u) \
  279. check_exp(sizeof((u)->ttuv_), (cast(char*, (u)) + sizeof(UUdata)))
  280. #define setuservalue(L,u,o) \
  281. { const TValue *io=(o); Udata *iu = (u); \
  282. iu->user_ = io->value_; iu->ttuv_ = rttype(io); \
  283. checkliveness(L,io); }
  284. #define getuservalue(L,u,o) \
  285. { TValue *io=(o); const Udata *iu = (u); \
  286. io->value_ = iu->user_; settt_(io, iu->ttuv_); \
  287. checkliveness(L,io); }
  288. /*
  289. ** Description of an upvalue for function prototypes
  290. */
  291. typedef struct Upvaldesc {
  292. TString *name; /* upvalue name (for debug information) */
  293. lu_byte instack; /* whether it is in stack (register) */
  294. lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
  295. } Upvaldesc;
  296. /*
  297. ** Description of a local variable for function prototypes
  298. ** (used for debug information)
  299. */
  300. typedef struct LocVar {
  301. TString *varname;
  302. int startpc; /* first point where variable is active */
  303. int endpc; /* first point where variable is dead */
  304. } LocVar;
  305. /*
  306. ** Function Prototypes
  307. */
  308. typedef struct Proto {
  309. CommonHeader;
  310. lu_byte numparams; /* number of fixed parameters */
  311. lu_byte is_vararg;
  312. lu_byte maxstacksize; /* number of registers needed by this function */
  313. lu_byte cachemiss; /* count for successive misses for 'cache' field */
  314. int sizeupvalues; /* size of 'upvalues' */
  315. int sizek; /* size of 'k' */
  316. int sizecode;
  317. int sizelineinfo;
  318. int sizep; /* size of 'p' */
  319. int sizelocvars;
  320. int linedefined; /* debug information */
  321. int lastlinedefined; /* debug information */
  322. TValue *k; /* constants used by the function */
  323. Instruction *code; /* opcodes */
  324. struct Proto **p; /* functions defined inside the function */
  325. int *lineinfo; /* map from opcodes to source lines (debug information) */
  326. LocVar *locvars; /* information about local variables (debug information) */
  327. Upvaldesc *upvalues; /* upvalue information */
  328. struct LClosure *cache; /* last-created closure with this prototype */
  329. TString *source; /* used for debug information */
  330. GCObject *gclist;
  331. } Proto;
  332. /*
  333. ** Upvalues for Lua closures
  334. */
  335. typedef struct UpVal {
  336. CommonHeader;
  337. TValue *v; /* points to stack or to its own value */
  338. union {
  339. struct { /* (when open) */
  340. struct UpVal *next; /* linked list */
  341. struct UpVal **previous;
  342. } open;
  343. TValue value; /* the value (when closed) */
  344. } u;
  345. } UpVal;
  346. /*
  347. ** Closures
  348. */
  349. #define ClosureHeader \
  350. CommonHeader; lu_byte nupvalues; GCObject *gclist
  351. typedef struct CClosure {
  352. ClosureHeader;
  353. lua_CFunction f;
  354. TValue upvalue[1]; /* list of upvalues */
  355. } CClosure;
  356. typedef struct LClosure {
  357. ClosureHeader;
  358. struct Proto *p;
  359. UpVal *upvals[1]; /* list of upvalues */
  360. } LClosure;
  361. typedef union Closure {
  362. CClosure c;
  363. LClosure l;
  364. } Closure;
  365. #define isLfunction(o) ttisLclosure(o)
  366. #define getproto(o) (clLvalue(o)->p)
  367. /*
  368. ** Tables
  369. */
  370. /*
  371. ** Nodes for Hash tables. A pack of two TValue's (key-value pairs)
  372. ** plus a 'next' field to link colliding entries. The distribuition
  373. ** of the key's fields ('key_tt' and 'key_val') not forming a proper
  374. ** 'TValue' allows for a smaller size for 'Node' both in 4-byte
  375. ** and 8-byte alignments.
  376. */
  377. typedef union Node {
  378. struct NodeKey {
  379. TValuefields; /* fields for value */
  380. lu_byte key_tt; /* key type */
  381. int next; /* for chaining */
  382. Value key_val; /* key value */
  383. } u;
  384. TValue i_val; /* direct access to node's value as a proper 'TValue' */
  385. } Node;
  386. /* copy a value into a key */
  387. #define setnodekey(L,node,obj) \
  388. { Node *n_=(node); const TValue *io_=(obj); \
  389. n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \
  390. (void)L; checkliveness(L,io_); }
  391. /* copy a value from a key */
  392. #define getnodekey(L,obj,node) \
  393. { TValue *io_=(obj); const Node *n_=(node); \
  394. io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \
  395. (void)L; checkliveness(L,io_); }
  396. typedef struct Table {
  397. CommonHeader;
  398. lu_byte flags; /* 1<<p means tagmethod(p) is not present */
  399. lu_byte lsizenode; /* log2 of size of 'node' array */
  400. unsigned int sizearray; /* size of 'array' array */
  401. TValue *array; /* array part */
  402. Node *node;
  403. Node *lastfree; /* any free position is before this position */
  404. struct Table *metatable;
  405. GCObject *gclist;
  406. } Table;
  407. /*
  408. ** Macros to manipulate keys inserted in nodes
  409. */
  410. #define keytt(node) ((node)->u.key_tt)
  411. #define keyval(node) ((node)->u.key_val)
  412. #define keyisnil(node) (keytt(node) == LUA_TNIL)
  413. #define keyisinteger(node) (keytt(node) == LUA_TNUMINT)
  414. #define keyival(node) (keyval(node).i)
  415. #define keyisshrstr(node) (keytt(node) == ctb(LUA_TSHRSTR))
  416. #define keystrval(node) (gco2ts(keyval(node).gc))
  417. #define keyisdead(node) (keytt(node) == LUA_TDEADKEY)
  418. #define setnilkey(node) (keytt(node) = LUA_TNIL)
  419. #define setdeadkey(node) (keytt(node) = LUA_TDEADKEY)
  420. /* a dead value may get the 'gc' field, but cannot access its contents */
  421. #define deadkey(n) \
  422. check_exp(keytt(n) == LUA_TDEADKEY, cast(void *, keyval(n).gc))
  423. #define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE)
  424. #define gckey(n) (keyval(n).gc)
  425. #define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL)
  426. /*
  427. ** 'module' operation for hashing (size is always a power of 2)
  428. */
  429. #define lmod(s,size) \
  430. (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
  431. #define twoto(x) (1<<(x))
  432. #define sizenode(t) (twoto((t)->lsizenode))
  433. /*
  434. ** (address of) a fixed nil value
  435. */
  436. #define luaO_nilobject (&luaO_nilobject_)
  437. LUAI_DDEC const TValue luaO_nilobject_;
  438. /* size of buffer for 'luaO_utf8esc' function */
  439. #define UTF8BUFFSZ 8
  440. LUAI_FUNC int luaO_int2fb (unsigned int x);
  441. LUAI_FUNC int luaO_fb2int (int x);
  442. LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
  443. LUAI_FUNC int luaO_ceillog2 (unsigned int x);
  444. LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
  445. const TValue *p2, TValue *res);
  446. LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
  447. LUAI_FUNC int luaO_hexavalue (int c);
  448. LUAI_FUNC void luaO_tostring (lua_State *L, StkId obj);
  449. LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
  450. va_list argp);
  451. LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  452. LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
  453. #endif