misc/liblua/lapi.c
author nemo
Tue, 09 Aug 2011 21:54:45 -0400
changeset 5531 52f4a5d9a222
parent 3697 d5b30d6373fc
child 10017 de822cd3df3a
permissions -rw-r--r--
Revert 3768e9a6ec40. I misread the test. Seems parachute works as expected now already
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: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     3
** Lua API
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     4
** See Copyright Notice in lua.h
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     5
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     6
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     7
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     8
#include <assert.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     9
#include <math.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 <string.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    12
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    13
#define lapi_c
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    14
#define LUA_CORE
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    15
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    16
#include "lua.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    17
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    18
#include "lapi.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    19
#include "ldebug.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    20
#include "ldo.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    21
#include "lfunc.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    22
#include "lgc.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    23
#include "lmem.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    24
#include "lobject.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    25
#include "lstate.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    26
#include "lstring.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    27
#include "ltable.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    28
#include "ltm.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    29
#include "lundump.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    30
#include "lvm.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    31
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    32
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    33
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    34
const char lua_ident[] =
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    35
  "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    36
  "$Authors: " LUA_AUTHORS " $\n"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    37
  "$URL: www.lua.org $\n";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    38
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
#define api_checknelems(L, n)	api_check(L, (n) <= (L->top - L->base))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    42
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    43
#define api_checkvalidindex(L, i)	api_check(L, (i) != luaO_nilobject)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    44
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    45
#define api_incr_top(L)   {api_check(L, L->top < L->ci->top); L->top++;}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    46
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    47
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    48
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    49
static TValue *index2adr (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    50
  if (idx > 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    51
    TValue *o = L->base + (idx - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    52
    api_check(L, idx <= L->ci->top - L->base);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    53
    if (o >= L->top) return cast(TValue *, luaO_nilobject);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    54
    else return o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    55
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    56
  else if (idx > LUA_REGISTRYINDEX) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    57
    api_check(L, idx != 0 && -idx <= L->top - L->base);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    58
    return L->top + idx;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    59
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    60
  else switch (idx) {  /* pseudo-indices */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    61
    case LUA_REGISTRYINDEX: return registry(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    62
    case LUA_ENVIRONINDEX: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    63
      Closure *func = curr_func(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    64
      sethvalue(L, &L->env, func->c.env);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    65
      return &L->env;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    66
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    67
    case LUA_GLOBALSINDEX: return gt(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    68
    default: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    69
      Closure *func = curr_func(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    70
      idx = LUA_GLOBALSINDEX - idx;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    71
      return (idx <= func->c.nupvalues)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    72
                ? &func->c.upvalue[idx-1]
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    73
                : cast(TValue *, luaO_nilobject);
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
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    77
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    78
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    79
static Table *getcurrenv (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    80
  if (L->ci == L->base_ci)  /* no enclosing function? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    81
    return hvalue(gt(L));  /* use global table as environment */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    82
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    83
    Closure *func = curr_func(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    84
    return func->c.env;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    85
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    86
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    87
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    88
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    89
void luaA_pushobject (lua_State *L, const TValue *o) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    90
  setobj2s(L, L->top, o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    91
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    92
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    93
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    94
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    95
LUA_API int lua_checkstack (lua_State *L, int size) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    96
  int res = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    97
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    98
  if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    99
    res = 0;  /* stack overflow */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   100
  else if (size > 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   101
    luaD_checkstack(L, size);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   102
    if (L->ci->top < L->top + size)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   103
      L->ci->top = L->top + size;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   104
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   105
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   106
  return res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   107
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   108
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   109
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   110
LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   111
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   112
  if (from == to) return;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   113
  lua_lock(to);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   114
  api_checknelems(from, n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   115
  api_check(from, G(from) == G(to));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   116
  api_check(from, to->ci->top - to->top >= n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   117
  from->top -= n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   118
  for (i = 0; i < n; i++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   119
    setobj2s(to, to->top++, from->top + i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   120
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   121
  lua_unlock(to);
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   125
LUA_API void lua_setlevel (lua_State *from, lua_State *to) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   126
  to->nCcalls = from->nCcalls;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   127
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   128
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   129
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   130
LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   131
  lua_CFunction old;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   132
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   133
  old = G(L)->panic;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   134
  G(L)->panic = panicf;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   135
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   136
  return old;
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
LUA_API lua_State *lua_newthread (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   141
  lua_State *L1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   142
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   143
  luaC_checkGC(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   144
  L1 = luaE_newthread(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   145
  setthvalue(L, L->top, L1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   146
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   147
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   148
  luai_userstatethread(L, L1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   149
  return L1;
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   153
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   154
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   155
** basic stack manipulation
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   159
LUA_API int lua_gettop (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   160
  return cast_int(L->top - L->base);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   161
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   162
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   163
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   164
LUA_API void lua_settop (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   165
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   166
  if (idx >= 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   167
    api_check(L, idx <= L->stack_last - L->base);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   168
    while (L->top < L->base + idx)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   169
      setnilvalue(L->top++);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   170
    L->top = L->base + idx;
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 {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   173
    api_check(L, -(idx+1) <= (L->top - L->base));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   174
    L->top += idx+1;  /* `subtract' index (index is negative) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   175
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   176
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   177
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   178
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   179
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   180
LUA_API void lua_remove (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   181
  StkId p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   182
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   183
  p = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   184
  api_checkvalidindex(L, p);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   185
  while (++p < L->top) setobjs2s(L, p-1, p);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   186
  L->top--;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   187
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   188
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   189
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   190
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   191
LUA_API void lua_insert (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   192
  StkId p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   193
  StkId q;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   194
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   195
  p = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   196
  api_checkvalidindex(L, p);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   197
  for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   198
  setobjs2s(L, p, L->top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   199
  lua_unlock(L);
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
LUA_API void lua_replace (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   204
  StkId o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   205
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   206
  /* explicit test for incompatible code */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   207
  if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   208
    luaG_runerror(L, "no calling environment");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   209
  api_checknelems(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   210
  o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   211
  api_checkvalidindex(L, o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   212
  if (idx == LUA_ENVIRONINDEX) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   213
    Closure *func = curr_func(L);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2812
diff changeset
   214
    api_check(L, ttistable(L->top - 1));
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   215
    func->c.env = hvalue(L->top - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   216
    luaC_barrier(L, func, L->top - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   217
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   218
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   219
    setobj(L, o, L->top - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   220
    if (idx < LUA_GLOBALSINDEX)  /* function upvalue? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   221
      luaC_barrier(L, curr_func(L), L->top - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   222
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   223
  L->top--;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   224
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   225
}
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
LUA_API void lua_pushvalue (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   229
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   230
  setobj2s(L, L->top, index2adr(L, idx));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   231
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   232
  lua_unlock(L);
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
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
** access functions (stack -> C)
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
LUA_API int lua_type (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   243
  StkId o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   244
  return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   248
LUA_API const char *lua_typename (lua_State *L, int t) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   249
  UNUSED(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   250
  return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   251
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   252
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   253
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   254
LUA_API int lua_iscfunction (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   255
  StkId o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   256
  return iscfunction(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   257
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   258
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   259
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   260
LUA_API int lua_isnumber (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   261
  TValue n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   262
  const TValue *o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   263
  return tonumber(o, &n);
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   267
LUA_API int lua_isstring (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   268
  int t = lua_type(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   269
  return (t == LUA_TSTRING || t == LUA_TNUMBER);
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
LUA_API int lua_isuserdata (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   274
  const TValue *o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   275
  return (ttisuserdata(o) || ttislightuserdata(o));
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   279
LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   280
  StkId o1 = index2adr(L, index1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   281
  StkId o2 = index2adr(L, index2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   282
  return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   283
         : luaO_rawequalObj(o1, o2);
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
LUA_API int lua_equal (lua_State *L, int index1, int index2) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   288
  StkId o1, o2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   289
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   290
  lua_lock(L);  /* may call tag method */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   291
  o1 = index2adr(L, index1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   292
  o2 = index2adr(L, index2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   293
  i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   294
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   295
  return i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   296
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   297
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   298
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   299
LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   300
  StkId o1, o2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   301
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   302
  lua_lock(L);  /* may call tag method */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   303
  o1 = index2adr(L, index1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   304
  o2 = index2adr(L, index2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   305
  i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   306
       : luaV_lessthan(L, o1, o2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   307
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   308
  return i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   309
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   310
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   311
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   312
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   313
LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   314
  TValue n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   315
  const TValue *o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   316
  if (tonumber(o, &n))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   317
    return nvalue(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   318
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   319
    return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   320
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   321
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   322
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   323
LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   324
  TValue n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   325
  const TValue *o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   326
  if (tonumber(o, &n)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   327
    lua_Integer res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   328
    lua_Number num = nvalue(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   329
    lua_number2integer(res, num);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   330
    return res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   331
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   332
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   333
    return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   334
}
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
LUA_API int lua_toboolean (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   338
  const TValue *o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   339
  return !l_isfalse(o);
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   343
LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   344
  StkId o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   345
  if (!ttisstring(o)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   346
    lua_lock(L);  /* `luaV_tostring' may create a new string */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   347
    if (!luaV_tostring(L, o)) {  /* conversion failed? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   348
      if (len != NULL) *len = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   349
      lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   350
      return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   351
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   352
    luaC_checkGC(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   353
    o = index2adr(L, idx);  /* previous call may reallocate the stack */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   354
    lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   355
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   356
  if (len != NULL) *len = tsvalue(o)->len;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   357
  return svalue(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   358
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   359
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   360
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   361
LUA_API size_t lua_objlen (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   362
  StkId o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   363
  switch (ttype(o)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   364
    case LUA_TSTRING: return tsvalue(o)->len;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   365
    case LUA_TUSERDATA: return uvalue(o)->len;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   366
    case LUA_TTABLE: return luaH_getn(hvalue(o));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   367
    case LUA_TNUMBER: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   368
      size_t l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   369
      lua_lock(L);  /* `luaV_tostring' may create a new string */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   370
      l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   371
      lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   372
      return l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   373
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   374
    default: return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   375
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   376
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   377
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   378
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   379
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   380
  StkId o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   381
  return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
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
LUA_API void *lua_touserdata (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   386
  StkId o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   387
  switch (ttype(o)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   388
    case LUA_TUSERDATA: return (rawuvalue(o) + 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   389
    case LUA_TLIGHTUSERDATA: return pvalue(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   390
    default: return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   391
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   392
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   393
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   394
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   395
LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   396
  StkId o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   397
  return (!ttisthread(o)) ? NULL : thvalue(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   398
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   399
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   400
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   401
LUA_API const void *lua_topointer (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   402
  StkId o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   403
  switch (ttype(o)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   404
    case LUA_TTABLE: return hvalue(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   405
    case LUA_TFUNCTION: return clvalue(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   406
    case LUA_TTHREAD: return thvalue(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   407
    case LUA_TUSERDATA:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   408
    case LUA_TLIGHTUSERDATA:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   409
      return lua_touserdata(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   410
    default: return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   411
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   412
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   413
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   414
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   415
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   416
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   417
** push functions (C -> stack)
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
LUA_API void lua_pushnil (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   422
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   423
  setnilvalue(L->top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   424
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   425
  lua_unlock(L);
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
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   430
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   431
  setnvalue(L->top, n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   432
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   433
  lua_unlock(L);
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   437
LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   438
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   439
  setnvalue(L->top, cast_num(n));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   440
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   441
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   442
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   443
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   444
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   445
LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   446
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   447
  luaC_checkGC(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   448
  setsvalue2s(L, L->top, luaS_newlstr(L, s, len));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   449
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   450
  lua_unlock(L);
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
LUA_API void lua_pushstring (lua_State *L, const char *s) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   455
  if (s == NULL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   456
    lua_pushnil(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   457
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   458
    lua_pushlstring(L, s, strlen(s));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   459
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   460
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   461
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   462
LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   463
                                      va_list argp) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   464
  const char *ret;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   465
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   466
  luaC_checkGC(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   467
  ret = luaO_pushvfstring(L, fmt, argp);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   468
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   469
  return ret;
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   473
LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   474
  const char *ret;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   475
  va_list argp;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   476
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   477
  luaC_checkGC(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   478
  va_start(argp, fmt);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   479
  ret = luaO_pushvfstring(L, fmt, argp);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   480
  va_end(argp);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   481
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   482
  return ret;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   483
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   484
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   485
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   486
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   487
  Closure *cl;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   488
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   489
  luaC_checkGC(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   490
  api_checknelems(L, n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   491
  cl = luaF_newCclosure(L, n, getcurrenv(L));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   492
  cl->c.f = fn;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   493
  L->top -= n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   494
  while (n--)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   495
    setobj2n(L, &cl->c.upvalue[n], L->top+n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   496
  setclvalue(L, L->top, cl);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   497
  lua_assert(iswhite(obj2gco(cl)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   498
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   499
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   500
}
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
LUA_API void lua_pushboolean (lua_State *L, int b) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   504
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   505
  setbvalue(L->top, (b != 0));  /* ensure that true is 1 */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   506
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   507
  lua_unlock(L);
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   511
LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   512
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   513
  setpvalue(L->top, p);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   514
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   515
  lua_unlock(L);
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   519
LUA_API int lua_pushthread (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   520
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   521
  setthvalue(L, L->top, L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   522
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   523
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   524
  return (G(L)->mainthread == L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   525
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   526
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
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   530
** get functions (Lua -> stack)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   531
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   532
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   533
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   534
LUA_API void lua_gettable (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   535
  StkId t;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   536
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   537
  t = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   538
  api_checkvalidindex(L, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   539
  luaV_gettable(L, t, L->top - 1, L->top - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   540
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   541
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   542
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   543
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   544
LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   545
  StkId t;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   546
  TValue key;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   547
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   548
  t = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   549
  api_checkvalidindex(L, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   550
  setsvalue(L, &key, luaS_new(L, k));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   551
  luaV_gettable(L, t, &key, L->top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   552
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   553
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   554
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   555
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   556
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   557
LUA_API void lua_rawget (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   558
  StkId t;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   559
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   560
  t = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   561
  api_check(L, ttistable(t));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   562
  setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   563
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   564
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   565
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   566
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   567
LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   568
  StkId o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   569
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   570
  o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   571
  api_check(L, ttistable(o));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   572
  setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   573
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   574
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   575
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   576
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   577
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   578
LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   579
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   580
  luaC_checkGC(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   581
  sethvalue(L, L->top, luaH_new(L, narray, nrec));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   582
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   583
  lua_unlock(L);
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   587
LUA_API int lua_getmetatable (lua_State *L, int objindex) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   588
  const TValue *obj;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   589
  Table *mt = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   590
  int res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   591
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   592
  obj = index2adr(L, objindex);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   593
  switch (ttype(obj)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   594
    case LUA_TTABLE:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   595
      mt = hvalue(obj)->metatable;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   596
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   597
    case LUA_TUSERDATA:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   598
      mt = uvalue(obj)->metatable;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   599
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   600
    default:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   601
      mt = G(L)->mt[ttype(obj)];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   602
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   603
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   604
  if (mt == NULL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   605
    res = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   606
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   607
    sethvalue(L, L->top, mt);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   608
    api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   609
    res = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   610
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   611
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   612
  return res;
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
LUA_API void lua_getfenv (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   617
  StkId o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   618
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   619
  o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   620
  api_checkvalidindex(L, o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   621
  switch (ttype(o)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   622
    case LUA_TFUNCTION:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   623
      sethvalue(L, L->top, clvalue(o)->c.env);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   624
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   625
    case LUA_TUSERDATA:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   626
      sethvalue(L, L->top, uvalue(o)->env);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   627
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   628
    case LUA_TTHREAD:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   629
      setobj2s(L, L->top,  gt(thvalue(o)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   630
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   631
    default:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   632
      setnilvalue(L->top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   633
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   634
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   635
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   636
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   637
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   638
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   639
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   640
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   641
** set functions (stack -> Lua)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   642
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   643
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   644
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   645
LUA_API void lua_settable (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   646
  StkId t;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   647
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   648
  api_checknelems(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   649
  t = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   650
  api_checkvalidindex(L, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   651
  luaV_settable(L, t, L->top - 2, L->top - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   652
  L->top -= 2;  /* pop index and value */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   653
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   654
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   655
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   656
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   657
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   658
  StkId t;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   659
  TValue key;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   660
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   661
  api_checknelems(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   662
  t = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   663
  api_checkvalidindex(L, t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   664
  setsvalue(L, &key, luaS_new(L, k));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   665
  luaV_settable(L, t, &key, L->top - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   666
  L->top--;  /* pop value */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   667
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   668
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   669
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   670
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   671
LUA_API void lua_rawset (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   672
  StkId t;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   673
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   674
  api_checknelems(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   675
  t = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   676
  api_check(L, ttistable(t));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   677
  setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   678
  luaC_barriert(L, hvalue(t), L->top-1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   679
  L->top -= 2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   680
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   681
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   682
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   683
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   684
LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   685
  StkId o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   686
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   687
  api_checknelems(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   688
  o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   689
  api_check(L, ttistable(o));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   690
  setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   691
  luaC_barriert(L, hvalue(o), L->top-1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   692
  L->top--;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   693
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   694
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   695
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   696
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   697
LUA_API int lua_setmetatable (lua_State *L, int objindex) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   698
  TValue *obj;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   699
  Table *mt;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   700
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   701
  api_checknelems(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   702
  obj = index2adr(L, objindex);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   703
  api_checkvalidindex(L, obj);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   704
  if (ttisnil(L->top - 1))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   705
    mt = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   706
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   707
    api_check(L, ttistable(L->top - 1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   708
    mt = hvalue(L->top - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   709
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   710
  switch (ttype(obj)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   711
    case LUA_TTABLE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   712
      hvalue(obj)->metatable = mt;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   713
      if (mt)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   714
        luaC_objbarriert(L, hvalue(obj), mt);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   715
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   716
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   717
    case LUA_TUSERDATA: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   718
      uvalue(obj)->metatable = mt;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   719
      if (mt)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   720
        luaC_objbarrier(L, rawuvalue(obj), mt);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   721
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   722
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   723
    default: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   724
      G(L)->mt[ttype(obj)] = mt;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   725
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   726
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   727
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   728
  L->top--;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   729
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   730
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   731
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   732
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   733
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   734
LUA_API int lua_setfenv (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   735
  StkId o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   736
  int res = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   737
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   738
  api_checknelems(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   739
  o = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   740
  api_checkvalidindex(L, o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   741
  api_check(L, ttistable(L->top - 1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   742
  switch (ttype(o)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   743
    case LUA_TFUNCTION:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   744
      clvalue(o)->c.env = hvalue(L->top - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   745
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   746
    case LUA_TUSERDATA:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   747
      uvalue(o)->env = hvalue(L->top - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   748
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   749
    case LUA_TTHREAD:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   750
      sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   751
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   752
    default:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   753
      res = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   754
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   755
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   756
  if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   757
  L->top--;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   758
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   759
  return res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   760
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   761
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   762
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   763
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   764
** `load' and `call' functions (run Lua code)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   765
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   766
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   767
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   768
#define adjustresults(L,nres) \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   769
    { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   770
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   771
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   772
#define checkresults(L,na,nr) \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   773
     api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2812
diff changeset
   774
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   775
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   776
LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   777
  StkId func;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   778
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   779
  api_checknelems(L, nargs+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   780
  checkresults(L, nargs, nresults);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   781
  func = L->top - (nargs+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   782
  luaD_call(L, func, nresults);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   783
  adjustresults(L, nresults);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   784
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   785
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   786
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   787
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   788
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   789
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   790
** Execute a protected call.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   791
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   792
struct CallS {  /* data to `f_call' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   793
  StkId func;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   794
  int nresults;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   795
};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   796
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   797
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   798
static void f_call (lua_State *L, void *ud) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   799
  struct CallS *c = cast(struct CallS *, ud);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   800
  luaD_call(L, c->func, c->nresults);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   801
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   802
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   803
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   804
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   805
LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   806
  struct CallS c;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   807
  int status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   808
  ptrdiff_t func;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   809
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   810
  api_checknelems(L, nargs+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   811
  checkresults(L, nargs, nresults);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   812
  if (errfunc == 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   813
    func = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   814
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   815
    StkId o = index2adr(L, errfunc);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   816
    api_checkvalidindex(L, o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   817
    func = savestack(L, o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   818
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   819
  c.func = L->top - (nargs+1);  /* function to be called */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   820
  c.nresults = nresults;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   821
  status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   822
  adjustresults(L, nresults);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   823
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   824
  return status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   825
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   826
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   827
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   828
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   829
** Execute a protected C call.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   830
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   831
struct CCallS {  /* data to `f_Ccall' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   832
  lua_CFunction func;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   833
  void *ud;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   834
};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   835
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   836
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   837
static void f_Ccall (lua_State *L, void *ud) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   838
  struct CCallS *c = cast(struct CCallS *, ud);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   839
  Closure *cl;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   840
  cl = luaF_newCclosure(L, 0, getcurrenv(L));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   841
  cl->c.f = c->func;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   842
  setclvalue(L, L->top, cl);  /* push function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   843
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   844
  setpvalue(L->top, c->ud);  /* push only argument */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   845
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   846
  luaD_call(L, L->top - 2, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   847
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   848
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   849
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   850
LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   851
  struct CCallS c;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   852
  int status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   853
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   854
  c.func = func;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   855
  c.ud = ud;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   856
  status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   857
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   858
  return status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   859
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   860
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   861
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   862
LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   863
                      const char *chunkname) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   864
  ZIO z;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   865
  int status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   866
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   867
  if (!chunkname) chunkname = "?";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   868
  luaZ_init(L, &z, reader, data);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   869
  status = luaD_protectedparser(L, &z, chunkname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   870
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   871
  return status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   872
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   873
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   874
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   875
LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   876
  int status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   877
  TValue *o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   878
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   879
  api_checknelems(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   880
  o = L->top - 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   881
  if (isLfunction(o))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   882
    status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   883
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   884
    status = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   885
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   886
  return status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   887
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   888
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   889
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   890
LUA_API int  lua_status (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   891
  return L->status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   892
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   893
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   894
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   895
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   896
** Garbage-collection function
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   897
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   898
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   899
LUA_API int lua_gc (lua_State *L, int what, int data) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   900
  int res = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   901
  global_State *g;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   902
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   903
  g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   904
  switch (what) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   905
    case LUA_GCSTOP: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   906
      g->GCthreshold = MAX_LUMEM;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   907
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   908
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   909
    case LUA_GCRESTART: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   910
      g->GCthreshold = g->totalbytes;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   911
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   912
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   913
    case LUA_GCCOLLECT: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   914
      luaC_fullgc(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   915
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   916
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   917
    case LUA_GCCOUNT: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   918
      /* GC values are expressed in Kbytes: #bytes/2^10 */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   919
      res = cast_int(g->totalbytes >> 10);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   920
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   921
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   922
    case LUA_GCCOUNTB: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   923
      res = cast_int(g->totalbytes & 0x3ff);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   924
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   925
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   926
    case LUA_GCSTEP: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   927
      lu_mem a = (cast(lu_mem, data) << 10);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   928
      if (a <= g->totalbytes)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   929
        g->GCthreshold = g->totalbytes - a;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   930
      else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   931
        g->GCthreshold = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   932
      while (g->GCthreshold <= g->totalbytes) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   933
        luaC_step(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   934
        if (g->gcstate == GCSpause) {  /* end of cycle? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   935
          res = 1;  /* signal it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   936
          break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   937
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   938
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   939
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   940
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   941
    case LUA_GCSETPAUSE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   942
      res = g->gcpause;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   943
      g->gcpause = data;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   944
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   945
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   946
    case LUA_GCSETSTEPMUL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   947
      res = g->gcstepmul;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   948
      g->gcstepmul = data;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   949
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   950
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   951
    default: res = -1;  /* invalid option */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   952
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   953
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   954
  return res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   955
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   956
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   957
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   958
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   959
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   960
** miscellaneous functions
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   961
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   962
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   963
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   964
LUA_API int lua_error (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   965
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   966
  api_checknelems(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   967
  luaG_errormsg(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   968
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   969
  return 0;  /* to avoid warnings */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   970
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   971
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   972
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   973
LUA_API int lua_next (lua_State *L, int idx) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   974
  StkId t;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   975
  int more;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   976
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   977
  t = index2adr(L, idx);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   978
  api_check(L, ttistable(t));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   979
  more = luaH_next(L, hvalue(t), L->top - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   980
  if (more) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   981
    api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   982
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   983
  else  /* no more elements */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   984
    L->top -= 1;  /* remove key */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   985
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   986
  return more;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   987
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   988
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   989
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   990
LUA_API void lua_concat (lua_State *L, int n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   991
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   992
  api_checknelems(L, n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   993
  if (n >= 2) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   994
    luaC_checkGC(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   995
    luaV_concat(L, n, cast_int(L->top - L->base) - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   996
    L->top -= (n-1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   997
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   998
  else if (n == 0) {  /* push empty string */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   999
    setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1000
    api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1001
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1002
  /* else n == 1; nothing to do */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1003
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1004
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1005
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1006
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1007
LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1008
  lua_Alloc f;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1009
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1010
  if (ud) *ud = G(L)->ud;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1011
  f = G(L)->frealloc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1012
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1013
  return f;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1014
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1015
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1016
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1017
LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1018
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1019
  G(L)->ud = ud;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1020
  G(L)->frealloc = f;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1021
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1022
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1023
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1024
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1025
LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1026
  Udata *u;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1027
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1028
  luaC_checkGC(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1029
  u = luaS_newudata(L, size, getcurrenv(L));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1030
  setuvalue(L, L->top, u);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1031
  api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1032
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1033
  return u + 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1034
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1035
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1036
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1037
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1038
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1039
static const char *aux_upvalue (StkId fi, int n, TValue **val) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1040
  Closure *f;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1041
  if (!ttisfunction(fi)) return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1042
  f = clvalue(fi);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1043
  if (f->c.isC) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1044
    if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1045
    *val = &f->c.upvalue[n-1];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1046
    return "";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1047
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1048
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1049
    Proto *p = f->l.p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1050
    if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1051
    *val = f->l.upvals[n-1]->v;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1052
    return getstr(p->upvalues[n-1]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1053
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1054
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1055
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1056
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1057
LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1058
  const char *name;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1059
  TValue *val;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1060
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1061
  name = aux_upvalue(index2adr(L, funcindex), n, &val);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1062
  if (name) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1063
    setobj2s(L, L->top, val);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1064
    api_incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1065
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1066
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1067
  return name;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1068
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1069
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1070
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1071
LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1072
  const char *name;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1073
  TValue *val;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1074
  StkId fi;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1075
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1076
  fi = index2adr(L, funcindex);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1077
  api_checknelems(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1078
  name = aux_upvalue(fi, n, &val);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1079
  if (name) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1080
    L->top--;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1081
    setobj(L, val, L->top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1082
    luaC_barrier(L, clvalue(fi), L->top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1083
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1084
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1085
  return name;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1086
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
  1087