lobject.h 14 KB

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