misc/liblua/lbaselib.c
author Wuzzy <Wuzzy2@mail.ru>
Wed, 15 May 2019 03:56:00 +0200
changeset 14973 8e4e508d153c
parent 10017 de822cd3df3a
permissions -rw-r--r--
Use HaltFatalError for all VideoRec failures
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: lbaselib.c,v 1.191.1.6 2008/02/14 16:46:22 roberto Exp $
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     3
** Basic library
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     9
#include <ctype.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    10
#include <stdio.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    11
#include <stdlib.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    12
#include <string.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    13
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    14
#define lbaselib_c
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    15
#define LUA_LIB
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 "lua.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    18
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    19
#include "lauxlib.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    20
#include "lualib.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    21
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    22
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    23
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    24
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    25
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    26
** If your system does not support `stdout', you can just remove this function.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    27
** If you need, you can define your own `print' function, following this
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    28
** model but changing `fputs' to put the strings at a proper place
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    29
** (a console window or a log file, for instance).
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    30
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    31
static int luaB_print (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    32
  int n = lua_gettop(L);  /* number of arguments */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    33
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    34
  lua_getglobal(L, "tostring");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    35
  for (i=1; i<=n; i++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    36
    const char *s;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    37
    lua_pushvalue(L, -1);  /* function to be called */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    38
    lua_pushvalue(L, i);   /* value to print */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    39
    lua_call(L, 1, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    40
    s = lua_tostring(L, -1);  /* get result */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    41
    if (s == NULL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    42
      return luaL_error(L, LUA_QL("tostring") " must return a string to "
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    43
                           LUA_QL("print"));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    44
    if (i>1) fputs("\t", stdout);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    45
    fputs(s, stdout);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    46
    lua_pop(L, 1);  /* pop result */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    47
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    48
  fputs("\n", stdout);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    49
  return 0;
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 luaB_tonumber (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    54
  int base = luaL_optint(L, 2, 10);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    55
  if (base == 10) {  /* standard conversion */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    56
    luaL_checkany(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    57
    if (lua_isnumber(L, 1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    58
      lua_pushnumber(L, lua_tonumber(L, 1));
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
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    63
    const char *s1 = luaL_checkstring(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    64
    char *s2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    65
    unsigned long n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    66
    luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    67
    n = strtoul(s1, &s2, base);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    68
    if (s1 != s2) {  /* at least one valid digit? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    69
      while (isspace((unsigned char)(*s2))) s2++;  /* skip trailing spaces */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    70
      if (*s2 == '\0') {  /* no invalid trailing characters? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    71
        lua_pushnumber(L, (lua_Number)n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    72
        return 1;
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
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    76
  lua_pushnil(L);  /* else not a number */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    77
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    78
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    79
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    80
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    81
static int luaB_error (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    82
  int level = luaL_optint(L, 2, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    83
  lua_settop(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    84
  if (lua_isstring(L, 1) && level > 0) {  /* add extra information? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    85
    luaL_where(L, level);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    86
    lua_pushvalue(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    87
    lua_concat(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    88
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    89
  return lua_error(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    90
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    91
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    92
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    93
static int luaB_getmetatable (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    94
  luaL_checkany(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    95
  if (!lua_getmetatable(L, 1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    96
    lua_pushnil(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    97
    return 1;  /* no metatable */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    98
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    99
  luaL_getmetafield(L, 1, "__metatable");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   100
  return 1;  /* returns either __metatable field (if present) or metatable */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   101
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   102
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   103
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   104
static int luaB_setmetatable (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   105
  int t = lua_type(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   106
  luaL_checktype(L, 1, LUA_TTABLE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   107
  luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   108
                    "nil or table expected");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   109
  if (luaL_getmetafield(L, 1, "__metatable"))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   110
    luaL_error(L, "cannot change a protected metatable");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   111
  lua_settop(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   112
  lua_setmetatable(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   113
  return 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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   116
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   117
static void getfunc (lua_State *L, int opt) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   118
  if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   119
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   120
    lua_Debug ar;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   121
    int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   122
    luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   123
    if (lua_getstack(L, level, &ar) == 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   124
      luaL_argerror(L, 1, "invalid level");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   125
    lua_getinfo(L, "f", &ar);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   126
    if (lua_isnil(L, -1))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   127
      luaL_error(L, "no function environment for tail call at level %d",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   128
                    level);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   129
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   130
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   131
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   132
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   133
static int luaB_getfenv (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   134
  getfunc(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   135
  if (lua_iscfunction(L, -1))  /* is a C function? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   136
    lua_pushvalue(L, LUA_GLOBALSINDEX);  /* return the thread's global env. */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   137
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   138
    lua_getfenv(L, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   139
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   140
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   141
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 luaB_setfenv (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   144
  luaL_checktype(L, 2, LUA_TTABLE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   145
  getfunc(L, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   146
  lua_pushvalue(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   147
  if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   148
    /* change environment of current thread */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   149
    lua_pushthread(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   150
    lua_insert(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   151
    lua_setfenv(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   152
    return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   153
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   154
  else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   155
    luaL_error(L,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   156
          LUA_QL("setfenv") " cannot change environment of given object");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   157
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   158
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   159
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   160
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   161
static int luaB_rawequal (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   162
  luaL_checkany(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   163
  luaL_checkany(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   164
  lua_pushboolean(L, lua_rawequal(L, 1, 2));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   165
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   166
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   167
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   168
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   169
static int luaB_rawget (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   170
  luaL_checktype(L, 1, LUA_TTABLE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   171
  luaL_checkany(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   172
  lua_settop(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   173
  lua_rawget(L, 1);
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
static int luaB_rawset (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   178
  luaL_checktype(L, 1, LUA_TTABLE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   179
  luaL_checkany(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   180
  luaL_checkany(L, 3);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   181
  lua_settop(L, 3);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   182
  lua_rawset(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   183
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   184
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   185
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   186
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   187
static int luaB_gcinfo (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   188
  lua_pushinteger(L, lua_getgccount(L));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   189
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   190
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   191
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   192
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   193
static int luaB_collectgarbage (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   194
  static const char *const opts[] = {"stop", "restart", "collect",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   195
    "count", "step", "setpause", "setstepmul", NULL};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   196
  static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   197
    LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   198
  int o = luaL_checkoption(L, 1, "collect", opts);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   199
  int ex = luaL_optint(L, 2, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   200
  int res = lua_gc(L, optsnum[o], ex);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   201
  switch (optsnum[o]) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   202
    case LUA_GCCOUNT: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   203
      int b = lua_gc(L, LUA_GCCOUNTB, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   204
      lua_pushnumber(L, res + ((lua_Number)b/1024));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   205
      return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   206
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   207
    case LUA_GCSTEP: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   208
      lua_pushboolean(L, res);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   209
      return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   210
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   211
    default: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   212
      lua_pushnumber(L, res);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   213
      return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   214
    }
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
static int luaB_type (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   220
  luaL_checkany(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   221
  lua_pushstring(L, luaL_typename(L, 1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   222
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   223
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   224
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   225
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   226
static int luaB_next (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   227
  luaL_checktype(L, 1, LUA_TTABLE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   228
  lua_settop(L, 2);  /* create a 2nd argument if there isn't one */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   229
  if (lua_next(L, 1))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   230
    return 2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   231
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   232
    lua_pushnil(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   233
    return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   234
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   235
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   236
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   237
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   238
static int luaB_pairs (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   239
  luaL_checktype(L, 1, LUA_TTABLE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   240
  lua_pushvalue(L, lua_upvalueindex(1));  /* return generator, */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   241
  lua_pushvalue(L, 1);  /* state, */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   242
  lua_pushnil(L);  /* and initial value */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   243
  return 3;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   244
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   245
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   246
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   247
static int ipairsaux (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   248
  int i = luaL_checkint(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   249
  luaL_checktype(L, 1, LUA_TTABLE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   250
  i++;  /* next value */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   251
  lua_pushinteger(L, i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   252
  lua_rawgeti(L, 1, i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   253
  return (lua_isnil(L, -1)) ? 0 : 2;
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 luaB_ipairs (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   258
  luaL_checktype(L, 1, LUA_TTABLE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   259
  lua_pushvalue(L, lua_upvalueindex(1));  /* return generator, */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   260
  lua_pushvalue(L, 1);  /* state, */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   261
  lua_pushinteger(L, 0);  /* and initial value */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   262
  return 3;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   263
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   264
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   265
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   266
static int load_aux (lua_State *L, int status) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   267
  if (status == 0)  /* OK? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   268
    return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   269
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   270
    lua_pushnil(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   271
    lua_insert(L, -2);  /* put before error message */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   272
    return 2;  /* return nil plus error message */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   273
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   274
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   275
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   276
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   277
static int luaB_loadstring (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   278
  size_t l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   279
  const char *s = luaL_checklstring(L, 1, &l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   280
  const char *chunkname = luaL_optstring(L, 2, s);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   281
  return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   282
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   283
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   284
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   285
static int luaB_loadfile (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   286
  const char *fname = luaL_optstring(L, 1, NULL);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   287
  return load_aux(L, luaL_loadfile(L, fname));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   288
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   289
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   290
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   291
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   292
** Reader for generic `load' function: `lua_load' uses the
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   293
** stack for internal stuff, so the reader cannot change the
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   294
** stack top. Instead, it keeps its resulting string in a
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   295
** reserved slot inside the stack.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   296
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   297
static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   298
  (void)ud;  /* to avoid warnings */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   299
  luaL_checkstack(L, 2, "too many nested functions");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   300
  lua_pushvalue(L, 1);  /* get function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   301
  lua_call(L, 0, 1);  /* call it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   302
  if (lua_isnil(L, -1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   303
    *size = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   304
    return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   305
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   306
  else if (lua_isstring(L, -1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   307
    lua_replace(L, 3);  /* save string in a reserved stack slot */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   308
    return lua_tolstring(L, 3, size);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   309
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   310
  else luaL_error(L, "reader function must return a string");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   311
  return NULL;  /* to avoid warnings */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   312
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   313
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   314
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   315
static int luaB_load (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   316
  int status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   317
  const char *cname = luaL_optstring(L, 2, "=(load)");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   318
  luaL_checktype(L, 1, LUA_TFUNCTION);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   319
  lua_settop(L, 3);  /* function, eventual name, plus one reserved slot */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   320
  status = lua_load(L, generic_reader, NULL, cname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   321
  return load_aux(L, status);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   322
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   323
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   324
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   325
static int luaB_dofile (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   326
  const char *fname = luaL_optstring(L, 1, NULL);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   327
  int n = lua_gettop(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   328
  if (luaL_loadfile(L, fname) != 0) lua_error(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   329
  lua_call(L, 0, LUA_MULTRET);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   330
  return lua_gettop(L) - n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   331
}
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
static int luaB_assert (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   335
  luaL_checkany(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   336
  if (!lua_toboolean(L, 1))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   337
    return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   338
  return lua_gettop(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   339
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   340
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   341
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   342
static int luaB_unpack (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   343
  int i, e, n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   344
  luaL_checktype(L, 1, LUA_TTABLE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   345
  i = luaL_optint(L, 2, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   346
  e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   347
  if (i > e) return 0;  /* empty range */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   348
  n = e - i + 1;  /* number of elements */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   349
  if (n <= 0 || !lua_checkstack(L, n))  /* n <= 0 means arith. overflow */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   350
    return luaL_error(L, "too many results to unpack");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   351
  lua_rawgeti(L, 1, i);  /* push arg[i] (avoiding overflow problems) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   352
  while (i++ < e)  /* push arg[i + 1...e] */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   353
    lua_rawgeti(L, 1, i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   354
  return n;
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   358
static int luaB_select (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   359
  int n = lua_gettop(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   360
  if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   361
    lua_pushinteger(L, n-1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   362
    return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   363
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   364
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   365
    int i = luaL_checkint(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   366
    if (i < 0) i = n + i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   367
    else if (i > n) i = n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   368
    luaL_argcheck(L, 1 <= i, 1, "index out of range");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   369
    return n - i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   370
  }
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 int luaB_pcall (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   375
  int status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   376
  luaL_checkany(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   377
  status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   378
  lua_pushboolean(L, (status == 0));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   379
  lua_insert(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   380
  return lua_gettop(L);  /* return status + all results */
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
static int luaB_xpcall (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   385
  int status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   386
  luaL_checkany(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   387
  lua_settop(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   388
  lua_insert(L, 1);  /* put error function under function to be called */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   389
  status = lua_pcall(L, 0, LUA_MULTRET, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   390
  lua_pushboolean(L, (status == 0));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   391
  lua_replace(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   392
  return lua_gettop(L);  /* return status + all results */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   393
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   394
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   395
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   396
static int luaB_tostring (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   397
  luaL_checkany(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   398
  if (luaL_callmeta(L, 1, "__tostring"))  /* is there a metafield? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   399
    return 1;  /* use its value */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   400
  switch (lua_type(L, 1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   401
    case LUA_TNUMBER:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   402
      lua_pushstring(L, lua_tostring(L, 1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   403
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   404
    case LUA_TSTRING:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   405
      lua_pushvalue(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   406
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   407
    case LUA_TBOOLEAN:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   408
      lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   409
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   410
    case LUA_TNIL:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   411
      lua_pushliteral(L, "nil");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   412
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   413
    default:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   414
      lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   415
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   416
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   417
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   418
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   419
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   420
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   421
static int luaB_newproxy (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   422
  lua_settop(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   423
  lua_newuserdata(L, 0);  /* create proxy */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   424
  if (lua_toboolean(L, 1) == 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   425
    return 1;  /* no metatable */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   426
  else if (lua_isboolean(L, 1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   427
    lua_newtable(L);  /* create a new metatable `m' ... */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   428
    lua_pushvalue(L, -1);  /* ... and mark `m' as a valid metatable */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   429
    lua_pushboolean(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   430
    lua_rawset(L, lua_upvalueindex(1));  /* weaktable[m] = true */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   431
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   432
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   433
    int validproxy = 0;  /* to check if weaktable[metatable(u)] == true */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   434
    if (lua_getmetatable(L, 1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   435
      lua_rawget(L, lua_upvalueindex(1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   436
      validproxy = lua_toboolean(L, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   437
      lua_pop(L, 1);  /* remove value */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   438
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   439
    luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   440
    lua_getmetatable(L, 1);  /* metatable is valid; get it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   441
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   442
  lua_setmetatable(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   443
  return 1;
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
static const luaL_Reg base_funcs[] = {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   448
  {"assert", luaB_assert},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   449
  {"collectgarbage", luaB_collectgarbage},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   450
  {"dofile", luaB_dofile},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   451
  {"error", luaB_error},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   452
  {"gcinfo", luaB_gcinfo},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   453
  {"getfenv", luaB_getfenv},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   454
  {"getmetatable", luaB_getmetatable},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   455
  {"loadfile", luaB_loadfile},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   456
  {"load", luaB_load},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   457
  {"loadstring", luaB_loadstring},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   458
  {"next", luaB_next},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   459
  {"pcall", luaB_pcall},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   460
  {"print", luaB_print},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   461
  {"rawequal", luaB_rawequal},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   462
  {"rawget", luaB_rawget},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   463
  {"rawset", luaB_rawset},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   464
  {"select", luaB_select},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   465
  {"setfenv", luaB_setfenv},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   466
  {"setmetatable", luaB_setmetatable},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   467
  {"tonumber", luaB_tonumber},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   468
  {"tostring", luaB_tostring},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   469
  {"type", luaB_type},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   470
  {"unpack", luaB_unpack},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   471
  {"xpcall", luaB_xpcall},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   472
  {NULL, NULL}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   473
};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   474
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   475
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   476
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   477
** {======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   478
** Coroutine library
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
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
   482
#define CO_RUN  0   /* running */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
   483
#define CO_SUS  1   /* suspended */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
   484
#define CO_NOR  2   /* 'normal' (it resumed another coroutine) */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
   485
#define CO_DEAD 3
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   486
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   487
static const char *const statnames[] =
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   488
    {"running", "suspended", "normal", "dead"};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   489
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   490
static int costatus (lua_State *L, lua_State *co) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   491
  if (L == co) return CO_RUN;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   492
  switch (lua_status(co)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   493
    case LUA_YIELD:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   494
      return CO_SUS;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   495
    case 0: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   496
      lua_Debug ar;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   497
      if (lua_getstack(co, 0, &ar) > 0)  /* does it have frames? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   498
        return CO_NOR;  /* it is running */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   499
      else if (lua_gettop(co) == 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   500
          return CO_DEAD;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   501
      else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   502
        return CO_SUS;  /* initial state */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   503
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   504
    default:  /* some error occured */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   505
      return CO_DEAD;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   506
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   507
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   508
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   509
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   510
static int luaB_costatus (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   511
  lua_State *co = lua_tothread(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   512
  luaL_argcheck(L, co, 1, "coroutine expected");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   513
  lua_pushstring(L, statnames[costatus(L, co)]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   514
  return 1;
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
static int auxresume (lua_State *L, lua_State *co, int narg) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   519
  int status = costatus(L, co);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   520
  if (!lua_checkstack(co, narg))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   521
    luaL_error(L, "too many arguments to resume");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   522
  if (status != CO_SUS) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   523
    lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   524
    return -1;  /* error flag */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   525
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   526
  lua_xmove(L, co, narg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   527
  lua_setlevel(L, co);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   528
  status = lua_resume(co, narg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   529
  if (status == 0 || status == LUA_YIELD) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   530
    int nres = lua_gettop(co);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   531
    if (!lua_checkstack(L, nres + 1))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   532
      luaL_error(L, "too many results to resume");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   533
    lua_xmove(co, L, nres);  /* move yielded values */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   534
    return nres;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   535
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   536
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   537
    lua_xmove(co, L, 1);  /* move error message */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   538
    return -1;  /* error flag */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   539
  }
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 luaB_coresume (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   544
  lua_State *co = lua_tothread(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   545
  int r;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   546
  luaL_argcheck(L, co, 1, "coroutine expected");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   547
  r = auxresume(L, co, lua_gettop(L) - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   548
  if (r < 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   549
    lua_pushboolean(L, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   550
    lua_insert(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   551
    return 2;  /* return false + error message */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   552
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   553
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   554
    lua_pushboolean(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   555
    lua_insert(L, -(r + 1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   556
    return r + 1;  /* return true + `resume' returns */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   557
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   558
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   559
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   560
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   561
static int luaB_auxwrap (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   562
  lua_State *co = lua_tothread(L, lua_upvalueindex(1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   563
  int r = auxresume(L, co, lua_gettop(L));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   564
  if (r < 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   565
    if (lua_isstring(L, -1)) {  /* error object is a string? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   566
      luaL_where(L, 1);  /* add extra info */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   567
      lua_insert(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   568
      lua_concat(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   569
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   570
    lua_error(L);  /* propagate error */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   571
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   572
  return r;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   573
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   574
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   575
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   576
static int luaB_cocreate (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   577
  lua_State *NL = lua_newthread(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   578
  luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   579
    "Lua function expected");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   580
  lua_pushvalue(L, 1);  /* move function to top */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   581
  lua_xmove(L, NL, 1);  /* move function from L to NL */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   582
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   583
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   584
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   585
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   586
static int luaB_cowrap (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   587
  luaB_cocreate(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   588
  lua_pushcclosure(L, luaB_auxwrap, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   589
  return 1;
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
static int luaB_yield (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   594
  return lua_yield(L, lua_gettop(L));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   595
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   596
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   597
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   598
static int luaB_corunning (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   599
  if (lua_pushthread(L))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   600
    lua_pushnil(L);  /* main thread is not a coroutine */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   601
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   602
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   603
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   604
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   605
static const luaL_Reg co_funcs[] = {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   606
  {"create", luaB_cocreate},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   607
  {"resume", luaB_coresume},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   608
  {"running", luaB_corunning},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   609
  {"status", luaB_costatus},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   610
  {"wrap", luaB_cowrap},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   611
  {"yield", luaB_yield},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   612
  {NULL, NULL}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   613
};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   614
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   615
/* }====================================================== */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   616
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   617
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   618
static void auxopen (lua_State *L, const char *name,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   619
                     lua_CFunction f, lua_CFunction u) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   620
  lua_pushcfunction(L, u);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   621
  lua_pushcclosure(L, f, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   622
  lua_setfield(L, -2, name);
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
static void base_open (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   627
  /* set global _G */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   628
  lua_pushvalue(L, LUA_GLOBALSINDEX);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   629
  lua_setglobal(L, "_G");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   630
  /* open lib into global table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   631
  luaL_register(L, "_G", base_funcs);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   632
  lua_pushliteral(L, LUA_VERSION);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   633
  lua_setglobal(L, "_VERSION");  /* set global _VERSION */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   634
  /* `ipairs' and `pairs' need auxliliary functions as upvalues */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   635
  auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   636
  auxopen(L, "pairs", luaB_pairs, luaB_next);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   637
  /* `newproxy' needs a weaktable as upvalue */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   638
  lua_createtable(L, 0, 1);  /* new table `w' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   639
  lua_pushvalue(L, -1);  /* `w' will be its own metatable */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   640
  lua_setmetatable(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   641
  lua_pushliteral(L, "kv");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   642
  lua_setfield(L, -2, "__mode");  /* metatable(w).__mode = "kv" */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   643
  lua_pushcclosure(L, luaB_newproxy, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   644
  lua_setglobal(L, "newproxy");  /* set global `newproxy' */
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   648
LUALIB_API int luaopen_base (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   649
  base_open(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   650
  luaL_register(L, LUA_COLIBNAME, co_funcs);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   651
  return 2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   652
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   653