lobject.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. ** $Id: lobject.h,v 2.20.1.2 2008/08/06 13:29:48 roberto Exp $
  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. /* tags for values visible from Lua */
  12. #define LAST_TAG LUA_TTHREAD
  13. #define NUM_TAGS (LAST_TAG+1)
  14. /* mask for 'read-only' objects. must match READONLYBIT in lgc.h' */
  15. #define READONLYMASK 128
  16. /*
  17. ** Extra tags for non-values
  18. */
  19. #define LUA_TPROTO (LAST_TAG+1)
  20. #define LUA_TUPVAL (LAST_TAG+2)
  21. #define LUA_TDEADKEY (LAST_TAG+3)
  22. /*
  23. ** Union of all collectable objects
  24. */
  25. typedef union GCObject GCObject;
  26. /*
  27. ** Common Header for all collectable objects (in macro form, to be
  28. ** included in other objects)
  29. */
  30. #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
  31. /*
  32. ** Common header in struct form
  33. */
  34. typedef struct GCheader {
  35. CommonHeader;
  36. } GCheader;
  37. /*
  38. ** Union of all Lua values
  39. */
  40. #if defined( LUA_PACK_VALUE ) && defined( ELUA_ENDIAN_BIG )
  41. typedef union {
  42. struct {
  43. int _pad0;
  44. GCObject *gc;
  45. };
  46. struct {
  47. int _pad1;
  48. void *p;
  49. };
  50. lua_Number n;
  51. struct {
  52. int _pad2;
  53. int b;
  54. };
  55. } Value;
  56. #else // #if defined( LUA_PACK_VALUE ) && defined( ELUA_ENDIAN_BIG )
  57. typedef union {
  58. GCObject *gc;
  59. void *p;
  60. lua_Number n;
  61. int b;
  62. } Value;
  63. #endif // #if defined( LUA_PACK_VALUE ) && defined( ELUA_ENDIAN_BIG )
  64. /*
  65. ** Tagged Values
  66. */
  67. #ifndef LUA_PACK_VALUE
  68. #define TValuefields Value value; int tt
  69. #define LUA_TVALUE_NIL {NULL}, LUA_TNIL
  70. typedef struct lua_TValue {
  71. TValuefields;
  72. } TValue;
  73. #else // #ifndef LUA_PACK_VALUE
  74. #ifdef ELUA_ENDIAN_LITTLE
  75. #define TValuefields union { \
  76. struct { \
  77. int _pad0; \
  78. int tt_sig; \
  79. } _ts; \
  80. struct { \
  81. int _pad; \
  82. short tt; \
  83. short sig; \
  84. } _t; \
  85. Value value; \
  86. }
  87. #define LUA_TVALUE_NIL {0, add_sig(LUA_TNIL)}
  88. #else // #ifdef ELUA_ENDIAN_LITTLE
  89. #define TValuefields union { \
  90. struct { \
  91. int tt_sig; \
  92. int _pad0; \
  93. } _ts; \
  94. struct { \
  95. short sig; \
  96. short tt; \
  97. int _pad; \
  98. } _t; \
  99. Value value; \
  100. }
  101. #define LUA_TVALUE_NIL {add_sig(LUA_TNIL), 0}
  102. #endif // #ifdef ELUA_ENDIAN_LITTLE
  103. #define LUA_NOTNUMBER_SIG (-1)
  104. #define add_sig(tt) ( 0xffff0000 | (tt) )
  105. typedef TValuefields TValue;
  106. #endif // #ifndef LUA_PACK_VALUE
  107. /* Macros to test type */
  108. #ifndef LUA_PACK_VALUE
  109. #define ttisnil(o) (ttype(o) == LUA_TNIL)
  110. #define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
  111. #define ttisstring(o) (ttype(o) == LUA_TSTRING)
  112. #define ttistable(o) (ttype(o) == LUA_TTABLE)
  113. #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
  114. #define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
  115. #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
  116. #define ttisthread(o) (ttype(o) == LUA_TTHREAD)
  117. #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
  118. #define ttisrotable(o) (ttype(o) == LUA_TROTABLE)
  119. #define ttislightfunction(o) (ttype(o) == LUA_TLIGHTFUNCTION)
  120. #else // #ifndef LUA_PACK_VALUE
  121. #define ttisnil(o) (ttype_sig(o) == add_sig(LUA_TNIL))
  122. #define ttisnumber(o) ((o)->_t.sig != LUA_NOTNUMBER_SIG)
  123. #define ttisstring(o) (ttype_sig(o) == add_sig(LUA_TSTRING))
  124. #define ttistable(o) (ttype_sig(o) == add_sig(LUA_TTABLE))
  125. #define ttisfunction(o) (ttype_sig(o) == add_sig(LUA_TFUNCTION))
  126. #define ttisboolean(o) (ttype_sig(o) == add_sig(LUA_TBOOLEAN))
  127. #define ttisuserdata(o) (ttype_sig(o) == add_sig(LUA_TUSERDATA))
  128. #define ttisthread(o) (ttype_sig(o) == add_sig(LUA_TTHREAD))
  129. #define ttislightuserdata(o) (ttype_sig(o) == add_sig(LUA_TLIGHTUSERDATA))
  130. #define ttisrotable(o) (ttype_sig(o) == add_sig(LUA_TROTABLE))
  131. #define ttislightfunction(o) (ttype_sig(o) == add_sig(LUA_TLIGHTFUNCTION))
  132. #endif // #ifndef LUA_PACK_VALUE
  133. /* Macros to access values */
  134. #ifndef LUA_PACK_VALUE
  135. #define ttype(o) ((o)->tt)
  136. #else // #ifndef LUA_PACK_VALUE
  137. #define ttype(o) ((o)->_t.sig == LUA_NOTNUMBER_SIG ? (o)->_t.tt : LUA_TNUMBER)
  138. #define ttype_sig(o) ((o)->_ts.tt_sig)
  139. #endif // #ifndef LUA_PACK_VALUE
  140. #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
  141. #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
  142. #define rvalue(o) check_exp(ttisrotable(o), (o)->value.p)
  143. #define fvalue(o) check_exp(ttislightfunction(o), (o)->value.p)
  144. #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
  145. #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
  146. #define tsvalue(o) (&rawtsvalue(o)->tsv)
  147. #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
  148. #define uvalue(o) (&rawuvalue(o)->uv)
  149. #define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
  150. #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
  151. #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
  152. #define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
  153. #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
  154. /*
  155. ** for internal debug only
  156. */
  157. #ifndef LUA_PACK_VALUE
  158. #define checkconsistency(obj) \
  159. lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
  160. #define checkliveness(g,obj) \
  161. lua_assert(!iscollectable(obj) || \
  162. ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
  163. #else // #ifndef LUA_PACK_VALUE
  164. #define checkconsistency(obj) \
  165. lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch._t.tt))
  166. #define checkliveness(g,obj) \
  167. lua_assert(!iscollectable(obj) || \
  168. ((ttype(obj) == (obj)->value.gc->gch._t.tt) && !isdead(g, (obj)->value.gc)))
  169. #endif // #ifndef LUA_PACK_VALUE
  170. /* Macros to set values */
  171. #ifndef LUA_PACK_VALUE
  172. #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
  173. #define setnvalue(obj,x) \
  174. { lua_Number i_x = (x); TValue *i_o=(obj); i_o->value.n=i_x; i_o->tt=LUA_TNUMBER; }
  175. #define setpvalue(obj,x) \
  176. { void *i_x = (x); TValue *i_o=(obj); i_o->value.p=i_x; i_o->tt=LUA_TLIGHTUSERDATA; }
  177. #define setrvalue(obj,x) \
  178. { void *i_x = (x); TValue *i_o=(obj); i_o->value.p=i_x; i_o->tt=LUA_TROTABLE; }
  179. #define setfvalue(obj,x) \
  180. { void *i_x = (x); TValue *i_o=(obj); i_o->value.p=i_x; i_o->tt=LUA_TLIGHTFUNCTION; }
  181. #define setbvalue(obj,x) \
  182. { int i_x = (x); TValue *i_o=(obj); i_o->value.b=i_x; i_o->tt=LUA_TBOOLEAN; }
  183. #define setsvalue(L,obj,x) \
  184. { GCObject *i_x = cast(GCObject *, (x)); \
  185. TValue *i_o=(obj); \
  186. i_o->value.gc=i_x; i_o->tt=LUA_TSTRING; \
  187. checkliveness(G(L),i_o); }
  188. #define setuvalue(L,obj,x) \
  189. { GCObject *i_x = cast(GCObject *, (x)); \
  190. TValue *i_o=(obj); \
  191. i_o->value.gc=i_x; i_o->tt=LUA_TUSERDATA; \
  192. checkliveness(G(L),i_o); }
  193. #define setthvalue(L,obj,x) \
  194. { GCObject *i_x = cast(GCObject *, (x)); \
  195. TValue *i_o=(obj); \
  196. i_o->value.gc=i_x; i_o->tt=LUA_TTHREAD; \
  197. checkliveness(G(L),i_o); }
  198. #define setclvalue(L,obj,x) \
  199. { GCObject *i_x = cast(GCObject *, (x)); \
  200. TValue *i_o=(obj); \
  201. i_o->value.gc=i_x; i_o->tt=LUA_TFUNCTION; \
  202. checkliveness(G(L),i_o); }
  203. #define sethvalue(L,obj,x) \
  204. { GCObject *i_x = cast(GCObject *, (x)); \
  205. TValue *i_o=(obj); \
  206. i_o->value.gc=i_x; i_o->tt=LUA_TTABLE; \
  207. checkliveness(G(L),i_o); }
  208. #define setptvalue(L,obj,x) \
  209. { GCObject *i_x = cast(GCObject *, (x)); \
  210. TValue *i_o=(obj); \
  211. i_o->value.gc=i_x; i_o->tt=LUA_TPROTO; \
  212. checkliveness(G(L),i_o); }
  213. #define setobj(L,obj1,obj2) \
  214. { const TValue *o2=(obj2); TValue *o1=(obj1); \
  215. o1->value = o2->value; o1->tt=o2->tt; \
  216. checkliveness(G(L),o1); }
  217. #else // #ifndef LUA_PACK_VALUE
  218. #define setnilvalue(obj) ( ttype_sig(obj) = add_sig(LUA_TNIL) )
  219. #define setnvalue(obj,x) \
  220. { TValue *i_o=(obj); i_o->value.n=(x); }
  221. #define setpvalue(obj,x) \
  222. { TValue *i_o=(obj); i_o->value.p=(x); i_o->_ts.tt_sig=add_sig(LUA_TLIGHTUSERDATA);}
  223. #define setrvalue(obj,x) \
  224. { TValue *i_o=(obj); i_o->value.p=(x); i_o->_ts.tt_sig=add_sig(LUA_TROTABLE);}
  225. #define setfvalue(obj,x) \
  226. { TValue *i_o=(obj); i_o->value.p=(x); i_o->_ts.tt_sig=add_sig(LUA_TLIGHTFUNCTION);}
  227. #define setbvalue(obj,x) \
  228. { TValue *i_o=(obj); i_o->value.b=(x); i_o->_ts.tt_sig=add_sig(LUA_TBOOLEAN);}
  229. #define setsvalue(L,obj,x) \
  230. { TValue *i_o=(obj); \
  231. i_o->value.gc=cast(GCObject *, (x)); i_o->_ts.tt_sig=add_sig(LUA_TSTRING); \
  232. checkliveness(G(L),i_o); }
  233. #define setuvalue(L,obj,x) \
  234. { TValue *i_o=(obj); \
  235. i_o->value.gc=cast(GCObject *, (x)); i_o->_ts.tt_sig=add_sig(LUA_TUSERDATA); \
  236. checkliveness(G(L),i_o); }
  237. #define setthvalue(L,obj,x) \
  238. { TValue *i_o=(obj); \
  239. i_o->value.gc=cast(GCObject *, (x)); i_o->_ts.tt_sig=add_sig(LUA_TTHREAD); \
  240. checkliveness(G(L),i_o); }
  241. #define setclvalue(L,obj,x) \
  242. { TValue *i_o=(obj); \
  243. i_o->value.gc=cast(GCObject *, (x)); i_o->_ts.tt_sig=add_sig(LUA_TFUNCTION); \
  244. checkliveness(G(L),i_o); }
  245. #define sethvalue(L,obj,x) \
  246. { TValue *i_o=(obj); \
  247. i_o->value.gc=cast(GCObject *, (x)); i_o->_ts.tt_sig=add_sig(LUA_TTABLE); \
  248. checkliveness(G(L),i_o); }
  249. #define setptvalue(L,obj,x) \
  250. { TValue *i_o=(obj); \
  251. i_o->value.gc=cast(GCObject *, (x)); i_o->_ts.tt_sig=add_sig(LUA_TPROTO); \
  252. checkliveness(G(L),i_o); }
  253. #define setobj(L,obj1,obj2) \
  254. { const TValue *o2=(obj2); TValue *o1=(obj1); \
  255. o1->value = o2->value; \
  256. checkliveness(G(L),o1); }
  257. #endif // #ifndef LUA_PACK_VALUE
  258. /*
  259. ** different types of sets, according to destination
  260. */
  261. /* from stack to (same) stack */
  262. #define setobjs2s setobj
  263. /* to stack (not from same stack) */
  264. #define setobj2s setobj
  265. #define setsvalue2s setsvalue
  266. #define sethvalue2s sethvalue
  267. #define setptvalue2s setptvalue
  268. /* from table to same table */
  269. #define setobjt2t setobj
  270. /* to table */
  271. #define setobj2t setobj
  272. /* to new object */
  273. #define setobj2n setobj
  274. #define setsvalue2n setsvalue
  275. #ifndef LUA_PACK_VALUE
  276. #define setttype(obj, tt) (ttype(obj) = (tt))
  277. #else // #ifndef LUA_PACK_VALUE
  278. /* considering it used only in lgc to set LUA_TDEADKEY */
  279. /* we could define it this way */
  280. #define setttype(obj, _tt) ( ttype_sig(obj) = add_sig(_tt) )
  281. #endif // #ifndef LUA_PACK_VALUE
  282. #define iscollectable(o) (ttype(o) >= LUA_TSTRING)
  283. typedef TValue *StkId; /* index to stack elements */
  284. /*
  285. ** String headers for string table
  286. */
  287. typedef union TString {
  288. L_Umaxalign dummy; /* ensures maximum alignment for strings */
  289. struct {
  290. CommonHeader;
  291. unsigned int hash;
  292. size_t len;
  293. } tsv;
  294. } TString;
  295. #define getstr(ts) (((ts)->tsv.marked & READONLYMASK) ? cast(const char *, *(const char**)((ts) + 1)) : cast(const char *, (ts) + 1))
  296. #define svalue(o) getstr(rawtsvalue(o))
  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;
  304. } uv;
  305. } Udata;
  306. /*
  307. ** Function Prototypes
  308. */
  309. typedef struct Proto {
  310. CommonHeader;
  311. TValue *k; /* constants used by the function */
  312. Instruction *code;
  313. struct Proto **p; /* functions defined inside the function */
  314. int *lineinfo; /* map from opcodes to source lines */
  315. struct LocVar *locvars; /* information about local variables */
  316. TString **upvalues; /* upvalue names */
  317. TString *source;
  318. int sizeupvalues;
  319. int sizek; /* size of `k' */
  320. int sizecode;
  321. int sizelineinfo;
  322. int sizep; /* size of `p' */
  323. int sizelocvars;
  324. int linedefined;
  325. int lastlinedefined;
  326. GCObject *gclist;
  327. lu_byte nups; /* number of upvalues */
  328. lu_byte numparams;
  329. lu_byte is_vararg;
  330. lu_byte maxstacksize;
  331. } Proto;
  332. /* masks for new-style vararg */
  333. #define VARARG_HASARG 1
  334. #define VARARG_ISVARARG 2
  335. #define VARARG_NEEDSARG 4
  336. typedef struct LocVar {
  337. TString *varname;
  338. int startpc; /* first point where variable is active */
  339. int endpc; /* first point where variable is dead */
  340. } LocVar;
  341. /*
  342. ** Upvalues
  343. */
  344. typedef struct UpVal {
  345. CommonHeader;
  346. TValue *v; /* points to stack or to its own value */
  347. union {
  348. TValue value; /* the value (when closed) */
  349. struct { /* double linked list (when open) */
  350. struct UpVal *prev;
  351. struct UpVal *next;
  352. } l;
  353. } u;
  354. } UpVal;
  355. /*
  356. ** Closures
  357. */
  358. #define ClosureHeader \
  359. CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
  360. struct Table *env
  361. typedef struct CClosure {
  362. ClosureHeader;
  363. lua_CFunction f;
  364. TValue upvalue[1];
  365. } CClosure;
  366. typedef struct LClosure {
  367. ClosureHeader;
  368. struct Proto *p;
  369. UpVal *upvals[1];
  370. } LClosure;
  371. typedef union Closure {
  372. CClosure c;
  373. LClosure l;
  374. } Closure;
  375. #define iscfunction(o) ((ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)||(ttype(o)==LUA_TLIGHTFUNCTION))
  376. #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
  377. /*
  378. ** Tables
  379. */
  380. #ifndef LUA_PACK_VALUE
  381. typedef union TKey {
  382. struct {
  383. TValuefields;
  384. struct Node *next; /* for chaining */
  385. } nk;
  386. TValue tvk;
  387. } TKey;
  388. #define LUA_TKEY_NIL {LUA_TVALUE_NIL, NULL}
  389. #else // #ifndef LUA_PACK_VALUE
  390. typedef struct TKey {
  391. TValue tvk;
  392. struct {
  393. struct Node *next; /* for chaining */
  394. } nk;
  395. } TKey;
  396. #define LUA_TKEY_NIL {LUA_TVALUE_NIL}, {NULL}
  397. #endif // #ifndef LUA_PACK_VALUE
  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. #define luaO_nilobject (&luaO_nilobject_)
  421. LUAI_DATA const TValue luaO_nilobject_;
  422. #define ceillog2(x) (luaO_log2((x)-1) + 1)
  423. LUAI_FUNC int luaO_log2 (unsigned int x);
  424. LUAI_FUNC int luaO_int2fb (unsigned int x);
  425. LUAI_FUNC int luaO_fb2int (int x);
  426. LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
  427. LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
  428. LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
  429. va_list argp);
  430. LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  431. LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
  432. #endif