lobject.h 14 KB

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