aboutsummaryrefslogtreecommitdiff
path: root/fallback.c
blob: 3100ec3135a80c330906e5c18c6697021310f771 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
** fallback.c
** TecCGraf - PUC-Rio
*/
 
char *rcs_fallback="$Id: fallback.c,v 1.25 1996/04/25 14:10:00 roberto Exp roberto $";

#include <stdio.h>
#include <string.h>
 
#include "mem.h"
#include "fallback.h"
#include "opcode.h"
#include "lua.h"
#include "table.h"
#include "tree.h"
#include "hash.h"


static void errorFB (void);
static void indexFB (void);
static void gettableFB (void);
static void arithFB (void);
static void concatFB (void);
static void orderFB (void);
static void GDFB (void);
static void funcFB (void);


/*
** Warning: This list must be in the same order as the #define's
*/
struct FB  luaI_fallBacks[] = {
{"gettable", {LUA_T_CFUNCTION, {gettableFB}}, 2, 1},
{"arith", {LUA_T_CFUNCTION, {arithFB}}, 3, 1},
{"order", {LUA_T_CFUNCTION, {orderFB}}, 3, 1},
{"concat", {LUA_T_CFUNCTION, {concatFB}}, 2, 1},
{"settable", {LUA_T_CFUNCTION, {gettableFB}}, 3, 0},
{"gc", {LUA_T_CFUNCTION, {GDFB}}, 1, 0},
{"function", {LUA_T_CFUNCTION, {funcFB}}, -1, -1},
                                /* no fixed number of params or results */
{"getglobal", {LUA_T_CFUNCTION, {indexFB}}, 1, 1},
                                /* same default behavior of index FB */
{"index", {LUA_T_CFUNCTION, {indexFB}}, 2, 1},
{"error", {LUA_T_CFUNCTION, {errorFB}}, 1, 0}
};

#define N_FB  (sizeof(luaI_fallBacks)/sizeof(struct FB))

static int luaI_findevent (char *name)
{
  int i;
  for (i=0; i<N_FB; i++)
    if (strcmp(luaI_fallBacks[i].kind, name) == 0)
      return i;
  /* name not found */
  lua_error("invalid event name");
  return 0;  /* to avoid warnings */
}


void luaI_setfallback (void)
{
  int i;
  char *name = lua_getstring(lua_getparam(1));
  lua_Object func = lua_getparam(2);
  if (name == NULL || !lua_isfunction(func))
    lua_error("incorrect argument to function `setfallback'");
  i = luaI_findevent(name);
  luaI_pushobject(&luaI_fallBacks[i].function);
  luaI_fallBacks[i].function = *luaI_Address(func);
}


static void errorFB (void)
{
  lua_Object o = lua_getparam(1);
  if (lua_isstring(o))
    fprintf (stderr, "lua: %s\n", lua_getstring(o));
  else
    fprintf(stderr, "lua: unknown error\n");
}
 

static void indexFB (void)
{
  lua_pushnil();
}
 

static void gettableFB (void)
{
  lua_error("indexed expression not a table");
}
 

static void arithFB (void)
{
  lua_error("unexpected type at conversion to number");
}

static void concatFB (void)
{
  lua_error("unexpected type at conversion to string");
}


static void orderFB (void)
{
  lua_error("unexpected type at comparison");
}

static void GDFB (void) { }

static void funcFB (void)
{
  lua_error("call expression not a function");
}


/* -------------------------------------------
** Reference routines
*/

static struct ref {
  Object o;
  enum {LOCK, HOLD, FREE, COLLECTED} status;
} *refArray = NULL;
static int refSize = 0;

int luaI_ref (Object *object, int lock)
{
  int i;
  int oldSize;
  if (tag(object) == LUA_T_NIL)
    return -1;   /* special ref for nil */
  for (i=0; i<refSize; i++)
    if (refArray[i].status == FREE)
      goto found;
  /* no more empty spaces */
  oldSize = refSize;
  refSize = growvector(&refArray, refSize, struct ref, refEM, MAX_WORD);
  for (i=oldSize; i<refSize; i++)
    refArray[i].status = FREE;
  i = oldSize;
 found:
  refArray[i].o = *object;
  refArray[i].status = lock ? LOCK : HOLD;
  return i;
}


