lobject.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. ** $Id: lobject.h,v 1.149 2002/10/22 17:18:28 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 "llimits.h"
  9. #include "lua.h"
  10. /* tags for values visible from Lua */
  11. #define NUM_TAGS LUA_TTHREAD
  12. /*
  13. ** Extra tags for non-values
  14. */
  15. #define LUA_TPROTO (NUM_TAGS+1)
  16. #define LUA_TUPVAL (NUM_TAGS+2)
  17. /*
  18. ** Union of all collectable objects
  19. */
  20. typedef union GCObject GCObject;
  21. /*
  22. ** Common header for all collectable objects
  23. */
  24. typedef struct GCheader {
  25. GCObject *next; /* pointer to next object */
  26. lu_byte tt; /* object type */
  27. lu_byte marked; /* GC informations */
  28. } GCheader;
  29. /*
  30. ** common header in macro form, to be included in other objects
  31. */
  32. #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
  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. ** Lua values (or `tagged objects')
  44. */
  45. typedef struct lua_TObject {
  46. int tt;
  47. Value value;
  48. } TObject;
  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 tsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
  65. #define uvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
  66. #define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
  67. #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
  68. #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
  69. #define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
  70. #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
  71. /* Macros to set values */
  72. #define setnvalue(obj,x) \
  73. { TObject *i_o=(obj); i_o->tt=LUA_TNUMBER; i_o->value.n=(x); }
  74. #define chgnvalue(obj,x) \
  75. check_exp(ttype(obj)==LUA_TNUMBER, (obj)->value.n=(x))
  76. #define setpvalue(obj,x) \
  77. { TObject *i_o=(obj); i_o->tt=LUA_TLIGHTUSERDATA; i_o->value.p=(x); }
  78. #define setbvalue(obj,x) \
  79. { TObject *i_o=(obj); i_o->tt=LUA_TBOOLEAN; i_o->value.b=(x); }
  80. #define setsvalue(obj,x) \
  81. { TObject *i_o=(obj); i_o->tt=LUA_TSTRING; \
  82. i_o->value.gc=cast(GCObject *, (x)); \
  83. lua_assert(i_o->value.gc->gch.tt == LUA_TSTRING); }
  84. #define setuvalue(obj,x) \
  85. { TObject *i_o=(obj); i_o->tt=LUA_TUSERDATA; \
  86. i_o->value.gc=cast(GCObject *, (x)); \
  87. lua_assert(i_o->value.gc->gch.tt == LUA_TUSERDATA); }
  88. #define setthvalue(obj,x) \
  89. { TObject *i_o=(obj); i_o->tt=LUA_TTHREAD; \
  90. i_o->value.gc=cast(GCObject *, (x)); \
  91. lua_assert(i_o->value.gc->gch.tt == LUA_TTHREAD); }
  92. #define setclvalue(obj,x) \
  93. { TObject *i_o=(obj); i_o->tt=LUA_TFUNCTION; \
  94. i_o->value.gc=cast(GCObject *, (x)); \
  95. lua_assert(i_o->value.gc->gch.tt == LUA_TFUNCTION); }
  96. #define sethvalue(obj,x) \
  97. { TObject *i_o=(obj); i_o->tt=LUA_TTABLE; \
  98. i_o->value.gc=cast(GCObject *, (x)); \
  99. lua_assert(i_o->value.gc->gch.tt == LUA_TTABLE); }
  100. #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
  101. /*
  102. ** for internal debug only
  103. */
  104. #define checkconsistency(obj) \
  105. lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
  106. #define setobj(obj1,obj2) \
  107. { const TObject *o2=(obj2); TObject *o1=(obj1); \
  108. checkconsistency(o2); \
  109. o1->tt=o2->tt; o1->value = o2->value; }
  110. #define setttype(obj, tt) (ttype(obj) = (tt))
  111. #define iscollectable(o) (ttype(o) >= LUA_TSTRING)
  112. typedef TObject *StkId; /* index to stack elements */
  113. /*
  114. ** String headers for string table
  115. */
  116. typedef union TString {
  117. L_Umaxalign dummy; /* ensures maximum alignment for strings */
  118. struct {
  119. CommonHeader;
  120. lu_byte reserved;
  121. lu_hash hash;
  122. size_t len;
  123. } tsv;
  124. } TString;
  125. #define getstr(ts) cast(const char *, (ts) + 1)
  126. #define svalue(o) getstr(tsvalue(o))
  127. typedef union Udata {
  128. L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
  129. struct {
  130. CommonHeader;
  131. struct Table *metatable;
  132. size_t len;
  133. } uv;
  134. } Udata;
  135. /*
  136. ** Function Prototypes
  137. */
  138. typedef struct Proto {
  139. CommonHeader;
  140. TObject *k; /* constants used by the function */
  141. Instruction *code;
  142. struct Proto **p; /* functions defined inside the function */
  143. int *lineinfo; /* map from opcodes to source lines */
  144. struct LocVar *locvars; /* information about local variables */
  145. TString *source;
  146. int sizek; /* size of `k' */
  147. int sizecode;
  148. int sizelineinfo;
  149. int sizep; /* size of `p' */
  150. int sizelocvars;
  151. int lineDefined;
  152. lu_byte nupvalues;
  153. lu_byte numparams;
  154. lu_byte is_vararg;
  155. lu_byte maxstacksize;
  156. } Proto;
  157. typedef struct LocVar {
  158. TString *varname;
  159. int startpc; /* first point where variable is active */
  160. int endpc; /* first point where variable is dead */
  161. } LocVar;
  162. /*
  163. ** Upvalues
  164. */
  165. typedef struct UpVal {
  166. CommonHeader;
  167. TObject *v; /* points to stack or to its own value */
  168. TObject value; /* the value (when closed) */
  169. } UpVal;
  170. /*
  171. ** Closures
  172. */
  173. typedef struct CClosure {
  174. CommonHeader;
  175. lu_byte isC; /* 0 for Lua functions, 1 for C functions */
  176. lu_byte nupvalues;
  177. lua_CFunction f;
  178. TObject upvalue[1];
  179. } CClosure;
  180. typedef struct LClosure {
  181. CommonHeader;
  182. lu_byte isC;
  183. lu_byte nupvalues; /* first five fields must be equal to CClosure!! */
  184. struct Proto *p;
  185. TObject g; /* global table for this closure */
  186. UpVal *upvals[1];
  187. } LClosure;
  188. typedef union Closure {
  189. CClosure c;
  190. LClosure l;
  191. } Closure;
  192. #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
  193. #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
  194. /*
  195. ** Tables
  196. */
  197. typedef struct Node {
  198. TObject i_key;
  199. TObject i_val;
  200. struct Node *next; /* for chaining */
  201. } Node;
  202. typedef struct Table {
  203. CommonHeader;
  204. lu_byte flags; /* 1<<p means tagmethod(p) is not present */
  205. lu_byte mode;
  206. lu_byte lsizenode; /* log2 of size of `node' array */
  207. struct Table *metatable;
  208. TObject *array; /* array part */
  209. Node *node;
  210. Node *firstfree; /* this position is free; all positions after it are full */
  211. struct Table *gclist;
  212. int sizearray; /* size of `array' array */
  213. } Table;
  214. /* bit masks for `mode' */
  215. #define WEAKKEY 1
  216. #define WEAKVALUE 2
  217. /*
  218. ** `module' operation for hashing (size is always a power of 2)
  219. */
  220. #define lmod(s,size) \
  221. check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1))))
  222. #define twoto(x) (1<<(x))
  223. #define sizenode(t) (twoto((t)->lsizenode))
  224. #define sizearray(t) ((t)->sizearray)
  225. extern const TObject luaO_nilobject;
  226. int luaO_log2 (unsigned int x);
  227. int luaO_rawequalObj (const TObject *t1, const TObject *t2);
  228. int luaO_str2d (const char *s, lua_Number *result);
  229. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp);
  230. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  231. void luaO_chunkid (char *out, const char *source, int len);
  232. #endif