lobject.h 4.6 KB

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