summaryrefslogtreecommitdiff
path: root/ltests.c
blob: 9aef6953b03001b1bd9791bc5f193074e705a3c7 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
** $Id: ltests.c,v 1.28 2000/06/28 17:06:07 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h
*/


#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#include "lua.h"

#include "lapi.h"
#include "lauxlib.h"
#include "lcode.h"
#include "ldo.h"
#include "lfunc.h"
#include "lmem.h"
#include "lopcodes.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "luadebug.h"


void luaB_opentests (lua_State *L);


/*
** The whole module only makes sense with DEBUG on
*/
#ifdef DEBUG



static void setnameval (lua_Object t, const char *name, int val) {
  lua_pushobject(t);
  lua_pushstring(name);
  lua_pushnumber(val);
  lua_settable();
}


/*
** {======================================================
** Disassembler
** =======================================================
*/


static const char *const instrname[NUM_OPCODES] = {
  "END", "RETURN", "CALL", "TAILCALL", "PUSHNIL", "POP", "PUSHINT", 
  "PUSHSTRING", "PUSHNUM", "PUSHNEGNUM", "PUSHUPVALUE", "GETLOCAL", 
  "GETGLOBAL", "GETTABLE", "GETDOTTED", "GETINDEXED", "PUSHSELF", 
  "CREATETABLE", "SETLOCAL", "SETGLOBAL", "SETTABLE", "SETLIST", "SETMAP", 
  "ADD", "ADDI", "SUB", "MULT", "DIV", "POW", "CONCAT", "MINUS", "NOT", 
  "JMPNE", "JMPEQ", "JMPLT", "JMPLE", "JMPGT", "JMPGE", "JMPT", "JMPF", 
  "JMPONT", "JMPONF", "JMP", "PUSHNILJMP", "FORPREP", "FORLOOP", "LFORPREP", 
  "LFORLOOP", "CLOSURE"
};


static int pushop (Proto *p, int pc) {
  char buff[100];
  Instruction i = p->code[pc];
  OpCode o = GET_OPCODE(i);
  const char *name = instrname[o];
  int *line = p->lines;
  if (line)
    sprintf(buff, "%5d - ", line[pc]);
  else
    strcpy(buff, "         ");
  switch ((enum Mode)luaK_opproperties[o].mode) {  
    case iO:
      sprintf(buff+8, "%s", name);
      break;
    case iU:
      sprintf(buff+8, "%-12s%4u", name, GETARG_U(i));
      break;
    case iS:
      sprintf(buff+8, "%-12s%4d", name, GETARG_S(i));
      break;
    case iAB:
      sprintf(buff+8, "%-12s%4d %4d", name, GETARG_A(i), GETARG_B(i));
      break;
  }
  lua_pushstring(buff);
  return (o != OP_END);
}


static void listcode (void) {
  lua_Object o = luaL_nonnullarg(1);
  lua_Object t = lua_createtable();
  int pc;
  Proto *p;
  int res;
  luaL_arg_check(ttype(o) == TAG_LCLOSURE, 1, "Lua function expected");
  p = clvalue(o)->f.l;
  setnameval(t, "maxstack", p->maxstacksize);
  setnameval(t, "numparams", p->numparams);
  pc = 0;
  do {
    lua_pushobject(t);
    lua_pushnumber(pc+1);
    res = pushop(p, pc++);
    lua_settable();
  } while (res);
  lua_pushobject(t);
}


static void liststrings (void) {
  lua_Object o = luaL_nonnullarg(1);
  lua_Object t = lua_createtable();
  Proto *p;
  int i;
  luaL_arg_check(ttype(o) == TAG_LCLOSURE, 1, "Lua function expected");
  p = clvalue(o)->f.l;
  for (i=0; i<p->nkstr; i++) {
    lua_pushobject(t);
    lua_pushnumber(i+1);
    lua_pushstring(p->kstr[i]->str);
    lua_settable();
  }
  lua_pushobject(t);
}


static void listlocals (void) {
  lua_Object o = luaL_nonnullarg(1);
  Proto *p;
  int pc = luaL_check_int(2) - 1;
  int i = 1;
  const char *name;
  luaL_arg_check(ttype(o) == TAG_LCLOSURE, 1, "Lua function expected");
  p = clvalue(o)->f.l;
  while ((name = luaF_getlocalname(p, i++, pc)) != NULL)
    lua_pushstring(name);
}

/* }====================================================== */



