lobject.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. ** $Id: lobject.h,v 2.63 2011/10/17 14:46:13 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_TUPVAL (LUA_NUMTAGS+1)
  16. #define LUA_TDEADKEY (LUA_NUMTAGS+2)
  17. /*
  18. ** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
  19. */
  20. #define LUA_TOTALTAGS (LUA_TUPVAL+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. /* Bit mark for collectable types */
  38. #define BIT_ISCOLLECTABLE (1 << 6)
  39. /* mark a tag as collectable */
  40. #define ctb(t) ((t) | BIT_ISCOLLECTABLE)
  41. /*
  42. ** Union of all collectable objects
  43. */
  44. typedef union GCObject GCObject;
  45. /*
  46. ** Common Header for all collectable objects (in macro form, to be
  47. ** included in other objects)
  48. */
  49. #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
  50. /*
  51. ** Common header in struct form
  52. */
  53. typedef struct GCheader {
  54. CommonHeader;
  55. } GCheader;
  56. /*
  57. ** Union of all Lua values
  58. */
  59. typedef union Value Value;
  60. #define numfield lua_Number n; /* numbers */
  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. #define TValuefields Value value_; int tt_
  66. typedef struct lua_TValue TValue;
  67. /* macro defining a nil value */
  68. #define NILCONSTANT {NULL}, LUA_TNIL
  69. #define val_(o) ((o)->value_)
  70. #define num_(o) (val_(o).n)
  71. /* raw type tag of a TValue */
  72. #define rttype(o) ((o)->tt_)
  73. /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
  74. #define ttype(o) (rttype(o) & 0x3F)
  75. /* type tag of a TValue with no variants (bits 0-3) */
  76. #define ttypenv(o) (rttype(o) & 0x0F)
  77. /* Macros to test type */
  78. #define checktag(o,t) (rttype(o) == (t))
  79. #define ttisnumber(o) checktag((o), LUA_TNUMBER)
  80. #define ttisnil(o) checktag((o), LUA_TNIL)
  81. #define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
  82. #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
  83. #define ttisstring(o) checktag((o), ctb(LUA_TSTRING))
  84. #define ttistable(o) checktag((o), ctb(LUA_TTABLE))
  85. #define ttisfunction(o) (ttypenv(o) == LUA_TFUNCTION)
  86. #define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION)
  87. #define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
  88. #define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
  89. #define ttislcf(o) checktag((o), LUA_TLCF)
  90. #define ttisuserdata(o) checktag((o), ctb(LUA_TUSERDATA))
  91. #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
  92. #define ttisdeadkey(o) checktag((o), LUA_TDEADKEY)
  93. #define ttisequal(o1,o2) (rttype(o1) == rttype(o2))
  94. /* Macros to access values */
  95. #define nvalue(o) check_exp(ttisnumber(o), num_(o))
  96. #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
  97. #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
  98. #define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts)
  99. #define tsvalue(o) (&rawtsvalue(o)->tsv)
  100. #define rawuvalue(o) check_exp(ttisuserdata(o), &val_(o).gc->u)
  101. #define uvalue(o) (&rawuvalue(o)->uv)
  102. #define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl)
  103. #define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l)
  104. #define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c)
  105. #define fvalue(o) check_exp(ttislcf(o), val_(o).f)
  106. #define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h)
  107. #define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
  108. #define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th)
  109. /* a dead value may get the 'gc' field, but cannot access its contents */
  110. #define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
  111. #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
  112. #define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE)
  113. /* Macros for internal tests */
  114. #define righttt(obj) (ttypenv(obj) == gcvalue(obj)->gch.tt)
  115. #define checkliveness(g,obj) \
  116. lua_longassert(!iscollectable(obj) || \
  117. (righttt(obj) && !isdead(g,gcvalue(obj))))
  118. /* Macros to set values */
  119. #define settt_(o,t) ((o)->tt_=(t))
  120. #define setnvalue(obj,x) \
  121. { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); }
  122. #define changenvalue(o,x) check_exp(ttisnumber(o), num_(o)=(x))
  123. #define setnilvalue(obj) settt_(obj, LUA_TNIL)
  124. #define setfvalue(obj,x) \
  125. { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
  126. #define setpvalue(obj,x) \
  127. { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
  128. #define setbvalue(obj,x) \
  129. { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
  130. #define setgcovalue(L,obj,x) \
  131. { TValue *io=(obj); GCObject *i_g=(x); \
  132. val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); }
  133. #define setsvalue(L,obj,x) \
  134. { TValue *io=(obj); \
  135. val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TSTRING)); \
  136. checkliveness(G(L),io); }
  137. #define setuvalue(L,obj,x) \
  138. { TValue *io=(obj); \
  139. val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \
  140. checkliveness(G(L),io); }
  141. #define setthvalue(L,obj,x) \
  142. { TValue *io=(obj); \
  143. val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \
  144. checkliveness(G(L),io); }
  145. #define setclLvalue(L,obj,x) \
  146. { TValue *io=(obj); \
  147. val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \
  148. checkliveness(G(L),io); }
  149. #define setclCvalue(L,obj,x) \
  150. { TValue *io=(obj); \
  151. val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \
  152. checkliveness(G(L),io); }
  153. #define sethvalue(L,obj,x) \
  154. { TValue *io=(obj); \
  155. val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \
  156. checkliveness(G(L),io); }
  157. #define setptvalue(L,obj,x) \
  158. { TValue *io=(obj); \
  159. val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TPROTO)); \
  160. checkliveness(G(L),io); }
  161. #define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
  162. #define setobj(L,obj1,obj2) \
  163. { const TValue *io2=(obj2); TValue *io1=(obj1); \
  164. io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
  165. checkliveness(G(L),io1); }
  166. /*
  167. ** different types of assignments, according to destination
  168. */
  169. /* from stack to (same) stack */
  170. #define setobjs2s setobj
  171. /* to stack (not from same stack) */
  172. #define setobj2s setobj
  173. #define setsvalue2s setsvalue
  174. #define sethvalue2s sethvalue
  175. #define setptvalue2s setptvalue
  176. /* from table to same table */
  177. #define setobjt2t setobj
  178. /* to table */
  179. #define setobj2t setobj
  180. /* to new object */
  181. #define setobj2n setobj
  182. #define setsvalue2n setsvalue
  183. /*
  184. ** {======================================================
  185. ** NaN Trick
  186. ** =======================================================
  187. */
  188. #if defined(LUA_NANTRICK) \
  189. || defined(LUA_NANTRICK_LE) \
  190. || defined(LUA_NANTRICK_BE)
  191. /*
  192. ** numbers are represented in the 'd_' field. All other values have the
  193. ** value (NNMARK | tag) in 'tt__'. A number with such pattern would be
  194. ** a "signaled NaN", which is never generated by regular operations by
  195. ** the CPU (nor by 'strtod')
  196. */
  197. #if !defined(NNMARK)
  198. #define NNMARK 0x7FF7A500
  199. #define NNMASK 0x7FFFFF00
  200. #endif
  201. #undef TValuefields
  202. #undef NILCONSTANT
  203. #if defined(LUA_NANTRICK_LE)
  204. /* little endian */
  205. #define TValuefields \
  206. union { struct { Value v__; int tt__; } i; double d__; } u
  207. #define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}}
  208. /* field-access macros */
  209. #define v_(o) ((o)->u.i.v__)
  210. #define d_(o) ((o)->u.d__)
  211. #define tt_(o) ((o)->u.i.tt__)
  212. #elif defined(LUA_NANTRICK_BE)
  213. /* big endian */
  214. #define TValuefields \
  215. union { struct { int tt__; Value v__; } i; double d__; } u
  216. #define NILCONSTANT {{tag2tt(LUA_TNIL), {NULL}}}
  217. /* field-access macros */
  218. #define v_(o) ((o)->u.i.v__)
  219. #define d_(o) ((o)->u.d__)
  220. #define tt_(o) ((o)->u.i.tt__)
  221. #elif !defined(TValuefields)
  222. #error option 'LUA_NANTRICK' needs declaration for 'TValuefields'
  223. #endif
  224. /* correspondence with standard representation */
  225. #undef val_
  226. #define val_(o) v_(o)
  227. #undef num_
  228. #define num_(o) d_(o)
  229. #undef numfield
  230. #define numfield /* no such field; numbers are the entire struct */
  231. /* basic check to distinguish numbers from non-numbers */
  232. #undef ttisnumber
  233. #define ttisnumber(o) ((tt_(o) & NNMASK) != NNMARK)
  234. #define tag2tt(t) (NNMARK | (t))
  235. #undef rttype
  236. #define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff)
  237. #undef settt_
  238. #define settt_(o,t) (tt_(o) = tag2tt(t))
  239. #undef setnvalue
  240. #define setnvalue(obj,x) \
  241. { TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); }
  242. #undef setobj
  243. #define setobj(L,obj1,obj2) \
  244. { const TValue *o2_=(obj2); TValue *o1_=(obj1); \
  245. o1_->u = o2_->u; \
  246. checkliveness(G(L),o1_); }
  247. /*
  248. ** these redefinitions are not mandatory, but these forms are more efficient
  249. */
  250. #undef checktag
  251. #define checktag(o,t) (tt_(o) == tag2tt(t))
  252. #undef ttisequal
  253. #define ttisequal(o1,o2) \
  254. (ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2)))
  255. #define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; }
  256. #else
  257. #define luai_checknum(L,o,c) { /* empty */ }
  258. #endif
  259. /* }====================================================== */
  260. /*
  261. ** {======================================================
  262. ** types and prototypes
  263. ** =======================================================
  264. */
  265. union Value {
  266. GCObject *gc; /* collectable objects */
  267. void *p; /* light userdata */
  268. int b; /* booleans */
  269. lua_CFunction f; /* light C functions */
  270. numfield /* numbers */
  271. };
  272. struct lua_TValue {
  273. TValuefields;
  274. };
  275. typedef TValue *StkId; /* index to stack elements */
  276. /*
  277. ** Header for string value; string bytes follow the end of this structure
  278. */
  279. typedef union TString {
  280. L_Umaxalign dummy; /* ensures maximum alignment for strings */
  281. struct {
  282. CommonHeader;
  283. lu_byte reserved;
  284. unsigned int hash;
  285. size_t len; /* number of characters in string */
  286. } tsv;
  287. } TString;
  288. /* get the actual string (array of bytes) from a TString */
  289. #define getstr(ts) cast(const char *, (ts) + 1)
  290. /* get the actual string (array of bytes) from a Lua value */
  291. #define svalue(o) getstr(rawtsvalue(o))
  292. /*
  293. ** Header for userdata; memory area follows the end of this structure
  294. */
  295. typedef union Udata {
  296. L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
  297. struct {
  298. CommonHeader;
  299. struct Table *metatable;
  300. struct Table *env;
  301. size_t len; /* number of bytes */
  302. } uv;
  303. } Udata;
  304. /*
  305. ** Description of an upvalue for function prototypes
  306. */
  307. typedef struct Upvaldesc {
  308. TString *name; /* upvalue name (for debug information) */
  309. lu_byte instack; /* whether it is in stack */
  310. lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
  311. } Upvaldesc;
  312. /*
  313. ** Description of a local variable for function prototypes
  314. ** (used for debug information)
  315. */
  316. typedef struct LocVar {
  317. TString *varname;
  318. int startpc; /* first point where variable is active */
  319. int endpc; /* first point where variable is dead */
  320. } LocVar;
  321. /*
  322. ** Function Prototypes
  323. */
  324. typedef struct Proto {
  325. CommonHeader;
  326. TValue *k; /* constants used by the function */
  327. Instruction *code;
  328. struct Proto **p; /* functions defined inside the function */
  329. int *lineinfo; /* map from opcodes to source lines (debug information) */
  330. LocVar *locvars; /* information about local variables (debug information) */
  331. Upvaldesc *upvalues; /* upvalue information */
  332. union Closure *cache; /* last created closure with this prototype */
  333. TString *source; /* used for debug information */
  334. int sizeupvalues; /* size of 'upvalues' */
  335. int sizek; /* size of `k' */
  336. int sizecode;
  337. int sizelineinfo;
  338. int sizep; /* size of `p' */
  339. int sizelocvars;
  340. int linedefined;
  341. int lastlinedefined;
  342. GCObject *gclist;
  343. lu_byte numparams; /* number of fixed parameters */
  344. lu_byte is_vararg;
  345. lu_byte maxstacksize; /* maximum stack used by this function */
  346. } Proto;
  347. /*
  348. ** Lua Upvalues
  349. */
  350. typedef struct UpVal {
  351. CommonHeader;
  352. TValue *v; /* points to stack or to its own value */
  353. union {
  354. TValue value; /* the value (when closed) */
  355. struct { /* double linked list (when open) */
  356. struct UpVal *prev;
  357. struct UpVal *next;
  358. } l;
  359. } u;
  360. } UpVal;
  361. /*
  362. ** Closures
  363. */
  364. #define ClosureHeader \
  365. CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist
  366. typedef struct CClosure {
  367. ClosureHeader;
  368. lua_CFunction f;
  369. TValue upvalue[1]; /* list of upvalues */
  370. } CClosure;
  371. typedef struct LClosure {
  372. ClosureHeader;
  373. struct Proto *p;
  374. UpVal *upvals[1]; /* list of upvalues */
  375. } LClosure;
  376. typedef union Closure {
  377. CClosure c;
  378. LClosure l;
  379. } Closure;
  380. #define isLfunction(o) ttisLclosure(o)
  381. #define getproto(o) (clLvalue(o)->p)
  382. /*
  383. ** Tables
  384. */
  385. typedef union TKey {
  386. struct {
  387. TValuefields;
  388. struct Node *next; /* for chaining */
  389. } nk;
  390. TValue tvk;
  391. } TKey;
  392. typedef struct Node {
  393. TValue i_val;
  394. TKey i_key;
  395. } Node;
  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. struct Table *metatable;
  401. TValue *array; /* array part */
  402. Node *node;
  403. Node *lastfree; /* any free position is before this position */
  404. GCObject *gclist;
  405. int sizearray; /* size of `array' array */
  406. } Table;
  407. /*
  408. ** `module' operation for hashing (size is always a power of 2)
  409. */
  410. #define lmod(s,size) \
  411. (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
  412. #define twoto(x) (1<<(x))
  413. #define sizenode(t) (twoto((t)->lsizenode))
  414. /*
  415. ** (address of) a fixed nil value
  416. */
  417. #define luaO_nilobject (&luaO_nilobject_)
  418. LUAI_DDEC const TValue luaO_nilobject_;
  419. LUAI_FUNC int luaO_int2fb (unsigned int x);
  420. LUAI_FUNC int luaO_fb2int (int x);
  421. LUAI_FUNC int luaO_ceillog2 (unsigned int x);
  422. LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
  423. LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
  424. LUAI_FUNC int luaO_hexavalue (int c);
  425. LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
  426. va_list argp);
  427. LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  428. LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
  429. #endif