lobject.h 14 KB

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