misc/liblua/lvm.c
author unc0rr
Wed, 03 Apr 2013 00:03:46 +0400
branchwebgl
changeset 8839 caa57115d7ea
parent 3697 d5b30d6373fc
child 10017 de822cd3df3a
permissions -rw-r--r--
Fixes to make pas2c parse this
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: lvm.c,v 2.63.1.3 2007/12/28 15:32:23 roberto Exp $
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     3
** Lua virtual machine
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     4
** See Copyright Notice in lua.h
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     5
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     6
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     7
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     8
#include <stdio.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     9
#include <stdlib.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    10
#include <string.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    11
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    12
#define lvm_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 "lobject.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    22
#include "lopcodes.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    23
#include "lstate.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    24
#include "lstring.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    25
#include "ltable.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    26
#include "ltm.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    27
#include "lvm.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    28
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    29
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    30
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    31
/* limit for table tag-method chains (to avoid loops) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    32
#define MAXTAGLOOP	100
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
const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    36
  lua_Number num;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    37
  if (ttisnumber(obj)) return obj;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    38
  if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    39
    setnvalue(n, num);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    40
    return n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    41
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    42
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    43
    return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    44
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    45
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    46
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    47
int luaV_tostring (lua_State *L, StkId obj) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    48
  if (!ttisnumber(obj))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    49
    return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    50
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    51
    char s[LUAI_MAXNUMBER2STR];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    52
    lua_Number n = nvalue(obj);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    53
    lua_number2str(s, n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    54
    setsvalue2s(L, obj, luaS_new(L, s));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    55
    return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    56
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    57
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    58
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    59
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    60
static void traceexec (lua_State *L, const Instruction *pc) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    61
  lu_byte mask = L->hookmask;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    62
  const Instruction *oldpc = L->savedpc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    63
  L->savedpc = pc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    64
  if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    65
    resethookcount(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    66
    luaD_callhook(L, LUA_HOOKCOUNT, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    67
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    68
  if (mask & LUA_MASKLINE) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    69
    Proto *p = ci_func(L->ci)->l.p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    70
    int npc = pcRel(pc, p);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    71
    int newline = getline(p, npc);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    72
    /* call linehook when enter a new function, when jump back (loop),
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    73
       or when enter a new line */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    74
    if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p)))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    75
      luaD_callhook(L, LUA_HOOKLINE, newline);
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    80
static void callTMres (lua_State *L, StkId res, const TValue *f,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    81
                        const TValue *p1, const TValue *p2) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    82
  ptrdiff_t result = savestack(L, res);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    83
  setobj2s(L, L->top, f);  /* push function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    84
  setobj2s(L, L->top+1, p1);  /* 1st argument */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    85
  setobj2s(L, L->top+2, p2);  /* 2nd argument */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    86
  luaD_checkstack(L, 3);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    87
  L->top += 3;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    88
  luaD_call(L, L->top - 3, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    89
  res = restorestack(L, result);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    90
  L->top--;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    91
  setobjs2s(L, res, L->top);
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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    96
static void callTM (lua_State *L, const TValue *f, const TValue *p1,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    97
                    const TValue *p2, const TValue *p3) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    98
  setobj2s(L, L->top, f);  /* push function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    99
  setobj2s(L, L->top+1, p1);  /* 1st argument */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   100
  setobj2s(L, L->top+2, p2);  /* 2nd argument */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   101
  setobj2s(L, L->top+3, p3);  /* 3th argument */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   102
  luaD_checkstack(L, 4);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   103
  L->top += 4;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   104
  luaD_call(L, L->top - 4, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   105
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   106
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   107
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   108
void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   109
  int loop;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   110
  for (loop = 0; loop < MAXTAGLOOP; loop++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   111
    const TValue *tm;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   112
    if (ttistable(t)) {  /* `t' is a table? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   113
      Table *h = hvalue(t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   114
      const TValue *res = luaH_get(h, key); /* do a primitive get */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   115
      if (!ttisnil(res) ||  /* result is no nil? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   116
          (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   117
        setobj2s(L, val, res);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   118
        return;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   119
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   120
      /* else will try the tag method */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   121
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   122
    else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   123
      luaG_typeerror(L, t, "index");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   124
    if (ttisfunction(tm)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   125
      callTMres(L, val, tm, t, key);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   126
      return;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   127
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2812
diff changeset
   128
    t = tm;  /* else repeat with `tm' */
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   129
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   130
  luaG_runerror(L, "loop in gettable");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   131
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   132
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   133
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   134
void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   135
  int loop;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   136
  for (loop = 0; loop < MAXTAGLOOP; loop++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   137
    const TValue *tm;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   138
    if (ttistable(t)) {  /* `t' is a table? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   139
      Table *h = hvalue(t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   140
      TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   141
      if (!ttisnil(oldval) ||  /* result is no nil? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   142
          (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   143
        setobj2t(L, oldval, val);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   144
        luaC_barriert(L, h, val);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   145
        return;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   146
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   147
      /* else will try the tag method */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   148
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   149
    else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   150
      luaG_typeerror(L, t, "index");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   151
    if (ttisfunction(tm)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   152
      callTM(L, tm, t, key, val);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   153
      return;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   154
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2812
diff changeset
   155
    t = tm;  /* else repeat with `tm' */
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   156
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   157
  luaG_runerror(L, "loop in settable");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   158
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   159
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   160
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   161
static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   162
                       StkId res, TMS event) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   163
  const TValue *tm = luaT_gettmbyobj(L, p1, event);  /* try first operand */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   164
  if (ttisnil(tm))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   165
    tm = luaT_gettmbyobj(L, p2, event);  /* try second operand */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   166
  if (ttisnil(tm)) return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   167
  callTMres(L, res, tm, p1, p2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   168
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   169
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   170
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   171
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   172
static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   173
                                  TMS event) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   174
  const TValue *tm1 = fasttm(L, mt1, event);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   175
  const TValue *tm2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   176
  if (tm1 == NULL) return NULL;  /* no metamethod */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   177
  if (mt1 == mt2) return tm1;  /* same metatables => same metamethods */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   178
  tm2 = fasttm(L, mt2, event);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   179
  if (tm2 == NULL) return NULL;  /* no metamethod */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   180
  if (luaO_rawequalObj(tm1, tm2))  /* same metamethods? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   181
    return tm1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   182
  return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   183
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   184
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   185
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   186
static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   187
                         TMS event) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   188
  const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   189
  const TValue *tm2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   190
  if (ttisnil(tm1)) return -1;  /* no metamethod? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   191
  tm2 = luaT_gettmbyobj(L, p2, event);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   192
  if (!luaO_rawequalObj(tm1, tm2))  /* different metamethods? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   193
    return -1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   194
  callTMres(L, L->top, tm1, p1, p2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   195
  return !l_isfalse(L->top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   196
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   197
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   198
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   199
static int l_strcmp (const TString *ls, const TString *rs) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   200
  const char *l = getstr(ls);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   201
  size_t ll = ls->tsv.len;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   202
  const char *r = getstr(rs);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   203
  size_t lr = rs->tsv.len;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   204
  for (;;) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   205
    int temp = strcoll(l, r);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   206
    if (temp != 0) return temp;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   207
    else {  /* strings are equal up to a `\0' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   208
      size_t len = strlen(l);  /* index of first `\0' in both strings */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   209
      if (len == lr)  /* r is finished? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   210
        return (len == ll) ? 0 : 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   211
      else if (len == ll)  /* l is finished? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   212
        return -1;  /* l is smaller than r (because r is not finished) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   213
      /* both strings longer than `len'; go on comparing (after the `\0') */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   214
      len++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   215
      l += len; ll -= len; r += len; lr -= len;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   216
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   217
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   218
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   219
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   220
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   221
int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   222
  int res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   223
  if (ttype(l) != ttype(r))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   224
    return luaG_ordererror(L, l, r);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   225
  else if (ttisnumber(l))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   226
    return luai_numlt(nvalue(l), nvalue(r));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   227
  else if (ttisstring(l))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   228
    return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   229
  else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   230
    return res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   231
  return luaG_ordererror(L, l, r);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   232
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   233
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   234
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   235
static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   236
  int res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   237
  if (ttype(l) != ttype(r))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   238
    return luaG_ordererror(L, l, r);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   239
  else if (ttisnumber(l))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   240
    return luai_numle(nvalue(l), nvalue(r));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   241
  else if (ttisstring(l))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   242
    return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   243
  else if ((res = call_orderTM(L, l, r, TM_LE)) != -1)  /* first try `le' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   244
    return res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   245
  else if ((res = call_orderTM(L, r, l, TM_LT)) != -1)  /* else try `lt' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   246
    return !res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   247
  return luaG_ordererror(L, l, r);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   248
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   249
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   250
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   251
int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   252
  const TValue *tm;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   253
  lua_assert(ttype(t1) == ttype(t2));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   254
  switch (ttype(t1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   255
    case LUA_TNIL: return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   256
    case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   257
    case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2);  /* true must be 1 !! */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   258
    case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   259
    case LUA_TUSERDATA: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   260
      if (uvalue(t1) == uvalue(t2)) return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   261
      tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   262
                         TM_EQ);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   263
      break;  /* will try TM */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   264
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   265
    case LUA_TTABLE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   266
      if (hvalue(t1) == hvalue(t2)) return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   267
      tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   268
      break;  /* will try TM */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   269
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   270
    default: return gcvalue(t1) == gcvalue(t2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   271
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   272
  if (tm == NULL) return 0;  /* no TM? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   273
  callTMres(L, L->top, tm, t1, t2);  /* call TM */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   274
  return !l_isfalse(L->top);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   275
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   276
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   277
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   278
void luaV_concat (lua_State *L, int total, int last) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   279
  do {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   280
    StkId top = L->base + last + 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   281
    int n = 2;  /* number of elements handled in this pass (at least 2) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   282
    if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   283
      if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   284
        luaG_concaterror(L, top-2, top-1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   285
    } else if (tsvalue(top-1)->len == 0)  /* second op is empty? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   286
      (void)tostring(L, top - 2);  /* result is first op (as string) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   287
    else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   288
      /* at least two string values; get as many as possible */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   289
      size_t tl = tsvalue(top-1)->len;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   290
      char *buffer;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   291
      int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   292
      /* collect total length */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   293
      for (n = 1; n < total && tostring(L, top-n-1); n++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   294
        size_t l = tsvalue(top-n-1)->len;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   295
        if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   296
        tl += l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   297
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   298
      buffer = luaZ_openspace(L, &G(L)->buff, tl);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   299
      tl = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   300
      for (i=n; i>0; i--) {  /* concat all strings */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   301
        size_t l = tsvalue(top-i)->len;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   302
        memcpy(buffer+tl, svalue(top-i), l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   303
        tl += l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   304
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   305
      setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   306
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   307
    total -= n-1;  /* got `n' strings to create 1 new */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   308
    last -= n-1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   309
  } while (total > 1);  /* repeat until only 1 result left */
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
static void Arith (lua_State *L, StkId ra, const TValue *rb,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   314
                   const TValue *rc, TMS op) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   315
  TValue tempb, tempc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   316
  const TValue *b, *c;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   317
  if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   318
      (c = luaV_tonumber(rc, &tempc)) != NULL) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   319
    lua_Number nb = nvalue(b), nc = nvalue(c);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   320
    switch (op) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   321
      case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   322
      case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   323
      case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   324
      case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   325
      case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   326
      case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   327
      case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   328
      default: lua_assert(0); break;
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
  else if (!call_binTM(L, rb, rc, ra, op))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   332
    luaG_aritherror(L, rb, rc);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   333
}
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
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   338
** some macros for common tasks in `luaV_execute'
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
#define runtime_check(L, c)	{ if (!(c)) break; }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   342
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   343
#define RA(i)	(base+GETARG_A(i))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   344
/* to be used after possible stack reallocation */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   345
#define RB(i)	check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   346
#define RC(i)	check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   347
#define RKB(i)	check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   348
	ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   349
#define RKC(i)	check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   350
	ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   351
#define KBx(i)	check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   352
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   353
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   354
#define dojump(L,pc,i)	{(pc) += (i); luai_threadyield(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
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   357
#define Protect(x)	{ L->savedpc = pc; {x;}; base = L->base; }
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
#define arith_op(op,tm) { \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   361
        TValue *rb = RKB(i); \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   362
        TValue *rc = RKC(i); \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   363
        if (ttisnumber(rb) && ttisnumber(rc)) { \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   364
          lua_Number nb = nvalue(rb), nc = nvalue(rc); \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   365
          setnvalue(ra, op(nb, nc)); \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   366
        } \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   367
        else \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   368
          Protect(Arith(L, ra, rb, rc, tm)); \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   369
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   370
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   371
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   372
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   373
void luaV_execute (lua_State *L, int nexeccalls) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   374
  LClosure *cl;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   375
  StkId base;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   376
  TValue *k;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   377
  const Instruction *pc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   378
 reentry:  /* entry point */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   379
  lua_assert(isLua(L->ci));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   380
  pc = L->savedpc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   381
  cl = &clvalue(L->ci->func)->l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   382
  base = L->base;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   383
  k = cl->p->k;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   384
  /* main loop of interpreter */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   385
  for (;;) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   386
    const Instruction i = *pc++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   387
    StkId ra;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   388
    if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   389
        (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   390
      traceexec(L, pc);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   391
      if (L->status == LUA_YIELD) {  /* did hook yield? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   392
        L->savedpc = pc - 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   393
        return;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   394
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   395
      base = L->base;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   396
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   397
    /* warning!! several calls may realloc the stack and invalidate `ra' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   398
    ra = RA(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   399
    lua_assert(base == L->base && L->base == L->ci->base);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   400
    lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   401
    lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   402
    switch (GET_OPCODE(i)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   403
      case OP_MOVE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   404
        setobjs2s(L, ra, RB(i));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   405
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   406
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   407
      case OP_LOADK: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   408
        setobj2s(L, ra, KBx(i));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   409
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   410
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   411
      case OP_LOADBOOL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   412
        setbvalue(ra, GETARG_B(i));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   413
        if (GETARG_C(i)) pc++;  /* skip next instruction (if C) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   414
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   415
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   416
      case OP_LOADNIL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   417
        TValue *rb = RB(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   418
        do {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   419
          setnilvalue(rb--);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   420
        } while (rb >= ra);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   421
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   422
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   423
      case OP_GETUPVAL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   424
        int b = GETARG_B(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   425
        setobj2s(L, ra, cl->upvals[b]->v);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   426
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   427
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   428
      case OP_GETGLOBAL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   429
        TValue g;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   430
        TValue *rb = KBx(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   431
        sethvalue(L, &g, cl->env);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   432
        lua_assert(ttisstring(rb));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   433
        Protect(luaV_gettable(L, &g, rb, ra));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   434
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   435
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   436
      case OP_GETTABLE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   437
        Protect(luaV_gettable(L, RB(i), RKC(i), ra));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   438
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   439
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   440
      case OP_SETGLOBAL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   441
        TValue g;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   442
        sethvalue(L, &g, cl->env);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   443
        lua_assert(ttisstring(KBx(i)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   444
        Protect(luaV_settable(L, &g, KBx(i), ra));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   445
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   446
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   447
      case OP_SETUPVAL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   448
        UpVal *uv = cl->upvals[GETARG_B(i)];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   449
        setobj(L, uv->v, ra);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   450
        luaC_barrier(L, uv, ra);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   451
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   452
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   453
      case OP_SETTABLE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   454
        Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   455
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   456
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   457
      case OP_NEWTABLE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   458
        int b = GETARG_B(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   459
        int c = GETARG_C(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   460
        sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   461
        Protect(luaC_checkGC(L));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   462
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   463
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   464
      case OP_SELF: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   465
        StkId rb = RB(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   466
        setobjs2s(L, ra+1, rb);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   467
        Protect(luaV_gettable(L, rb, RKC(i), ra));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   468
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   469
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   470
      case OP_ADD: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   471
        arith_op(luai_numadd, TM_ADD);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   472
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   473
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   474
      case OP_SUB: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   475
        arith_op(luai_numsub, TM_SUB);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   476
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   477
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   478
      case OP_MUL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   479
        arith_op(luai_nummul, TM_MUL);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   480
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   481
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   482
      case OP_DIV: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   483
        arith_op(luai_numdiv, TM_DIV);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   484
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   485
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   486
      case OP_MOD: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   487
        arith_op(luai_nummod, TM_MOD);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   488
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   489
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   490
      case OP_POW: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   491
        arith_op(luai_numpow, TM_POW);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   492
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   493
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   494
      case OP_UNM: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   495
        TValue *rb = RB(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   496
        if (ttisnumber(rb)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   497
          lua_Number nb = nvalue(rb);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   498
          setnvalue(ra, luai_numunm(nb));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   499
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   500
        else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   501
          Protect(Arith(L, ra, rb, rb, TM_UNM));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   502
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   503
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   504
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   505
      case OP_NOT: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   506
        int res = l_isfalse(RB(i));  /* next assignment may change this value */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   507
        setbvalue(ra, res);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   508
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   509
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   510
      case OP_LEN: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   511
        const TValue *rb = RB(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   512
        switch (ttype(rb)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   513
          case LUA_TTABLE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   514
            setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   515
            break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   516
          }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   517
          case LUA_TSTRING: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   518
            setnvalue(ra, cast_num(tsvalue(rb)->len));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   519
            break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   520
          }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   521
          default: {  /* try metamethod */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   522
            Protect(
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   523
              if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   524
                luaG_typeerror(L, rb, "get length of");
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
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   529
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   530
      case OP_CONCAT: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   531
        int b = GETARG_B(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   532
        int c = GETARG_C(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   533
        Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   534
        setobjs2s(L, RA(i), base+b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   535
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   536
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   537
      case OP_JMP: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   538
        dojump(L, pc, GETARG_sBx(i));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   539
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   540
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   541
      case OP_EQ: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   542
        TValue *rb = RKB(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   543
        TValue *rc = RKC(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   544
        Protect(
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   545
          if (equalobj(L, rb, rc) == GETARG_A(i))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   546
            dojump(L, pc, GETARG_sBx(*pc));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   547
        )
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   548
        pc++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   549
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   550
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   551
      case OP_LT: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   552
        Protect(
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   553
          if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   554
            dojump(L, pc, GETARG_sBx(*pc));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   555
        )
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   556
        pc++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   557
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   558
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   559
      case OP_LE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   560
        Protect(
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   561
          if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   562
            dojump(L, pc, GETARG_sBx(*pc));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   563
        )
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   564
        pc++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   565
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   566
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   567
      case OP_TEST: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   568
        if (l_isfalse(ra) != GETARG_C(i))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   569
          dojump(L, pc, GETARG_sBx(*pc));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   570
        pc++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   571
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   572
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   573
      case OP_TESTSET: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   574
        TValue *rb = RB(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   575
        if (l_isfalse(rb) != GETARG_C(i)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   576
          setobjs2s(L, ra, rb);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   577
          dojump(L, pc, GETARG_sBx(*pc));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   578
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   579
        pc++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   580
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   581
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   582
      case OP_CALL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   583
        int b = GETARG_B(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   584
        int nresults = GETARG_C(i) - 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   585
        if (b != 0) L->top = ra+b;  /* else previous instruction set top */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   586
        L->savedpc = pc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   587
        switch (luaD_precall(L, ra, nresults)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   588
          case PCRLUA: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   589
            nexeccalls++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   590
            goto reentry;  /* restart luaV_execute over new Lua function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   591
          }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   592
          case PCRC: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   593
            /* it was a C function (`precall' called it); adjust results */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   594
            if (nresults >= 0) L->top = L->ci->top;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   595
            base = L->base;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   596
            continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   597
          }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   598
          default: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   599
            return;  /* yield */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   600
          }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   601
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   602
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   603
      case OP_TAILCALL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   604
        int b = GETARG_B(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   605
        if (b != 0) L->top = ra+b;  /* else previous instruction set top */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   606
        L->savedpc = pc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   607
        lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   608
        switch (luaD_precall(L, ra, LUA_MULTRET)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   609
          case PCRLUA: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   610
            /* tail call: put new frame in place of previous one */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   611
            CallInfo *ci = L->ci - 1;  /* previous frame */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   612
            int aux;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   613
            StkId func = ci->func;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   614
            StkId pfunc = (ci+1)->func;  /* previous function index */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   615
            if (L->openupval) luaF_close(L, ci->base);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   616
            L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   617
            for (aux = 0; pfunc+aux < L->top; aux++)  /* move frame down */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   618
              setobjs2s(L, func+aux, pfunc+aux);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   619
            ci->top = L->top = func+aux;  /* correct top */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   620
            lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   621
            ci->savedpc = L->savedpc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   622
            ci->tailcalls++;  /* one more call lost */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   623
            L->ci--;  /* remove new frame */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   624
            goto reentry;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   625
          }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   626
          case PCRC: {  /* it was a C function (`precall' called it) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   627
            base = L->base;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   628
            continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   629
          }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   630
          default: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   631
            return;  /* yield */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   632
          }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   633
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   634
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   635
      case OP_RETURN: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   636
        int b = GETARG_B(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   637
        if (b != 0) L->top = ra+b-1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   638
        if (L->openupval) luaF_close(L, base);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   639
        L->savedpc = pc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   640
        b = luaD_poscall(L, ra);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   641
        if (--nexeccalls == 0)  /* was previous function running `here'? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   642
          return;  /* no: return */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   643
        else {  /* yes: continue its execution */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   644
          if (b) L->top = L->ci->top;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   645
          lua_assert(isLua(L->ci));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   646
          lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   647
          goto reentry;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   648
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   649
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   650
      case OP_FORLOOP: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   651
        lua_Number step = nvalue(ra+2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   652
        lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   653
        lua_Number limit = nvalue(ra+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   654
        if (luai_numlt(0, step) ? luai_numle(idx, limit)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   655
                                : luai_numle(limit, idx)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   656
          dojump(L, pc, GETARG_sBx(i));  /* jump back */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   657
          setnvalue(ra, idx);  /* update internal index... */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   658
          setnvalue(ra+3, idx);  /* ...and external index */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   659
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   660
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   661
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   662
      case OP_FORPREP: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   663
        const TValue *init = ra;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   664
        const TValue *plimit = ra+1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   665
        const TValue *pstep = ra+2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   666
        L->savedpc = pc;  /* next steps may throw errors */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   667
        if (!tonumber(init, ra))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   668
          luaG_runerror(L, LUA_QL("for") " initial value must be a number");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   669
        else if (!tonumber(plimit, ra+1))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   670
          luaG_runerror(L, LUA_QL("for") " limit must be a number");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   671
        else if (!tonumber(pstep, ra+2))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   672
          luaG_runerror(L, LUA_QL("for") " step must be a number");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   673
        setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   674
        dojump(L, pc, GETARG_sBx(i));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   675
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   676
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   677
      case OP_TFORLOOP: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   678
        StkId cb = ra + 3;  /* call base */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   679
        setobjs2s(L, cb+2, ra+2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   680
        setobjs2s(L, cb+1, ra+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   681
        setobjs2s(L, cb, ra);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   682
        L->top = cb+3;  /* func. + 2 args (state and index) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   683
        Protect(luaD_call(L, cb, GETARG_C(i)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   684
        L->top = L->ci->top;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   685
        cb = RA(i) + 3;  /* previous call may change the stack */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   686
        if (!ttisnil(cb)) {  /* continue loop? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   687
          setobjs2s(L, cb-1, cb);  /* save control variable */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   688
          dojump(L, pc, GETARG_sBx(*pc));  /* jump back */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   689
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   690
        pc++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   691
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   692
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   693
      case OP_SETLIST: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   694
        int n = GETARG_B(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   695
        int c = GETARG_C(i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   696
        int last;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   697
        Table *h;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   698
        if (n == 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   699
          n = cast_int(L->top - ra) - 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   700
          L->top = L->ci->top;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   701
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   702
        if (c == 0) c = cast_int(*pc++);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   703
        runtime_check(L, ttistable(ra));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   704
        h = hvalue(ra);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   705
        last = ((c-1)*LFIELDS_PER_FLUSH) + n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   706
        if (last > h->sizearray)  /* needs more space? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   707
          luaH_resizearray(L, h, last);  /* pre-alloc it at once */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   708
        for (; n > 0; n--) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   709
          TValue *val = ra+n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   710
          setobj2t(L, luaH_setnum(L, h, last--), val);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   711
          luaC_barriert(L, h, val);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   712
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   713
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   714
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   715
      case OP_CLOSE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   716
        luaF_close(L, ra);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   717
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   718
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   719
      case OP_CLOSURE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   720
        Proto *p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   721
        Closure *ncl;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   722
        int nup, j;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   723
        p = cl->p->p[GETARG_Bx(i)];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   724
        nup = p->nups;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   725
        ncl = luaF_newLclosure(L, nup, cl->env);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   726
        ncl->l.p = p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   727
        for (j=0; j<nup; j++, pc++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   728
          if (GET_OPCODE(*pc) == OP_GETUPVAL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   729
            ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   730
          else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   731
            lua_assert(GET_OPCODE(*pc) == OP_MOVE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   732
            ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   733
          }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   734
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   735
        setclvalue(L, ra, ncl);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   736
        Protect(luaC_checkGC(L));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   737
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   738
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   739
      case OP_VARARG: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   740
        int b = GETARG_B(i) - 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   741
        int j;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   742
        CallInfo *ci = L->ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   743
        int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   744
        if (b == LUA_MULTRET) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   745
          Protect(luaD_checkstack(L, n));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   746
          ra = RA(i);  /* previous call may change the stack */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   747
          b = n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   748
          L->top = ra + n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   749
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   750
        for (j = 0; j < b; j++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   751
          if (j < n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   752
            setobjs2s(L, ra + j, ci->base - n + j);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   753
          }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   754
          else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   755
            setnilvalue(ra + j);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   756
          }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   757
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   758
        continue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   759
      }
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