lobject.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. ** $Id: lobject.h,v 1.141 2002/08/05 14:08:02 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 test type */
  26. #define ttisnil(o) (ttype(o) == LUA_TNIL)
  27. #define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
  28. #define ttisstring(o) (ttype(o) == LUA_TSTRING)
  29. #define ttistable(o) (ttype(o) == LUA_TTABLE)
  30. #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
  31. #define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
  32. #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
  33. #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
  34. /* Macros to access values */
  35. #define ttype(o) ((o)->tt)
  36. #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
  37. #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
  38. #define tsvalue(o) check_exp(ttisstring(o), (o)->value.ts)
  39. #define uvalue(o) check_exp(ttisuserdata(o), (o)->value.u)
  40. #define clvalue(o) check_exp(ttisfunction(o), (o)->value.cl)
  41. #define hvalue(o) check_exp(ttistable(o), (o)->value.h)
  42. #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
  43. #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
  44. /* Macros to set values */
  45. #define setnvalue(obj,x) \
  46. { TObject *i_o=(obj); i_o->tt=LUA_TNUMBER; i_o->value.n=(x); }
  47. #define chgnvalue(obj,x) \
  48. check_exp(ttype(obj)==LUA_TNUMBER, (obj)->value.n=(x))
  49. #define setpvalue(obj,x) \
  50. { TObject *i_o=(obj); i_o->tt=LUA_TLIGHTUSERDATA; i_o->value.p=(x); }
  51. #define setbvalue(obj,x) \
  52. { TObject *i_o=(obj); i_o->tt=LUA_TBOOLEAN; i_o->value.b=(x); }
  53. #define setsvalue(obj,x) \
  54. { TObject *i_o=(obj); i_o->tt=LUA_TSTRING; i_o->value.ts=(x); }
  55. #define setuvalue(obj,x) \
  56. { TObject *i_o=(obj); i_o->tt=LUA_TUSERDATA; i_o->value.u=(x); }
  57. #define setclvalue(obj,x) \
  58. { TObject *i_o=(obj); i_o->tt=LUA_TFUNCTION; i_o->value.cl=(x); }
  59. #define sethvalue(obj,x) \
  60. { TObject *i_o=(obj); i_o->tt=LUA_TTABLE; i_o->value.h=(x); }
  61. #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
  62. #define setobj(obj1,obj2) \
  63. { const TObject *o2=(obj2); TObject *o1=(obj1); \
  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(const 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 *metatable;
  85. union Udata *next; /* chain for list of all udata */
  86. size_t len; /* least 2 bits reserved for gc mark */
  87. } uv;
  88. } Udata;
  89. /*
  90. ** Function Prototypes
  91. */
  92. typedef struct Proto {
  93. TObject *k; /* constants used by the function */
  94. Instruction *code;
  95. struct Proto **p; /* functions defined inside the function */
  96. struct Proto *next;
  97. int *lineinfo; /* map from opcodes to source lines */
  98. struct LocVar *locvars; /* information about local variables */
  99. TString *source;
  100. int sizek; /* size of `k' */
  101. int sizecode;
  102. int sizep; /* size of `p' */
  103. int sizelocvars;
  104. int lineDefined;
  105. lu_byte nupvalues;
  106. lu_byte numparams;
  107. lu_byte is_vararg;
  108. lu_byte maxstacksize;
  109. lu_byte marked;
  110. } Proto;
  111. typedef struct LocVar {
  112. TString *varname;
  113. int startpc; /* first point where variable is active */
  114. int endpc; /* first point where variable is dead */
  115. } LocVar;
  116. /*
  117. ** Upvalues
  118. */
  119. typedef struct UpVal {
  120. TObject *v; /* points to stack or to its own value */
  121. struct UpVal *next;
  122. TObject value; /* the value (when closed) */
  123. } UpVal;
  124. /*
  125. ** Closures
  126. */
  127. typedef struct CClosure {
  128. lu_byte isC; /* 0 for Lua functions, 1 for C functions */
  129. lu_byte nupvalues;
  130. lu_byte marked;
  131. union Closure *next;
  132. lua_CFunction f;
  133. TObject upvalue[1];
  134. } CClosure;
  135. typedef struct LClosure {
  136. lu_byte isC;
  137. lu_byte nupvalues;
  138. lu_byte marked;
  139. union Closure *next; /* first four fields must be equal to CClosure!! */
  140. struct Proto *p;
  141. TObject g; /* global table for this closure */
  142. UpVal *upvals[1];
  143. } LClosure;
  144. typedef union Closure {
  145. CClosure c;
  146. LClosure l;
  147. } Closure;
  148. #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
  149. #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
  150. /*
  151. ** Tables
  152. */
  153. typedef struct Node {
  154. TObject i_key;
  155. TObject i_val;
  156. struct Node *next; /* for chaining */
  157. } Node;
  158. typedef struct Table {
  159. struct Table *metatable;
  160. TObject *array; /* array part */
  161. Node *node;
  162. Node *firstfree; /* this position is free; all positions after it are full */
  163. struct Table *next;
  164. struct Table *mark; /* marked tables (point to itself when not marked) */
  165. int sizearray; /* size of `array' array */
  166. lu_byte flags; /* 1<<p means tagmethod(p) is not present */
  167. lu_byte lsizenode; /* log2 of size of `node' array */
  168. lu_byte mode;
  169. } Table;
  170. /* bit masks for `mode' */
  171. #define WEAKKEY 1
  172. #define WEAKVALUE 2
  173. /*
  174. ** `module' operation for hashing (size is always a power of 2)
  175. */
  176. #define lmod(s,size) \
  177. check_exp((size&(size-1))==0, (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. extern const TObject luaO_nilobject;
  182. int luaO_log2 (unsigned int x);
  183. #define luaO_openspace(L,n,t) cast(t *, luaO_openspaceaux(L,(n)*sizeof(t)))
  184. void *luaO_openspaceaux (lua_State *L, size_t n);
  185. int luaO_rawequalObj (const TObject *t1, const TObject *t2);
  186. int luaO_str2d (const char *s, lua_Number *result);
  187. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp);
  188. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  189. void luaO_chunkid (char *out, const char *source, int len);
  190. #endif