lobject.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. ** $Id: lobject.h,v 1.59 2000/03/31 16:28:45 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(((void)s,0))
  14. #define LUA_ASSERT(L,c,s) assert(((void)s,(c)))
  15. #else
  16. #define LUA_INTERNALERROR(L,s) /* empty */
  17. #define LUA_ASSERT(L,c,s) /* empty */
  18. #endif
  19. #ifdef DEBUG
  20. /* to avoid warnings, and make sure value is really unused */
  21. #define UNUSED(x) (x=0, (void)(x))
  22. #else
  23. #define UNUSED(x) ((void)(x)) /* to avoid warnings */
  24. #endif
  25. /*
  26. ** Lua TYPES
  27. ** WARNING: if you change the order of this enumeration,
  28. ** grep "ORDER LUA_T"
  29. */
  30. typedef enum {
  31. TAG_USERDATA = 0, /* default tag for userdata */
  32. TAG_NUMBER, /* fixed tag for numbers */
  33. TAG_STRING, /* fixed tag for strings */
  34. TAG_TABLE, /* default tag for tables */
  35. TAG_LCLOSURE, /* fixed tag for Lua closures */
  36. TAG_CCLOSURE, /* fixed tag for C closures */
  37. TAG_NIL, /* last "pre-defined" tag */
  38. TAG_LMARK, /* mark for Lua closures */
  39. TAG_CMARK, /* mark for C closures */
  40. TAG_LINE
  41. } lua_Type;
  42. /* tags for values visible from Lua == first user-created tag */
  43. #define NUM_TAGS 7
  44. /*
  45. ** check whether `t' is a mark
  46. */
  47. #define is_T_MARK(t) ((t) == TAG_LMARK || (t) == TAG_CMARK)
  48. typedef union {
  49. struct TString *ts; /* TAG_STRING, TAG_USERDATA */
  50. struct Closure *cl; /* TAG_[CL]CLOSURE, TAG_[CL]MARK */
  51. struct Hash *a; /* TAG_TABLE */
  52. Number n; /* TAG_NUMBER */
  53. int i; /* TAG_LINE */
  54. } Value;
  55. /* Macros to access values */
  56. #define ttype(o) ((o)->ttype)
  57. #define nvalue(o) ((o)->value.n)
  58. #define svalue(o) ((o)->value.ts->str)
  59. #define tsvalue(o) ((o)->value.ts)
  60. #define clvalue(o) ((o)->value.cl)
  61. #define avalue(o) ((o)->value.a)
  62. typedef struct TObject {
  63. lua_Type ttype;
  64. Value value;
  65. } TObject;
  66. typedef struct GlobalVar {
  67. TObject value;
  68. struct GlobalVar *next;
  69. struct TString *name;
  70. } GlobalVar;
  71. /*
  72. ** String headers for string table
  73. */
  74. typedef struct TString {
  75. union {
  76. struct { /* for strings */
  77. GlobalVar *gv; /* eventual global value with this name */
  78. long len;
  79. } s;
  80. struct { /* for userdata */
  81. int tag;
  82. void *value;
  83. } d;
  84. } u;
  85. struct TString *nexthash; /* chain for hash table */
  86. unsigned long hash;
  87. int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
  88. unsigned char marked;
  89. char str[1]; /* variable length string!! must be the last field! */
  90. } TString;
  91. /*
  92. ** Function Prototypes
  93. */
  94. typedef struct Proto {
  95. struct Proto *next;
  96. int marked;
  97. struct TString **kstr; /* strings used by the function */
  98. int nkstr; /* size of `kstr' */
  99. Number *knum; /* Number numbers used by the function */
  100. int nknum; /* size of `knum' */
  101. struct Proto **kproto; /* functions defined inside the function */
  102. int nkproto; /* size of `kproto' */
  103. Instruction *code; /* ends with opcode ENDCODE */
  104. int lineDefined;
  105. TString *source;
  106. int numparams;
  107. int is_vararg;
  108. int maxstacksize;
  109. struct LocVar *locvars; /* ends with line = -1 */
  110. } Proto;
  111. typedef struct LocVar {
  112. TString *varname; /* NULL signals end of scope */
  113. int line;
  114. } LocVar;
  115. /*
  116. ** Closures
  117. */
  118. typedef struct Closure {
  119. struct Closure *next;
  120. int marked;
  121. union {
  122. lua_CFunction c; /* C functions */
  123. struct Proto *l; /* Lua functions */
  124. } f;
  125. int nelems;
  126. TObject consts[1];
  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