static void get_limits (void) {
  lua_Object t = lua_createtable();
  setnameval(t, "BITS_INT", BITS_INT);
  setnameval(t, "LFPF", LFIELDS_PER_FLUSH);
  setnameval(t, "MAXARG_A", MAXARG_A);
  setnameval(t, "MAXARG_B", MAXARG_B);
  setnameval(t, "MAXARG_S", MAXARG_S);
  setnameval(t, "MAXARG_U", MAXARG_U);
  setnameval(t, "MAXLOCALS", MAXLOCALS);
  setnameval(t, "MAXPARAMS", MAXPARAMS);
  setnameval(t, "MAXSTACK", MAXSTACK);
  setnameval(t, "MAXUPVALUES", MAXUPVALUES);
  setnameval(t, "MAXVARSLH", MAXVARSLH);
  setnameval(t, "RFPF", RFIELDS_PER_FLUSH);
  setnameval(t, "SIZE_A", SIZE_A);
  setnameval(t, "SIZE_B", SIZE_B);
  setnameval(t, "SIZE_OP", SIZE_OP);
  setnameval(t, "SIZE_U", SIZE_U);
  lua_pushobject(t);
}


static void mem_query (void) {
  lua_pushnumber(memdebug_total);
  lua_pushnumber(memdebug_numblocks);
  lua_pushnumber(memdebug_maxmem);
}


static void hash_query (void) {
  lua_Object o = luaL_nonnullarg(1);
  if (lua_getparam(2) == LUA_NOOBJECT) {
    luaL_arg_check(ttype(o) == TAG_STRING, 1, "string expected");
    lua_pushnumber(tsvalue(o)->u.s.hash);
  }
  else {
    const Hash *t = hvalue(luaL_tablearg(2));
    lua_pushnumber(luaH_mainposition(t, o) - t->node);
  }
}


static void table_query (void) {
  const Hash *t = hvalue(luaL_tablearg(1));
  int i = luaL_opt_int(2, -1);
  if (i == -1) {
    lua_pushnumber(t->size);
    lua_pushnumber(t->firstfree - t->node);
  }
  else if (i < t->size) {
    luaA_pushobject(lua_state, &t->node[i].key);
    luaA_pushobject(lua_state, &t->node[i].val);
    if (t->node[i].next)
      lua_pushnumber(t->node[i].next - t->node);
  }
}


static void string_query (void) {
  lua_State *L = lua_state;
  stringtable *tb = (*luaL_check_string(1) == 's') ? &L->strt : &L->udt;
  int s = luaL_opt_int(2, 0) - 1;
  if (s==-1) {
    lua_pushnumber(tb->nuse);
    lua_pushnumber(tb->size);
  }
  else if (s < tb->size) {
    TString *ts;
    for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
      ttype(L->top) = TAG_STRING;
      tsvalue(L->top) = ts;
      incr_top;
    }
  }
}

/*
** {======================================================
** function to test the API with C. It interprets a kind of "assembler"
** language with calls to the API, so the test can be driven by Lua code
** =======================================================
*/

static const char *const delimits = " \t\n,;";

static void skip (const char **pc) {
  while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
}

static int getnum (const char **pc) {
  int res = 0;
  skip(pc);
  while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
  return res;
}
  
static int getreg (const char **pc) {
  skip(pc);
  if (*(*pc)++ != 'r') lua_error("`testC' expecting a register");
  return getnum(pc);
}

static const char *getname (const char **pc) {
  static char buff[30];
  int i = 0;
  skip(pc);
  while (**pc != '\0' && !strchr(delimits, **pc))
    buff[i++] = *(*pc)++;
  buff[i] = '\0';
  return buff;
}


#define EQ(s1)	(strcmp(s1, inst) == 0)


