lobject.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. ** $Id: lobject.h,v 1.22 1998/07/12 16:11:55 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. #include "lauxlib.h"
  12. #define LUA_INTERNALERROR(s) \
  13. luaL_verror("INTERNAL ERROR - %s [%s:%d]",(s),__FILE__,__LINE__)
  14. #define LUA_ASSERT(c,s) { if (!(c)) LUA_INTERNALERROR(s); }
  15. #else
  16. #define LUA_INTERNALERROR(s) /* empty */
  17. #define LUA_ASSERT(c,s) /* empty */
  18. #endif
  19. /*
  20. ** "real" is the type "number" of Lua
  21. ** GREP LUA_NUMBER to change that
  22. */
  23. #ifndef LUA_NUM_TYPE
  24. #define LUA_NUM_TYPE double
  25. #define NUMBER_FMT "%g"
  26. #endif
  27. typedef LUA_NUM_TYPE real;
  28. #define Byte lua_Byte /* some systems have Byte as a predefined type */
  29. typedef unsigned char Byte; /* unsigned 8 bits */
  30. #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
  31. /* maximum value of a word of 2 bytes (-2 for safety); must fit in an "int" */
  32. #if MAX_INT < 65534
  33. #define MAX_WORD MAX_INT
  34. #else
  35. #define MAX_WORD 65534
  36. #endif
  37. typedef unsigned int IntPoint; /* unsigned with same size as a pointer (for hashing) */
  38. /*
  39. ** Lua TYPES
  40. ** WARNING: if you change the order of this enumeration,
  41. ** grep "ORDER LUA_T"
  42. */
  43. typedef enum {
  44. LUA_T_USERDATA = 0, /* tag default for userdata */
  45. LUA_T_NUMBER = -1, /* fixed tag for numbers */
  46. LUA_T_STRING = -2, /* fixed tag for strings */
  47. LUA_T_ARRAY = -3, /* tag default for tables (or arrays) */
  48. LUA_T_PROTO = -4, /* fixed tag for functions */
  49. LUA_T_CPROTO = -5, /* fixed tag for Cfunctions */
  50. LUA_T_NIL = -6, /* last "pre-defined" tag */
  51. LUA_T_CLOSURE = -7,
  52. LUA_T_CLMARK = -8, /* mark for closures */
  53. LUA_T_PMARK = -9, /* mark for Lua prototypes */
  54. LUA_T_CMARK = -10, /* mark for C prototypes */
  55. LUA_T_LINE = -11
  56. } lua_Type;
  57. #define NUM_TYPES 11
  58. #define NUM_TAGS 7
  59. typedef union {
  60. lua_CFunction f; /* LUA_T_CPROTO, LUA_T_CMARK */
  61. real n; /* LUA_T_NUMBER */
  62. struct TaggedString *ts; /* LUA_T_STRING, LUA_T_USERDATA */
  63. struct TProtoFunc *tf; /* LUA_T_PROTO, LUA_T_PMARK */
  64. struct Closure *cl; /* LUA_T_CLOSURE, LUA_T_CLMARK */
  65. struct Hash *a; /* LUA_T_ARRAY */
  66. int i; /* LUA_T_LINE */
  67. } Value;
  68. typedef struct TObject {
  69. lua_Type ttype;
  70. Value value;
  71. } TObject;
  72. /*
  73. ** generic header for garbage collector lists
  74. */
  75. typedef struct GCnode {
  76. struct GCnode *next;
  77. int marked;
  78. } GCnode;
  79. /*
  80. ** String headers for string table
  81. */
  82. typedef struct TaggedString {
  83. GCnode head;
  84. unsigned long hash;
  85. int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
  86. union {
  87. struct {
  88. TObject globalval;
  89. long len; /* if this is a string, here is its length */
  90. } s;
  91. struct {
  92. int tag;
  93. void *v; /* if this is a userdata, here is its value */
  94. } d;
  95. } u;
  96. char str[1]; /* \0 byte already reserved */
  97. } TaggedString;
  98. /*
  99. ** Function Prototypes
  100. */
  101. typedef struct TProtoFunc {
  102. GCnode head;
  103. struct TObject *consts;
  104. int nconsts;
  105. Byte *code; /* ends with opcode ENDCODE */
  106. int lineDefined;
  107. TaggedString *fileName;
  108. struct LocVar *locvars; /* ends with line = -1 */
  109. } TProtoFunc;
  110. typedef struct LocVar {
  111. TaggedString *varname; /* NULL signals end of scope */
  112. int line;
  113. } LocVar;
  114. /* Macros to access structure members */
  115. #define ttype(o) ((o)->ttype)
  116. #define nvalue(o) ((o)->value.n)
  117. #define svalue(o) ((o)->value.ts->str)
  118. #define tsvalue(o) ((o)->value.ts)
  119. #define clvalue(o) ((o)->value.cl)
  120. #define avalue(o) ((o)->value.a)
  121. #define fvalue(o) ((o)->value.f)
  122. #define tfvalue(o) ((o)->value.tf)
  123. #define protovalue(o) ((o)->value.cl->consts)
  124. /*
  125. ** Closures
  126. */
  127. typedef struct Closure {
  128. GCnode head;
  129. int nelems; /* not included the first one (always the prototype) */
  130. TObject consts[1]; /* at least one for prototype */
  131. } Closure;
  132. typedef struct node {
  133. TObject ref;
  134. TObject val;
  135. } Node;
  136. typedef struct Hash {
  137. GCnode head;
  138. Node *node;
  139. int nhash;
  140. int nuse;
  141. int htag;
  142. } Hash;
  143. extern char *luaO_typenames[];
  144. #define luaO_typename(o) luaO_typenames[-ttype(o)]
  145. extern TObject luaO_nilobject;
  146. int luaO_equalObj (TObject *t1, TObject *t2);
  147. int luaO_redimension (int oldsize);
  148. void luaO_insertlist (GCnode *root, GCnode *node);
  149. #ifdef OLD_ANSI
  150. void luaO_memup (void *dest, void *src, int size);
  151. void luaO_memdown (void *dest, void *src, int size);
  152. #else
  153. #include <string.h>
  154. #define luaO_memup(d,s,n) memmove(d,s,n)
  155. #define luaO_memdown(d,s,n) memmove(d,s,n)
  156. #endif
  157. #endif