lobject.h 4.6 KB

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