misc/liblua/lauxlib.c
author unc0rr
Tue, 18 Dec 2012 20:48:37 +0400
branchflibqtfrontend
changeset 8306 50fe80adbfcb
parent 2812 0a24853de796
child 9162 2c6ad606f4fb
permissions -rw-r--r--
Use physfs here too
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: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     3
** Auxiliary functions for building Lua libraries
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 <ctype.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     9
#include <errno.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    10
#include <stdarg.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    11
#include <stdio.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    12
#include <stdlib.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    13
#include <string.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    14
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    15
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    16
/* This file uses only the official API of Lua.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    17
** Any function declared here could be written as an application function.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    18
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    19
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    20
#define lauxlib_c
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    21
#define LUA_LIB
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    22
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    23
#include "lua.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    24
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    25
#include "lauxlib.h"
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
#define FREELIST_REF	0	/* free list of references */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    29
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    30
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    31
/* convert a stack index to positive */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    32
#define abs_index(L, i)		((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    33
					lua_gettop(L) + (i) + 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
** {======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    38
** Error-report functions
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    39
** =======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    40
*/
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
LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    44
  lua_Debug ar;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    45
  if (!lua_getstack(L, 0, &ar))  /* no stack frame? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    46
    return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    47
  lua_getinfo(L, "n", &ar);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    48
  if (strcmp(ar.namewhat, "method") == 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    49
    narg--;  /* do not count `self' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    50
    if (narg == 0)  /* error is in the self argument itself? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    51
      return luaL_error(L, "calling " LUA_QS " on bad self (%s)",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    52
                           ar.name, extramsg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    53
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    54
  if (ar.name == NULL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    55
    ar.name = "?";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    56
  return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    57
                        narg, ar.name, extramsg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    58
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    59
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    60
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    61
LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    62
  const char *msg = lua_pushfstring(L, "%s expected, got %s",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    63
                                    tname, luaL_typename(L, narg));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    64
  return luaL_argerror(L, narg, msg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    65
}
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
static void tag_error (lua_State *L, int narg, int tag) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    69
  luaL_typerror(L, narg, lua_typename(L, tag));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    70
}
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
LUALIB_API void luaL_where (lua_State *L, int level) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    74
  lua_Debug ar;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    75
  if (lua_getstack(L, level, &ar)) {  /* check function at level */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    76
    lua_getinfo(L, "Sl", &ar);  /* get info about it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    77
    if (ar.currentline > 0) {  /* is there info? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    78
      lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    79
      return;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    80
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    81
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    82
  lua_pushliteral(L, "");  /* else, no information available... */
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
LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    87
  va_list argp;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    88
  va_start(argp, fmt);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    89
  luaL_where(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    90
  lua_pushvfstring(L, fmt, argp);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    91
  va_end(argp);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    92
  lua_concat(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    93
  return lua_error(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    94
}
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    99
LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   100
                                 const char *const lst[]) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   101
  const char *name = (def) ? luaL_optstring(L, narg, def) :
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   102
                             luaL_checkstring(L, narg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   103
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   104
  for (i=0; lst[i]; i++)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   105
    if (strcmp(lst[i], name) == 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   106
      return i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   107
  return luaL_argerror(L, narg,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   108
                       lua_pushfstring(L, "invalid option " LUA_QS, name));
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
LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   113
  lua_getfield(L, LUA_REGISTRYINDEX, tname);  /* get registry.name */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   114
  if (!lua_isnil(L, -1))  /* name already in use? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   115
    return 0;  /* leave previous value on top, but return 0 */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   116
  lua_pop(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   117
  lua_newtable(L);  /* create metatable */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   118
  lua_pushvalue(L, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   119
  lua_setfield(L, LUA_REGISTRYINDEX, tname);  /* registry.name = metatable */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   120
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   121
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   122
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   123
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   124
LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   125
  void *p = lua_touserdata(L, ud);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   126
  if (p != NULL) {  /* value is a userdata? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   127
    if (lua_getmetatable(L, ud)) {  /* does it have a metatable? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   128
      lua_getfield(L, LUA_REGISTRYINDEX, tname);  /* get correct metatable */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   129
      if (lua_rawequal(L, -1, -2)) {  /* does it have the correct mt? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   130
        lua_pop(L, 2);  /* remove both metatables */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   131
        return p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   132
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   133
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   134
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   135
  luaL_typerror(L, ud, tname);  /* else error */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   136
  return NULL;  /* to avoid warnings */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   137
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   138
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   139
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   140
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   141
  if (!lua_checkstack(L, space))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   142
    luaL_error(L, "stack overflow (%s)", mes);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   143
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   144
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   145
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   146
LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   147
  if (lua_type(L, narg) != t)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   148
    tag_error(L, narg, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   149
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   150
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   151
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   152
LUALIB_API void luaL_checkany (lua_State *L, int narg) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   153
  if (lua_type(L, narg) == LUA_TNONE)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   154
    luaL_argerror(L, narg, "value expected");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   155
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   156
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   157
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   158
LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   159
  const char *s = lua_tolstring(L, narg, len);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   160
  if (!s) tag_error(L, narg, LUA_TSTRING);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   161
  return s;
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   165
LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   166
                                        const char *def, size_t *len) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   167
  if (lua_isnoneornil(L, narg)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   168
    if (len)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   169
      *len = (def ? strlen(def) : 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   170
    return def;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   171
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   172
  else return luaL_checklstring(L, narg, len);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   173
}
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
LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   177
  lua_Number d = lua_tonumber(L, narg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   178
  if (d == 0 && !lua_isnumber(L, narg))  /* avoid extra test when d is not 0 */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   179
    tag_error(L, narg, LUA_TNUMBER);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   180
  return d;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   181
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   182
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   183
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   184
LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   185
  return luaL_opt(L, luaL_checknumber, narg, def);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   186
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   187
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   188
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   189
LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   190
  lua_Integer d = lua_tointeger(L, narg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   191
  if (d == 0 && !lua_isnumber(L, narg))  /* avoid extra test when d is not 0 */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   192
    tag_error(L, narg, LUA_TNUMBER);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   193
  return d;
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   197
LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   198
                                                      lua_Integer def) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   199
  return luaL_opt(L, luaL_checkinteger, narg, def);
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
LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   204
  if (!lua_getmetatable(L, obj))  /* no metatable? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   205
    return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   206
  lua_pushstring(L, event);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   207
  lua_rawget(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   208
  if (lua_isnil(L, -1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   209
    lua_pop(L, 2);  /* remove metatable and metafield */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   210
    return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   211
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   212
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   213
    lua_remove(L, -2);  /* remove only metatable */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   214
    return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   215
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   216
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   217
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   218
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   219
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   220
  obj = abs_index(L, obj);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   221
  if (!luaL_getmetafield(L, obj, event))  /* no metafield? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   222
    return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   223
  lua_pushvalue(L, obj);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   224
  lua_call(L, 1, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   225
  return 1;
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   229
LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   230
                                const luaL_Reg *l) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   231
  luaI_openlib(L, libname, l, 0);
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   235
static int libsize (const luaL_Reg *l) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   236
  int size = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   237
  for (; l->name; l++) size++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   238
  return size;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   239
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   240
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   241
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   242
LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   243
                              const luaL_Reg *l, int nup) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   244
  if (libname) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   245
    int size = libsize(l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   246
    /* check whether lib already exists */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   247
    luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   248
    lua_getfield(L, -1, libname);  /* get _LOADED[libname] */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   249
    if (!lua_istable(L, -1)) {  /* not found? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   250
      lua_pop(L, 1);  /* remove previous result */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   251
      /* try global variable (and create one if it does not exist) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   252
      if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   253
        luaL_error(L, "name conflict for module " LUA_QS, libname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   254
      lua_pushvalue(L, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   255
      lua_setfield(L, -3, libname);  /* _LOADED[libname] = new table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   256
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   257
    lua_remove(L, -2);  /* remove _LOADED table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   258
    lua_insert(L, -(nup+1));  /* move library table to below upvalues */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   259
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   260
  for (; l->name; l++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   261
    int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   262
    for (i=0; i<nup; i++)  /* copy upvalues to the top */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   263
      lua_pushvalue(L, -nup);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   264
    lua_pushcclosure(L, l->func, nup);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   265
    lua_setfield(L, -(nup+2), l->name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   266
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   267
  lua_pop(L, nup);  /* remove upvalues */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   268
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   269
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   270
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   271
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   272
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   273
** {======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   274
** getn-setn: size for arrays
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   275
** =======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   276
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   277
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   278
#if defined(LUA_COMPAT_GETN)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   279
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   280
static int checkint (lua_State *L, int topop) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   281
  int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   282
  lua_pop(L, topop);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   283
  return n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   284
}
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
static void getsizes (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   288
  lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   289
  if (lua_isnil(L, -1)) {  /* no `size' table? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   290
    lua_pop(L, 1);  /* remove nil */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   291
    lua_newtable(L);  /* create it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   292
    lua_pushvalue(L, -1);  /* `size' will be its own metatable */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   293
    lua_setmetatable(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   294
    lua_pushliteral(L, "kv");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   295
    lua_setfield(L, -2, "__mode");  /* metatable(N).__mode = "kv" */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   296
    lua_pushvalue(L, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   297
    lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES");  /* store in register */
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   302
LUALIB_API void luaL_setn (lua_State *L, int t, int n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   303
  t = abs_index(L, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   304
  lua_pushliteral(L, "n");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   305
  lua_rawget(L, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   306
  if (checkint(L, 1) >= 0) {  /* is there a numeric field `n'? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   307
    lua_pushliteral(L, "n");  /* use it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   308
    lua_pushinteger(L, n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   309
    lua_rawset(L, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   310
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   311
  else {  /* use `sizes' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   312
    getsizes(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   313
    lua_pushvalue(L, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   314
    lua_pushinteger(L, n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   315
    lua_rawset(L, -3);  /* sizes[t] = n */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   316
    lua_pop(L, 1);  /* remove `sizes' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   317
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   318
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   319
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   320
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   321
LUALIB_API int luaL_getn (lua_State *L, int t) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   322
  int n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   323
  t = abs_index(L, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   324
  lua_pushliteral(L, "n");  /* try t.n */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   325
  lua_rawget(L, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   326
  if ((n = checkint(L, 1)) >= 0) return n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   327
  getsizes(L);  /* else try sizes[t] */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   328
  lua_pushvalue(L, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   329
  lua_rawget(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   330
  if ((n = checkint(L, 2)) >= 0) return n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   331
  return (int)lua_objlen(L, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   332
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   333
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   334
#endif
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   335
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   336
/* }====================================================== */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   337
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   338
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   339
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   340
LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   341
                                                               const char *r) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   342
  const char *wild;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   343
  size_t l = strlen(p);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   344
  luaL_Buffer b;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   345
  luaL_buffinit(L, &b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   346
  while ((wild = strstr(s, p)) != NULL) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   347
    luaL_addlstring(&b, s, wild - s);  /* push prefix */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   348
    luaL_addstring(&b, r);  /* push replacement in place of pattern */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   349
    s = wild + l;  /* continue after `p' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   350
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   351
  luaL_addstring(&b, s);  /* push last suffix */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   352
  luaL_pushresult(&b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   353
  return lua_tostring(L, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   354
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   355
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   356
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   357
LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   358
                                       const char *fname, int szhint) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   359
  const char *e;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   360
  lua_pushvalue(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   361
  do {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   362
    e = strchr(fname, '.');
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   363
    if (e == NULL) e = fname + strlen(fname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   364
    lua_pushlstring(L, fname, e - fname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   365
    lua_rawget(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   366
    if (lua_isnil(L, -1)) {  /* no such field? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   367
      lua_pop(L, 1);  /* remove this nil */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   368
      lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   369
      lua_pushlstring(L, fname, e - fname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   370
      lua_pushvalue(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   371
      lua_settable(L, -4);  /* set new table into field */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   372
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   373
    else if (!lua_istable(L, -1)) {  /* field has a non-table value? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   374
      lua_pop(L, 2);  /* remove table and value */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   375
      return fname;  /* return problematic part of the name */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   376
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   377
    lua_remove(L, -2);  /* remove previous table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   378
    fname = e + 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   379
  } while (*e == '.');
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   380
  return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   381
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   382
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   383
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   384
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   385
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   386
** {======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   387
** Generic Buffer manipulation
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   388
** =======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   389
*/
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
#define bufflen(B)	((B)->p - (B)->buffer)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   393
#define bufffree(B)	((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   394
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   395
#define LIMIT	(LUA_MINSTACK/2)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   396
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   397
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   398
static int emptybuffer (luaL_Buffer *B) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   399
  size_t l = bufflen(B);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   400
  if (l == 0) return 0;  /* put nothing on stack */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   401
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   402
    lua_pushlstring(B->L, B->buffer, l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   403
    B->p = B->buffer;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   404
    B->lvl++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   405
    return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   406
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   407
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   408
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   409
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   410
static void adjuststack (luaL_Buffer *B) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   411
  if (B->lvl > 1) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   412
    lua_State *L = B->L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   413
    int toget = 1;  /* number of levels to concat */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   414
    size_t toplen = lua_strlen(L, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   415
    do {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   416
      size_t l = lua_strlen(L, -(toget+1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   417
      if (B->lvl - toget + 1 >= LIMIT || toplen > l) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   418
        toplen += l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   419
        toget++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   420
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   421
      else break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   422
    } while (toget < B->lvl);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   423
    lua_concat(L, toget);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   424
    B->lvl = B->lvl - toget + 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   425
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   426
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   427
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   428
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   429
LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   430
  if (emptybuffer(B))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   431
    adjuststack(B);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   432
  return B->buffer;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   433
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   434
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   435
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   436
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   437
  while (l--)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   438
    luaL_addchar(B, *s++);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   439
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   440
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   441
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   442
LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   443
  luaL_addlstring(B, s, strlen(s));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   444
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   445
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   446
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   447
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   448
  emptybuffer(B);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   449
  lua_concat(B->L, B->lvl);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   450
  B->lvl = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   451
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   452
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   453
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   454
LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   455
  lua_State *L = B->L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   456
  size_t vl;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   457
  const char *s = lua_tolstring(L, -1, &vl);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   458
  if (vl <= bufffree(B)) {  /* fit into buffer? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   459
    memcpy(B->p, s, vl);  /* put it there */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   460
    B->p += vl;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   461
    lua_pop(L, 1);  /* remove from stack */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   462
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   463
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   464
    if (emptybuffer(B))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   465
      lua_insert(L, -2);  /* put buffer before new value */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   466
    B->lvl++;  /* add new value into B stack */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   467
    adjuststack(B);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   468
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   469
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   470
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   471
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   472
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   473
  B->L = L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   474
  B->p = B->buffer;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   475
  B->lvl = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   476
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   477
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   478
/* }====================================================== */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   479
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   480
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   481
LUALIB_API int luaL_ref (lua_State *L, int t) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   482
  int ref;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   483
  t = abs_index(L, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   484
  if (lua_isnil(L, -1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   485
    lua_pop(L, 1);  /* remove from stack */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   486
    return LUA_REFNIL;  /* `nil' has a unique fixed reference */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   487
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   488
  lua_rawgeti(L, t, FREELIST_REF);  /* get first free element */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   489
  ref = (int)lua_tointeger(L, -1);  /* ref = t[FREELIST_REF] */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   490
  lua_pop(L, 1);  /* remove it from stack */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   491
  if (ref != 0) {  /* any free element? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   492
    lua_rawgeti(L, t, ref);  /* remove it from list */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   493
    lua_rawseti(L, t, FREELIST_REF);  /* (t[FREELIST_REF] = t[ref]) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   494
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   495
  else {  /* no free elements */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   496
    ref = (int)lua_objlen(L, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   497
    ref++;  /* create new reference */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   498
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   499
  lua_rawseti(L, t, ref);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   500
  return ref;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   501
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   502
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   503
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   504
LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   505
  if (ref >= 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   506
    t = abs_index(L, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   507
    lua_rawgeti(L, t, FREELIST_REF);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   508
    lua_rawseti(L, t, ref);  /* t[ref] = t[FREELIST_REF] */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   509
    lua_pushinteger(L, ref);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   510
    lua_rawseti(L, t, FREELIST_REF);  /* t[FREELIST_REF] = ref */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   511
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   512
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   513
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   514
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   515
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   516
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   517
** {======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   518
** Load functions
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   519
** =======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   520
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   521
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   522
typedef struct LoadF {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   523
  int extraline;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   524
  FILE *f;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   525
  char buff[LUAL_BUFFERSIZE];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   526
} LoadF;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   527
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   528
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   529
static const char *getF (lua_State *L, void *ud, size_t *size) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   530
  LoadF *lf = (LoadF *)ud;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   531
  (void)L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   532
  if (lf->extraline) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   533
    lf->extraline = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   534
    *size = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   535
    return "\n";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   536
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   537
  if (feof(lf->f)) return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   538
  *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   539
  return (*size > 0) ? lf->buff : NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   540
}
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
static int errfile (lua_State *L, const char *what, int fnameindex) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   544
  const char *serr = strerror(errno);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   545
  const char *filename = lua_tostring(L, fnameindex) + 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   546
  lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   547
  lua_remove(L, fnameindex);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   548
  return LUA_ERRFILE;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   549
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   550
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   551
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   552
LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   553
  LoadF lf;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   554
  int status, readstatus;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   555
  int c;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   556
  int fnameindex = lua_gettop(L) + 1;  /* index of filename on the stack */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   557
  lf.extraline = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   558
  if (filename == NULL) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   559
    lua_pushliteral(L, "=stdin");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   560
    lf.f = stdin;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   561
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   562
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   563
    lua_pushfstring(L, "@%s", filename);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   564
    lf.f = fopen(filename, "r");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   565
    if (lf.f == NULL) return errfile(L, "open", fnameindex);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   566
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   567
  c = getc(lf.f);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   568
  if (c == '#') {  /* Unix exec. file? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   569
    lf.extraline = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   570
    while ((c = getc(lf.f)) != EOF && c != '\n') ;  /* skip first line */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   571
    if (c == '\n') c = getc(lf.f);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   572
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   573
  if (c == LUA_SIGNATURE[0] && filename) {  /* binary file? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   574
    lf.f = freopen(filename, "rb", lf.f);  /* reopen in binary mode */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   575
    if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   576
    /* skip eventual `#!...' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   577
   while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   578
    lf.extraline = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   579
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   580
  ungetc(c, lf.f);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   581
  status = lua_load(L, getF, &lf, lua_tostring(L, -1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   582
  readstatus = ferror(lf.f);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   583
  if (filename) fclose(lf.f);  /* close file (even in case of errors) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   584
  if (readstatus) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   585
    lua_settop(L, fnameindex);  /* ignore results from `lua_load' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   586
    return errfile(L, "read", fnameindex);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   587
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   588
  lua_remove(L, fnameindex);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   589
  return status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   590
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   591
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   592
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   593
typedef struct LoadS {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   594
  const char *s;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   595
  size_t size;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   596
} LoadS;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   597
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   598
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   599
static const char *getS (lua_State *L, void *ud, size_t *size) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   600
  LoadS *ls = (LoadS *)ud;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   601
  (void)L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   602
  if (ls->size == 0) return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   603
  *size = ls->size;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   604
  ls->size = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   605
  return ls->s;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   606
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   607
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   608
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   609
LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   610
                                const char *name) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   611
  LoadS ls;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   612
  ls.s = buff;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   613
  ls.size = size;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   614
  return lua_load(L, getS, &ls, name);
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
LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   619
  return luaL_loadbuffer(L, s, strlen(s), s);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   620
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   621
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   622
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   623
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   624
/* }====================================================== */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   625
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   626
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   627
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   628
  (void)ud;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   629
  (void)osize;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   630
  if (nsize == 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   631
    free(ptr);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   632
    return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   633
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   634
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   635
    return realloc(ptr, nsize);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   636
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   637
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   638
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   639
static int panic (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   640
  (void)L;  /* to avoid warnings */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   641
  fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   642
                   lua_tostring(L, -1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   643
  return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   644
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   645
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   646
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   647
LUALIB_API lua_State *luaL_newstate (void) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   648
  lua_State *L = lua_newstate(l_alloc, NULL);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   649
  if (L) lua_atpanic(L, &panic);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   650
  return L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   651
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   652