lobject.h 14 KB

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