void lua_unref (int ref)
{
  if (ref >= 0 && ref < refSize)
    refArray[ref].status = FREE;
}


Object *luaI_getref (int ref)
{
  static Object nul = {LUA_T_NIL, {0}};
  if (ref == -1)
    return &nul;
  if (ref >= 0 && ref < refSize &&
      (refArray[ref].status == LOCK || refArray[ref].status == HOLD))
    return &refArray[ref].o;
  else
    return NULL;
}


void luaI_travlock (int (*fn)(Object *))
{
  int i;
  for (i=0; i<refSize; i++)
    if (refArray[i].status == LOCK)
      fn(&refArray[i].o);
}


void luaI_invalidaterefs (void)
{
  int i;
  for (i=0; i<refSize; i++)
    if (refArray[i].status == HOLD && !luaI_ismarked(&refArray[i].o))
      refArray[i].status = COLLECTED;
}

char *luaI_travfallbacks (int (*fn)(Object *))
{
  int i;
  for (i=0; i<N_FB; i++)
    if (fn(&luaI_fallBacks[i].function))
      return luaI_fallBacks[i].kind;
  return NULL;
}


/* -------------------------------------------
* Internal Methods 
*/
#define BASE_TAG 1000

static struct IM {
  lua_Type tp;
  Object int_method[FB_N];
 } *luaI_IMtable = NULL;
static int IMtable_size = 0;
static int last_tag = BASE_TAG-1;

int lua_newtag (char *t)
{
  int i;
  ++last_tag;
  if ((last_tag-BASE_TAG) >= IMtable_size)
    IMtable_size = growvector(&luaI_IMtable, IMtable_size,
                              struct IM, memEM, MAX_INT);
  if (strcmp(t, "table") == 0)
    luaI_IMtable[last_tag-BASE_TAG].tp = LUA_T_ARRAY;
  else if (strcmp(t, "userdata") == 0)
    luaI_IMtable[last_tag-BASE_TAG].tp = LUA_T_USERDATA;
  else
    lua_error("invalid type for new tag");
  for (i=0; i<FB_N; i++)
    luaI_IMtable[last_tag-BASE_TAG].int_method[i].tag = LUA_T_NIL;
  return last_tag;
}

static int validtag (int tag)
{
  return (BASE_TAG <= tag && tag <= last_tag);
}

static void checktag (int tag)
{
  if (!validtag(tag))
    lua_error("invalid tag");
}

void luaI_settag (int tag, Object *o)
{
  checktag(tag);
  if (tag(o) != luaI_IMtable[tag-BASE_TAG].tp)
    lua_error("Tag is not compatible with this type");
  if (o->tag == LUA_T_ARRAY)
    o->value.a->htag = tag;
  else  /* must be userdata */
    o->value.ts->tag = tag;
}

int luaI_tag (Object *o)
{
  lua_Type t = tag(o);
  if (t == LUA_T_USERDATA)
    return o->value.ts->tag;
  else if (t == LUA_T_ARRAY)
    return o->value.a->htag;
  else return t;
}

Object *luaI_getim (int tag, int event)
{
  if (tag == 0)
    return &luaI_fallBacks[event].function;
  else if (validtag(tag)) {
    Object *func = &luaI_IMtable[tag-BASE_TAG].int_method[event];
    if (func->tag == LUA_T_NIL)
      return NULL;
    else
      return func;
  }
  else return NULL;
}

void luaI_setintmethod (void)
{
  lua_Object tag = lua_getparam(1);
  lua_Object event = lua_getparam(2);
  lua_Object func = lua_getparam(3);
  if (!(lua_isnumber(tag) && lua_isstring(event) && lua_isfunction(func)))
    lua_error("incorrect arguments to function `setintmethod'");
  else {
    int i = luaI_findevent(lua_getstring(event));
    int t = lua_getnumber(tag);
    checktag(t);
    luaI_IMtable[t-BASE_TAG].int_method[i] = *luaI_Address(func);
  }
}