lobject.h 4.4 KB

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