Imported from ../lua-3.2.tar.gz.
[lua.git] / src / lobject.h
blobf3b21477c4dbe43bda8798836df909b83ea63439
1 /*
2 ** $Id: lobject.h,v 1.28 1999/03/16 16:43:27 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 typedef LUA_NUM_TYPE real;
38 #define Byte lua_Byte /* some systems have Byte as a predefined type */
39 typedef unsigned char Byte; /* unsigned 8 bits */
42 #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
44 typedef unsigned int IntPoint; /* unsigned with same size as a pointer (for hashing) */
48 ** Lua TYPES
49 ** WARNING: if you change the order of this enumeration,
50 ** grep "ORDER LUA_T"
52 typedef enum {
53 LUA_T_USERDATA = 0, /* tag default for userdata */
54 LUA_T_NUMBER = -1, /* fixed tag for numbers */
55 LUA_T_STRING = -2, /* fixed tag for strings */
56 LUA_T_ARRAY = -3, /* tag default for tables (or arrays) */
57 LUA_T_PROTO = -4, /* fixed tag for functions */
58 LUA_T_CPROTO = -5, /* fixed tag for Cfunctions */
59 LUA_T_NIL = -6, /* last "pre-defined" tag */
60 LUA_T_CLOSURE = -7,
61 LUA_T_CLMARK = -8, /* mark for closures */
62 LUA_T_PMARK = -9, /* mark for Lua prototypes */
63 LUA_T_CMARK = -10, /* mark for C prototypes */
64 LUA_T_LINE = -11
65 } lua_Type;
67 #define NUM_TAGS 7
70 typedef union {
71 lua_CFunction f; /* LUA_T_CPROTO, LUA_T_CMARK */
72 real n; /* LUA_T_NUMBER */
73 struct TaggedString *ts; /* LUA_T_STRING, LUA_T_USERDATA */
74 struct TProtoFunc *tf; /* LUA_T_PROTO, LUA_T_PMARK */
75 struct Closure *cl; /* LUA_T_CLOSURE, LUA_T_CLMARK */
76 struct Hash *a; /* LUA_T_ARRAY */
77 int i; /* LUA_T_LINE */
78 } Value;
81 typedef struct TObject {
82 lua_Type ttype;
83 Value value;
84 } TObject;
89 ** generic header for garbage collector lists
91 typedef struct GCnode {
92 struct GCnode *next;
93 int marked;
94 } GCnode;
98 ** String headers for string table
101 typedef struct TaggedString {
102 GCnode head;
103 unsigned long hash;
104 int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
105 union {
106 struct {
107 TObject globalval;
108 long len; /* if this is a string, here is its length */
109 } s;
110 struct {
111 int tag;
112 void *v; /* if this is a userdata, here is its value */
113 } d;
114 } u;
115 char str[1]; /* \0 byte already reserved */
116 } TaggedString;
122 ** Function Prototypes
124 typedef struct TProtoFunc {
125 GCnode head;
126 struct TObject *consts;
127 int nconsts;
128 Byte *code; /* ends with opcode ENDCODE */
129 int lineDefined;
130 TaggedString *source;
131 struct LocVar *locvars; /* ends with line = -1 */
132 } TProtoFunc;
134 typedef struct LocVar {
135 TaggedString *varname; /* NULL signals end of scope */
136 int line;
137 } LocVar;
143 /* Macros to access structure members */
144 #define ttype(o) ((o)->ttype)
145 #define nvalue(o) ((o)->value.n)
146 #define svalue(o) ((o)->value.ts->str)
147 #define tsvalue(o) ((o)->value.ts)
148 #define clvalue(o) ((o)->value.cl)
149 #define avalue(o) ((o)->value.a)
150 #define fvalue(o) ((o)->value.f)
151 #define tfvalue(o) ((o)->value.tf)
153 #define protovalue(o) ((o)->value.cl->consts)
157 ** Closures
159 typedef struct Closure {
160 GCnode head;
161 int nelems; /* not included the first one (always the prototype) */
162 TObject consts[1]; /* at least one for prototype */
163 } Closure;
167 typedef struct node {
168 TObject ref;
169 TObject val;
170 } Node;
172 typedef struct Hash {
173 GCnode head;
174 Node *node;
175 int nhash;
176 int nuse;
177 int htag;
178 } Hash;
181 extern char *luaO_typenames[];
183 #define luaO_typename(o) luaO_typenames[-ttype(o)]
186 extern TObject luaO_nilobject;
188 #define luaO_equalObj(t1,t2) ((ttype(t1) != ttype(t2)) ? 0 \
189 : luaO_equalval(t1,t2))
190 int luaO_equalval (TObject *t1, TObject *t2);
191 int luaO_redimension (int oldsize);
192 void luaO_insertlist (GCnode *root, GCnode *node);
193 double luaO_str2d (char *s);
195 #ifdef OLD_ANSI
196 void luaO_memup (void *dest, void *src, int size);
197 void luaO_memdown (void *dest, void *src, int size);
198 #else
199 #include <string.h>
200 #define luaO_memup(d,s,n) memmove(d,s,n)
201 #define luaO_memdown(d,s,n) memmove(d,s,n)
202 #endif
204 #endif