lobject.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. ** $Id: lobject.h,v 1.38 1999/11/26 18:59:20 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. /* convertion 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, /* tag default 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, /* tag default for tables (or arrays) */
  52. LUA_T_PROTO = -4, /* fixed tag for functions */
  53. LUA_T_CPROTO = -5, /* fixed tag for Cfunctions */
  54. LUA_T_NIL = -6, /* last "pre-defined" tag */
  55. LUA_T_CLOSURE = -7,
  56. LUA_T_CLMARK = -8, /* mark for closures */
  57. LUA_T_PMARK = -9, /* mark for Lua prototypes */
  58. LUA_T_CMARK = -10, /* mark for C prototypes */
  59. LUA_T_LINE = -11
  60. } lua_Type;
  61. #define NUM_TAGS 7
  62. typedef union {
  63. lua_CFunction f; /* LUA_T_CPROTO, LUA_T_CMARK */
  64. real n; /* LUA_T_NUMBER */
  65. struct TaggedString *ts; /* LUA_T_STRING, LUA_T_USERDATA */
  66. struct TProtoFunc *tf; /* LUA_T_PROTO, LUA_T_PMARK */
  67. struct Closure *cl; /* LUA_T_CLOSURE, LUA_T_CLMARK */
  68. struct Hash *a; /* LUA_T_ARRAY */
  69. int i; /* LUA_T_LINE */
  70. } Value;
  71. typedef struct TObject {
  72. lua_Type ttype;
  73. Value value;
  74. } TObject;
  75. typedef struct GlobalVar {
  76. TObject value;
  77. struct GlobalVar *next;
  78. struct TaggedString *name;
  79. } GlobalVar;
  80. /*
  81. ** String headers for string table
  82. */
  83. typedef struct TaggedString {
  84. union {
  85. struct { /* for strings */
  86. GlobalVar *gv; /* eventual global value with this name */
  87. long len;
  88. } s;
  89. struct { /* for userdata */
  90. int tag;
  91. void *value;
  92. } d;
  93. } u;
  94. struct TaggedString *nexthash; /* chain for hash table */
  95. unsigned long hash;
  96. int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
  97. unsigned char marked;
  98. char str[1]; /* \0 byte already reserved */
  99. } TaggedString;
  100. /*
  101. ** Function Prototypes
  102. */
  103. typedef struct TProtoFunc {
  104. struct TProtoFunc *next;
  105. int marked;
  106. struct TObject *consts;
  107. int nconsts;
  108. Byte *code; /* ends with opcode ENDCODE */
  109. int lineDefined;
  110. TaggedString *source;
  111. struct LocVar *locvars; /* ends with line = -1 */
  112. } TProtoFunc;
  113. typedef struct LocVar {
  114. TaggedString *varname; /* NULL signals end of scope */
  115. int line;
  116. } LocVar;
  117. /* Macros to access structure members */
  118. #define ttype(o) ((o)->ttype)
  119. #define nvalue(o) ((o)->value.n)
  120. #define svalue(o) ((o)->value.ts->str)
  121. #define tsvalue(o) ((o)->value.ts)
  122. #define clvalue(o) ((o)->value.cl)
  123. #define avalue(o) ((o)->value.a)
  124. #define fvalue(o) ((o)->value.f)
  125. #define tfvalue(o) ((o)->value.tf)
  126. #define protovalue(o) ((o)->value.cl->consts)
  127. /*
  128. ** Closures
  129. */
  130. typedef struct Closure {
  131. struct Closure *next;
  132. int marked;
  133. int nelems; /* not included the first one (always the prototype) */
  134. TObject consts[1]; /* at least one for prototype */
  135. } Closure;
  136. typedef struct node {
  137. TObject key;
  138. TObject val;
  139. struct node *next; /* for chaining */
  140. } Node;
  141. typedef struct Hash {
  142. int htag;
  143. Node *node;
  144. int size;
  145. Node *firstfree; /* this position is free; all positions after it are full */
  146. struct Hash *next;
  147. int marked;
  148. } Hash;
  149. extern const char *const luaO_typenames[];
  150. #define luaO_typename(L, o) luaO_typenames[-ttype(o)]
  151. #define MINPOWER2 4 /* minimum size for "growing" vectors */
  152. unsigned long luaO_power2 (unsigned long n);
  153. extern const TObject luaO_nilobject;
  154. #define luaO_equalObj(t1,t2) ((ttype(t1) != ttype(t2)) ? 0 \
  155. : luaO_equalval(t1,t2))
  156. int luaO_equalval (const TObject *t1, const TObject *t2);
  157. int luaO_redimension (lua_State *L, int oldsize);
  158. int luaO_str2d (const char *s, real *result);
  159. #ifdef OLD_ANSI
  160. void luaO_memup (void *dest, void *src, int size);
  161. void luaO_memdown (void *dest, void *src, int size);
  162. #else
  163. #include <string.h>
  164. #define luaO_memup(d,s,n) memmove(d,s,n)
  165. #define luaO_memdown(d,s,n) memmove(d,s,n)
  166. #endif
  167. #endif