Imported from ../lua-3.1.tar.gz.
[lua.git] / src / lobject.h
blobfbd6070c925ce9579400b34c9621fffed67a0ba7
1 /*
2 ** $Id: lobject.h,v 1.21 1998/06/18 16:57:03 roberto Exp $
3 ** Type definitions for Lua objects
4 ** See Copyright Notice in lua.h
5 */
7 #ifndef lobject_h
8 #define lobject_h
11 #include <limits.h>
13 #include "lua.h"
16 #ifdef DEBUG
17 #include "lauxlib.h"
18 #define LUA_INTERNALERROR(s) \
19 luaL_verror("INTERNAL ERROR - %s [%s:%d]",(s),__FILE__,__LINE__)
20 #define LUA_ASSERT(c,s) { if (!(c)) LUA_INTERNALERROR(s); }
21 #else
22 #define LUA_INTERNALERROR(s) /* empty */
23 #define LUA_ASSERT(c,s) /* empty */
24 #endif
28 ** "real" is the type "number" of Lua
29 ** GREP LUA_NUMBER to change that
31 #ifndef LUA_NUM_TYPE
32 #define LUA_NUM_TYPE double
33 #endif
36 ** format to convert number to strings
38 #define NUMBER_FMT "%g"
40 typedef LUA_NUM_TYPE real;
42 #define Byte lua_Byte /* some systems have Byte as a predefined type */
43 typedef unsigned char Byte; /* unsigned 8 bits */
46 #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
48 /* maximum value of a word of 2 bytes (-2 for safety); must fit in an "int" */
49 #if MAX_INT < 65534
50 #define MAX_WORD MAX_INT
51 #else
52 #define MAX_WORD 65534
53 #endif
55 typedef unsigned int IntPoint; /* unsigned with same size as a pointer (for hashing) */
59 ** Lua TYPES
60 ** WARNING: if you change the order of this enumeration,
61 ** grep "ORDER LUA_T"
63 typedef enum {
64 LUA_T_USERDATA = 0, /* tag default for userdata */
65 LUA_T_NUMBER = -1, /* fixed tag for numbers */
66 LUA_T_STRING = -2, /* fixed tag for strings */
67 LUA_T_ARRAY = -3, /* tag default for tables (or arrays) */
68 LUA_T_PROTO = -4, /* fixed tag for functions */
69 LUA_T_CPROTO = -5, /* fixed tag for Cfunctions */
70 LUA_T_NIL = -6, /* last "pre-defined" tag */
71 LUA_T_CLOSURE = -7,
72 LUA_T_CLMARK = -8, /* mark for closures */
73 LUA_T_PMARK = -9, /* mark for Lua prototypes */
74 LUA_T_CMARK = -10, /* mark for C prototypes */
75 LUA_T_LINE = -11
76 } lua_Type;
78 #define NUM_TYPES 11
79 #define NUM_TAGS 7
82 typedef union {
83 lua_CFunction f; /* LUA_T_CPROTO, LUA_T_CMARK */
84 real n; /* LUA_T_NUMBER */
85 struct TaggedString *ts; /* LUA_T_STRING, LUA_T_USERDATA */
86 struct TProtoFunc *tf; /* LUA_T_PROTO, LUA_T_PMARK */
87 struct Closure *cl; /* LUA_T_CLOSURE, LUA_T_CLMARK */
88 struct Hash *a; /* LUA_T_ARRAY */
89 int i; /* LUA_T_LINE */
90 } Value;
93 typedef struct TObject {
94 lua_Type ttype;
95 Value value;
96 } TObject;
101 ** generic header for garbage collector lists
103 typedef struct GCnode {
104 struct GCnode *next;
105 int marked;
106 } GCnode;
110 ** String headers for string table
113 typedef struct TaggedString {
114 GCnode head;
115 unsigned long hash;
116 int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
117 union {
118 struct {
119 TObject globalval;
120 long len; /* if this is a string, here is its length */
121 } s;
122 struct {
123 int tag;
124 void *v; /* if this is a userdata, here is its value */
125 } d;
126 } u;
127 char str[1]; /* \0 byte already reserved */
128 } TaggedString;
134 ** Function Prototypes
136 typedef struct TProtoFunc {
137 GCnode head;
138 struct TObject *consts;
139 int nconsts;
140 Byte *code; /* ends with opcode ENDCODE */
141 int lineDefined;
142 TaggedString *fileName;
143 struct LocVar *locvars; /* ends with line = -1 */
144 } TProtoFunc;
146 typedef struct LocVar {
147 TaggedString *varname; /* NULL signals end of scope */
148 int line;
149 } LocVar;
155 /* Macros to access structure members */
156 #define ttype(o) ((o)->ttype)
157 #define nvalue(o) ((o)->value.n)
158 #define svalue(o) ((o)->value.ts->str)
159 #define tsvalue(o) ((o)->value.ts)
160 #define clvalue(o) ((o)->value.cl)
161 #define avalue(o) ((o)->value.a)
162 #define fvalue(o) ((o)->value.f)
163 #define tfvalue(o) ((o)->value.tf)
165 #define protovalue(o) ((o)->value.cl->consts)
169 ** Closures
171 typedef struct Closure {
172 GCnode head;
173 int nelems; /* not included the first one (always the prototype) */
174 TObject consts[1]; /* at least one for prototype */
175 } Closure;
179 typedef struct node {
180 TObject ref;
181 TObject val;
182 } Node;
184 typedef struct Hash {
185 GCnode head;
186 Node *node;
187 int nhash;
188 int nuse;
189 int htag;
190 } Hash;
193 extern char *luaO_typenames[];
195 extern TObject luaO_nilobject;
197 int luaO_equalObj (TObject *t1, TObject *t2);
198 int luaO_redimension (int oldsize);
199 void luaO_insertlist (GCnode *root, GCnode *node);
201 #ifdef OLD_ANSI
202 void luaO_memup (void *dest, void *src, int size);
203 void luaO_memdown (void *dest, void *src, int size);
204 #else
205 #include <string.h>
206 #define luaO_memup(d,s,n) memmove(d,s,n)
207 #define luaO_memdown(d,s,n) memmove(d,s,n)
208 #endif
210 #endif