lobject.h 4.9 KB

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