lobject.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. ** $Id: lobject.h,v 1.49 2000/02/21 18:33:26 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 <limits.h>
  9. #include "lua.h"
  10. #ifdef DEBUG
  11. #undef NDEBUG
  12. #include <assert.h>
  13. #define LUA_INTERNALERROR(L,s) assert(0)
  14. #define LUA_ASSERT(L,c,s) assert(c)
  15. #else
  16. #define LUA_INTERNALERROR(L,s) /* empty */
  17. #define LUA_ASSERT(L,c,s) /* empty */
  18. #endif
  19. #define UNUSED(x) (void)x /* to avoid warnings */
  20. /*
  21. ** "real" is the type "number" of Lua
  22. ** GREP LUA_NUMBER to change that
  23. */
  24. #ifndef LUA_NUM_TYPE
  25. #define LUA_NUM_TYPE double
  26. #endif
  27. typedef LUA_NUM_TYPE real;
  28. /*
  29. ** type for virtual-machine instructions
  30. ** must be an unsigned with 4 bytes (see details in lopcodes.h)
  31. */
  32. typedef unsigned long Instruction;
  33. #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
  34. /* conversion of pointer to int (for hashing only) */
  35. /* (the shift removes bits that are usually 0 because of alignment) */
  36. #define IntPoint(L, p) (((unsigned long)(p)) >> 3)
  37. /*
  38. ** number of `blocks' for garbage collection: each reference to other
  39. ** objects count 1, and each 32 bytes of `raw' memory count 1; we add
  40. ** 2 to the total as a minimum (and also to count the overhead of malloc)
  41. */
  42. #define numblocks(L, o,b) ((o)+(b)/32+2)
  43. /*
  44. ** Lua TYPES
  45. ** WARNING: if you change the order of this enumeration,
  46. ** grep "ORDER LUA_T"
  47. */
  48. typedef enum {
  49. LUA_T_USERDATA = 0, /* default tag for userdata */
  50. LUA_T_NUMBER = -1, /* fixed tag for numbers */
  51. LUA_T_STRING = -2, /* fixed tag for strings */
  52. LUA_T_ARRAY = -3, /* default tag for tables (or arrays) */
  53. LUA_T_LPROTO = -4, /* fixed tag for Lua functions */
  54. LUA_T_CPROTO = -5, /* fixed tag for C functions */
  55. LUA_T_NIL = -6, /* last "pre-defined" tag */
  56. LUA_T_LCLOSURE = -7, /* Lua closure */
  57. LUA_T_CCLOSURE = -8, /* C closure */
  58. LUA_T_LCLMARK = -9 ,/* mark for Lua closures */
  59. LUA_T_CCLMARK = -10,/* mark for C closures */
  60. LUA_T_LMARK = -11,/* mark for Lua prototypes */
  61. LUA_T_CMARK = -12,/* mark for C prototypes */
  62. LUA_T_LINE = -13
  63. } lua_Type;
  64. #define NUM_TAGS 7 /* tags for values visible from Lua */
  65. #define LAST_REGULAR_TAG LUA_T_CCLOSURE /* after that, are all marks */
  66. /*
  67. ** check whether `t' is a mark; ttypes are negative numbers, so the
  68. ** comparisons look reversed. (ORDER LUA_T)
  69. */
  70. #define is_T_MARK(t) (LUA_T_CMARK <= (t) && (t) <= LUA_T_LCLMARK)
  71. typedef union {
  72. lua_CFunction f; /* LUA_T_CPROTO, LUA_T_CMARK */
  73. real n; /* LUA_T_NUMBER */
  74. struct TaggedString *ts; /* LUA_T_STRING, LUA_T_USERDATA */
  75. struct TProtoFunc *tf; /* LUA_T_LPROTO, LUA_T_LMARK */
  76. struct Closure *cl; /* LUA_T_[CL]CLOSURE, LUA_T_[CL]CLMARK */
  77. struct Hash *a; /* LUA_T_ARRAY */
  78. int i; /* LUA_T_LINE */
  79. } Value;
  80. typedef struct TObject {
  81. lua_Type ttype;
  82. Value value;
  83. } TObject;
  84. typedef struct GlobalVar {
  85. TObject value;
  86. struct GlobalVar *next;
  87. struct TaggedString *name;
  88. } GlobalVar;
  89. /*
  90. ** String headers for string table
  91. */
  92. typedef struct TaggedString {
  93. union {
  94. struct { /* for strings */
  95. GlobalVar *gv; /* eventual global value with this name */
  96. long len;
  97. } s;
  98. struct { /* for userdata */
  99. int tag;
  100. void *value;
  101. } d;
  102. } u;
  103. struct TaggedString *nexthash; /* chain for hash table */
  104. unsigned long hash;
  105. int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
  106. unsigned char marked;
  107. char str[1]; /* \0 byte already reserved */
  108. } TaggedString;
  109. /*
  110. ** Function Prototypes
  111. */
  112. typedef struct TProtoFunc {
  113. struct TProtoFunc *next;
  114. int marked;
  115. struct TaggedString **kstr; /* strings used by the function */
  116. int nkstr; /* size of `kstr' */
  117. real *knum; /* real numbers used by the function */
  118. int nknum; /* size of `knum' */
  119. struct TProtoFunc **kproto; /* functions defined inside the function */
  120. int nkproto; /* size of `kproto' */
  121. Instruction *code; /* ends with opcode ENDCODE */
  122. int lineDefined;
  123. TaggedString *source;
  124. int numparams;
  125. int is_vararg;
  126. int maxstacksize;
  127. struct LocVar *locvars; /* ends with line = -1 */
  128. } TProtoFunc;
  129. typedef struct LocVar {
  130. TaggedString *varname; /* NULL signals end of scope */
  131. int line;
  132. } LocVar;
  133. /* Macros to access structure members */
  134. #define ttype(o) ((o)->ttype)
  135. #define nvalue(o) ((o)->value.n)
  136. #define svalue(o) ((o)->value.ts->str)
  137. #define tsvalue(o) ((o)->value.ts)
  138. #define clvalue(o) ((o)->value.cl)
  139. #define avalue(o) ((o)->value.a)
  140. #define fvalue(o) ((o)->value.f)
  141. #define tfvalue(o) ((o)->value.tf)
  142. #define protovalue(o) ((o)->value.cl->consts)
  143. /*
  144. ** Closures
  145. */
  146. typedef struct Closure {
  147. struct Closure *next;
  148. int marked;
  149. int nelems; /* not including the first one (always the prototype) */
  150. TObject consts[1]; /* at least one for prototype */
  151. } Closure;
  152. typedef struct node {
  153. TObject key;
  154. TObject val;
  155. struct node *next; /* for chaining */
  156. } Node;
  157. typedef struct Hash {
  158. int htag;
  159. Node *node;
  160. int size;
  161. Node *firstfree; /* this position is free; all positions after it are full */
  162. struct Hash *next;
  163. int marked;
  164. } Hash;
  165. extern const char *const luaO_typenames[];
  166. extern const TObject luaO_nilobject;
  167. #define luaO_typename(o) luaO_typenames[-ttype(o)]
  168. #define MINPOWER2 4 /* minimum size for "growing" vectors */
  169. unsigned long luaO_power2 (unsigned long n);
  170. #define luaO_equalObj(t1,t2) (ttype(t1) == ttype(t2) && luaO_equalval(t1,t2))
  171. int luaO_equalval (const TObject *t1, const TObject *t2);
  172. int luaO_redimension (lua_State *L, int oldsize);
  173. int luaO_str2d (const char *s, real *result);
  174. #endif