misc/liblua/ldebug.c
author unc0rr
Fri, 15 Jul 2016 22:39:07 +0300
branchqmlfrontend
changeset 11846 6adf27e85cae
parent 10017 de822cd3df3a
permissions -rw-r--r--
merge default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     1
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     2
** $Id: ldebug.c,v 2.29.1.6 2008/05/08 16:56:26 roberto Exp $
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     3
** Debug Interface
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     4
** See Copyright Notice in lua.h
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     5
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     6
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     7
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     8
#include <stdarg.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     9
#include <stddef.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    10
#include <string.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    11
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    12
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    13
#define ldebug_c
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    14
#define LUA_CORE
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    15
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    16
#include "lua.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    17
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    18
#include "lapi.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    19
#include "lcode.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    20
#include "ldebug.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    21
#include "ldo.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    22
#include "lfunc.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    23
#include "lobject.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    24
#include "lopcodes.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    25
#include "lstate.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    26
#include "lstring.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    27
#include "ltable.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    28
#include "ltm.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    29
#include "lvm.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    30
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    31
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    32
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    33
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    34
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    35
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    36
static int currentpc (lua_State *L, CallInfo *ci) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    37
  if (!isLua(ci)) return -1;  /* function is not a Lua function? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    38
  if (ci == L->ci)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    39
    ci->savedpc = L->savedpc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    40
  return pcRel(ci->savedpc, ci_func(ci)->l.p);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    41
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    42
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    43
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    44
static int currentline (lua_State *L, CallInfo *ci) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    45
  int pc = currentpc(L, ci);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    46
  if (pc < 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    47
    return -1;  /* only active lua functions have current-line information */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    48
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    49
    return getline(ci_func(ci)->l.p, pc);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    50
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    51
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    52
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    53
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    54
** this function can be called asynchronous (e.g. during a signal)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    55
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    56
LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    57
  if (func == NULL || mask == 0) {  /* turn off hooks? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    58
    mask = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    59
    func = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    60
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    61
  L->hook = func;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    62
  L->basehookcount = count;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    63
  resethookcount(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    64
  L->hookmask = cast_byte(mask);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    65
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    66
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    67
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    68
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    69
LUA_API lua_Hook lua_gethook (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    70
  return L->hook;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    71
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    72
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    73
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    74
LUA_API int lua_gethookmask (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    75
  return L->hookmask;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    76
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    77
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    78
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    79
LUA_API int lua_gethookcount (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    80
  return L->basehookcount;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    81
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    82
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    83
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    84
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    85
  int status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    86
  CallInfo *ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    87
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    88
  for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    89
    level--;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    90
    if (f_isLua(ci))  /* Lua function? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    91
      level -= ci->tailcalls;  /* skip lost tail calls */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    92
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    93
  if (level == 0 && ci > L->base_ci) {  /* level found? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    94
    status = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    95
    ar->i_ci = cast_int(ci - L->base_ci);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    96
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    97
  else if (level < 0) {  /* level is of a lost tail call? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    98
    status = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    99
    ar->i_ci = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   100
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   101
  else status = 0;  /* no such level */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   102
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   103
  return status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   104
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   105
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   106
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   107
static Proto *getluaproto (CallInfo *ci) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   108
  return (isLua(ci) ? ci_func(ci)->l.p : NULL);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   109
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   110
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   111
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   112
static const char *findlocal (lua_State *L, CallInfo *ci, int n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   113
  const char *name;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   114
  Proto *fp = getluaproto(ci);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   115
  if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   116
    return name;  /* is a local variable in a Lua function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   117
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   118
    StkId limit = (ci == L->ci) ? L->top : (ci+1)->func;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   119
    if (limit - ci->base >= n && n > 0)  /* is 'n' inside 'ci' stack? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   120
      return "(*temporary)";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   121
    else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   122
      return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   123
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   124
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   125
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   126
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   127
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   128
  CallInfo *ci = L->base_ci + ar->i_ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   129
  const char *name = findlocal(L, ci, n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   130
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   131
  if (name)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   132
      luaA_pushobject(L, ci->base + (n - 1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   133
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   134
  return name;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   135
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   136
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   137
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   138
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   139
  CallInfo *ci = L->base_ci + ar->i_ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   140
  const char *name = findlocal(L, ci, n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   141
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   142
  if (name)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   143
      setobjs2s(L, ci->base + (n - 1), L->top - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   144
  L->top--;  /* pop value */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   145
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   146
  return name;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   147
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   148
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   149
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   150
static void funcinfo (lua_Debug *ar, Closure *cl) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   151
  if (cl->c.isC) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   152
    ar->source = "=[C]";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   153
    ar->linedefined = -1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   154
    ar->lastlinedefined = -1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   155
    ar->what = "C";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   156
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   157
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   158
    ar->source = getstr(cl->l.p->source);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   159
    ar->linedefined = cl->l.p->linedefined;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   160
    ar->lastlinedefined = cl->l.p->lastlinedefined;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   161
    ar->what = (ar->linedefined == 0) ? "main" : "Lua";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   162
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   163
  luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   164
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   165
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   166
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   167
static void info_tailcall (lua_Debug *ar) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   168
  ar->name = ar->namewhat = "";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   169
  ar->what = "tail";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   170
  ar->lastlinedefined = ar->linedefined = ar->currentline = -1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   171
  ar->source = "=(tail call)";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   172
  luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   173
  ar->nups = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   174
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   175
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   176
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   177
static void collectvalidlines (lua_State *L, Closure *f) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   178
  if (f == NULL || f->c.isC) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   179
    setnilvalue(L->top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   180
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   181
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   182
    Table *t = luaH_new(L, 0, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   183
    int *lineinfo = f->l.p->lineinfo;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   184
    int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   185
    for (i=0; i<f->l.p->sizelineinfo; i++)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   186
      setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2812
diff changeset
   187
    sethvalue(L, L->top, t);
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   188
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   189
  incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   190
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   191
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   192
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   193
static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   194
                    Closure *f, CallInfo *ci) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   195
  int status = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   196
  if (f == NULL) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   197
    info_tailcall(ar);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   198
    return status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   199
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   200
  for (; *what; what++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   201
    switch (*what) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   202
      case 'S': {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   203
        funcinfo(ar, f);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   204
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   205
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   206
      case 'l': {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   207
        ar->currentline = (ci) ? currentline(L, ci) : -1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   208
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   209
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   210
      case 'u': {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   211
        ar->nups = f->c.nupvalues;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   212
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   213
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   214
      case 'n': {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   215
        ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   216
        if (ar->namewhat == NULL) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   217
          ar->namewhat = "";  /* not found */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   218
          ar->name = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   219
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   220
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   221
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   222
      case 'L':
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   223
      case 'f':  /* handled by lua_getinfo */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   224
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   225
      default: status = 0;  /* invalid option */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   226
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   227
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   228
  return status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   229
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   230
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   231
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   232
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   233
  int status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   234
  Closure *f = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   235
  CallInfo *ci = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   236
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   237
  if (*what == '>') {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   238
    StkId func = L->top - 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   239
    luai_apicheck(L, ttisfunction(func));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   240
    what++;  /* skip the '>' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   241
    f = clvalue(func);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   242
    L->top--;  /* pop function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   243
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   244
  else if (ar->i_ci != 0) {  /* no tail call? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   245
    ci = L->base_ci + ar->i_ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   246
    lua_assert(ttisfunction(ci->func));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   247
    f = clvalue(ci->func);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   248
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   249
  status = auxgetinfo(L, what, ar, f, ci);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   250
  if (strchr(what, 'f')) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   251
    if (f == NULL) setnilvalue(L->top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   252
    else setclvalue(L, L->top, f);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   253
    incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   254
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   255
  if (strchr(what, 'L'))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   256
    collectvalidlines(L, f);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   257
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   258
  return status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   259
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   260
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   261
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   262
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   263
** {======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   264
** Symbolic Execution and code checker
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   265
** =======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   266
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   267
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 3697
diff changeset
   268
#define check(x)        if (!(x)) return 0;
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   269
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 3697
diff changeset
   270
#define checkjump(pt,pc)    check(0 <= pc && pc < pt->sizecode)
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   271
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 3697
diff changeset
   272
#define checkreg(pt,reg)    check((reg) < (pt)->maxstacksize)
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   273
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   274
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   275
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   276
static int precheck (const Proto *pt) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   277
  check(pt->maxstacksize <= MAXSTACK);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   278
  check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   279
  check(!(pt->is_vararg & VARARG_NEEDSARG) ||
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   280
              (pt->is_vararg & VARARG_HASARG));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   281
  check(pt->sizeupvalues <= pt->nups);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   282
  check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   283
  check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   284
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   285
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   286
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   287
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 3697
diff changeset
   288
#define checkopenop(pt,pc)  luaG_checkopenop((pt)->code[(pc)+1])
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   289
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   290
int luaG_checkopenop (Instruction i) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   291
  switch (GET_OPCODE(i)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   292
    case OP_CALL:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   293
    case OP_TAILCALL:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   294
    case OP_RETURN:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   295
    case OP_SETLIST: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   296
      check(GETARG_B(i) == 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   297
      return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   298
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   299
    default: return 0;  /* invalid instruction after an open call */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   300
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   301
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   302
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   303
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   304
static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   305
  switch (mode) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   306
    case OpArgN: check(r == 0); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   307
    case OpArgU: break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   308
    case OpArgR: checkreg(pt, r); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   309
    case OpArgK:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   310
      check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   311
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   312
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   313
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   314
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   315
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   316
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   317
static Instruction symbexec (const Proto *pt, int lastpc, int reg) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   318
  int pc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   319
  int last;  /* stores position of last instruction that changed `reg' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   320
  last = pt->sizecode-1;  /* points to final return (a `neutral' instruction) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   321
  check(precheck(pt));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   322
  for (pc = 0; pc < lastpc; pc++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   323
    Instruction i = pt->code[pc];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   324
    OpCode op = GET_OPCODE(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   325
    int a = GETARG_A(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   326
    int b = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   327
    int c = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   328
    check(op < NUM_OPCODES);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   329
    checkreg(pt, a);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   330
    switch (getOpMode(op)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   331
      case iABC: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   332
        b = GETARG_B(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   333
        c = GETARG_C(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   334
        check(checkArgMode(pt, b, getBMode(op)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   335
        check(checkArgMode(pt, c, getCMode(op)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   336
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   337
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   338
      case iABx: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   339
        b = GETARG_Bx(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   340
        if (getBMode(op) == OpArgK) check(b < pt->sizek);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   341
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   342
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   343
      case iAsBx: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   344
        b = GETARG_sBx(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   345
        if (getBMode(op) == OpArgR) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   346
          int dest = pc+1+b;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   347
          check(0 <= dest && dest < pt->sizecode);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   348
          if (dest > 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   349
            int j;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   350
            /* check that it does not jump to a setlist count; this
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   351
               is tricky, because the count from a previous setlist may
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   352
               have the same value of an invalid setlist; so, we must
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   353
               go all the way back to the first of them (if any) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   354
            for (j = 0; j < dest; j++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   355
              Instruction d = pt->code[dest-1-j];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   356
              if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   357
            }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   358
            /* if 'j' is even, previous value is not a setlist (even if
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   359
               it looks like one) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   360
            check((j&1) == 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   361
          }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   362
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   363
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   364
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   365
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   366
    if (testAMode(op)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   367
      if (a == reg) last = pc;  /* change register `a' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   368
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   369
    if (testTMode(op)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   370
      check(pc+2 < pt->sizecode);  /* check skip */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   371
      check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   372
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   373
    switch (op) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   374
      case OP_LOADBOOL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   375
        if (c == 1) {  /* does it jump? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   376
          check(pc+2 < pt->sizecode);  /* check its jump */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   377
          check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST ||
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   378
                GETARG_C(pt->code[pc+1]) != 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   379
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   380
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   381
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   382
      case OP_LOADNIL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   383
        if (a <= reg && reg <= b)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   384
          last = pc;  /* set registers from `a' to `b' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   385
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   386
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   387
      case OP_GETUPVAL:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   388
      case OP_SETUPVAL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   389
        check(b < pt->nups);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   390
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   391
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   392
      case OP_GETGLOBAL:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   393
      case OP_SETGLOBAL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   394
        check(ttisstring(&pt->k[b]));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   395
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   396
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   397
      case OP_SELF: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   398
        checkreg(pt, a+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   399
        if (reg == a+1) last = pc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   400
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   401
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   402
      case OP_CONCAT: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   403
        check(b < c);  /* at least two operands */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   404
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   405
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   406
      case OP_TFORLOOP: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   407
        check(c >= 1);  /* at least one result (control variable) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   408
        checkreg(pt, a+2+c);  /* space for results */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   409
        if (reg >= a+2) last = pc;  /* affect all regs above its base */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   410
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   411
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   412
      case OP_FORLOOP:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   413
      case OP_FORPREP:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   414
        checkreg(pt, a+3);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   415
        /* go through */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   416
      case OP_JMP: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   417
        int dest = pc+1+b;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   418
        /* not full check and jump is forward and do not skip `lastpc'? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   419
        if (reg != NO_REG && pc < dest && dest <= lastpc)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   420
          pc += b;  /* do the jump */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   421
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   422
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   423
      case OP_CALL:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   424
      case OP_TAILCALL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   425
        if (b != 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   426
          checkreg(pt, a+b-1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   427
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   428
        c--;  /* c = num. returns */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   429
        if (c == LUA_MULTRET) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   430
          check(checkopenop(pt, pc));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   431
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   432
        else if (c != 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   433
          checkreg(pt, a+c-1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   434
        if (reg >= a) last = pc;  /* affect all registers above base */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   435
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   436
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   437
      case OP_RETURN: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   438
        b--;  /* b = num. returns */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   439
        if (b > 0) checkreg(pt, a+b-1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   440
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   441
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   442
      case OP_SETLIST: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   443
        if (b > 0) checkreg(pt, a + b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   444
        if (c == 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   445
          pc++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   446
          check(pc < pt->sizecode - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   447
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   448
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   449
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   450
      case OP_CLOSURE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   451
        int nup, j;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   452
        check(b < pt->sizep);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   453
        nup = pt->p[b]->nups;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   454
        check(pc + nup < pt->sizecode);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   455
        for (j = 1; j <= nup; j++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   456
          OpCode op1 = GET_OPCODE(pt->code[pc + j]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   457
          check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   458
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   459
        if (reg != NO_REG)  /* tracing? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   460
          pc += nup;  /* do not 'execute' these pseudo-instructions */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   461
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   462
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   463
      case OP_VARARG: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   464
        check((pt->is_vararg & VARARG_ISVARARG) &&
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   465
             !(pt->is_vararg & VARARG_NEEDSARG));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   466
        b--;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   467
        if (b == LUA_MULTRET) check(checkopenop(pt, pc));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   468
        checkreg(pt, a+b-1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   469
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   470
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   471
      default: break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   472
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   473
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   474
  return pt->code[last];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   475
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   476
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   477
#undef check
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   478
#undef checkjump
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   479
#undef checkreg
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   480
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   481
/* }====================================================== */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   482
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   483
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   484
int luaG_checkcode (const Proto *pt) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   485
  return (symbexec(pt, pt->sizecode, NO_REG) != 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   486
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   487
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   488
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   489
static const char *kname (Proto *p, int c) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   490
  if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   491
    return svalue(&p->k[INDEXK(c)]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   492
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   493
    return "?";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   494
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   495
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   496
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   497
static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   498
                               const char **name) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   499
  if (isLua(ci)) {  /* a Lua function? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   500
    Proto *p = ci_func(ci)->l.p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   501
    int pc = currentpc(L, ci);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   502
    Instruction i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   503
    *name = luaF_getlocalname(p, stackpos+1, pc);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   504
    if (*name)  /* is a local? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   505
      return "local";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   506
    i = symbexec(p, pc, stackpos);  /* try symbolic execution */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   507
    lua_assert(pc != -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   508
    switch (GET_OPCODE(i)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   509
      case OP_GETGLOBAL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   510
        int g = GETARG_Bx(i);  /* global index */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   511
        lua_assert(ttisstring(&p->k[g]));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   512
        *name = svalue(&p->k[g]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   513
        return "global";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   514
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   515
      case OP_MOVE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   516
        int a = GETARG_A(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   517
        int b = GETARG_B(i);  /* move from `b' to `a' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   518
        if (b < a)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   519
          return getobjname(L, ci, b, name);  /* get name for `b' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   520
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   521
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   522
      case OP_GETTABLE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   523
        int k = GETARG_C(i);  /* key index */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   524
        *name = kname(p, k);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   525
        return "field";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   526
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   527
      case OP_GETUPVAL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   528
        int u = GETARG_B(i);  /* upvalue index */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   529
        *name = p->upvalues ? getstr(p->upvalues[u]) : "?";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   530
        return "upvalue";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   531
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   532
      case OP_SELF: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   533
        int k = GETARG_C(i);  /* key index */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   534
        *name = kname(p, k);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   535
        return "method";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   536
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   537
      default: break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   538
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   539
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   540
  return NULL;  /* no useful name found */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   541
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   542
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   543
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   544
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   545
  Instruction i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   546
  if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   547
    return NULL;  /* calling function is not Lua (or is unknown) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   548
  ci--;  /* calling function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   549
  i = ci_func(ci)->l.p->code[currentpc(L, ci)];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   550
  if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   551
      GET_OPCODE(i) == OP_TFORLOOP)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   552
    return getobjname(L, ci, GETARG_A(i), name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   553
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   554
    return NULL;  /* no useful name can be found */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   555
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   556
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   557
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   558
/* only ANSI way to check whether a pointer points to an array */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   559
static int isinstack (CallInfo *ci, const TValue *o) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   560
  StkId p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   561
  for (p = ci->base; p < ci->top; p++)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   562
    if (o == p) return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   563
  return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   564
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   565
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   566
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   567
void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   568
  const char *name = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   569
  const char *t = luaT_typenames[ttype(o)];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   570
  const char *kind = (isinstack(L->ci, o)) ?
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   571
                         getobjname(L, L->ci, cast_int(o - L->base), &name) :
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   572
                         NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   573
  if (kind)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   574
    luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   575
                op, kind, name, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   576
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   577
    luaG_runerror(L, "attempt to %s a %s value", op, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   578
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   579
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   580
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   581
void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   582
  if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   583
  lua_assert(!ttisstring(p1) && !ttisnumber(p1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   584
  luaG_typeerror(L, p1, "concatenate");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   585
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   586
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   587
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   588
void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   589
  TValue temp;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   590
  if (luaV_tonumber(p1, &temp) == NULL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   591
    p2 = p1;  /* first operand is wrong */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   592
  luaG_typeerror(L, p2, "perform arithmetic on");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   593
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   594
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   595
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   596
int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   597
  const char *t1 = luaT_typenames[ttype(p1)];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   598
  const char *t2 = luaT_typenames[ttype(p2)];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   599
  if (t1[2] == t2[2])
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   600
    luaG_runerror(L, "attempt to compare two %s values", t1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   601
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   602
    luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   603
  return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   604
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   605
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   606
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   607
static void addinfo (lua_State *L, const char *msg) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   608
  CallInfo *ci = L->ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   609
  if (isLua(ci)) {  /* is Lua code? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   610
    char buff[LUA_IDSIZE];  /* add file:line information */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   611
    int line = currentline(L, ci);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   612
    luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   613
    luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   614
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   615
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   616
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   617
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   618
void luaG_errormsg (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   619
  if (L->errfunc != 0) {  /* is there an error handling function? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   620
    StkId errfunc = restorestack(L, L->errfunc);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   621
    if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   622
    setobjs2s(L, L->top, L->top - 1);  /* move argument */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   623
    setobjs2s(L, L->top - 1, errfunc);  /* push function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   624
    incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   625
    luaD_call(L, L->top - 2, 1);  /* call it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   626
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   627
  luaD_throw(L, LUA_ERRRUN);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   628
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   629
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   630
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   631
void luaG_runerror (lua_State *L, const char *fmt, ...) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   632
  va_list argp;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   633
  va_start(argp, fmt);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   634
  addinfo(L, luaO_pushvfstring(L, fmt, argp));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   635
  va_end(argp);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   636
  luaG_errormsg(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   637
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   638