static void testC (void) {
  lua_Object reg[10];
  const char *pc = luaL_check_string(1);
  for (;;) {
    const char *inst = getname(&pc);
    if EQ("") return;
    else if EQ("pushnum") {
      lua_pushnumber(getnum(&pc));
    }
    else if EQ("createtable") {
      reg[getreg(&pc)] = lua_createtable();
    }
    else if EQ("closure") {
      lua_CFunction f = lua_getcfunction(lua_getglobal(getname(&pc)));
      lua_pushcclosure(f, getnum(&pc));
    }
    else if EQ("pop") {
      reg[getreg(&pc)] = lua_pop();
    }
    else if EQ("getglobal") {
      int n = getreg(&pc);
      reg[n] = lua_getglobal(getname(&pc));
    }
    else if EQ("ref") {
      lua_pushnumber(lua_ref(0));
      reg[getreg(&pc)] = lua_pop();
    }
    else if EQ("reflock") {
      lua_pushnumber(lua_ref(1));
      reg[getreg(&pc)] = lua_pop();
    }
    else if EQ("getref") {
      int n = getreg(&pc);
      reg[n] = lua_getref((int)lua_getnumber(reg[getreg(&pc)]));
    }
    else if EQ("unref") {
      lua_unref((int)lua_getnumber(reg[getreg(&pc)]));
    }
    else if EQ("getparam") {
      int n = getreg(&pc);
      reg[n] = lua_getparam(getnum(&pc)+1);  /* skips the command itself */
    }
    else if EQ("getresult") {
      int n = getreg(&pc);
      reg[n] = lua_getparam(getnum(&pc));
    }
    else if EQ("setglobal") {
      lua_setglobal(getname(&pc));
    }
    else if EQ("pushglobals") {
      lua_pushglobaltable();
    }
    else if EQ("pushstring") {
      lua_pushstring(getname(&pc));
    }
    else if EQ("pushreg") {
      lua_pushobject(reg[getreg(&pc)]);
    }
    else if EQ("call") {
      if (lua_call(getname(&pc))) lua_error(NULL);
    }
    else if EQ("gettable") {
      reg[getreg(&pc)] = lua_gettable();
    }
    else if EQ("rawget") {
      reg[getreg(&pc)] = lua_rawget();
    }
    else if EQ("settable") {
      lua_settable();
    }
    else if EQ("rawset") {
      lua_rawset();
    }
    else if EQ("tag") {
      lua_pushnumber(lua_tag(reg[getreg(&pc)]));
    }
    else if EQ("type") {
      lua_pushstring(lua_type(reg[getreg(&pc)]));
    }
    else if EQ("next") {
      int n = getreg(&pc);
      n = lua_next(reg[n], (int)lua_getnumber(reg[getreg(&pc)]));
      lua_pushnumber(n);
    }
    else if EQ("equal") {
      int n1 = getreg(&pc);
      int n2 = getreg(&pc);
      lua_pushnumber(lua_equal(reg[n1], reg[n2]));
    }
    else if EQ("pushusertag") {
      int val = getreg(&pc);
      int tag = getreg(&pc);
      lua_pushusertag((void *)(int)lua_getnumber(reg[val]),
                         (int)lua_getnumber(reg[tag]));
    }
    else if EQ("udataval") {
      int n = getreg(&pc);
      lua_pushnumber((int)lua_getuserdata(reg[getreg(&pc)]));
      reg[n] = lua_pop();
    }
    else if EQ("settagmethod") {
      int n = getreg(&pc);
      lua_settagmethod((int)lua_getnumber(reg[n]), getname(&pc));
    }
    else if EQ("beginblock") {
      lua_beginblock();
    }
    else if EQ("endblock") {
      lua_endblock();
    }
    else if EQ("newstate") {
      int stacksize = getnum(&pc);
      lua_State *L1 = lua_newstate(stacksize, getnum(&pc));
      lua_pushuserdata(L1);
    }
    else if EQ("closestate") {
      (lua_close)((lua_State *)lua_getuserdata(reg[getreg(&pc)]));
    }
    else if EQ("doremote") {
      lua_Object ol1 = reg[getreg(&pc)];
      lua_Object str = reg[getreg(&pc)];
      lua_State *L1;
      lua_Object temp;
      int i;
      if (!lua_isuserdata(ol1) || !lua_isstring(str))
        lua_error("bad arguments for `doremote'");
      L1 = (lua_State *)lua_getuserdata(ol1);
      (lua_dostring)(L1, lua_getstring(str));
      i = 1;
      while ((temp = (lua_getresult)(L1, i++)) != LUA_NOOBJECT)
        lua_pushstring((lua_getstring)(L1, temp));
    }
#if LUA_DEPRECATETFUNCS
    else if EQ("rawsetglobal") {
      lua_rawsetglobal(getname(&pc));
    }
    else if EQ("rawgetglobal") {
      int n = getreg(&pc);
      reg[n] = lua_rawgetglobal(getname(&pc));
    }
#endif
    else luaL_verror(lua_state, "unknown command in `testC': %.20s", inst);
  }
}

/* }====================================================== */



static const struct luaL_reg tests_funcs[] = {
  {"hash", (lua_CFunction)hash_query},
  {"limits", (lua_CFunction)get_limits},
  {"listcode", (lua_CFunction)listcode},
  {"liststrings", (lua_CFunction)liststrings},
  {"listlocals", (lua_CFunction)listlocals},
  {"querystr", (lua_CFunction)string_query},
  {"querytab", (lua_CFunction)table_query},
  {"testC", (lua_CFunction)testC},
  {"totalmem", (lua_CFunction)mem_query}
};


void luaB_opentests (lua_State *L) {
  if (lua_state != NULL) return;  /* do not open tests for auxiliar states */
  lua_state = L;
  luaL_openl(tests_funcs);
}

#endif