lobject.h 14 KB

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