lobject.h 6.2 KB

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