lobject.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. ** $Id: $
  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. ** String headers for string table
  22. */
  23. #define NOT_USED 0xFFFE
  24. typedef struct TaggedString {
  25. int tag; /* if != LUA_T_STRING, this is a userdata */
  26. union {
  27. unsigned long hash;
  28. struct TaggedString *next;
  29. } uu;
  30. union {
  31. struct {
  32. Word varindex; /* != NOT_USED if this is a symbol */
  33. Word constindex; /* hint to reuse constant indexes */
  34. } s;
  35. void *v; /* if this is a userdata, here is its value */
  36. } u;
  37. int marked; /* for garbage collection; never collect (nor change) if > 1 */
  38. char str[1]; /* \0 byte already reserved */
  39. } TaggedString;
  40. /*
  41. ** generic header for garbage collector lists
  42. */
  43. typedef struct GCnode {
  44. struct GCnode *next;
  45. int marked;
  46. } GCnode;
  47. /*
  48. ** Function Prototypes
  49. */
  50. typedef struct TProtoFunc {
  51. GCnode head;
  52. Byte *code; /* ends with opcode ENDCODE */
  53. int lineDefined;
  54. TaggedString *fileName;
  55. struct TObject *consts;
  56. int nconsts;
  57. struct LocVar *locvars; /* ends with line = -1 */
  58. int nupvalues;
  59. } TProtoFunc;
  60. typedef struct LocVar {
  61. TaggedString *varname; /* NULL signals end of scope */
  62. int line;
  63. } LocVar;
  64. /*
  65. ** Lua TYPES
  66. ** WARNING: if you change the order of this enumeration,
  67. ** grep "ORDER LUA_T"
  68. */
  69. typedef enum {
  70. LUA_T_NIL = -10,
  71. LUA_T_NUMBER = -9,
  72. LUA_T_STRING = -8,
  73. LUA_T_ARRAY = -7, /* array==table */
  74. LUA_T_PROTO = -6,
  75. LUA_T_FUNCTION = -5,
  76. LUA_T_CFUNCTION= -4,
  77. LUA_T_MARK = -3,
  78. LUA_T_CMARK = -2,
  79. LUA_T_LINE = -1,
  80. LUA_T_USERDATA = 0
  81. } lua_Type;
  82. #define NUM_TYPES 11
  83. typedef union {
  84. lua_CFunction f;
  85. real n;
  86. TaggedString *ts;
  87. TProtoFunc *tf;
  88. struct Closure *cl;
  89. struct Hash *a;
  90. int i;
  91. } Value;
  92. typedef struct TObject {
  93. lua_Type ttype;
  94. Value value;
  95. } TObject;
  96. /* Macros to access structure members */
  97. #define ttype(o) ((o)->ttype)
  98. #define nvalue(o) ((o)->value.n)
  99. #define svalue(o) ((o)->value.ts->str)
  100. #define tsvalue(o) ((o)->value.ts)
  101. #define clvalue(o) ((o)->value.cl)
  102. #define avalue(o) ((o)->value.a)
  103. #define fvalue(o) ((o)->value.f)
  104. #define tfvalue(o) ((o)->value.tf)
  105. /*
  106. ** Closures
  107. */
  108. typedef struct Closure {
  109. GCnode head;
  110. TObject consts[1]; /* at least one for prototype */
  111. } Closure;
  112. typedef struct node {
  113. TObject ref;
  114. TObject val;
  115. } Node;
  116. typedef struct Hash {
  117. GCnode head;
  118. Node *node;
  119. int nhash;
  120. int nuse;
  121. int htag;
  122. } Hash;
  123. extern long luaO_nentities;
  124. extern char *luaO_typenames[];
  125. int luaO_equalObj (TObject *t1, TObject *t2);
  126. int luaO_redimension (int oldsize);
  127. int luaO_findstring (char *name, char *list[]);
  128. #endif