lobject.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. ** $Id: lobject.h,v 1.114 2001/10/02 16:45:03 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 "llimits.h"
  9. #include "lua.h"
  10. #ifndef lua_assert
  11. #define lua_assert(c) /* empty */
  12. #endif
  13. #ifndef UNUSED
  14. #define UNUSED(x) ((void)(x)) /* to avoid warnings */
  15. #endif
  16. #ifndef cast
  17. #define cast(t, exp) ((t)(exp))
  18. #endif
  19. /* tags for values visible from Lua */
  20. #define NUM_TAGS 6
  21. /* extra tag: used locally when moving an upvalue from the stack to the heap */
  22. #define LUA_TUPVAL 6
  23. typedef union {
  24. union TString *ts;
  25. union Udata *u;
  26. union Closure *cl;
  27. struct Table *h;
  28. struct UpVal *v;
  29. lua_Number n; /* LUA_TNUMBER */
  30. } Value;
  31. typedef struct lua_TObject {
  32. int tt;
  33. Value value;
  34. } TObject;
  35. /* Macros to access values */
  36. #define ttype(o) ((o)->tt)
  37. #define nvalue(o) ((o)->value.n)
  38. #define tsvalue(o) ((o)->value.ts)
  39. #define uvalue(o) ((o)->value.u)
  40. #define clvalue(o) ((o)->value.cl)
  41. #define hvalue(o) ((o)->value.h)
  42. #define vvalue(o) ((o)->value.v)
  43. /* Macros to set values */
  44. #define setnvalue(obj,x) \
  45. { TObject *_o=(obj); _o->tt=LUA_TNUMBER; _o->value.n=(x); }
  46. #define chgnvalue(obj,x) ((obj)->value.n=(x))
  47. #define setsvalue(obj,x) \
  48. { TObject *_o=(obj); _o->tt=LUA_TSTRING; _o->value.ts=(x); }
  49. #define setuvalue(obj,x) \
  50. { TObject *_o=(obj); _o->tt=LUA_TUSERDATA; _o->value.u=(x); }
  51. #define setclvalue(obj,x) \
  52. { TObject *_o=(obj); _o->tt=LUA_TFUNCTION; _o->value.cl=(x); }
  53. #define sethvalue(obj,x) \
  54. { TObject *_o=(obj); _o->tt=LUA_TTABLE; _o->value.h=(x); }
  55. #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
  56. #define setupvalue(obj,x) \
  57. { TObject *_o=(obj); _o->tt=LUA_TUPVAL; _o->value.v=(x); }
  58. #define setobj(obj1,obj2) \
  59. { TObject *o1=(obj1); const TObject *o2=(obj2); \
  60. o1->tt=o2->tt; o1->value = o2->value; }
  61. #define setttype(obj, tt) (ttype(obj) = (tt))
  62. typedef TObject *StkId; /* index to stack elements */
  63. /*
  64. ** String headers for string table
  65. */
  66. typedef union TString {
  67. union L_Umaxalign dummy; /* ensures maximum alignment for strings */
  68. struct {
  69. lu_hash hash;
  70. size_t len;
  71. int marked;
  72. union TString *nexthash; /* chain for hash table */
  73. } tsv;
  74. } TString;
  75. #define getstr(ts) cast(l_char *, (ts) + 1)
  76. #define svalue(o) getstr(tsvalue(o))
  77. typedef union Udata {
  78. union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
  79. struct {
  80. int tag; /* negative means `marked' (only during GC) */
  81. void *value;
  82. size_t len;
  83. union Udata *next; /* chain for list of all udata */
  84. } uv;
  85. } Udata;
  86. #define switchudatamark(u) ((u)->uv.tag = (-((u)->uv.tag+1)))
  87. #define ismarkedudata(u) ((u)->uv.tag < 0)
  88. /*
  89. ** Function Prototypes
  90. */
  91. typedef struct Proto {
  92. TObject *k; /* constants used by the function */
  93. int sizek; /* size of `k' */
  94. struct Proto **p; /* functions defined inside the function */
  95. int sizep; /* size of `p' */
  96. Instruction *code;
  97. int sizecode;
  98. short nupvalues;
  99. short numparams;
  100. short is_vararg;
  101. short maxstacksize;
  102. short marked;
  103. struct Proto *next;
  104. /* debug information */
  105. int *lineinfo; /* map from opcodes to source lines */
  106. int sizelineinfo; /* size of `lineinfo' */
  107. struct LocVar *locvars; /* information about local variables */
  108. int sizelocvars;
  109. int lineDefined;
  110. TString *source;
  111. } Proto;
  112. typedef struct LocVar {
  113. TString *varname;
  114. int startpc; /* first point where variable is active */
  115. int endpc; /* first point where variable is dead */
  116. } LocVar;
  117. /*
  118. ** Upvalues in the heap
  119. */
  120. typedef struct UpVal {
  121. TObject val;
  122. struct UpVal *next;
  123. int marked;
  124. } UpVal;
  125. /*
  126. ** Closures
  127. */
  128. typedef struct CClosure {
  129. lu_byte isC; /* 0 for Lua functions, 1 for C functions */
  130. lu_byte nupvalues;
  131. lu_byte marked;
  132. union Closure *next;
  133. lua_CFunction f;
  134. TObject upvalue[1];
  135. } CClosure;
  136. typedef struct LClosureEntry {
  137. TObject *val;
  138. UpVal *heap; /* NULL when upvalue is still in the stack */
  139. } LClosureEntry;
  140. typedef struct LClosure {
  141. lu_byte isC;
  142. lu_byte nupvalues;
  143. lu_byte marked;
  144. union Closure *next; /* first four fields must be equal to CClosure!! */
  145. struct Proto *p;
  146. LClosureEntry upvals[1];
  147. } LClosure;
  148. typedef union Closure {
  149. CClosure c;
  150. LClosure l;
  151. } Closure;
  152. #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
  153. /*
  154. ** Tables
  155. */
  156. typedef struct Node {
  157. struct Node *next; /* for chaining */
  158. TObject key;
  159. TObject val;
  160. } Node;
  161. typedef struct Table {
  162. TObject *array; /* array part */
  163. Node *node;
  164. int htag;
  165. int sizearray; /* size of `array' array */
  166. lu_byte lsizenode; /* log2 of size of `node' array */
  167. lu_byte weakmode;
  168. Node *firstfree; /* this position is free; all positions after it are full */
  169. struct Table *next;
  170. struct Table *mark; /* marked tables (point to itself when not marked) */
  171. } Table;
  172. /* unmarked tables are represented by pointing `mark' to themselves */
  173. #define ismarked(x) ((x)->mark != (x))
  174. /*
  175. ** `module' operation for hashing (size is always a power of 2)
  176. */
  177. #define lmod(s,size) (cast(int, (s) & ((size)-1)))
  178. #define twoto(x) (1<<(x))
  179. #define sizenode(t) (twoto((t)->lsizenode))
  180. #define sizearray(t) ((t)->sizearray)
  181. /*
  182. ** informations about a call (for debugging)
  183. */
  184. typedef struct CallInfo {
  185. struct CallInfo *prev; /* linked list */
  186. StkId base; /* base for called function */
  187. const Instruction **pc; /* current pc of called function */
  188. int lastpc; /* last pc traced */
  189. int line; /* current line */
  190. int refi; /* current index in `lineinfo' */
  191. } CallInfo;
  192. #define ci_func(ci) (clvalue((ci)->base - 1))
  193. extern const TObject luaO_nilobject;
  194. int luaO_log2 (unsigned int x);
  195. #define luaO_openspace(L,n,t) cast(t *, luaO_openspaceaux(L,(n)*sizeof(t)))
  196. void *luaO_openspaceaux (lua_State *L, size_t n);
  197. int luaO_equalObj (const TObject *t1, const TObject *t2);
  198. int luaO_str2d (const l_char *s, lua_Number *result);
  199. void luaO_verror (lua_State *L, const l_char *fmt, ...);
  200. void luaO_chunkid (l_char *out, const l_char *source, int len);
  201. #endif