lobject.h 15 KB

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