1997-09-16 23:25:59 +04:00
|
|
|
/*
|
1998-01-12 16:35:37 +03:00
|
|
|
** $Id: lvm.c,v 1.21 1997/12/30 19:08:23 roberto Exp roberto $
|
1997-09-16 23:25:59 +04:00
|
|
|
** Lua virtual machine
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "ldo.h"
|
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lgc.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lopcodes.h"
|
1997-11-19 20:29:23 +03:00
|
|
|
#include "lstate.h"
|
1997-09-16 23:25:59 +04:00
|
|
|
#include "lstring.h"
|
|
|
|
#include "ltable.h"
|
|
|
|
#include "ltm.h"
|
|
|
|
#include "luadebug.h"
|
|
|
|
#include "lvm.h"
|
|
|
|
|
|
|
|
|
1997-09-24 23:43:11 +04:00
|
|
|
#define skip_word(pc) (pc+=2)
|
|
|
|
#define get_word(pc) (*(pc)+(*((pc)+1)<<8))
|
|
|
|
#define next_word(pc) (pc+=2, get_word(pc-2))
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
|
1997-12-17 23:48:58 +03:00
|
|
|
/* Extra stack size to run a function: LUA_T_LINE(1), TM calls(2), ... */
|
1997-09-23 00:53:20 +04:00
|
|
|
#define EXTRA_STACK 5
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static TaggedString *strconc (char *l, char *r)
|
|
|
|
{
|
|
|
|
size_t nl = strlen(l);
|
1997-12-17 23:48:58 +03:00
|
|
|
char *buffer = luaL_openspace(nl+strlen(r)+1);
|
1997-09-16 23:25:59 +04:00
|
|
|
strcpy(buffer, l);
|
|
|
|
strcpy(buffer+nl, r);
|
|
|
|
return luaS_new(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int luaV_tonumber (TObject *obj)
|
1997-12-23 22:24:19 +03:00
|
|
|
{ /* LUA_NUMBER */
|
1997-09-16 23:25:59 +04:00
|
|
|
double t;
|
|
|
|
char c;
|
|
|
|
if (ttype(obj) != LUA_T_STRING)
|
|
|
|
return 1;
|
|
|
|
else if (sscanf(svalue(obj), "%lf %c",&t, &c) == 1) {
|
|
|
|
nvalue(obj) = (real)t;
|
|
|
|
ttype(obj) = LUA_T_NUMBER;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int luaV_tostring (TObject *obj)
|
1997-12-23 22:24:19 +03:00
|
|
|
{ /* LUA_NUMBER */
|
1997-09-16 23:25:59 +04:00
|
|
|
if (ttype(obj) != LUA_T_NUMBER)
|
|
|
|
return 1;
|
|
|
|
else {
|
|
|
|
char s[60];
|
|
|
|
real f = nvalue(obj);
|
|
|
|
int i;
|
|
|
|
if ((real)(-MAX_INT) <= f && f <= (real)MAX_INT && (real)(i=(int)f) == f)
|
|
|
|
sprintf (s, "%d", i);
|
|
|
|
else
|
|
|
|
sprintf (s, "%g", (double)nvalue(obj));
|
|
|
|
tsvalue(obj) = luaS_new(s);
|
|
|
|
ttype(obj) = LUA_T_STRING;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-10-16 14:59:34 +04:00
|
|
|
void luaV_closure (int nelems)
|
1997-09-16 23:25:59 +04:00
|
|
|
{
|
1997-12-15 19:17:20 +03:00
|
|
|
if (nelems > 0) {
|
|
|
|
struct Stack *S = &L->stack;
|
|
|
|
Closure *c = luaF_newclosure(nelems);
|
|
|
|
c->consts[0] = *(S->top-1);
|
|
|
|
memcpy(&c->consts[1], S->top-(nelems+1), nelems*sizeof(TObject));
|
|
|
|
S->top -= nelems;
|
|
|
|
ttype(S->top-1) = LUA_T_CLOSURE;
|
|
|
|
(S->top-1)->value.cl = c;
|
|
|
|
}
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Function to index a table.
|
|
|
|
** Receives the table at top-2 and the index at top-1.
|
|
|
|
*/
|
|
|
|
void luaV_gettable (void)
|
|
|
|
{
|
1997-11-21 22:00:46 +03:00
|
|
|
struct Stack *S = &L->stack;
|
1997-09-16 23:25:59 +04:00
|
|
|
TObject *im;
|
1997-11-21 22:00:46 +03:00
|
|
|
if (ttype(S->top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */
|
|
|
|
im = luaT_getimbyObj(S->top-2, IM_GETTABLE);
|
1997-09-16 23:25:59 +04:00
|
|
|
else { /* object is a table... */
|
1997-11-21 22:00:46 +03:00
|
|
|
int tg = (S->top-2)->value.a->htag;
|
1997-09-16 23:25:59 +04:00
|
|
|
im = luaT_getim(tg, IM_GETTABLE);
|
|
|
|
if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */
|
1997-11-21 22:00:46 +03:00
|
|
|
TObject *h = luaH_get(avalue(S->top-2), S->top-1);
|
1997-09-16 23:25:59 +04:00
|
|
|
if (h != NULL && ttype(h) != LUA_T_NIL) {
|
1997-11-21 22:00:46 +03:00
|
|
|
--S->top;
|
|
|
|
*(S->top-1) = *h;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
else if (ttype(im=luaT_getim(tg, IM_INDEX)) != LUA_T_NIL)
|
|
|
|
luaD_callTM(im, 2, 1);
|
|
|
|
else {
|
1997-11-21 22:00:46 +03:00
|
|
|
--S->top;
|
|
|
|
ttype(S->top-1) = LUA_T_NIL;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* else it has a "gettable" method, go through to next command */
|
|
|
|
}
|
|
|
|
/* object is not a table, or it has a "gettable" method */
|
|
|
|
if (ttype(im) != LUA_T_NIL)
|
|
|
|
luaD_callTM(im, 2, 1);
|
|
|
|
else
|
|
|
|
lua_error("indexed expression not a table");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1997-11-21 22:00:46 +03:00
|
|
|
** Function to store indexed based on values at the stack.top
|
1997-09-16 23:25:59 +04:00
|
|
|
** mode = 0: raw store (without internal methods)
|
|
|
|
** mode = 1: normal store (with internal methods)
|
1997-11-19 20:29:23 +03:00
|
|
|
** mode = 2: "deep L->stack.stack" store (with internal methods)
|
1997-09-16 23:25:59 +04:00
|
|
|
*/
|
|
|
|
void luaV_settable (TObject *t, int mode)
|
|
|
|
{
|
1997-11-21 22:00:46 +03:00
|
|
|
struct Stack *S = &L->stack;
|
1997-09-16 23:25:59 +04:00
|
|
|
TObject *im = (mode == 0) ? NULL : luaT_getimbyObj(t, IM_SETTABLE);
|
|
|
|
if (ttype(t) == LUA_T_ARRAY && (im == NULL || ttype(im) == LUA_T_NIL)) {
|
|
|
|
TObject *h = luaH_set(avalue(t), t+1);
|
1997-11-21 22:00:46 +03:00
|
|
|
*h = *(S->top-1);
|
|
|
|
S->top -= (mode == 2) ? 1 : 3;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
else { /* object is not a table, and/or has a specific "settable" method */
|
|
|
|
if (im && ttype(im) != LUA_T_NIL) {
|
|
|
|
if (mode == 2) {
|
1997-11-21 22:00:46 +03:00
|
|
|
*(S->top+1) = *(L->stack.top-1);
|
|
|
|
*(S->top) = *(t+1);
|
|
|
|
*(S->top-1) = *t;
|
|
|
|
S->top += 2; /* WARNING: caller must assure stack space */
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
luaD_callTM(im, 3, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lua_error("indexed expression not a table");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-26 19:02:26 +04:00
|
|
|
void luaV_getglobal (TaggedString *ts)
|
1997-09-16 23:25:59 +04:00
|
|
|
{
|
|
|
|
/* WARNING: caller must assure stack space */
|
1997-09-26 19:02:26 +04:00
|
|
|
TObject *value = &ts->u.globalval;
|
1997-09-16 23:25:59 +04:00
|
|
|
TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL);
|
|
|
|
if (ttype(im) == LUA_T_NIL) { /* default behavior */
|
1997-11-19 20:29:23 +03:00
|
|
|
*L->stack.top++ = *value;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
else {
|
1997-11-21 22:00:46 +03:00
|
|
|
struct Stack *S = &L->stack;
|
|
|
|
ttype(S->top) = LUA_T_STRING;
|
|
|
|
tsvalue(S->top) = ts;
|
|
|
|
S->top++;
|
|
|
|
*S->top++ = *value;
|
1997-09-16 23:25:59 +04:00
|
|
|
luaD_callTM(im, 2, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-26 19:02:26 +04:00
|
|
|
void luaV_setglobal (TaggedString *ts)
|
1997-09-16 23:25:59 +04:00
|
|
|
{
|
1997-09-26 19:02:26 +04:00
|
|
|
TObject *oldvalue = &ts->u.globalval;
|
1997-09-16 23:25:59 +04:00
|
|
|
TObject *im = luaT_getimbyObj(oldvalue, IM_SETGLOBAL);
|
|
|
|
if (ttype(im) == LUA_T_NIL) /* default behavior */
|
1997-11-19 20:29:23 +03:00
|
|
|
luaS_rawsetglobal(ts, --L->stack.top);
|
1997-09-16 23:25:59 +04:00
|
|
|
else {
|
|
|
|
/* WARNING: caller must assure stack space */
|
1997-11-21 22:00:46 +03:00
|
|
|
struct Stack *S = &L->stack;
|
|
|
|
TObject newvalue = *(S->top-1);
|
|
|
|
ttype(S->top-1) = LUA_T_STRING;
|
|
|
|
tsvalue(S->top-1) = ts;
|
|
|
|
*S->top++ = *oldvalue;
|
|
|
|
*S->top++ = newvalue;
|
1997-09-16 23:25:59 +04:00
|
|
|
luaD_callTM(im, 3, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void call_binTM (IMS event, char *msg)
|
|
|
|
{
|
1997-11-19 20:29:23 +03:00
|
|
|
TObject *im = luaT_getimbyObj(L->stack.top-2, event);/* try first operand */
|
1997-09-16 23:25:59 +04:00
|
|
|
if (ttype(im) == LUA_T_NIL) {
|
1997-11-19 20:29:23 +03:00
|
|
|
im = luaT_getimbyObj(L->stack.top-1, event); /* try second operand */
|
1997-09-16 23:25:59 +04:00
|
|
|
if (ttype(im) == LUA_T_NIL) {
|
|
|
|
im = luaT_getim(0, event); /* try a 'global' i.m. */
|
|
|
|
if (ttype(im) == LUA_T_NIL)
|
|
|
|
lua_error(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lua_pushstring(luaT_eventname[event]);
|
|
|
|
luaD_callTM(im, 3, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void call_arith (IMS event)
|
|
|
|
{
|
1997-12-09 16:50:08 +03:00
|
|
|
call_binTM(event, "unexpected type in arithmetic operation");
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
|
|
|
|
lua_Type ttype_great, IMS op)
|
|
|
|
{
|
1997-11-21 22:00:46 +03:00
|
|
|
struct Stack *S = &L->stack;
|
|
|
|
TObject *l = S->top-2;
|
|
|
|
TObject *r = S->top-1;
|
1997-09-16 23:25:59 +04:00
|
|
|
int result;
|
|
|
|
if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
|
|
|
|
result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
|
|
|
|
else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING)
|
|
|
|
result = strcoll(svalue(l), svalue(r));
|
|
|
|
else {
|
1997-12-09 16:50:08 +03:00
|
|
|
call_binTM(op, "unexpected type in comparison");
|
1997-09-16 23:25:59 +04:00
|
|
|
return;
|
|
|
|
}
|
1997-11-21 22:00:46 +03:00
|
|
|
S->top--;
|
|
|
|
nvalue(S->top-1) = 1;
|
|
|
|
ttype(S->top-1) = (result < 0) ? ttype_less :
|
1997-09-16 23:25:59 +04:00
|
|
|
(result == 0) ? ttype_equal : ttype_great;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaV_pack (StkId firstel, int nvararg, TObject *tab)
|
|
|
|
{
|
1997-11-19 20:29:23 +03:00
|
|
|
TObject *firstelem = L->stack.stack+firstel;
|
1997-09-16 23:25:59 +04:00
|
|
|
int i;
|
|
|
|
if (nvararg < 0) nvararg = 0;
|
|
|
|
avalue(tab) = luaH_new(nvararg+1); /* +1 for field 'n' */
|
|
|
|
ttype(tab) = LUA_T_ARRAY;
|
|
|
|
for (i=0; i<nvararg; i++) {
|
|
|
|
TObject index;
|
|
|
|
ttype(&index) = LUA_T_NUMBER;
|
|
|
|
nvalue(&index) = i+1;
|
|
|
|
*(luaH_set(avalue(tab), &index)) = *(firstelem+i);
|
|
|
|
}
|
|
|
|
/* store counter in field "n" */ {
|
|
|
|
TObject index, extra;
|
|
|
|
ttype(&index) = LUA_T_STRING;
|
|
|
|
tsvalue(&index) = luaS_new("n");
|
|
|
|
ttype(&extra) = LUA_T_NUMBER;
|
|
|
|
nvalue(&extra) = nvararg;
|
|
|
|
*(luaH_set(avalue(tab), &index)) = extra;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void adjust_varargs (StkId first_extra_arg)
|
|
|
|
{
|
|
|
|
TObject arg;
|
|
|
|
luaV_pack(first_extra_arg,
|
1997-11-19 20:29:23 +03:00
|
|
|
(L->stack.top-L->stack.stack)-first_extra_arg, &arg);
|
1997-09-16 23:25:59 +04:00
|
|
|
luaD_adjusttop(first_extra_arg);
|
1997-11-19 20:29:23 +03:00
|
|
|
*L->stack.top++ = arg;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Execute the given opcode, until a RET. Parameters are between
|
1997-11-19 20:29:23 +03:00
|
|
|
** [stack+base,top). Returns n such that the the results are between
|
|
|
|
** [stack+n,top).
|
1997-09-16 23:25:59 +04:00
|
|
|
*/
|
1997-12-15 19:17:20 +03:00
|
|
|
StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base)
|
1997-09-16 23:25:59 +04:00
|
|
|
{
|
1997-11-21 22:00:46 +03:00
|
|
|
struct Stack *S = &L->stack; /* to optimize */
|
1997-12-15 19:17:20 +03:00
|
|
|
Byte *pc = tf->code;
|
|
|
|
TObject *consts = tf->consts;
|
1997-09-16 23:25:59 +04:00
|
|
|
if (lua_callhook)
|
1997-12-15 19:17:20 +03:00
|
|
|
luaD_callHook(base, tf, 0);
|
1997-09-16 23:25:59 +04:00
|
|
|
luaD_checkstack((*pc++)+EXTRA_STACK);
|
1998-01-12 16:35:37 +03:00
|
|
|
if (*pc < ZEROVARARG)
|
|
|
|
luaD_adjusttop(base+*(pc++));
|
|
|
|
else { /* varargs */
|
|
|
|
luaC_checkGC();
|
|
|
|
adjust_varargs(base+(*pc++)-ZEROVARARG);
|
|
|
|
}
|
1997-09-16 23:25:59 +04:00
|
|
|
while (1) {
|
1997-09-23 00:53:20 +04:00
|
|
|
int aux;
|
|
|
|
switch ((OpCode)(aux = *pc++)) {
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case PUSHNIL0:
|
1997-11-21 22:00:46 +03:00
|
|
|
ttype(S->top++) = LUA_T_NIL;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case PUSHNIL:
|
|
|
|
aux = *pc++;
|
|
|
|
do {
|
1997-11-21 22:00:46 +03:00
|
|
|
ttype(S->top++) = LUA_T_NIL;
|
1997-10-14 02:12:04 +04:00
|
|
|
} while (aux--);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case PUSHNUMBER:
|
1997-09-23 00:53:20 +04:00
|
|
|
aux = *pc++; goto pushnumber;
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case PUSHNUMBERW:
|
1997-09-24 23:43:11 +04:00
|
|
|
aux = next_word(pc); goto pushnumber;
|
1997-09-23 00:53:20 +04:00
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case PUSHNUMBER0: case PUSHNUMBER1: case PUSHNUMBER2:
|
|
|
|
aux -= PUSHNUMBER0;
|
1997-09-23 00:53:20 +04:00
|
|
|
pushnumber:
|
1997-11-21 22:00:46 +03:00
|
|
|
ttype(S->top) = LUA_T_NUMBER;
|
|
|
|
nvalue(S->top) = aux;
|
|
|
|
S->top++;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1997-09-23 00:53:20 +04:00
|
|
|
case PUSHLOCAL:
|
|
|
|
aux = *pc++; goto pushlocal;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3:
|
|
|
|
case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
|
1997-09-23 00:53:20 +04:00
|
|
|
aux -= PUSHLOCAL0;
|
|
|
|
pushlocal:
|
1997-11-21 22:00:46 +03:00
|
|
|
*S->top++ = *((S->stack+base) + aux);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case GETGLOBALW:
|
1997-09-24 23:43:11 +04:00
|
|
|
aux = next_word(pc); goto getglobal;
|
1997-09-20 01:17:52 +04:00
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case GETGLOBAL:
|
1997-09-24 23:43:11 +04:00
|
|
|
aux = *pc++; goto getglobal;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case GETGLOBAL0: case GETGLOBAL1: case GETGLOBAL2: case GETGLOBAL3:
|
|
|
|
case GETGLOBAL4: case GETGLOBAL5: case GETGLOBAL6: case GETGLOBAL7:
|
1997-09-24 23:43:11 +04:00
|
|
|
aux -= GETGLOBAL0;
|
1997-09-23 00:53:20 +04:00
|
|
|
getglobal:
|
1997-09-26 19:02:26 +04:00
|
|
|
luaV_getglobal(tsvalue(&consts[aux]));
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1997-09-19 22:40:32 +04:00
|
|
|
case GETTABLE:
|
1997-09-16 23:25:59 +04:00
|
|
|
luaV_gettable();
|
|
|
|
break;
|
|
|
|
|
1997-10-24 22:40:29 +04:00
|
|
|
case GETDOTTEDW:
|
|
|
|
aux = next_word(pc); goto getdotted;
|
|
|
|
|
|
|
|
case GETDOTTED:
|
|
|
|
aux = *pc++; goto getdotted;
|
|
|
|
|
|
|
|
case GETDOTTED0: case GETDOTTED1: case GETDOTTED2: case GETDOTTED3:
|
|
|
|
case GETDOTTED4: case GETDOTTED5: case GETDOTTED6: case GETDOTTED7:
|
|
|
|
aux -= GETDOTTED0;
|
|
|
|
getdotted:
|
1997-11-21 22:00:46 +03:00
|
|
|
*S->top++ = consts[aux];
|
1997-10-24 22:40:29 +04:00
|
|
|
luaV_gettable();
|
|
|
|
break;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case PUSHSELFW:
|
1997-09-24 23:43:11 +04:00
|
|
|
aux = next_word(pc); goto pushself;
|
1997-09-23 00:53:20 +04:00
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case PUSHSELF:
|
1997-12-29 20:35:46 +03:00
|
|
|
aux = *pc++; goto pushself;
|
|
|
|
|
|
|
|
case PUSHSELF0: case PUSHSELF1: case PUSHSELF2: case PUSHSELF3:
|
|
|
|
case PUSHSELF4: case PUSHSELF5: case PUSHSELF6: case PUSHSELF7:
|
|
|
|
aux -= PUSHSELF0;
|
1997-09-23 00:53:20 +04:00
|
|
|
pushself: {
|
1997-11-21 22:00:46 +03:00
|
|
|
TObject receiver = *(S->top-1);
|
|
|
|
*S->top++ = consts[aux];
|
1997-09-16 23:25:59 +04:00
|
|
|
luaV_gettable();
|
1997-11-21 22:00:46 +03:00
|
|
|
*S->top++ = receiver;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case PUSHCONSTANTW:
|
1997-09-24 23:43:11 +04:00
|
|
|
aux = next_word(pc); goto pushconstant;
|
1997-09-23 00:53:20 +04:00
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case PUSHCONSTANT:
|
1997-09-24 23:43:11 +04:00
|
|
|
aux = *pc++; goto pushconstant;
|
|
|
|
|
|
|
|
case PUSHCONSTANT0: case PUSHCONSTANT1: case PUSHCONSTANT2:
|
|
|
|
case PUSHCONSTANT3: case PUSHCONSTANT4: case PUSHCONSTANT5:
|
1997-10-14 02:12:04 +04:00
|
|
|
case PUSHCONSTANT6: case PUSHCONSTANT7:
|
1997-09-24 23:43:11 +04:00
|
|
|
aux -= PUSHCONSTANT0;
|
1997-09-23 00:53:20 +04:00
|
|
|
pushconstant:
|
1997-11-21 22:00:46 +03:00
|
|
|
*S->top++ = consts[aux];
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1997-09-23 00:53:20 +04:00
|
|
|
case PUSHUPVALUE:
|
|
|
|
aux = *pc++; goto pushupvalue;
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1997-09-24 23:43:11 +04:00
|
|
|
case PUSHUPVALUE0: case PUSHUPVALUE1:
|
|
|
|
aux -= PUSHUPVALUE0;
|
1997-09-23 00:53:20 +04:00
|
|
|
pushupvalue:
|
1997-11-21 22:00:46 +03:00
|
|
|
*S->top++ = cl->consts[aux+1];
|
1997-09-19 22:40:32 +04:00
|
|
|
break;
|
|
|
|
|
1997-09-23 00:53:20 +04:00
|
|
|
case SETLOCAL:
|
|
|
|
aux = *pc++; goto setlocal;
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case SETLOCAL0: case SETLOCAL1: case SETLOCAL2: case SETLOCAL3:
|
|
|
|
case SETLOCAL4: case SETLOCAL5: case SETLOCAL6: case SETLOCAL7:
|
1997-09-23 00:53:20 +04:00
|
|
|
aux -= SETLOCAL0;
|
|
|
|
setlocal:
|
1997-11-21 22:00:46 +03:00
|
|
|
*((S->stack+base) + aux) = *(--S->top);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case SETGLOBALW:
|
1997-09-24 23:43:11 +04:00
|
|
|
aux = next_word(pc); goto setglobal;
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case SETGLOBAL:
|
|
|
|
aux = *pc++; goto setglobal;
|
|
|
|
|
|
|
|
case SETGLOBAL0: case SETGLOBAL1: case SETGLOBAL2: case SETGLOBAL3:
|
|
|
|
case SETGLOBAL4: case SETGLOBAL5: case SETGLOBAL6: case SETGLOBAL7:
|
|
|
|
aux -= SETGLOBAL0;
|
1997-09-23 00:53:20 +04:00
|
|
|
setglobal:
|
1997-09-26 19:02:26 +04:00
|
|
|
luaV_setglobal(tsvalue(&consts[aux]));
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SETTABLE0:
|
1997-11-21 22:00:46 +03:00
|
|
|
luaV_settable(S->top-3, 1);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1997-09-19 22:40:32 +04:00
|
|
|
case SETTABLE:
|
1997-11-21 22:00:46 +03:00
|
|
|
luaV_settable(S->top-3-(*pc++), 2);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case SETLISTW:
|
|
|
|
aux = next_word(pc); aux *= LFIELDS_PER_FLUSH; goto setlist;
|
|
|
|
|
1997-09-23 00:53:20 +04:00
|
|
|
case SETLIST:
|
|
|
|
aux = *(pc++) * LFIELDS_PER_FLUSH; goto setlist;
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
case SETLIST0:
|
1997-09-23 00:53:20 +04:00
|
|
|
aux = 0;
|
|
|
|
setlist: {
|
1997-09-19 22:40:32 +04:00
|
|
|
int n = *(pc++);
|
1997-11-21 22:00:46 +03:00
|
|
|
TObject *arr = S->top-n-1;
|
1997-09-19 22:40:32 +04:00
|
|
|
for (; n; n--) {
|
1997-11-21 22:00:46 +03:00
|
|
|
ttype(S->top) = LUA_T_NUMBER;
|
|
|
|
nvalue(S->top) = n+aux;
|
|
|
|
*(luaH_set(avalue(arr), S->top)) = *(S->top-1);
|
|
|
|
S->top--;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case SETMAP0:
|
|
|
|
aux = 0; goto setmap;
|
|
|
|
|
|
|
|
case SETMAP:
|
1997-10-16 14:59:34 +04:00
|
|
|
aux = *pc++;
|
1997-10-14 02:12:04 +04:00
|
|
|
setmap: {
|
1997-11-21 22:00:46 +03:00
|
|
|
TObject *arr = S->top-(2*aux)-3;
|
1997-10-14 02:12:04 +04:00
|
|
|
do {
|
1997-11-21 22:00:46 +03:00
|
|
|
*(luaH_set(avalue(arr), S->top-2)) = *(S->top-1);
|
|
|
|
S->top-=2;
|
1997-10-14 02:12:04 +04:00
|
|
|
} while (aux--);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case POP:
|
1997-09-23 00:53:20 +04:00
|
|
|
aux = *pc++; goto pop;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case POP0: case POP1:
|
|
|
|
aux -= POP0;
|
1997-09-23 00:53:20 +04:00
|
|
|
pop:
|
1997-11-21 22:00:46 +03:00
|
|
|
S->top -= (aux+1);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case CREATEARRAYW:
|
1997-10-02 00:05:34 +04:00
|
|
|
aux = next_word(pc); goto createarray;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case CREATEARRAY0: case CREATEARRAY1:
|
|
|
|
aux -= CREATEARRAY0; goto createarray;
|
|
|
|
|
|
|
|
case CREATEARRAY:
|
1997-10-02 00:05:34 +04:00
|
|
|
aux = *pc++;
|
|
|
|
createarray:
|
1997-09-16 23:25:59 +04:00
|
|
|
luaC_checkGC();
|
1997-11-21 22:00:46 +03:00
|
|
|
avalue(S->top) = luaH_new(aux);
|
|
|
|
ttype(S->top) = LUA_T_ARRAY;
|
|
|
|
S->top++;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EQOP: case NEQOP: {
|
1997-11-21 22:00:46 +03:00
|
|
|
int res = luaO_equalObj(S->top-2, S->top-1);
|
|
|
|
S->top--;
|
1997-09-23 00:53:20 +04:00
|
|
|
if (aux == NEQOP) res = !res;
|
1997-11-21 22:00:46 +03:00
|
|
|
ttype(S->top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
|
|
|
|
nvalue(S->top-1) = 1;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case LTOP:
|
|
|
|
comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LEOP:
|
|
|
|
comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, IM_LE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GTOP:
|
|
|
|
comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, IM_GT);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GEOP:
|
|
|
|
comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, IM_GE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ADDOP: {
|
1997-11-21 22:00:46 +03:00
|
|
|
TObject *l = S->top-2;
|
|
|
|
TObject *r = S->top-1;
|
1997-09-16 23:25:59 +04:00
|
|
|
if (tonumber(r) || tonumber(l))
|
|
|
|
call_arith(IM_ADD);
|
|
|
|
else {
|
|
|
|
nvalue(l) += nvalue(r);
|
1997-11-21 22:00:46 +03:00
|
|
|
--S->top;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SUBOP: {
|
1997-11-21 22:00:46 +03:00
|
|
|
TObject *l = S->top-2;
|
|
|
|
TObject *r = S->top-1;
|
1997-09-16 23:25:59 +04:00
|
|
|
if (tonumber(r) || tonumber(l))
|
|
|
|
call_arith(IM_SUB);
|
|
|
|
else {
|
|
|
|
nvalue(l) -= nvalue(r);
|
1997-11-21 22:00:46 +03:00
|
|
|
--S->top;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MULTOP: {
|
1997-11-21 22:00:46 +03:00
|
|
|
TObject *l = S->top-2;
|
|
|
|
TObject *r = S->top-1;
|
1997-09-16 23:25:59 +04:00
|
|
|
if (tonumber(r) || tonumber(l))
|
|
|
|
call_arith(IM_MUL);
|
|
|
|
else {
|
|
|
|
nvalue(l) *= nvalue(r);
|
1997-11-21 22:00:46 +03:00
|
|
|
--S->top;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DIVOP: {
|
1997-11-21 22:00:46 +03:00
|
|
|
TObject *l = S->top-2;
|
|
|
|
TObject *r = S->top-1;
|
1997-09-16 23:25:59 +04:00
|
|
|
if (tonumber(r) || tonumber(l))
|
|
|
|
call_arith(IM_DIV);
|
|
|
|
else {
|
|
|
|
nvalue(l) /= nvalue(r);
|
1997-11-21 22:00:46 +03:00
|
|
|
--S->top;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case POWOP:
|
|
|
|
call_arith(IM_POW);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CONCOP: {
|
1997-11-21 22:00:46 +03:00
|
|
|
TObject *l = S->top-2;
|
|
|
|
TObject *r = S->top-1;
|
1997-09-16 23:25:59 +04:00
|
|
|
if (tostring(l) || tostring(r))
|
|
|
|
call_binTM(IM_CONCAT, "unexpected type for concatenation");
|
|
|
|
else {
|
|
|
|
tsvalue(l) = strconc(svalue(l), svalue(r));
|
1997-11-21 22:00:46 +03:00
|
|
|
--S->top;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
luaC_checkGC();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MINUSOP:
|
1997-11-21 22:00:46 +03:00
|
|
|
if (tonumber(S->top-1)) {
|
|
|
|
ttype(S->top) = LUA_T_NIL;
|
|
|
|
S->top++;
|
1997-09-16 23:25:59 +04:00
|
|
|
call_arith(IM_UNM);
|
|
|
|
}
|
|
|
|
else
|
1997-11-21 22:00:46 +03:00
|
|
|
nvalue(S->top-1) = - nvalue(S->top-1);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NOTOP:
|
1997-11-21 22:00:46 +03:00
|
|
|
ttype(S->top-1) =
|
|
|
|
(ttype(S->top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
|
|
|
|
nvalue(S->top-1) = 1;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case ONTJMPW:
|
|
|
|
aux = next_word(pc); goto ontjmp;
|
|
|
|
|
1997-09-19 22:40:32 +04:00
|
|
|
case ONTJMP:
|
1997-10-06 18:51:11 +04:00
|
|
|
aux = *pc++;
|
1997-10-14 02:12:04 +04:00
|
|
|
ontjmp:
|
1997-11-21 22:00:46 +03:00
|
|
|
if (ttype(S->top-1) != LUA_T_NIL) pc += aux;
|
|
|
|
else S->top--;
|
1997-09-19 22:40:32 +04:00
|
|
|
break;
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case ONFJMPW:
|
|
|
|
aux = next_word(pc); goto onfjmp;
|
|
|
|
|
1997-09-19 22:40:32 +04:00
|
|
|
case ONFJMP:
|
1997-10-06 18:51:11 +04:00
|
|
|
aux = *pc++;
|
1997-10-14 02:12:04 +04:00
|
|
|
onfjmp:
|
1997-11-21 22:00:46 +03:00
|
|
|
if (ttype(S->top-1) == LUA_T_NIL) pc += aux;
|
|
|
|
else S->top--;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case JMPW:
|
1997-10-06 18:51:11 +04:00
|
|
|
aux = next_word(pc); goto jmp;
|
1997-09-24 23:43:11 +04:00
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case JMP:
|
1997-10-06 18:51:11 +04:00
|
|
|
aux = *pc++;
|
|
|
|
jmp:
|
|
|
|
pc += aux;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case IFFJMPW:
|
1997-10-06 18:51:11 +04:00
|
|
|
aux = next_word(pc); goto iffjmp;
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case IFFJMP:
|
1997-10-06 18:51:11 +04:00
|
|
|
aux = *pc++;
|
|
|
|
iffjmp:
|
1997-11-21 22:00:46 +03:00
|
|
|
if (ttype(--S->top) == LUA_T_NIL) pc += aux;
|
1997-10-02 00:05:34 +04:00
|
|
|
break;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case IFTUPJMPW:
|
1997-10-06 18:51:11 +04:00
|
|
|
aux = next_word(pc); goto iftupjmp;
|
1997-09-24 23:43:11 +04:00
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case IFTUPJMP:
|
1997-10-06 18:51:11 +04:00
|
|
|
aux = *pc++;
|
|
|
|
iftupjmp:
|
1997-11-21 22:00:46 +03:00
|
|
|
if (ttype(--S->top) != LUA_T_NIL) pc -= aux;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case IFFUPJMPW:
|
1997-10-06 18:51:11 +04:00
|
|
|
aux = next_word(pc); goto iffupjmp;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case IFFUPJMP:
|
1997-10-06 18:51:11 +04:00
|
|
|
aux = *pc++;
|
|
|
|
iffupjmp:
|
1997-11-21 22:00:46 +03:00
|
|
|
if (ttype(--S->top) == LUA_T_NIL) pc -= aux;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case CLOSURE:
|
1997-10-16 14:59:34 +04:00
|
|
|
aux = *pc++; goto closure;
|
|
|
|
|
|
|
|
case CLOSURE0: case CLOSURE1:
|
|
|
|
aux -= CLOSURE0;
|
1997-09-23 00:53:20 +04:00
|
|
|
closure:
|
1997-10-16 14:59:34 +04:00
|
|
|
luaV_closure(aux);
|
1997-09-16 23:25:59 +04:00
|
|
|
luaC_checkGC();
|
|
|
|
break;
|
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case CALLFUNC:
|
|
|
|
aux = *pc++; goto callfunc;
|
|
|
|
|
|
|
|
case CALLFUNC0: case CALLFUNC1:
|
|
|
|
aux -= CALLFUNC0;
|
|
|
|
callfunc: {
|
1997-11-21 22:00:46 +03:00
|
|
|
StkId newBase = (S->top-S->stack)-(*pc++);
|
1997-10-14 02:12:04 +04:00
|
|
|
luaD_call(newBase, aux);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ENDCODE:
|
1997-11-21 22:00:46 +03:00
|
|
|
S->top = S->stack + base;
|
1997-09-16 23:25:59 +04:00
|
|
|
/* goes through */
|
|
|
|
case RETCODE:
|
|
|
|
if (lua_callhook)
|
1997-12-15 19:17:20 +03:00
|
|
|
luaD_callHook(base, NULL, 1);
|
1997-09-23 00:53:20 +04:00
|
|
|
return (base + ((aux==RETCODE) ? *pc : 0));
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1997-10-14 02:12:04 +04:00
|
|
|
case SETLINEW:
|
|
|
|
aux = next_word(pc); goto setline;
|
|
|
|
|
|
|
|
case SETLINE:
|
|
|
|
aux = *pc++;
|
|
|
|
setline:
|
1997-11-21 22:00:46 +03:00
|
|
|
if ((S->stack+base-1)->ttype != LUA_T_LINE) {
|
1997-09-16 23:25:59 +04:00
|
|
|
/* open space for LINE value */
|
1997-11-21 22:00:46 +03:00
|
|
|
luaD_openstack((S->top-S->stack)-base);
|
1997-09-16 23:25:59 +04:00
|
|
|
base++;
|
1997-11-21 22:00:46 +03:00
|
|
|
(S->stack+base-1)->ttype = LUA_T_LINE;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
1997-11-21 22:00:46 +03:00
|
|
|
(S->stack+base-1)->value.i = aux;
|
1997-09-16 23:25:59 +04:00
|
|
|
if (lua_linehook)
|
1997-10-14 02:12:04 +04:00
|
|
|
luaD_lineHook(aux);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
default:
|
|
|
|
lua_error("internal error - opcode doesn't match");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|