lobject.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. ** $Id: lobject.h,v 2.6 2004/10/06 18:34:16 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 NUM_TAGS LUA_TTHREAD
  13. /*
  14. ** Extra tags for non-values
  15. */
  16. #define LUA_TPROTO (NUM_TAGS+1)
  17. #define LUA_TUPVAL (NUM_TAGS+2)
  18. /*
  19. ** Union of all collectable objects
  20. */
  21. typedef union GCObject GCObject;
  22. /*
  23. ** Common Header for all collectable objects (in macro form, to be
  24. ** included in other objects)
  25. */
  26. #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
  27. /*
  28. ** Common header in struct form
  29. */
  30. typedef struct GCheader {
  31. CommonHeader;
  32. } GCheader;
  33. /*
  34. ** Union of all Lua values
  35. */
  36. typedef union {
  37. GCObject *gc;
  38. void *p;
  39. lua_Number n;
  40. int b;
  41. } Value;
  42. /*
  43. ** Tagged Values
  44. */
  45. #define TValuefields Value value; int tt
  46. typedef struct lua_TValue {
  47. TValuefields;
  48. } TValue;
  49. /* Macros to test type */
  50. #define ttisnil(o) (ttype(o) == LUA_TNIL)
  51. #define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
  52. #define ttisstring(o) (ttype(o) == LUA_TSTRING)
  53. #define ttistable(o) (ttype(o) == LUA_TTABLE)
  54. #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
  55. #define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
  56. #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
  57. #define ttisthread(o) (ttype(o) == LUA_TTHREAD)
  58. #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
  59. /* Macros to access values */
  60. #define ttype(o) ((o)->tt)
  61. #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
  62. #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
  63. #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
  64. #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
  65. #define tsvalue(o) (&rawtsvalue(o)->tsv)
  66. #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
  67. #define uvalue(o) (&rawuvalue(o)->uv)
  68. #define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
  69. #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
  70. #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
  71. #define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
  72. #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
  73. /*
  74. ** for internal debug only
  75. */
  76. #define checkconsistency(obj) \
  77. lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
  78. #define checkliveness(g,obj) \
  79. lua_assert(!iscollectable(obj) || \
  80. ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
  81. /* Macros to set values */
  82. #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
  83. #define setnvalue(obj,x) \
  84. { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
  85. #define chgnvalue(obj,x) \
  86. check_exp(ttype(obj)==LUA_TNUMBER, (obj)->value.n=(x))
  87. #define setpvalue(obj,x) \
  88. { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
  89. #define setbvalue(obj,x) \
  90. { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }
  91. #define setsvalue(L,obj,x) \
  92. { TValue *i_o=(obj); \
  93. i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \
  94. checkliveness(G(L),i_o); }
  95. #define setuvalue(L,obj,x) \
  96. { TValue *i_o=(obj); \
  97. i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \
  98. checkliveness(G(L),i_o); }
  99. #define setthvalue(L,obj,x) \
  100. { TValue *i_o=(obj); \
  101. i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \
  102. checkliveness(G(L),i_o); }
  103. #define setclvalue(L,obj,x) \
  104. { TValue *i_o=(obj); \
  105. i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \
  106. checkliveness(G(L),i_o); }
  107. #define sethvalue(L,obj,x) \
  108. { TValue *i_o=(obj); \
  109. i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \
  110. checkliveness(G(L),i_o); }
  111. #define setptvalue(L,obj,x) \
  112. { TValue *i_o=(obj); \
  113. i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
  114. checkliveness(G(L),i_o); }
  115. #define setobj(L,obj1,obj2) \
  116. { const TValue *o2=(obj2); TValue *o1=(obj1); \
  117. o1->value = o2->value; o1->tt=o2->tt; \
  118. checkliveness(G(L),o1); }
  119. /*
  120. ** different types of sets, according to destination
  121. */
  122. /* from stack to (same) stack */
  123. #define setobjs2s setobj
  124. /* to stack (not from same stack) */
  125. #define setobj2s setobj
  126. #define setsvalue2s setsvalue
  127. #define sethvalue2s sethvalue
  128. #define setptvalue2s setptvalue
  129. /* from table to same table */
  130. #define setobjt2t setobj
  131. /* to table */
  132. #define setobj2t setobj
  133. /* to new object */
  134. #define setobj2n setobj
  135. #define setsvalue2n setsvalue
  136. #define setttype(obj, tt) (ttype(obj) = (tt))
  137. #define iscollectable(o) (ttype(o) >= LUA_TSTRING)
  138. typedef TValue *StkId; /* index to stack elements */
  139. /*
  140. ** String headers for string table
  141. */
  142. typedef union TString {
  143. L_Umaxalign dummy; /* ensures maximum alignment for strings */
  144. struct {
  145. CommonHeader;
  146. lu_byte reserved;
  147. unsigned int hash;
  148. size_t len;
  149. } tsv;
  150. } TString;
  151. #define getstr(ts) cast(const char *, (ts) + 1)
  152. #define svalue(o) getstr(tsvalue(o))
  153. typedef union Udata {
  154. L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
  155. struct {
  156. CommonHeader;
  157. struct Table *metatable;
  158. size_t len;
  159. } uv;
  160. } Udata;
  161. /*
  162. ** Function Prototypes
  163. */
  164. typedef struct Proto {
  165. CommonHeader;
  166. TValue *k; /* constants used by the function */
  167. Instruction *code;
  168. struct Proto **p; /* functions defined inside the function */
  169. int *lineinfo; /* map from opcodes to source lines */
  170. struct LocVar *locvars; /* information about local variables */
  171. TString **upvalues; /* upvalue names */
  172. TString *source;
  173. int sizeupvalues;
  174. int sizek; /* size of `k' */
  175. int sizecode;
  176. int sizelineinfo;
  177. int sizep; /* size of `p' */
  178. int sizelocvars;
  179. int lineDefined;
  180. GCObject *gclist;
  181. lu_byte nups; /* number of upvalues */
  182. lu_byte numparams;
  183. lu_byte is_vararg;
  184. lu_byte maxstacksize;
  185. } Proto;
  186. /* mask for new-style vararg */
  187. #define NEWSTYLEVARARG 2
  188. typedef struct LocVar {
  189. TString *varname;
  190. int startpc; /* first point where variable is active */
  191. int endpc; /* first point where variable is dead */
  192. } LocVar;
  193. /*
  194. ** Upvalues
  195. */
  196. typedef struct UpVal {
  197. CommonHeader;
  198. TValue *v; /* points to stack or to its own value */
  199. TValue value; /* the value (when closed) */
  200. } UpVal;
  201. /*
  202. ** Closures
  203. */
  204. #define ClosureHeader \
  205. CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist
  206. typedef struct CClosure {
  207. ClosureHeader;
  208. lua_CFunction f;
  209. TValue upvalue[1];
  210. } CClosure;
  211. typedef struct LClosure {
  212. ClosureHeader;
  213. struct Proto *p;
  214. TValue g; /* global table for this closure */
  215. UpVal *upvals[1];
  216. } LClosure;
  217. typedef union Closure {
  218. CClosure c;
  219. LClosure l;
  220. } Closure;
  221. #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
  222. #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
  223. /*
  224. ** Tables
  225. */
  226. typedef struct TKey {
  227. TValuefields;
  228. struct Node *next; /* for chaining */
  229. } TKey;
  230. typedef struct Node {
  231. TValue i_val;
  232. TKey i_key;
  233. } Node;
  234. typedef struct Table {
  235. CommonHeader;
  236. lu_byte flags; /* 1<<p means tagmethod(p) is not present */
  237. lu_byte lsizenode; /* log2 of size of `node' array */
  238. struct Table *metatable;
  239. TValue *array; /* array part */
  240. Node *node;
  241. Node *firstfree; /* this position is free; all positions after it are full */
  242. GCObject *gclist;
  243. int sizearray; /* size of `array' array */
  244. } Table;
  245. /*
  246. ** `module' operation for hashing (size is always a power of 2)
  247. */
  248. #define lmod(s,size) \
  249. check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1))))
  250. #define twoto(x) (1<<(x))
  251. #define sizenode(t) (twoto((t)->lsizenode))
  252. extern const TValue luaO_nilobject;
  253. int luaO_log2 (unsigned int x);
  254. int luaO_int2fb (unsigned int x);
  255. int luaO_fb2int (int x);
  256. int luaO_rawequalObj (const TValue *t1, const TValue *t2);
  257. int luaO_str2d (const char *s, lua_Number *result);
  258. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp);
  259. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  260. void luaO_chunkid (char *out, const char *source, int len);
  261. #endif