lobject.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. ** $Id: lobject.h,v 1.25 1999/01/04 13:37:07 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_TYPES 11
  51. #define NUM_TAGS 7
  52. typedef union {
  53. lua_CFunction f; /* LUA_T_CPROTO, LUA_T_CMARK */
  54. real n; /* LUA_T_NUMBER */
  55. struct TaggedString *ts; /* LUA_T_STRING, LUA_T_USERDATA */
  56. struct TProtoFunc *tf; /* LUA_T_PROTO, LUA_T_PMARK */
  57. struct Closure *cl; /* LUA_T_CLOSURE, LUA_T_CLMARK */
  58. struct Hash *a; /* LUA_T_ARRAY */
  59. int i; /* LUA_T_LINE */
  60. } Value;
  61. typedef struct TObject {
  62. lua_Type ttype;
  63. Value value;
  64. } TObject;
  65. /*
  66. ** generic header for garbage collector lists
  67. */
  68. typedef struct GCnode {
  69. struct GCnode *next;
  70. int marked;
  71. } GCnode;
  72. /*
  73. ** String headers for string table
  74. */
  75. typedef struct TaggedString {
  76. GCnode head;
  77. unsigned long hash;
  78. int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
  79. union {
  80. struct {
  81. TObject globalval;
  82. long len; /* if this is a string, here is its length */
  83. } s;
  84. struct {
  85. int tag;
  86. void *v; /* if this is a userdata, here is its value */
  87. } d;
  88. } u;
  89. char str[1]; /* \0 byte already reserved */
  90. } TaggedString;
  91. /*
  92. ** Function Prototypes
  93. */
  94. typedef struct TProtoFunc {
  95. GCnode head;
  96. struct TObject *consts;
  97. int nconsts;
  98. Byte *code; /* ends with opcode ENDCODE */
  99. int lineDefined;
  100. TaggedString *fileName;
  101. struct LocVar *locvars; /* ends with line = -1 */
  102. } TProtoFunc;
  103. typedef struct LocVar {
  104. TaggedString *varname; /* NULL signals end of scope */
  105. int line;
  106. } LocVar;
  107. /* Macros to access structure members */
  108. #define ttype(o) ((o)->ttype)
  109. #define nvalue(o) ((o)->value.n)
  110. #define svalue(o) ((o)->value.ts->str)
  111. #define tsvalue(o) ((o)->value.ts)
  112. #define clvalue(o) ((o)->value.cl)
  113. #define avalue(o) ((o)->value.a)
  114. #define fvalue(o) ((o)->value.f)
  115. #define tfvalue(o) ((o)->value.tf)
  116. #define protovalue(o) ((o)->value.cl->consts)
  117. /*
  118. ** Closures
  119. */
  120. typedef struct Closure {
  121. GCnode head;
  122. int nelems; /* not included the first one (always the prototype) */
  123. TObject consts[1]; /* at least one for prototype */
  124. } Closure;
  125. typedef struct node {
  126. TObject ref;
  127. TObject val;
  128. } Node;
  129. typedef struct Hash {
  130. GCnode head;
  131. Node *node;
  132. int nhash;
  133. int nuse;
  134. int htag;
  135. } Hash;
  136. extern char *luaO_typenames[];
  137. #define luaO_typename(o) luaO_typenames[-ttype(o)]
  138. extern TObject luaO_nilobject;
  139. #define luaO_equalObj(t1,t2) ((ttype(t1) != ttype(t2)) ? 0 \
  140. : luaO_equalval(t1,t2))
  141. int luaO_equalval (TObject *t1, TObject *t2);
  142. int luaO_redimension (int oldsize);
  143. void luaO_insertlist (GCnode *root, GCnode *node);
  144. double luaO_str2d (char *s);
  145. #ifdef OLD_ANSI
  146. void luaO_memup (void *dest, void *src, int size);
  147. void luaO_memdown (void *dest, void *src, int size);
  148. #else
  149. #include <string.h>
  150. #define luaO_memup(d,s,n) memmove(d,s,n)
  151. #define luaO_memdown(d,s,n) memmove(d,s,n)
  152. #endif
  153. #endif