misc/liblua/ldblib.c
author nemo
Thu, 03 Mar 2016 15:41:53 -0500
changeset 11588 7c8fd2f66e9b
parent 10017 de822cd3df3a
permissions -rw-r--r--
fix and/or screwup that was causing girders in tunnels to erase the tunnel. also a little bit of indentation/case fixing
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: ldblib.c,v 1.104.1.3 2008/01/21 13:11:21 roberto Exp $
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     3
** Interface from Lua to its debug API
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 <stdio.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     9
#include <stdlib.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
#define ldblib_c
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    13
#define LUA_LIB
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    14
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    15
#include "lua.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    16
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    17
#include "lauxlib.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    18
#include "lualib.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    19
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    20
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    21
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    22
static int db_getregistry (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    23
  lua_pushvalue(L, LUA_REGISTRYINDEX);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    24
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    25
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    26
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    27
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    28
static int db_getmetatable (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    29
  luaL_checkany(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    30
  if (!lua_getmetatable(L, 1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    31
    lua_pushnil(L);  /* no metatable */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    32
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    33
  return 1;
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    37
static int db_setmetatable (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    38
  int t = lua_type(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    39
  luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    40
                    "nil or table expected");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    41
  lua_settop(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    42
  lua_pushboolean(L, lua_setmetatable(L, 1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    43
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    44
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    45
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    46
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    47
static int db_getfenv (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    48
  lua_getfenv(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    49
  return 1;
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
static int db_setfenv (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    54
  luaL_checktype(L, 2, LUA_TTABLE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    55
  lua_settop(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    56
  if (lua_setfenv(L, 1) == 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    57
    luaL_error(L, LUA_QL("setfenv")
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    58
                  " cannot change environment of given object");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    59
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    60
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    61
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    62
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    63
static void settabss (lua_State *L, const char *i, const char *v) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    64
  lua_pushstring(L, v);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    65
  lua_setfield(L, -2, i);
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
static void settabsi (lua_State *L, const char *i, int v) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    70
  lua_pushinteger(L, v);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    71
  lua_setfield(L, -2, i);
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    75
static lua_State *getthread (lua_State *L, int *arg) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    76
  if (lua_isthread(L, 1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    77
    *arg = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    78
    return lua_tothread(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    79
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    80
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    81
    *arg = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    82
    return L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    83
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    84
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    85
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    86
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    87
static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    88
  if (L == L1) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    89
    lua_pushvalue(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    90
    lua_remove(L, -3);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    91
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    92
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    93
    lua_xmove(L1, L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    94
  lua_setfield(L, -2, fname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    95
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    96
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    97
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    98
static int db_getinfo (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    99
  lua_Debug ar;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   100
  int arg;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   101
  lua_State *L1 = getthread(L, &arg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   102
  const char *options = luaL_optstring(L, arg+2, "flnSu");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   103
  if (lua_isnumber(L, arg+1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   104
    if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   105
      lua_pushnil(L);  /* level out of range */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   106
      return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   107
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   108
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   109
  else if (lua_isfunction(L, arg+1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   110
    lua_pushfstring(L, ">%s", options);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   111
    options = lua_tostring(L, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   112
    lua_pushvalue(L, arg+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   113
    lua_xmove(L, L1, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   114
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   115
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   116
    return luaL_argerror(L, arg+1, "function or level expected");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   117
  if (!lua_getinfo(L1, options, &ar))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   118
    return luaL_argerror(L, arg+2, "invalid option");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   119
  lua_createtable(L, 0, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   120
  if (strchr(options, 'S')) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   121
    settabss(L, "source", ar.source);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   122
    settabss(L, "short_src", ar.short_src);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   123
    settabsi(L, "linedefined", ar.linedefined);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   124
    settabsi(L, "lastlinedefined", ar.lastlinedefined);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   125
    settabss(L, "what", ar.what);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   126
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   127
  if (strchr(options, 'l'))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   128
    settabsi(L, "currentline", ar.currentline);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   129
  if (strchr(options, 'u'))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   130
    settabsi(L, "nups", ar.nups);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   131
  if (strchr(options, 'n')) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   132
    settabss(L, "name", ar.name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   133
    settabss(L, "namewhat", ar.namewhat);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   134
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   135
  if (strchr(options, 'L'))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   136
    treatstackoption(L, L1, "activelines");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   137
  if (strchr(options, 'f'))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   138
    treatstackoption(L, L1, "func");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   139
  return 1;  /* return table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   140
}
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2812
diff changeset
   141
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   142
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   143
static int db_getlocal (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   144
  int arg;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   145
  lua_State *L1 = getthread(L, &arg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   146
  lua_Debug ar;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   147
  const char *name;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   148
  if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar))  /* out of range? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   149
    return luaL_argerror(L, arg+1, "level out of range");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   150
  name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   151
  if (name) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   152
    lua_xmove(L1, L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   153
    lua_pushstring(L, name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   154
    lua_pushvalue(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   155
    return 2;
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
    lua_pushnil(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   159
    return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   160
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   161
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   162
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   163
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   164
static int db_setlocal (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   165
  int arg;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   166
  lua_State *L1 = getthread(L, &arg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   167
  lua_Debug ar;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   168
  if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar))  /* out of range? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   169
    return luaL_argerror(L, arg+1, "level out of range");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   170
  luaL_checkany(L, arg+3);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   171
  lua_settop(L, arg+3);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   172
  lua_xmove(L, L1, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   173
  lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   174
  return 1;
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   178
static int auxupvalue (lua_State *L, int get) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   179
  const char *name;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   180
  int n = luaL_checkint(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   181
  luaL_checktype(L, 1, LUA_TFUNCTION);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   182
  if (lua_iscfunction(L, 1)) return 0;  /* cannot touch C upvalues from Lua */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   183
  name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   184
  if (name == NULL) return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   185
  lua_pushstring(L, name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   186
  lua_insert(L, -(get+1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   187
  return get + 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   188
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   189
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   190
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   191
static int db_getupvalue (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   192
  return auxupvalue(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   193
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   194
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   195
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   196
static int db_setupvalue (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   197
  luaL_checkany(L, 3);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   198
  return auxupvalue(L, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   199
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   200
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   201
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   202
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   203
static const char KEY_HOOK = 'h';
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   204
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   205
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   206
static void hookf (lua_State *L, lua_Debug *ar) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   207
  static const char *const hooknames[] =
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   208
    {"call", "return", "line", "count", "tail return"};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   209
  lua_pushlightuserdata(L, (void *)&KEY_HOOK);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   210
  lua_rawget(L, LUA_REGISTRYINDEX);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   211
  lua_pushlightuserdata(L, L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   212
  lua_rawget(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   213
  if (lua_isfunction(L, -1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   214
    lua_pushstring(L, hooknames[(int)ar->event]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   215
    if (ar->currentline >= 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   216
      lua_pushinteger(L, ar->currentline);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   217
    else lua_pushnil(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   218
    lua_assert(lua_getinfo(L, "lS", ar));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   219
    lua_call(L, 2, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   220
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   221
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   222
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   223
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   224
static int makemask (const char *smask, int count) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   225
  int mask = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   226
  if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   227
  if (strchr(smask, 'r')) mask |= LUA_MASKRET;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   228
  if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   229
  if (count > 0) mask |= LUA_MASKCOUNT;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   230
  return mask;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   231
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   232
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   233
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   234
static char *unmakemask (int mask, char *smask) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   235
  int i = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   236
  if (mask & LUA_MASKCALL) smask[i++] = 'c';
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   237
  if (mask & LUA_MASKRET) smask[i++] = 'r';
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   238
  if (mask & LUA_MASKLINE) smask[i++] = 'l';
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   239
  smask[i] = '\0';
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   240
  return smask;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   241
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   242
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   243
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   244
static void gethooktable (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   245
  lua_pushlightuserdata(L, (void *)&KEY_HOOK);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   246
  lua_rawget(L, LUA_REGISTRYINDEX);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   247
  if (!lua_istable(L, -1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   248
    lua_pop(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   249
    lua_createtable(L, 0, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   250
    lua_pushlightuserdata(L, (void *)&KEY_HOOK);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   251
    lua_pushvalue(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   252
    lua_rawset(L, LUA_REGISTRYINDEX);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   253
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   254
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   255
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   256
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   257
static int db_sethook (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   258
  int arg, mask, count;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   259
  lua_Hook func;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   260
  lua_State *L1 = getthread(L, &arg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   261
  if (lua_isnoneornil(L, arg+1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   262
    lua_settop(L, arg+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   263
    func = NULL; mask = 0; count = 0;  /* turn off hooks */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   264
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   265
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   266
    const char *smask = luaL_checkstring(L, arg+2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   267
    luaL_checktype(L, arg+1, LUA_TFUNCTION);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   268
    count = luaL_optint(L, arg+3, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   269
    func = hookf; mask = makemask(smask, count);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   270
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   271
  gethooktable(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   272
  lua_pushlightuserdata(L, L1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   273
  lua_pushvalue(L, arg+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   274
  lua_rawset(L, -3);  /* set new hook */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   275
  lua_pop(L, 1);  /* remove hook table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   276
  lua_sethook(L1, func, mask, count);  /* set hooks */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   277
  return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   278
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   279
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   280
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   281
static int db_gethook (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   282
  int arg;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   283
  lua_State *L1 = getthread(L, &arg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   284
  char buff[5];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   285
  int mask = lua_gethookmask(L1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   286
  lua_Hook hook = lua_gethook(L1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   287
  if (hook != NULL && hook != hookf)  /* external hook? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   288
    lua_pushliteral(L, "external hook");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   289
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   290
    gethooktable(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   291
    lua_pushlightuserdata(L, L1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   292
    lua_rawget(L, -2);   /* get hook */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   293
    lua_remove(L, -2);  /* remove hook table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   294
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   295
  lua_pushstring(L, unmakemask(mask, buff));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   296
  lua_pushinteger(L, lua_gethookcount(L1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   297
  return 3;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   298
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   299
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   300
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   301
static int db_debug (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   302
  for (;;) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   303
    char buffer[250];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   304
    fputs("lua_debug> ", stderr);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   305
    if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   306
        strcmp(buffer, "cont\n") == 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   307
      return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   308
    if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   309
        lua_pcall(L, 0, 0, 0)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   310
      fputs(lua_tostring(L, -1), stderr);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   311
      fputs("\n", stderr);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   312
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   313
    lua_settop(L, 0);  /* remove eventual returns */
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
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 3697
diff changeset
   318
#define LEVELS1 12  /* size of the first part of the stack */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 3697
diff changeset
   319
#define LEVELS2 10  /* size of the second part of the stack */
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   320
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   321
static int db_errorfb (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   322
  int level;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   323
  int firstpart = 1;  /* still before eventual `...' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   324
  int arg;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   325
  lua_State *L1 = getthread(L, &arg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   326
  lua_Debug ar;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   327
  if (lua_isnumber(L, arg+2)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   328
    level = (int)lua_tointeger(L, arg+2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   329
    lua_pop(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   330
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   331
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   332
    level = (L == L1) ? 1 : 0;  /* level 0 may be this own function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   333
  if (lua_gettop(L) == arg)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   334
    lua_pushliteral(L, "");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   335
  else if (!lua_isstring(L, arg+1)) return 1;  /* message is not a string */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   336
  else lua_pushliteral(L, "\n");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   337
  lua_pushliteral(L, "stack traceback:");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   338
  while (lua_getstack(L1, level++, &ar)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   339
    if (level > LEVELS1 && firstpart) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   340
      /* no more than `LEVELS2' more levels? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   341
      if (!lua_getstack(L1, level+LEVELS2, &ar))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   342
        level--;  /* keep going */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   343
      else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   344
        lua_pushliteral(L, "\n\t...");  /* too many levels */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   345
        while (lua_getstack(L1, level+LEVELS2, &ar))  /* find last levels */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   346
          level++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   347
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   348
      firstpart = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   349
      continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   350
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   351
    lua_pushliteral(L, "\n\t");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   352
    lua_getinfo(L1, "Snl", &ar);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   353
    lua_pushfstring(L, "%s:", ar.short_src);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   354
    if (ar.currentline > 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   355
      lua_pushfstring(L, "%d:", ar.currentline);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   356
    if (*ar.namewhat != '\0')  /* is there a name? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   357
        lua_pushfstring(L, " in function " LUA_QS, ar.name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   358
    else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   359
      if (*ar.what == 'm')  /* main? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   360
        lua_pushfstring(L, " in main chunk");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   361
      else if (*ar.what == 'C' || *ar.what == 't')
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   362
        lua_pushliteral(L, " ?");  /* C function or tail call */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   363
      else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   364
        lua_pushfstring(L, " in function <%s:%d>",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   365
                           ar.short_src, ar.linedefined);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   366
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   367
    lua_concat(L, lua_gettop(L) - arg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   368
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   369
  lua_concat(L, lua_gettop(L) - arg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   370
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   371
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   372
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   373
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   374
static const luaL_Reg dblib[] = {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   375
  {"debug", db_debug},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   376
  {"getfenv", db_getfenv},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   377
  {"gethook", db_gethook},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   378
  {"getinfo", db_getinfo},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   379
  {"getlocal", db_getlocal},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   380
  {"getregistry", db_getregistry},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   381
  {"getmetatable", db_getmetatable},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   382
  {"getupvalue", db_getupvalue},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   383
  {"setfenv", db_setfenv},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   384
  {"sethook", db_sethook},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   385
  {"setlocal", db_setlocal},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   386
  {"setmetatable", db_setmetatable},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   387
  {"setupvalue", db_setupvalue},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   388
  {"traceback", db_errorfb},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   389
  {NULL, NULL}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   390
};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   391
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   392
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   393
LUALIB_API int luaopen_debug (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   394
  luaL_register(L, LUA_DBLIBNAME, dblib);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   395
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   396
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   397