lobject.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. ** $Id: lobject.h,v 1.30 1999/09/06 20:34:18 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. ** String headers for string table
  66. */
  67. typedef struct TaggedString {
  68. struct TaggedString *next;
  69. int marked;
  70. unsigned long hash;
  71. int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
  72. union {
  73. struct {
  74. TObject globalval;
  75. long len; /* if this is a string, here is its length */
  76. } s;
  77. struct {
  78. int tag;
  79. void *v; /* if this is a userdata, here is its value */
  80. } d;
  81. } u;
  82. char str[1]; /* \0 byte already reserved */
  83. } TaggedString;
  84. /*
  85. ** Function Prototypes
  86. */
  87. typedef struct TProtoFunc {
  88. struct TProtoFunc *next;
  89. int marked;
  90. struct TObject *consts;
  91. int nconsts;
  92. Byte *code; /* ends with opcode ENDCODE */
  93. int lineDefined;
  94. TaggedString *source;
  95. struct LocVar *locvars; /* ends with line = -1 */
  96. } TProtoFunc;
  97. typedef struct LocVar {
  98. TaggedString *varname; /* NULL signals end of scope */
  99. int line;
  100. } LocVar;
  101. /* Macros to access structure members */
  102. #define ttype(o) ((o)->ttype)
  103. #define nvalue(o) ((o)->value.n)
  104. #define svalue(o) ((o)->value.ts->str)
  105. #define tsvalue(o) ((o)->value.ts)
  106. #define clvalue(o) ((o)->value.cl)
  107. #define avalue(o) ((o)->value.a)
  108. #define fvalue(o) ((o)->value.f)
  109. #define tfvalue(o) ((o)->value.tf)
  110. #define protovalue(o) ((o)->value.cl->consts)
  111. /*
  112. ** Closures
  113. */
  114. typedef struct Closure {
  115. struct Closure *next;
  116. int marked;
  117. int nelems; /* not included the first one (always the prototype) */
  118. TObject consts[1]; /* at least one for prototype */
  119. } Closure;
  120. typedef struct node {
  121. TObject ref;
  122. TObject val;
  123. } Node;
  124. typedef struct Hash {
  125. struct Hash *next;
  126. int marked;
  127. Node *node;
  128. int nhash;
  129. int nuse;
  130. int htag;
  131. } Hash;
  132. extern const char *const luaO_typenames[];
  133. #define luaO_typename(o) luaO_typenames[-ttype(o)]
  134. extern const TObject luaO_nilobject;
  135. #define luaO_equalObj(t1,t2) ((ttype(t1) != ttype(t2)) ? 0 \
  136. : luaO_equalval(t1,t2))
  137. int luaO_equalval (const TObject *t1, const TObject *t2);
  138. int luaO_redimension (int oldsize);
  139. int luaO_str2d (const char *s, real *result);
  140. #ifdef OLD_ANSI
  141. void luaO_memup (void *dest, void *src, int size);
  142. void luaO_memdown (void *dest, void *src, int size);
  143. #else
  144. #include <string.h>
  145. #define luaO_memup(d,s,n) memmove(d,s,n)
  146. #define luaO_memdown(d,s,n) memmove(d,s,n)
  147. #endif
  148. #endif