lobject.h 7.7 KB

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