lobject.h 13 KB

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