lobject.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. ** $Id: lobject.h,v 1.78 2000/10/02 20:10:55 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(s) assert(((void)s,0))
  14. #define LUA_ASSERT(c,s) assert(((void)s,(c)))
  15. #else
  16. #define LUA_INTERNALERROR(s) /* empty */
  17. #define LUA_ASSERT(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. /* mark for closures active in the stack */
  26. #define LUA_TMARK 6
  27. /* tags for values visible from Lua == first user-created tag */
  28. #define NUM_TAGS 6
  29. /* check whether `t' is a mark */
  30. #define is_T_MARK(t) ((t) == LUA_TMARK)
  31. typedef union {
  32. struct TString *ts; /* LUA_TSTRING, LUA_TUSERDATA */
  33. struct Closure *cl; /* LUA_TFUNCTION */
  34. struct Hash *a; /* LUA_TTABLE */
  35. struct CallInfo *i; /* LUA_TLMARK */
  36. Number n; /* LUA_TNUMBER */
  37. } Value;
  38. /* Macros to access values */
  39. #define ttype(o) ((o)->ttype)
  40. #define nvalue(o) ((o)->value.n)
  41. #define tsvalue(o) ((o)->value.ts)
  42. #define clvalue(o) ((o)->value.cl)
  43. #define hvalue(o) ((o)->value.a)
  44. #define infovalue(o) ((o)->value.i)
  45. #define svalue(o) (tsvalue(o)->str)
  46. typedef struct lua_TObject {
  47. int ttype;
  48. Value value;
  49. } TObject;
  50. /*
  51. ** String headers for string table
  52. */
  53. typedef struct TString {
  54. union {
  55. struct { /* for strings */
  56. unsigned long hash;
  57. size_t len;
  58. int constindex; /* hint to reuse constants */
  59. } s;
  60. struct { /* for userdata */
  61. int tag;
  62. void *value;
  63. } d;
  64. } u;
  65. struct TString *nexthash; /* chain for hash table */
  66. unsigned char marked;
  67. char str[1]; /* variable length string!! must be the last field! */
  68. } TString;
  69. /*
  70. ** Function Prototypes
  71. */
  72. typedef struct Proto {
  73. Number *knum; /* Number numbers used by the function */
  74. int nknum; /* size of `knum' */
  75. struct TString **kstr; /* strings used by the function */
  76. int nkstr; /* size of `kstr' */
  77. struct Proto **kproto; /* functions defined inside the function */
  78. int nkproto; /* size of `kproto' */
  79. Instruction *code;
  80. int ncode; /* size of `code'; when 0 means an incomplete `Proto' */
  81. short numparams;
  82. short is_vararg;
  83. short maxstacksize;
  84. short marked;
  85. struct Proto *next;
  86. /* debug information */
  87. int *lineinfo; /* map from opcodes to source lines */
  88. int nlineinfo; /* size of `lineinfo' */
  89. int nlocvars;
  90. struct LocVar *locvars; /* information about local variables */
  91. int lineDefined;
  92. TString *source;
  93. } Proto;
  94. typedef struct LocVar {
  95. TString *varname;
  96. int startpc; /* first point where variable is active */
  97. int endpc; /* first point where variable is dead */
  98. } LocVar;
  99. /*
  100. ** Closures
  101. */
  102. typedef struct Closure {
  103. union {
  104. lua_CFunction c; /* C functions */
  105. struct Proto *l; /* Lua functions */
  106. } f;
  107. struct Closure *next;
  108. struct Closure *mark; /* marked closures (point to itself when not marked) */
  109. short isC; /* 0 for Lua functions, 1 for C functions */
  110. short nupvalues;
  111. TObject upvalue[1];
  112. } Closure;
  113. #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->isC)
  114. typedef struct Node {
  115. TObject key;
  116. TObject val;
  117. struct Node *next; /* for chaining */
  118. } Node;
  119. typedef struct Hash {
  120. Node *node;
  121. int htag;
  122. int size;
  123. Node *firstfree; /* this position is free; all positions after it are full */
  124. struct Hash *next;
  125. struct Hash *mark; /* marked tables (point to itself when not marked) */
  126. } Hash;
  127. /* unmarked tables and closures are represented by pointing `mark' to
  128. ** themselves
  129. */
  130. #define ismarked(x) ((x)->mark != (x))
  131. /*
  132. ** informations about a call (for debugging)
  133. */
  134. typedef struct CallInfo {
  135. struct Closure *func; /* function being called */
  136. const Instruction **pc; /* current pc of called function */
  137. int lastpc; /* last pc traced */
  138. int line; /* current line */
  139. int refi; /* current index in `lineinfo' */
  140. } CallInfo;
  141. extern const TObject luaO_nilobject;
  142. extern const char *const luaO_typenames[];
  143. #define luaO_typename(o) (luaO_typenames[ttype(o)])
  144. lint32 luaO_power2 (lint32 n);
  145. char *luaO_openspace (lua_State *L, size_t n);
  146. int luaO_equalObj (const TObject *t1, const TObject *t2);
  147. int luaO_str2d (const char *s, Number *result);
  148. void luaO_verror (lua_State *L, const char *fmt, ...);
  149. void luaO_chunkid (char *out, const char *source, int len);
  150. #endif