lobject.h 5.6 KB

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