lobject.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. ** $Id: lobject.h,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
  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. typedef union {
  22. union TString *ts;
  23. union Udata *u;
  24. union Closure *cl;
  25. struct Table *h;
  26. struct lua_TObject *v;
  27. lua_Number n;
  28. int b;
  29. } Value;
  30. typedef struct lua_TObject {
  31. int tt;
  32. Value value;
  33. } TObject;
  34. /* Macros to access values */
  35. #define ttype(o) ((o)->tt)
  36. #define nvalue(o) ((o)->value.n)
  37. #define tsvalue(o) ((o)->value.ts)
  38. #define uvalue(o) ((o)->value.u)
  39. #define clvalue(o) ((o)->value.cl)
  40. #define hvalue(o) ((o)->value.h)
  41. #define vvalue(o) ((o)->value.v)
  42. #define bvalue(o) ((o)->value.b)
  43. #define l_isfalse(o) (ttype(o) == LUA_TNIL || \
  44. (ttype(o) == LUA_TBOOLEAN && bvalue(o) == 0))
  45. /* Macros to set values */
  46. #define setnvalue(obj,x) \
  47. { TObject *_o=(obj); _o->tt=LUA_TNUMBER; _o->value.n=(x); }
  48. #define chgnvalue(obj,x) ((obj)->value.n=(x))
  49. #define setbvalue(obj,x) \
  50. { TObject *_o=(obj); _o->tt=LUA_TBOOLEAN; _o->value.b=(x); }
  51. #define setsvalue(obj,x) \
  52. { TObject *_o=(obj); _o->tt=LUA_TSTRING; _o->value.ts=(x); }
  53. #define setuvalue(obj,x) \
  54. { TObject *_o=(obj); _o->tt=LUA_TUSERDATA; _o->value.u=(x); }
  55. #define setclvalue(obj,x) \
  56. { TObject *_o=(obj); _o->tt=LUA_TFUNCTION; _o->value.cl=(x); }
  57. #define sethvalue(obj,x) \
  58. { TObject *_o=(obj); _o->tt=LUA_TTABLE; _o->value.h=(x); }
  59. #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
  60. #define setupvalue(obj,x,t) \
  61. { TObject *_o=(obj); _o->tt=(t); _o->value.v=(x); }
  62. #define setobj(obj1,obj2) \
  63. { TObject *o1=(obj1); const TObject *o2=(obj2); \
  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. int marked;
  76. union TString *nexthash; /* chain for hash table */
  77. } tsv;
  78. } TString;
  79. #define getstr(ts) cast(char *, (ts) + 1)
  80. #define svalue(o) getstr(tsvalue(o))
  81. typedef union Udata {
  82. union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
  83. struct {
  84. struct Table *eventtable;
  85. void *value;
  86. size_t len; /* least bit reserved for gc mark */
  87. union Udata *next; /* chain for list of all udata */
  88. } uv;
  89. } Udata;
  90. /*
  91. ** Function Prototypes
  92. */
  93. typedef struct Proto {
  94. TObject *k; /* constants used by the function */
  95. int sizek; /* size of `k' */
  96. struct Proto **p; /* functions defined inside the function */
  97. int sizep; /* size of `p' */
  98. Instruction *code;
  99. int sizecode;
  100. short nupvalues;
  101. short numparams;
  102. short is_vararg;
  103. short maxstacksize;
  104. short marked;
  105. struct Proto *next;
  106. /* debug information */
  107. int *lineinfo; /* map from opcodes to source lines */
  108. int sizelineinfo; /* size of `lineinfo' */
  109. struct LocVar *locvars; /* information about local variables */
  110. int sizelocvars;
  111. int lineDefined;
  112. TString *source;
  113. } Proto;
  114. typedef struct LocVar {
  115. TString *varname;
  116. int startpc; /* first point where variable is active */
  117. int endpc; /* first point where variable is dead */
  118. } LocVar;
  119. /*
  120. ** Upvalues
  121. */
  122. typedef struct UpVal {
  123. TObject *v; /* points to stack or to its own value */
  124. struct UpVal *next;
  125. TObject value; /* the value (when closed) */
  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. UpVal *upvals[1];
  145. } LClosure;
  146. typedef union Closure {
  147. CClosure c;
  148. LClosure l;
  149. } Closure;
  150. #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
  151. /*
  152. ** Tables
  153. */
  154. typedef struct Node {
  155. TObject _key;
  156. TObject _val;
  157. struct Node *next; /* for chaining */
  158. } Node;
  159. typedef struct Table {
  160. struct Table *eventtable;
  161. TObject *array; /* array part */
  162. Node *node;
  163. int sizearray; /* size of `array' array */
  164. lu_byte lsizenode; /* log2 of size of `node' array */
  165. unsigned short flags; /* 1<<p means tagmethod(p) is not present */
  166. Node *firstfree; /* this position is free; all positions after it are full */
  167. struct Table *next;
  168. struct Table *mark; /* marked tables (point to itself when not marked) */
  169. } Table;
  170. /* unmarked tables are represented by pointing `mark' to themselves */
  171. #define ismarked(x) ((x)->mark != (x))
  172. /*
  173. ** `module' operation for hashing (size is always a power of 2)
  174. */
  175. #define lmod(s,size) (cast(int, (s) & ((size)-1)))
  176. #define twoto(x) (1<<(x))
  177. #define sizenode(t) (twoto((t)->lsizenode))
  178. #define sizearray(t) ((t)->sizearray)
  179. extern const TObject luaO_nilobject;
  180. int luaO_log2 (unsigned int x);
  181. #define luaO_openspace(L,n,t) cast(t *, luaO_openspaceaux(L,(n)*sizeof(t)))
  182. void *luaO_openspaceaux (lua_State *L, size_t n);
  183. int luaO_equalObj (const TObject *t1, const TObject *t2);
  184. int luaO_str2d (const char *s, lua_Number *result);
  185. void luaO_verror (lua_State *L, const char *fmt, ...);
  186. void luaO_chunkid (char *out, const char *source, int len);
  187. #endif