misc/liblua/ldo.c
author Wuzzy <almikes@aol.com>
Sun, 01 Oct 2017 01:48:30 +0200
changeset 12631 ca958a859322
parent 3697 d5b30d6373fc
permissions -rw-r--r--
Change Highlander.cfg to select Default by default Because if you scroll through the list, you often end up with HedgeEditor being chosen as game scheme. xD
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: ldo.c,v 2.38.1.3 2008/01/18 22:31:22 roberto Exp $
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     3
** Stack and Call structure of Lua
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 <setjmp.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     9
#include <stdlib.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    10
#include <string.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    11
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    12
#define ldo_c
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    13
#define LUA_CORE
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    14
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    15
#include "lua.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    16
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    17
#include "ldebug.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    18
#include "ldo.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    19
#include "lfunc.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    20
#include "lgc.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    21
#include "lmem.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    22
#include "lobject.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    23
#include "lopcodes.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    24
#include "lparser.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
#include "lzio.h"
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    35
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    36
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    37
** {======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    38
** Error-recovery functions
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    39
** =======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    40
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    41
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    42
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    43
/* chain list of long jump buffers */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    44
struct lua_longjmp {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    45
  struct lua_longjmp *previous;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    46
  luai_jmpbuf b;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    47
  volatile int status;  /* error code */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    48
};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    49
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    50
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    51
void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    52
  switch (errcode) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    53
    case LUA_ERRMEM: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    54
      setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    55
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    56
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    57
    case LUA_ERRERR: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    58
      setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    59
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    60
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    61
    case LUA_ERRSYNTAX:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    62
    case LUA_ERRRUN: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    63
      setobjs2s(L, oldtop, L->top - 1);  /* error message on current top */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    64
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    65
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    66
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    67
  L->top = oldtop + 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    68
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    69
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    70
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    71
static void restore_stack_limit (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    72
  lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    73
  if (L->size_ci > LUAI_MAXCALLS) {  /* there was an overflow? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    74
    int inuse = cast_int(L->ci - L->base_ci);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    75
    if (inuse + 1 < LUAI_MAXCALLS)  /* can `undo' overflow? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    76
      luaD_reallocCI(L, LUAI_MAXCALLS);
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    80
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    81
static void resetstack (lua_State *L, int status) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    82
  L->ci = L->base_ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    83
  L->base = L->ci->base;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    84
  luaF_close(L, L->base);  /* close eventual pending closures */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    85
  luaD_seterrorobj(L, status, L->base);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    86
  L->nCcalls = L->baseCcalls;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    87
  L->allowhook = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    88
  restore_stack_limit(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    89
  L->errfunc = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    90
  L->errorJmp = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    91
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    92
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    93
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    94
void luaD_throw (lua_State *L, int errcode) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    95
  if (L->errorJmp) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    96
    L->errorJmp->status = errcode;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    97
    LUAI_THROW(L, L->errorJmp);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    98
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    99
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   100
    L->status = cast_byte(errcode);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   101
    if (G(L)->panic) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   102
      resetstack(L, errcode);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   103
      lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   104
      G(L)->panic(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   105
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   106
    exit(EXIT_FAILURE);
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   111
int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   112
  struct lua_longjmp lj;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   113
  lj.status = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   114
  lj.previous = L->errorJmp;  /* chain new error handler */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   115
  L->errorJmp = &lj;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   116
  LUAI_TRY(L, &lj,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   117
    (*f)(L, ud);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   118
  );
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   119
  L->errorJmp = lj.previous;  /* restore old error handler */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   120
  return lj.status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   121
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   122
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   123
/* }====================================================== */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   124
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   125
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   126
static void correctstack (lua_State *L, TValue *oldstack) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   127
  CallInfo *ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   128
  GCObject *up;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   129
  L->top = (L->top - oldstack) + L->stack;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   130
  for (up = L->openupval; up != NULL; up = up->gch.next)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   131
    gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   132
  for (ci = L->base_ci; ci <= L->ci; ci++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   133
    ci->top = (ci->top - oldstack) + L->stack;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   134
    ci->base = (ci->base - oldstack) + L->stack;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   135
    ci->func = (ci->func - oldstack) + L->stack;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   136
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   137
  L->base = (L->base - oldstack) + L->stack;
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   141
void luaD_reallocstack (lua_State *L, int newsize) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   142
  TValue *oldstack = L->stack;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   143
  int realsize = newsize + 1 + EXTRA_STACK;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   144
  lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   145
  luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   146
  L->stacksize = realsize;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   147
  L->stack_last = L->stack+newsize;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   148
  correctstack(L, oldstack);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   149
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   150
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   151
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   152
void luaD_reallocCI (lua_State *L, int newsize) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   153
  CallInfo *oldci = L->base_ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   154
  luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   155
  L->size_ci = newsize;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   156
  L->ci = (L->ci - oldci) + L->base_ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   157
  L->end_ci = L->base_ci + L->size_ci - 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   158
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   159
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   160
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   161
void luaD_growstack (lua_State *L, int n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   162
  if (n <= L->stacksize)  /* double size is enough? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   163
    luaD_reallocstack(L, 2*L->stacksize);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   164
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   165
    luaD_reallocstack(L, L->stacksize + n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   166
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   167
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   168
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   169
static CallInfo *growCI (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   170
  if (L->size_ci > LUAI_MAXCALLS)  /* overflow while handling overflow? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   171
    luaD_throw(L, LUA_ERRERR);
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
    luaD_reallocCI(L, 2*L->size_ci);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   174
    if (L->size_ci > LUAI_MAXCALLS)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   175
      luaG_runerror(L, "stack overflow");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   176
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   177
  return ++L->ci;
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   181
void luaD_callhook (lua_State *L, int event, int line) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   182
  lua_Hook hook = L->hook;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   183
  if (hook && L->allowhook) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   184
    ptrdiff_t top = savestack(L, L->top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   185
    ptrdiff_t ci_top = savestack(L, L->ci->top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   186
    lua_Debug ar;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   187
    ar.event = event;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   188
    ar.currentline = line;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   189
    if (event == LUA_HOOKTAILRET)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   190
      ar.i_ci = 0;  /* tail call; no debug information about it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   191
    else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   192
      ar.i_ci = cast_int(L->ci - L->base_ci);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   193
    luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   194
    L->ci->top = L->top + LUA_MINSTACK;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   195
    lua_assert(L->ci->top <= L->stack_last);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   196
    L->allowhook = 0;  /* cannot call hooks inside a hook */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   197
    lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   198
    (*hook)(L, &ar);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   199
    lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   200
    lua_assert(!L->allowhook);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   201
    L->allowhook = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   202
    L->ci->top = restorestack(L, ci_top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   203
    L->top = restorestack(L, top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   204
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   205
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   206
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   207
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   208
static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   209
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   210
  int nfixargs = p->numparams;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   211
  Table *htab = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   212
  StkId base, fixed;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   213
  for (; actual < nfixargs; ++actual)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   214
    setnilvalue(L->top++);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   215
#if defined(LUA_COMPAT_VARARG)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   216
  if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   217
    int nvar = actual - nfixargs;  /* number of extra arguments */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   218
    lua_assert(p->is_vararg & VARARG_HASARG);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   219
    luaC_checkGC(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   220
    htab = luaH_new(L, nvar, 1);  /* create `arg' table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   221
    for (i=0; i<nvar; i++)  /* put extra arguments into `arg' table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   222
      setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   223
    /* store counter in field `n' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   224
    setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   225
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   226
#endif
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   227
  /* move fixed parameters to final position */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   228
  fixed = L->top - actual;  /* first fixed argument */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   229
  base = L->top;  /* final position of first argument */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   230
  for (i=0; i<nfixargs; i++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   231
    setobjs2s(L, L->top++, fixed+i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   232
    setnilvalue(fixed+i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   233
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   234
  /* add `arg' parameter */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   235
  if (htab) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   236
    sethvalue(L, L->top++, htab);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   237
    lua_assert(iswhite(obj2gco(htab)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   238
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   239
  return base;
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   243
static StkId tryfuncTM (lua_State *L, StkId func) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   244
  const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   245
  StkId p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   246
  ptrdiff_t funcr = savestack(L, func);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   247
  if (!ttisfunction(tm))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   248
    luaG_typeerror(L, func, "call");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   249
  /* Open a hole inside the stack at `func' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   250
  for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   251
  incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   252
  func = restorestack(L, funcr);  /* previous call may change stack */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   253
  setobj2s(L, func, tm);  /* tag method is the new function to be called */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   254
  return func;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   255
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   256
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   257
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   258
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   259
#define inc_ci(L) \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   260
  ((L->ci == L->end_ci) ? growCI(L) : \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   261
   (condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   262
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   263
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   264
int luaD_precall (lua_State *L, StkId func, int nresults) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   265
  LClosure *cl;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   266
  ptrdiff_t funcr;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   267
  if (!ttisfunction(func)) /* `func' is not a function? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   268
    func = tryfuncTM(L, func);  /* check the `function' tag method */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   269
  funcr = savestack(L, func);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   270
  cl = &clvalue(func)->l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   271
  L->ci->savedpc = L->savedpc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   272
  if (!cl->isC) {  /* Lua function? prepare its call */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   273
    CallInfo *ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   274
    StkId st, base;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   275
    Proto *p = cl->p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   276
    luaD_checkstack(L, p->maxstacksize);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   277
    func = restorestack(L, funcr);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   278
    if (!p->is_vararg) {  /* no varargs? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   279
      base = func + 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   280
      if (L->top > base + p->numparams)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   281
        L->top = base + p->numparams;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   282
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   283
    else {  /* vararg function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   284
      int nargs = cast_int(L->top - func) - 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   285
      base = adjust_varargs(L, p, nargs);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   286
      func = restorestack(L, funcr);  /* previous call may change the stack */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   287
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   288
    ci = inc_ci(L);  /* now `enter' new function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   289
    ci->func = func;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   290
    L->base = ci->base = base;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   291
    ci->top = L->base + p->maxstacksize;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   292
    lua_assert(ci->top <= L->stack_last);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   293
    L->savedpc = p->code;  /* starting point */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   294
    ci->tailcalls = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   295
    ci->nresults = nresults;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   296
    for (st = L->top; st < ci->top; st++)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   297
      setnilvalue(st);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   298
    L->top = ci->top;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   299
    if (L->hookmask & LUA_MASKCALL) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   300
      L->savedpc++;  /* hooks assume 'pc' is already incremented */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   301
      luaD_callhook(L, LUA_HOOKCALL, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   302
      L->savedpc--;  /* correct 'pc' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   303
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   304
    return PCRLUA;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   305
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   306
  else {  /* if is a C function, call it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   307
    CallInfo *ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   308
    int n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   309
    luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   310
    ci = inc_ci(L);  /* now `enter' new function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   311
    ci->func = restorestack(L, funcr);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   312
    L->base = ci->base = ci->func + 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   313
    ci->top = L->top + LUA_MINSTACK;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   314
    lua_assert(ci->top <= L->stack_last);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   315
    ci->nresults = nresults;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   316
    if (L->hookmask & LUA_MASKCALL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   317
      luaD_callhook(L, LUA_HOOKCALL, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   318
    lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   319
    n = (*curr_func(L)->c.f)(L);  /* do the actual call */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   320
    lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   321
    if (n < 0)  /* yielding? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   322
      return PCRYIELD;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   323
    else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   324
      luaD_poscall(L, L->top - n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   325
      return PCRC;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   326
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   327
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   328
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   329
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   330
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   331
static StkId callrethooks (lua_State *L, StkId firstResult) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   332
  ptrdiff_t fr = savestack(L, firstResult);  /* next call may change stack */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   333
  luaD_callhook(L, LUA_HOOKRET, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   334
  if (f_isLua(L->ci)) {  /* Lua function? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   335
    while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   336
      luaD_callhook(L, LUA_HOOKTAILRET, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   337
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   338
  return restorestack(L, fr);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   339
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   340
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   341
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   342
int luaD_poscall (lua_State *L, StkId firstResult) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   343
  StkId res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   344
  int wanted, i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   345
  CallInfo *ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   346
  if (L->hookmask & LUA_MASKRET)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   347
    firstResult = callrethooks(L, firstResult);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   348
  ci = L->ci--;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   349
  res = ci->func;  /* res == final position of 1st result */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   350
  wanted = ci->nresults;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   351
  L->base = (ci - 1)->base;  /* restore base */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   352
  L->savedpc = (ci - 1)->savedpc;  /* restore savedpc */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   353
  /* move results to correct place */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   354
  for (i = wanted; i != 0 && firstResult < L->top; i--)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   355
    setobjs2s(L, res++, firstResult++);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   356
  while (i-- > 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   357
    setnilvalue(res++);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   358
  L->top = res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   359
  return (wanted - LUA_MULTRET);  /* 0 iff wanted == LUA_MULTRET */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   360
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   361
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   362
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   363
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   364
** Call a function (C or Lua). The function to be called is at *func.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   365
** The arguments are on the stack, right after the function.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   366
** When returns, all the results are on the stack, starting at the original
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   367
** function position.
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2812
diff changeset
   368
*/
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   369
void luaD_call (lua_State *L, StkId func, int nResults) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   370
  if (++L->nCcalls >= LUAI_MAXCCALLS) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   371
    if (L->nCcalls == LUAI_MAXCCALLS)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   372
      luaG_runerror(L, "C stack overflow");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   373
    else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   374
      luaD_throw(L, LUA_ERRERR);  /* error while handing stack error */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   375
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   376
  if (luaD_precall(L, func, nResults) == PCRLUA)  /* is a Lua function? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   377
    luaV_execute(L, 1);  /* call it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   378
  L->nCcalls--;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   379
  luaC_checkGC(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   380
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   381
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   382
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   383
static void resume (lua_State *L, void *ud) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   384
  StkId firstArg = cast(StkId, ud);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   385
  CallInfo *ci = L->ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   386
  if (L->status == 0) {  /* start coroutine? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   387
    lua_assert(ci == L->base_ci && firstArg > L->base);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   388
    if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   389
      return;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   390
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   391
  else {  /* resuming from previous yield */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   392
    lua_assert(L->status == LUA_YIELD);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   393
    L->status = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   394
    if (!f_isLua(ci)) {  /* `common' yield? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   395
      /* finish interrupted execution of `OP_CALL' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   396
      lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL ||
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   397
                 GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   398
      if (luaD_poscall(L, firstArg))  /* complete it... */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   399
        L->top = L->ci->top;  /* and correct top if not multiple results */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   400
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   401
    else  /* yielded inside a hook: just continue its execution */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   402
      L->base = L->ci->base;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   403
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   404
  luaV_execute(L, cast_int(L->ci - L->base_ci));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   405
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   406
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   407
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   408
static int resume_error (lua_State *L, const char *msg) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   409
  L->top = L->ci->base;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   410
  setsvalue2s(L, L->top, luaS_new(L, msg));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   411
  incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   412
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   413
  return LUA_ERRRUN;
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
LUA_API int lua_resume (lua_State *L, int nargs) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   418
  int status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   419
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   420
  if (L->status != LUA_YIELD && (L->status != 0 || L->ci != L->base_ci))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   421
      return resume_error(L, "cannot resume non-suspended coroutine");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   422
  if (L->nCcalls >= LUAI_MAXCCALLS)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   423
    return resume_error(L, "C stack overflow");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   424
  luai_userstateresume(L, nargs);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   425
  lua_assert(L->errfunc == 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   426
  L->baseCcalls = ++L->nCcalls;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   427
  status = luaD_rawrunprotected(L, resume, L->top - nargs);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   428
  if (status != 0) {  /* error? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   429
    L->status = cast_byte(status);  /* mark thread as `dead' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   430
    luaD_seterrorobj(L, status, L->top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   431
    L->ci->top = L->top;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   432
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   433
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   434
    lua_assert(L->nCcalls == L->baseCcalls);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   435
    status = L->status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   436
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   437
  --L->nCcalls;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   438
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   439
  return status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   440
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   441
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   442
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   443
LUA_API int lua_yield (lua_State *L, int nresults) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   444
  luai_userstateyield(L, nresults);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   445
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   446
  if (L->nCcalls > L->baseCcalls)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   447
    luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   448
  L->base = L->top - nresults;  /* protect stack slots below */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   449
  L->status = LUA_YIELD;
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
  return -1;
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   455
int luaD_pcall (lua_State *L, Pfunc func, void *u,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   456
                ptrdiff_t old_top, ptrdiff_t ef) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   457
  int status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   458
  unsigned short oldnCcalls = L->nCcalls;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   459
  ptrdiff_t old_ci = saveci(L, L->ci);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   460
  lu_byte old_allowhooks = L->allowhook;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   461
  ptrdiff_t old_errfunc = L->errfunc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   462
  L->errfunc = ef;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   463
  status = luaD_rawrunprotected(L, func, u);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   464
  if (status != 0) {  /* an error occurred? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   465
    StkId oldtop = restorestack(L, old_top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   466
    luaF_close(L, oldtop);  /* close eventual pending closures */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   467
    luaD_seterrorobj(L, status, oldtop);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   468
    L->nCcalls = oldnCcalls;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   469
    L->ci = restoreci(L, old_ci);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   470
    L->base = L->ci->base;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   471
    L->savedpc = L->ci->savedpc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   472
    L->allowhook = old_allowhooks;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   473
    restore_stack_limit(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   474
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   475
  L->errfunc = old_errfunc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   476
  return status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   477
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   478
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   479
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   480
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   481
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   482
** Execute a protected parser.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   483
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   484
struct SParser {  /* data to `f_parser' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   485
  ZIO *z;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   486
  Mbuffer buff;  /* buffer to be used by the scanner */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   487
  const char *name;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   488
};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   489
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   490
static void f_parser (lua_State *L, void *ud) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   491
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   492
  Proto *tf;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   493
  Closure *cl;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   494
  struct SParser *p = cast(struct SParser *, ud);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   495
  int c = luaZ_lookahead(p->z);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   496
  luaC_checkGC(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   497
  tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   498
                                                             &p->buff, p->name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   499
  cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   500
  cl->l.p = tf;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   501
  for (i = 0; i < tf->nups; i++)  /* initialize eventual upvalues */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   502
    cl->l.upvals[i] = luaF_newupval(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   503
  setclvalue(L, L->top, cl);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   504
  incr_top(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   505
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   506
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   507
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   508
int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   509
  struct SParser p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   510
  int status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   511
  p.z = z; p.name = name;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   512
  luaZ_initbuffer(L, &p.buff);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   513
  status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   514
  luaZ_freebuffer(L, &p.buff);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   515
  return status;
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