summaryrefslogtreecommitdiff
path: root/lobject.h
blob: 921a7a05390b00404801761b8ac08b0b9d26bfe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
** $Id: lobject.h,v 1.60 2000/04/10 19:20:24 roberto Exp roberto $
** Type definitions for Lua objects
** See Copyright Notice in lua.h
*/

#ifndef lobject_h
#define lobject_h


#include "llimits.h"
#include "lua.h"


#ifdef DEBUG
#undef NDEBUG
#include <assert.h>
#define LUA_INTERNALERROR(L,s)	assert(((void)s,0))
#define LUA_ASSERT(L,c,s)	assert(((void)s,(c)))
#else
#define LUA_INTERNALERROR(L,s)	/* empty */
#define LUA_ASSERT(L,c,s)	/* empty */
#endif


#ifdef DEBUG
/* to avoid warnings, and make sure value is really unused */
#define UNUSED(x)	(x=0, (void)(x))
#else
#define UNUSED(x)	((void)(x))	/* to avoid warnings */
#endif


/*
** Lua TYPES
** WARNING: if you change the order of this enumeration,
** grep "ORDER LUA_T"
*/
typedef enum {
  TAG_USERDATA  =  0, /* default tag for userdata */
  TAG_NUMBER,	/* fixed tag for numbers */
  TAG_STRING,	/* fixed tag for strings */
  TAG_TABLE,	/* default tag for tables */
  TAG_LCLOSURE,	/* fixed tag for Lua closures */
  TAG_CCLOSURE,	/* fixed tag for C closures */
  TAG_NIL,	/* last "pre-defined" tag */

  TAG_LMARK,	/* mark for Lua closures */
  TAG_CMARK,	/* mark for C closures */

  TAG_LINE
} lua_Type;

/* tags for values visible from Lua == first user-created tag */
#define NUM_TAGS	7


/*
** check whether `t' is a mark
*/
#define is_T_MARK(t)	((t) == TAG_LMARK || (t) == TAG_CMARK)


typedef union {
  struct TString *ts;	/* TAG_STRING, TAG_USERDATA */
  struct Closure *cl;	/* TAG_[CL]CLOSURE, TAG_[CL]MARK */
  struct Hash *a;	/* TAG_TABLE */
  Number n;		/* TAG_NUMBER */
  int i;		/* TAG_LINE */
} Value;


/* Macros to access values */
#define ttype(o)        ((o)->ttype)
#define nvalue(o)       ((o)->value.n)
#define svalue(o)       ((o)->value.ts->str)
#define tsvalue(o)      ((o)->value.ts)
#define clvalue(o)      ((o)->value.cl)
#define avalue(o)       ((o)->value.a)


typedef struct TObject {
  lua_Type ttype;
  Value value;
} TObject;


typedef struct GlobalVar {
  TObject value;
  struct GlobalVar *next;
  struct TString *name;
} GlobalVar;


/*
** String headers for string table
*/
typedef struct TString {
  union {
    struct {  /* for strings */
      GlobalVar *gv;  /* eventual global value with this name */
      long len;
    } s;
    struct {  /* for userdata */
      int tag;
      void *value;
    } d;
  } u;
  struct TString *nexthash;  /* chain for hash table */
  unsigned long hash;
  int constindex;  /* hint to reuse constants (= -1 if this is a userdata) */
  unsigned char marked;
  char str[1];   /* variable length string!! must be the last field! */
} TString;


/*
** Function Prototypes
*/
typedef struct Proto {
  struct Proto *next;
  int marked;
  struct TString **kstr;  /* strings used by the function */
  int nkstr;  /* size of `kstr' */
  Number *knum;  /* Number numbers used by the function */
  int nknum;  /* size of `knum' */
  struct Proto **kproto;  /* functions defined inside the function */
  int nkproto;  /* size of `kproto' */
  Instruction *code;  /* ends with opcode ENDCODE */
  int lineDefined;
  TString  *source;
  int numparams;
  int is_vararg;
  int maxstacksize;
  struct LocVar *locvars;  /* ends with line = -1 */
} Proto;


typedef struct LocVar {
  TString *varname;           /* NULL signals end of scope */
  int line;
} LocVar;


/*
** Closures
*/
typedef struct Closure {
  struct Closure *next;
  int marked;
  union {
    lua_CFunction c;  /* C functions */
    struct Proto *l;  /* Lua functions */
  } f;
  int nelems;
  TObject consts[1];
} Closure;


typedef struct Node {
  TObject key;
  TObject val;
  struct Node *next;  /* for chaining */
} Node;

typedef struct Hash {
  int htag;
  Node *node;
  int size;
  Node *firstfree;  /* this position is free; all positions after it are full */
  struct Hash *next;
  int marked;
} Hash;


extern const char *const luaO_typenames[];
extern const TObject luaO_nilobject;

#define luaO_typename(o)        luaO_typenames[ttype(o)]

unsigned long luaO_power2 (unsigned long n);

int luaO_equalObj (const TObject *t1, const TObject *t2);
int luaO_redimension (lua_State *L, int oldsize);
int luaO_str2d (const char *s, Number *result);


#endif