lobject.h 5.7 KB

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