lobject.h 6.1 KB

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