lobject.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. ** $Id: lobject.h,v 1.54 2000/03/24 17:26:08 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 "llimits.h"
  9. #include "lua.h"
  10. #ifdef DEBUG
  11. #undef NDEBUG
  12. #include <assert.h>
  13. #define LUA_INTERNALERROR(L,s) assert(0)
  14. #define LUA_ASSERT(L,c,s) assert(c)
  15. #else
  16. #define LUA_INTERNALERROR(L,s) /* empty */
  17. #define LUA_ASSERT(L,c,s) /* empty */
  18. #endif
  19. #define UNUSED(x) (void)x /* to avoid warnings */
  20. /*
  21. ** Lua TYPES
  22. ** WARNING: if you change the order of this enumeration,
  23. ** grep "ORDER LUA_T"
  24. */
  25. typedef enum {
  26. TAG_USERDATA = 0, /* default tag for userdata */
  27. TAG_NUMBER, /* fixed tag for numbers */
  28. TAG_STRING, /* fixed tag for strings */
  29. TAG_ARRAY, /* default tag for tables (or arrays) */
  30. TAG_LPROTO, /* fixed tag for Lua functions */
  31. TAG_CPROTO, /* fixed tag for C functions */
  32. TAG_NIL, /* last "pre-defined" tag */
  33. TAG_LCLOSURE, /* Lua closure */
  34. TAG_CCLOSURE, /* C closure */
  35. TAG_LCLMARK, /* mark for Lua closures */
  36. TAG_CCLMARK, /* mark for C closures */
  37. TAG_LMARK, /* mark for Lua prototypes */
  38. TAG_CMARK, /* mark for C prototypes */
  39. TAG_LINE
  40. } lua_Type;
  41. /* tags for values visible from Lua == first user-created tag */
  42. #define NUM_TAGS 7
  43. /*
  44. ** check whether `t' is a mark
  45. */
  46. #define is_T_MARK(t) (TAG_LCLMARK <= (t) && (t) <= TAG_CMARK)
  47. typedef union {
  48. lua_CFunction f; /* TAG_CPROTO, TAG_CMARK */
  49. Number n; /* TAG_NUMBER */
  50. struct TString *ts; /* TAG_STRING, TAG_USERDATA */
  51. struct Proto *tf; /* TAG_LPROTO, TAG_LMARK */
  52. struct Closure *cl; /* TAG_[CL]CLOSURE, TAG_[CL]CLMARK */
  53. struct Hash *a; /* TAG_ARRAY */
  54. int i; /* TAG_LINE */
  55. } Value;
  56. typedef struct TObject {
  57. lua_Type ttype;
  58. Value value;
  59. } TObject;
  60. typedef struct GlobalVar {
  61. TObject value;
  62. struct GlobalVar *next;
  63. struct TString *name;
  64. } GlobalVar;
  65. /*
  66. ** String headers for string table
  67. */
  68. typedef struct TString {
  69. union {
  70. struct { /* for strings */
  71. GlobalVar *gv; /* eventual global value with this name */
  72. long len;
  73. } s;
  74. struct { /* for userdata */
  75. int tag;
  76. void *value;
  77. } d;
  78. } u;
  79. struct TString *nexthash; /* chain for hash table */
  80. unsigned long hash;
  81. int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
  82. unsigned char marked;
  83. char str[1]; /* \0 byte already reserved */
  84. } TString;
  85. /*
  86. ** Function Prototypes
  87. */
  88. typedef struct Proto {
  89. struct Proto *next;
  90. int marked;
  91. struct TString **kstr; /* strings used by the function */
  92. int nkstr; /* size of `kstr' */
  93. Number *knum; /* Number numbers used by the function */
  94. int nknum; /* size of `knum' */
  95. struct Proto **kproto; /* functions defined inside the function */
  96. int nkproto; /* size of `kproto' */
  97. Instruction *code; /* ends with opcode ENDCODE */
  98. int lineDefined;
  99. TString *source;
  100. int numparams;
  101. int is_vararg;
  102. int maxstacksize;
  103. struct LocVar *locvars; /* ends with line = -1 */
  104. } Proto;
  105. typedef struct LocVar {
  106. TString *varname; /* NULL signals end of scope */
  107. int line;
  108. } LocVar;
  109. /* Macros to access structure members */
  110. #define ttype(o) ((o)->ttype)
  111. #define nvalue(o) ((o)->value.n)
  112. #define svalue(o) ((o)->value.ts->str)
  113. #define tsvalue(o) ((o)->value.ts)
  114. #define clvalue(o) ((o)->value.cl)
  115. #define avalue(o) ((o)->value.a)
  116. #define fvalue(o) ((o)->value.f)
  117. #define tfvalue(o) ((o)->value.tf)
  118. #define protovalue(o) ((o)->value.cl->consts)
  119. /*
  120. ** Closures
  121. */
  122. typedef struct Closure {
  123. struct Closure *next;
  124. int marked;
  125. int nelems; /* not including the first one (always the prototype) */
  126. TObject consts[1]; /* at least one for prototype */
  127. } Closure;
  128. typedef struct Node {
  129. TObject key;
  130. TObject val;
  131. struct Node *next; /* for chaining */
  132. } Node;
  133. typedef struct Hash {
  134. int htag;
  135. Node *node;
  136. int size;
  137. Node *firstfree; /* this position is free; all positions after it are full */
  138. struct Hash *next;
  139. int marked;
  140. } Hash;
  141. extern const char *const luaO_typenames[];
  142. extern const TObject luaO_nilobject;
  143. #define luaO_typename(o) luaO_typenames[ttype(o)]
  144. unsigned long luaO_power2 (unsigned long n);
  145. #define luaO_equalObj(t1,t2) (ttype(t1) == ttype(t2) && luaO_equalval(t1,t2))
  146. int luaO_equalval (const TObject *t1, const TObject *t2);
  147. int luaO_redimension (lua_State *L, int oldsize);
  148. int luaO_str2d (const char *s, Number *result);
  149. #endif