lobject.h 5.4 KB

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