lobject.h 4.0 KB

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