lobject.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. ** $Id: lobject.h,v 1.20 1998/06/11 18:21:37 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. #endif
  26. /*
  27. ** format to convert number to strings
  28. */
  29. #define NUMBER_FMT "%g"
  30. typedef LUA_NUM_TYPE real;
  31. #define Byte lua_Byte /* some systems have Byte as a predefined type */
  32. typedef unsigned char Byte; /* unsigned 8 bits */
  33. #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
  34. /* maximum value of a word of 2 bytes (-2 for safety); must fit in an "int" */
  35. #if MAX_INT < 65534
  36. #define MAX_WORD MAX_INT
  37. #else
  38. #define MAX_WORD 65534
  39. #endif
  40. typedef unsigned int IntPoint; /* unsigned with same size as a pointer (for hashing) */
  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_TYPES 11
  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. /*
  76. ** generic header for garbage collector lists
  77. */
  78. typedef struct GCnode {
  79. struct GCnode *next;
  80. int marked;
  81. } GCnode;
  82. /*
  83. ** String headers for string table
  84. */
  85. typedef struct TaggedString {
  86. GCnode head;
  87. unsigned long hash;
  88. int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
  89. union {
  90. struct {
  91. TObject globalval;
  92. long len; /* if this is a string, here is its length */
  93. } s;
  94. struct {
  95. int tag;
  96. void *v; /* if this is a userdata, here is its value */
  97. } d;
  98. } u;
  99. char str[1]; /* \0 byte already reserved */
  100. } TaggedString;
  101. /*
  102. ** Function Prototypes
  103. */
  104. typedef struct TProtoFunc {
  105. GCnode head;
  106. struct TObject *consts;
  107. int nconsts;
  108. Byte *code; /* ends with opcode ENDCODE */
  109. int lineDefined;
  110. TaggedString *fileName;
  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. GCnode head;
  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 ref;
  137. TObject val;
  138. } Node;
  139. typedef struct Hash {
  140. GCnode head;
  141. Node *node;
  142. int nhash;
  143. int nuse;
  144. int htag;
  145. } Hash;
  146. extern char *luaO_typenames[];
  147. extern TObject luaO_nilobject;
  148. int luaO_equalObj (TObject *t1, TObject *t2);
  149. int luaO_redimension (int oldsize);
  150. void luaO_insertlist (GCnode *root, GCnode *node);
  151. #ifdef OLD_ANSI
  152. void luaO_memup (void *dest, void *src, int size);
  153. void luaO_memdown (void *dest, void *src, int size);
  154. #else
  155. #include <string.h>
  156. #define luaO_memup(d,s,n) memmove(d,s,n)
  157. #define luaO_memdown(d,s,n) memmove(d,s,n)
  158. #endif
  159. #endif