lobject.h 4.4 KB

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