lobject.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. ** $Id: lobject.h,v 1.10 1997/11/27 18:25:06 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 "lua.h"
  9. #include <limits.h>
  10. #ifndef real
  11. #define real float
  12. #endif
  13. #define Byte lua_Byte /* some systems have Byte as a predefined type */
  14. typedef unsigned char Byte; /* unsigned 8 bits */
  15. #define Word lua_Word /* some systems have Word as a predefined type */
  16. typedef unsigned short Word; /* unsigned 16 bits */
  17. #define MAX_WORD (USHRT_MAX-2) /* maximum value of a word (-2 for safety) */
  18. #define MAX_INT (INT_MAX-2) /* maximum value of a int (-2 for safety) */
  19. typedef unsigned int IntPoint; /* unsigned with same size as a pointer (for hashing) */
  20. /*
  21. ** Lua TYPES
  22. ** WARNING: if you change the order of this enumeration,
  23. ** grep "ORDER LUA_T"
  24. */
  25. typedef enum {
  26. LUA_T_USERDATA = 0, /* tag default for userdata */
  27. LUA_T_NUMBER = -1, /* fixed tag for numbers */
  28. LUA_T_STRING = -2, /* fixed tag for strings */
  29. LUA_T_ARRAY = -3, /* tag default for tables (or arrays) */
  30. LUA_T_PROTO = -4, /* fixed tag for functions */
  31. LUA_T_CPROTO = -5, /* fixed tag for Cfunctions */
  32. LUA_T_NIL = -6, /* last "pre-defined" tag */
  33. LUA_T_CLOSURE = -7,
  34. LUA_T_CLMARK = -8, /* mark for closures */
  35. LUA_T_PMARK = -9, /* mark for Lua prototypes */
  36. LUA_T_CMARK = -10, /* mark for C prototypes */
  37. LUA_T_LINE = -11
  38. } lua_Type;
  39. #define NUM_TYPES 11
  40. #define NUM_TAGS 7
  41. typedef union {
  42. lua_CFunction f; /* LUA_T_CPROTO, LUA_T_CMARK */
  43. real n; /* LUA_T_NUMBER */
  44. struct TaggedString *ts; /* LUA_T_STRING, LUA_T_USERDATA */
  45. struct TProtoFunc *tf; /* LUA_T_PROTO, LUA_T_PMARK */
  46. struct Closure *cl; /* LUA_T_CLOSURE, LUA_T_CLMARK */
  47. struct Hash *a; /* LUA_T_ARRAY */
  48. int i; /* LUA_T_LINE */
  49. } Value;
  50. typedef struct TObject {
  51. lua_Type ttype;
  52. Value value;
  53. } TObject;
  54. /*
  55. ** generic header for garbage collector lists
  56. */
  57. typedef struct GCnode {
  58. struct GCnode *next;
  59. int marked;
  60. } GCnode;
  61. /*
  62. ** String headers for string table
  63. */
  64. typedef struct TaggedString {
  65. GCnode head;
  66. int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
  67. unsigned long hash;
  68. union {
  69. TObject globalval;
  70. struct {
  71. void *v; /* if this is a userdata, here is its value */
  72. int tag;
  73. } d;
  74. } u;
  75. char str[1]; /* \0 byte already reserved */
  76. } TaggedString;
  77. /*
  78. ** Function Prototypes
  79. */
  80. typedef struct TProtoFunc {
  81. GCnode head;
  82. Byte *code; /* ends with opcode ENDCODE */
  83. int lineDefined;
  84. TaggedString *fileName;
  85. struct TObject *consts;
  86. int nconsts;
  87. struct LocVar *locvars; /* ends with line = -1 */
  88. } TProtoFunc;
  89. typedef struct LocVar {
  90. TaggedString *varname; /* NULL signals end of scope */
  91. int line;
  92. } LocVar;
  93. /* Macros to access structure members */
  94. #define ttype(o) ((o)->ttype)
  95. #define nvalue(o) ((o)->value.n)
  96. #define svalue(o) ((o)->value.ts->str)
  97. #define tsvalue(o) ((o)->value.ts)
  98. #define clvalue(o) ((o)->value.cl)
  99. #define avalue(o) ((o)->value.a)
  100. #define fvalue(o) ((o)->value.f)
  101. #define tfvalue(o) ((o)->value.tf)
  102. #define protovalue(o) ((o)->value.cl->consts)
  103. /*
  104. ** Closures
  105. */
  106. typedef struct Closure {
  107. GCnode head;
  108. int nelems; /* not included the first one (always the prototype) */
  109. TObject consts[1]; /* at least one for prototype */
  110. } Closure;
  111. typedef struct node {
  112. TObject ref;
  113. TObject val;
  114. } Node;
  115. typedef struct Hash {
  116. GCnode head;
  117. Node *node;
  118. int nhash;
  119. int nuse;
  120. int htag;
  121. } Hash;
  122. /*
  123. ** a gross estimation of number of memory "blocks" allocated
  124. ** (a block is *roughly* 32 bytes)
  125. */
  126. extern char *luaO_typenames[];
  127. extern TObject luaO_nilobject;
  128. int luaO_equalObj (TObject *t1, TObject *t2);
  129. int luaO_redimension (int oldsize);
  130. int luaO_findstring (char *name, char *list[]);
  131. void luaO_insertlist (GCnode *root, GCnode *node);
  132. #endif