lobject.h 5.1 KB

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