lua/lvm.c

674 lines
17 KiB
C
Raw Normal View History

1997-09-16 23:25:59 +04:00
/*
** $Id: lvm.c,v 1.103 2000/04/14 17:45:25 roberto Exp roberto $
1997-09-16 23:25:59 +04:00
** Lua virtual machine
** See Copyright Notice in lua.h
*/
#include <stdio.h>
#include <stdlib.h>
1997-09-16 23:25:59 +04:00
#include <string.h>
#define LUA_REENTRANT
1997-09-16 23:25:59 +04:00
#include "lauxlib.h"
#include "ldebug.h"
1997-09-16 23:25:59 +04:00
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "lobject.h"
1997-09-16 23:25:59 +04:00
#include "lopcodes.h"
#include "lstate.h"
1997-09-16 23:25:59 +04:00
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#include "lvm.h"
#ifdef OLD_ANSI
#define strcoll(a,b) strcmp(a,b)
#endif
1997-09-16 23:25:59 +04:00
2000-01-13 18:56:03 +03:00
/*
** Extra stack size to run a function:
2000-03-10 21:37:44 +03:00
** TAG_LINE(1), NAME(1), TM calls(3) (plus some extra...)
2000-01-13 18:56:03 +03:00
*/
#define EXTRA_STACK 8
1997-09-16 23:25:59 +04:00
int luaV_tonumber (TObject *obj) { /* LUA_NUMBER */
2000-03-10 21:37:44 +03:00
if (ttype(obj) != TAG_STRING)
1997-09-16 23:25:59 +04:00
return 1;
else {
if (!luaO_str2d(svalue(obj), &nvalue(obj)))
1999-04-13 23:28:49 +04:00
return 2;
2000-03-10 21:37:44 +03:00
ttype(obj) = TAG_NUMBER;
1997-09-16 23:25:59 +04:00
return 0;
}
}
int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
2000-03-10 21:37:44 +03:00
if (ttype(obj) != TAG_NUMBER)
1997-09-16 23:25:59 +04:00
return 1;
else {
1999-12-14 21:33:29 +03:00
char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
sprintf(s, "%.16g", (double)nvalue(obj));
tsvalue(obj) = luaS_new(L, s);
2000-03-10 21:37:44 +03:00
ttype(obj) = TAG_STRING;
1997-09-16 23:25:59 +04:00
return 0;
}
}
void luaV_setn (lua_State *L, Hash *t, int val) {
1998-12-30 16:16:50 +03:00
TObject index, value;
2000-03-10 21:37:44 +03:00
ttype(&index) = TAG_STRING; tsvalue(&index) = luaS_new(L, "n");
ttype(&value) = TAG_NUMBER; nvalue(&value) = val;
luaH_set(L, t, &index, &value);
1998-12-30 16:16:50 +03:00
}
static Closure *luaV_closure (lua_State *L, lua_Type t, int nelems) {
Closure *c = luaF_newclosure(L, nelems);
L->top -= nelems;
while (nelems--)
c->consts[nelems] = *(L->top+nelems);
ttype(L->top) = t;
clvalue(L->top) = c;
incr_top;
return c;
}
void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems) {
Closure *cl = luaV_closure(L, TAG_CCLOSURE, nelems);
cl->f.c = c;
}
void luaV_Lclosure (lua_State *L, Proto *l, int nelems) {
Closure *cl = luaV_closure(L, TAG_LCLOSURE, nelems);
cl->f.l = l;
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 (lua_State *L, StkId top) {
TObject *table = top-2;
1999-08-17 00:52:00 +04:00
const TObject *im;
2000-03-28 00:10:21 +04:00
if (ttype(table) != TAG_TABLE) { /* not a table, get gettable TM */
im = luaT_getimbyObj(L, table, IM_GETTABLE);
2000-03-10 21:37:44 +03:00
if (ttype(im) == TAG_NIL) {
L->top = top;
luaG_indexerror(L, table);
}
1999-02-08 20:07:59 +03:00
}
1997-09-16 23:25:59 +04:00
else { /* object is a table... */
1999-02-08 20:07:59 +03:00
int tg = table->value.a->htag;
im = luaT_getim(L, tg, IM_GETTABLE);
2000-03-10 21:37:44 +03:00
if (ttype(im) == TAG_NIL) { /* and does not have a `gettable' TM */
const TObject *h = luaH_get(L, avalue(table), table+1);
2000-03-10 21:37:44 +03:00
if (ttype(h) == TAG_NIL &&
(ttype(im=luaT_getim(L, tg, IM_INDEX)) != TAG_NIL)) {
1999-12-06 14:40:55 +03:00
/* result is nil and there is an `index' tag method */
L->top = top;
luaD_callTM(L, im, 2, 1); /* calls it */
1997-09-16 23:25:59 +04:00
}
else
1999-12-06 14:40:55 +03:00
*table = *h; /* `push' result into table position */
1997-09-16 23:25:59 +04:00
return;
}
2000-03-03 17:58:26 +03:00
/* else it has a `gettable' TM, go through to next command */
1997-09-16 23:25:59 +04:00
}
2000-03-03 17:58:26 +03:00
/* object is not a table, or it has a `gettable' TM */
L->top = top;
luaD_callTM(L, im, 2, 1);
1997-09-16 23:25:59 +04:00
}
/*
** Receives table at *t, index at *(t+1) and value at `top'.
1997-09-16 23:25:59 +04:00
*/
void luaV_settable (lua_State *L, StkId t, StkId top) {
1999-08-17 00:52:00 +04:00
const TObject *im;
2000-03-28 00:10:21 +04:00
if (ttype(t) != TAG_TABLE) { /* not a table, get `settable' method */
L->top = top;
im = luaT_getimbyObj(L, t, IM_SETTABLE);
2000-03-10 21:37:44 +03:00
if (ttype(im) == TAG_NIL)
luaG_indexerror(L, t);
1999-02-08 20:07:59 +03:00
}
1998-12-30 20:26:49 +03:00
else { /* object is a table... */
im = luaT_getim(L, avalue(t)->htag, IM_SETTABLE);
2000-03-10 21:37:44 +03:00
if (ttype(im) == TAG_NIL) { /* and does not have a `settable' method */
luaH_set(L, avalue(t), t+1, top-1);
1998-12-30 20:26:49 +03:00
return;
1997-09-16 23:25:59 +04:00
}
1999-12-27 20:33:22 +03:00
/* else it has a `settable' method, go through to next command */
1998-12-30 20:26:49 +03:00
}
1999-12-27 20:33:22 +03:00
/* object is not a table, or it has a `settable' method */
1999-02-08 20:07:59 +03:00
/* prepare arguments and call the tag method */
luaD_checkstack(L, 3);
*(top+2) = *(top-1);
*(top+1) = *(t+1);
*(top) = *t;
*(top-1) = *im;
L->top = top+3;
luaD_call(L, top-1, 0);
1998-12-30 20:26:49 +03:00
}
1999-12-01 22:50:08 +03:00
void luaV_rawsettable (lua_State *L, StkId t) {
2000-03-28 00:10:21 +04:00
if (ttype(t) != TAG_TABLE)
lua_error(L, "indexed expression not a table");
1998-12-30 20:26:49 +03:00
else {
1999-12-01 22:50:08 +03:00
luaH_set(L, avalue(t), t+1, L->top-1);
L->top -= 3;
1997-09-16 23:25:59 +04:00
}
}
void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top) {
const TObject *value = &gv->value;
TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
2000-03-10 21:37:44 +03:00
if (ttype(im) == TAG_NIL) /* is there a tag method? */
*top = *value; /* default behavior */
2000-01-13 18:56:03 +03:00
else { /* tag method */
luaD_checkstack(L, 3);
*top = *im;
2000-03-10 21:37:44 +03:00
ttype(top+1) = TAG_STRING;
tsvalue(top+1) = gv->name; /* global name */
*(top+2) = *value;
L->top = top+3;
luaD_call(L, top, 1);
1997-09-16 23:25:59 +04:00
}
}
void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top) {
const TObject *oldvalue = &gv->value;
const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
2000-03-10 21:37:44 +03:00
if (ttype(im) == TAG_NIL) /* is there a tag method? */
gv->value = *(top-1);
1997-09-16 23:25:59 +04:00
else {
luaD_checkstack(L, 3);
*(top+2) = *(top-1); /* new value */
*(top+1) = *oldvalue;
2000-03-10 21:37:44 +03:00
ttype(top) = TAG_STRING;
tsvalue(top) = gv->name;
*(top-1) = *im;
L->top = top+3;
luaD_call(L, top-1, 0);
1997-09-16 23:25:59 +04:00
}
}
1999-12-01 22:50:08 +03:00
static void call_binTM (lua_State *L, StkId top, IMS event, const char *msg) {
1999-08-17 00:52:00 +04:00
/* try first operand */
1999-12-01 22:50:08 +03:00
const TObject *im = luaT_getimbyObj(L, top-2, event);
L->top = top;
2000-03-10 21:37:44 +03:00
if (ttype(im) == TAG_NIL) {
1999-12-01 22:50:08 +03:00
im = luaT_getimbyObj(L, top-1, event); /* try second operand */
2000-03-10 21:37:44 +03:00
if (ttype(im) == TAG_NIL) {
1999-12-27 20:33:22 +03:00
im = luaT_getim(L, 0, event); /* try a `global' method */
2000-03-10 21:37:44 +03:00
if (ttype(im) == TAG_NIL)
lua_error(L, msg);
1997-09-16 23:25:59 +04:00
}
}
lua_pushstring(L, luaT_eventname[event]);
luaD_callTM(L, im, 3, 1);
1997-09-16 23:25:59 +04:00
}
1999-12-01 22:50:08 +03:00
static void call_arith (lua_State *L, StkId top, IMS event) {
call_binTM(L, top, event, "unexpected type in arithmetic operation");
1997-09-16 23:25:59 +04:00
}
2000-04-07 17:13:11 +04:00
static void addK (lua_State *L, StkId top, int k) {
ttype(top) = TAG_NUMBER;
nvalue(top) = (Number)k;
call_arith(L, top+1, IM_ADD);
}
2000-03-10 21:37:44 +03:00
static int luaV_strcomp (const TString *ls, const TString *rs) {
const char *l = ls->str;
long ll = ls->u.s.len;
const char *r = rs->str;
long lr = rs->u.s.len;
1998-03-06 19:54:42 +03:00
for (;;) {
long temp = strcoll(l, r);
if (temp != 0) return temp;
/* strings are equal up to a '\0' */
temp = strlen(l); /* index of first '\0' in both strings */
if (temp == ll) /* l is finished? */
return (temp == lr) ? 0 : -1; /* l is equal or smaller than r */
else if (temp == lr) /* r is finished? */
return 1; /* l is greater than r (because l is not finished) */
/* both strings longer than temp; go on comparing (after the '\0') */
temp++;
l += temp; ll -= temp; r += temp; lr -= temp;
}
}
2000-03-09 03:19:22 +03:00
int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top) {
2000-03-10 21:37:44 +03:00
if (ttype(l) == TAG_NUMBER && ttype(r) == TAG_NUMBER)
return (nvalue(l) < nvalue(r));
2000-03-10 21:37:44 +03:00
else if (ttype(l) == TAG_STRING && ttype(r) == TAG_STRING)
return (luaV_strcomp(tsvalue(l), tsvalue(r)) < 0);
2000-03-09 03:19:22 +03:00
else { /* call TM */
luaD_checkstack(L, 2);
*top++ = *l;
*top++ = *r;
call_binTM(L, top, IM_LT, "unexpected type in comparison");
L->top--;
2000-03-10 21:37:44 +03:00
return (ttype(L->top) != TAG_NIL);
1997-09-16 23:25:59 +04:00
}
}
2000-03-09 03:19:22 +03:00
static void strconc (lua_State *L, int total, StkId top) {
do {
int n = 2; /* number of elements handled in this pass (at least 2) */
if (tostring(L, top-2) || tostring(L, top-1))
call_binTM(L, top, IM_CONCAT, "unexpected type for concatenation");
2000-03-17 16:09:12 +03:00
else if (tsvalue(top-1)->u.s.len > 0) { /* if len=0, do nothing */
/* at least two string values; get as many as possible */
long tl = tsvalue(top-1)->u.s.len + tsvalue(top-2)->u.s.len;
2000-03-09 03:19:22 +03:00
char *buffer;
int i;
while (n < total && !tostring(L, top-n-1)) { /* collect total length */
tl += tsvalue(top-n-1)->u.s.len;
n++;
}
buffer = luaL_openspace(L, tl);
tl = 0;
for (i=n; i>0; i--) { /* concat all strings */
long l = tsvalue(top-i)->u.s.len;
memcpy(buffer+tl, tsvalue(top-i)->str, l);
tl += l;
}
tsvalue(top-n) = luaS_newlstr(L, buffer, tl);
}
total -= n-1; /* got `n' strings to create 1 new */
top -= n-1;
} while (total > 1); /* repeat until only 1 result left */
}
1999-12-01 22:50:08 +03:00
void luaV_pack (lua_State *L, StkId firstelem, int nvararg, TObject *tab) {
1997-09-16 23:25:59 +04:00
int i;
Hash *htab;
1999-12-01 22:50:08 +03:00
htab = avalue(tab) = luaH_new(L, nvararg+1); /* +1 for field `n' */
2000-03-28 00:10:21 +04:00
ttype(tab) = TAG_TABLE;
for (i=0; i<nvararg; i++)
luaH_setint(L, htab, i+1, firstelem+i);
1999-12-01 22:50:08 +03:00
luaV_setn(L, htab, nvararg); /* store counter in field `n' */
1997-09-16 23:25:59 +04:00
}
1999-12-01 22:50:08 +03:00
static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
1997-09-16 23:25:59 +04:00
TObject arg;
1999-12-01 22:50:08 +03:00
int nvararg = (L->top-base) - nfixargs;
if (nvararg < 0) {
luaV_pack(L, base, 0, &arg);
luaD_adjusttop(L, base, nfixargs);
}
else {
luaV_pack(L, base+nfixargs, nvararg, &arg);
L->top = base+nfixargs;
}
*L->top++ = arg;
1997-09-16 23:25:59 +04:00
}
/*
1999-12-09 23:01:48 +03:00
** Executes the given Lua function. Parameters are between [base,top).
** Returns n such that the the results are between [n,top).
1997-09-16 23:25:59 +04:00
*/
StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
const Proto *tf = cl->f.l;
StkId top; /* keep top local, for performance */
const Instruction *pc = tf->code;
2000-03-10 21:37:44 +03:00
TString **kstr = tf->kstr;
2000-02-14 19:51:08 +03:00
luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK);
if (tf->is_vararg) { /* varargs? */
adjust_varargs(L, base, tf->numparams);
luaC_checkGC(L);
1998-01-12 16:35:37 +03:00
}
2000-02-14 19:51:08 +03:00
else
luaD_adjusttop(L, base, tf->numparams);
1999-12-01 22:50:08 +03:00
top = L->top;
1998-12-03 18:45:15 +03:00
for (;;) {
Instruction i = *pc++;
2000-02-14 19:51:08 +03:00
switch (GET_OPCODE(i)) {
1997-09-16 23:25:59 +04:00
2000-03-10 21:37:44 +03:00
case OP_END:
return L->top; /* no results */
1999-12-09 23:01:48 +03:00
2000-03-10 21:37:44 +03:00
case OP_RETURN:
L->top = top;
2000-02-14 19:51:08 +03:00
return base+GETARG_U(i);
1999-03-06 00:16:07 +03:00
2000-03-10 21:37:44 +03:00
case OP_CALL:
1999-12-01 22:50:08 +03:00
L->top = top;
2000-02-14 19:51:08 +03:00
luaD_call(L, base+GETARG_A(i), GETARG_B(i));
1999-12-01 22:50:08 +03:00
top = L->top;
1999-03-06 00:16:07 +03:00
break;
2000-03-10 21:37:44 +03:00
case OP_TAILCALL:
1999-12-01 22:50:08 +03:00
L->top = top;
2000-02-14 19:51:08 +03:00
luaD_call(L, base+GETARG_A(i), MULT_RET);
return base+GETARG_B(i);
2000-03-10 21:37:44 +03:00
case OP_PUSHNIL: {
2000-03-04 23:18:15 +03:00
int n = GETARG_U(i);
2000-03-10 17:38:10 +03:00
LUA_ASSERT(L, n>0, "invalid argument");
2000-04-05 00:48:44 +04:00
do {
2000-03-10 21:37:44 +03:00
ttype(top++) = TAG_NIL;
2000-04-05 00:48:44 +04:00
} while (--n > 0);
1999-02-08 21:54:19 +03:00
break;
2000-02-14 19:51:08 +03:00
}
1999-02-08 21:54:19 +03:00
2000-03-10 21:37:44 +03:00
case OP_POP:
2000-02-14 19:51:08 +03:00
top -= GETARG_U(i);
1999-02-09 18:59:10 +03:00
break;
2000-03-10 21:37:44 +03:00
case OP_PUSHINT:
ttype(top) = TAG_NUMBER;
nvalue(top) = (Number)GETARG_S(i);
1999-12-01 22:50:08 +03:00
top++;
1997-09-16 23:25:59 +04:00
break;
2000-03-10 21:37:44 +03:00
case OP_PUSHSTRING:
ttype(top) = TAG_STRING;
2000-02-14 19:51:08 +03:00
tsvalue(top) = kstr[GETARG_U(i)];
top++;
break;
2000-03-10 21:37:44 +03:00
case OP_PUSHNUM:
ttype(top) = TAG_NUMBER;
2000-02-14 19:51:08 +03:00
nvalue(top) = tf->knum[GETARG_U(i)];
top++;
break;
2000-03-10 21:37:44 +03:00
case OP_PUSHNEGNUM:
ttype(top) = TAG_NUMBER;
2000-02-22 16:31:43 +03:00
nvalue(top) = -tf->knum[GETARG_U(i)];
top++;
break;
2000-03-10 21:37:44 +03:00
case OP_PUSHUPVALUE:
*top++ = cl->consts[GETARG_U(i)];
break;
2000-04-07 17:13:11 +04:00
case OP_GETLOCAL:
2000-02-14 19:51:08 +03:00
*top++ = *(base+GETARG_U(i));
1997-09-16 23:25:59 +04:00
break;
2000-03-10 21:37:44 +03:00
case OP_GETGLOBAL:
2000-02-14 19:51:08 +03:00
luaV_getglobal(L, kstr[GETARG_U(i)]->u.s.gv, top);
1999-12-01 22:50:08 +03:00
top++;
1997-09-16 23:25:59 +04:00
break;
2000-03-10 21:37:44 +03:00
case OP_GETTABLE:
luaV_gettable(L, top);
1999-12-01 22:50:08 +03:00
top--;
break;
1997-09-16 23:25:59 +04:00
2000-03-10 21:37:44 +03:00
case OP_GETDOTTED:
ttype(top) = TAG_STRING;
2000-02-14 19:51:08 +03:00
tsvalue(top++) = kstr[GETARG_U(i)];
luaV_gettable(L, top);
1999-12-01 22:50:08 +03:00
top--;
break;
2000-04-07 17:13:11 +04:00
case OP_GETINDEXED:
*top++ = *(base+GETARG_U(i));
luaV_gettable(L, top);
top--;
break;
2000-03-10 21:37:44 +03:00
case OP_PUSHSELF: {
TObject receiver;
1999-12-01 22:50:08 +03:00
receiver = *(top-1);
2000-03-10 21:37:44 +03:00
ttype(top) = TAG_STRING;
2000-02-14 19:51:08 +03:00
tsvalue(top++) = kstr[GETARG_U(i)];
luaV_gettable(L, top);
1999-12-01 22:50:08 +03:00
*(top-1) = receiver;
1997-09-16 23:25:59 +04:00
break;
}
2000-03-10 21:37:44 +03:00
case OP_CREATETABLE:
1999-12-01 22:50:08 +03:00
L->top = top;
luaC_checkGC(L);
2000-02-14 19:51:08 +03:00
avalue(top) = luaH_new(L, GETARG_U(i));
2000-03-28 00:10:21 +04:00
ttype(top) = TAG_TABLE;
1999-12-01 22:50:08 +03:00
top++;
1997-09-19 22:40:32 +04:00
break;
2000-03-10 21:37:44 +03:00
case OP_SETLOCAL:
*(base+GETARG_U(i)) = *(--top);
1997-09-16 23:25:59 +04:00
break;
2000-03-10 21:37:44 +03:00
case OP_SETGLOBAL:
2000-02-14 19:51:08 +03:00
luaV_setglobal(L, kstr[GETARG_U(i)]->u.s.gv, top);
1999-12-01 22:50:08 +03:00
top--;
1997-09-16 23:25:59 +04:00
break;
2000-03-10 21:37:44 +03:00
case OP_SETTABLE:
2000-04-07 17:13:11 +04:00
luaV_settable(L, top-GETARG_A(i), top);
top -= GETARG_B(i); /* pop values */
1997-09-16 23:25:59 +04:00
break;
2000-03-10 21:37:44 +03:00
case OP_SETLIST: {
2000-02-14 19:51:08 +03:00
int aux = GETARG_A(i) * LFIELDS_PER_FLUSH;
int n = GETARG_B(i)+1;
1999-12-01 22:50:08 +03:00
Hash *arr = avalue(top-n-1);
1999-12-06 14:40:55 +03:00
L->top = top-n; /* final value of `top' (in case of errors) */
1999-01-25 20:39:28 +03:00
for (; n; n--)
1999-12-01 22:50:08 +03:00
luaH_setint(L, arr, n+aux, --top);
1997-09-16 23:25:59 +04:00
break;
}
2000-03-10 21:37:44 +03:00
case OP_SETMAP: {
2000-02-14 19:51:08 +03:00
int n = GETARG_U(i);
StkId finaltop = top-2*(n+1);
1999-12-06 14:40:55 +03:00
Hash *arr = avalue(finaltop-1);
L->top = finaltop; /* final value of `top' (in case of errors) */
do {
1999-12-01 22:50:08 +03:00
luaH_set(L, arr, top-2, top-1);
top-=2;
2000-02-14 19:51:08 +03:00
} while (n--);
1997-09-16 23:25:59 +04:00
break;
}
2000-03-10 21:37:44 +03:00
case OP_ADD:
1999-12-01 22:50:08 +03:00
if (tonumber(top-1) || tonumber(top-2))
call_arith(L, top, IM_ADD);
else
nvalue(top-2) += nvalue(top-1);
top--;
1997-09-16 23:25:59 +04:00
break;
2000-03-10 21:37:44 +03:00
case OP_ADDI:
2000-04-07 17:13:11 +04:00
if (tonumber(top-1))
addK(L, top, GETARG_S(i));
2000-02-22 16:31:43 +03:00
else
2000-03-10 21:37:44 +03:00
nvalue(top-1) += (Number)GETARG_S(i);
2000-02-22 16:31:43 +03:00
break;
2000-03-10 21:37:44 +03:00
case OP_SUB:
1999-12-01 22:50:08 +03:00
if (tonumber(top-1) || tonumber(top-2))
call_arith(L, top, IM_SUB);
else
nvalue(top-2) -= nvalue(top-1);
top--;
1997-09-16 23:25:59 +04:00
break;
2000-03-10 21:37:44 +03:00
case OP_MULT:
1999-12-01 22:50:08 +03:00
if (tonumber(top-1) || tonumber(top-2))
call_arith(L, top, IM_MUL);
else
nvalue(top-2) *= nvalue(top-1);
top--;
1997-09-16 23:25:59 +04:00
break;
2000-03-10 21:37:44 +03:00
case OP_DIV:
1999-12-01 22:50:08 +03:00
if (tonumber(top-1) || tonumber(top-2))
call_arith(L, top, IM_DIV);
else
nvalue(top-2) /= nvalue(top-1);
top--;
1997-09-16 23:25:59 +04:00
break;
2000-03-10 21:37:44 +03:00
case OP_POW:
1999-12-01 22:50:08 +03:00
call_binTM(L, top, IM_POW, "undefined operation");
top--;
1997-09-16 23:25:59 +04:00
break;
2000-04-07 17:13:11 +04:00
case OP_CONCAT: {
2000-03-09 03:19:22 +03:00
int n = GETARG_U(i);
strconc(L, n, top);
top -= n-1;
1999-12-01 22:50:08 +03:00
L->top = top;
luaC_checkGC(L);
1997-09-16 23:25:59 +04:00
break;
2000-03-09 03:19:22 +03:00
}
1997-09-16 23:25:59 +04:00
2000-03-10 21:37:44 +03:00
case OP_MINUS:
1999-12-01 22:50:08 +03:00
if (tonumber(top-1)) {
2000-03-10 21:37:44 +03:00
ttype(top) = TAG_NIL;
1999-12-01 22:50:08 +03:00
call_arith(L, top+1, IM_UNM);
1997-09-16 23:25:59 +04:00
}
else
2000-03-03 17:58:26 +03:00
nvalue(top-1) = -nvalue(top-1);
1997-09-16 23:25:59 +04:00
break;
2000-03-10 21:37:44 +03:00
case OP_NOT:
1999-12-01 22:50:08 +03:00
ttype(top-1) =
2000-03-10 21:37:44 +03:00
(ttype(top-1) == TAG_NIL) ? TAG_NUMBER : TAG_NIL;
1999-12-01 22:50:08 +03:00
nvalue(top-1) = 1;
1997-09-16 23:25:59 +04:00
break;
2000-04-07 17:13:11 +04:00
case OP_JMPNE:
top -= 2;
if (!luaO_equalObj(top, top+1)) pc += GETARG_S(i);
break;
2000-04-05 00:48:44 +04:00
case OP_JMPEQ:
top -= 2;
if (luaO_equalObj(top, top+1)) pc += GETARG_S(i);
break;
2000-04-05 00:48:44 +04:00
case OP_JMPLT:
top -= 2;
if (luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i);
break;
2000-04-05 00:48:44 +04:00
case OP_JMPLE: /* a <= b === !(b<a) */
top -= 2;
if (!luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i);
break;
2000-04-05 00:48:44 +04:00
case OP_JMPGT: /* a > b === (b<a) */
top -= 2;
if (luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i);
break;
2000-04-05 00:48:44 +04:00
case OP_JMPGE: /* a >= b === !(a<b) */
top -= 2;
if (!luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i);
break;
2000-04-05 00:48:44 +04:00
case OP_JMPT:
2000-03-10 21:37:44 +03:00
if (ttype(--top) != TAG_NIL) pc += GETARG_S(i);
break;
2000-04-05 00:48:44 +04:00
case OP_JMPF:
2000-03-10 21:37:44 +03:00
if (ttype(--top) == TAG_NIL) pc += GETARG_S(i);
break;
2000-04-05 00:48:44 +04:00
case OP_JMPONT:
2000-03-10 21:37:44 +03:00
if (ttype(top-1) != TAG_NIL) pc += GETARG_S(i);
1999-12-01 22:50:08 +03:00
else top--;
1997-09-19 22:40:32 +04:00
break;
1997-09-16 23:25:59 +04:00
2000-04-05 00:48:44 +04:00
case OP_JMPONF:
2000-03-10 21:37:44 +03:00
if (ttype(top-1) == TAG_NIL) pc += GETARG_S(i);
1999-12-01 22:50:08 +03:00
else top--;
1997-09-16 23:25:59 +04:00
break;
2000-03-10 21:37:44 +03:00
case OP_JMP:
2000-02-14 19:51:08 +03:00
pc += GETARG_S(i);
1997-09-16 23:25:59 +04:00
break;
2000-03-10 21:37:44 +03:00
case OP_PUSHNILJMP:
ttype(top++) = TAG_NIL;
pc++;
1997-09-16 23:25:59 +04:00
break;
2000-04-12 22:57:19 +04:00
case OP_FORPREP:
if (tonumber(top-1))
lua_error(L, "`for' step must be a number");
if (tonumber(top-2))
lua_error(L, "`for' limit must be a number");
if (tonumber(top-3))
lua_error(L, "`for' initial value must be a number");
nvalue(top-3) -= nvalue(top-1); /* to be undone by first FORLOOP */
pc += GETARG_S(i);
break;
case OP_FORLOOP: {
Number step = nvalue(top-1);
Number limit = nvalue(top-2);
Number index;
LUA_ASSERT(L, ttype(top-1) == TAG_NUMBER, "invalid step");
LUA_ASSERT(L, ttype(top-2) == TAG_NUMBER, "invalid limit");
if (ttype(top-3) != TAG_NUMBER)
lua_error(L, "`for' index must be a number");
2000-04-12 22:57:19 +04:00
index = nvalue(top-3)+step;
if ((step>0) ? index<=limit : index>=limit) {
nvalue(top-3) = index;
pc += GETARG_S(i);
}
else /* end of `for': remove control variables */
top -= 3;
break;
}
2000-03-10 21:37:44 +03:00
case OP_CLOSURE:
L->top = top;
luaV_Lclosure(L, tf->kproto[GETARG_A(i)], GETARG_B(i));
top = L->top;
luaC_checkGC(L);
1997-09-16 23:25:59 +04:00
break;
2000-03-10 21:37:44 +03:00
case OP_SETLINE:
if ((base-1)->ttype != TAG_LINE) {
/* open space for LINE value */
2000-02-14 19:51:08 +03:00
int n = top-base;
while (n--) base[n+1] = base[n];
base++;
top++;
2000-03-10 21:37:44 +03:00
(base-1)->ttype = TAG_LINE;
1997-09-16 23:25:59 +04:00
}
2000-02-14 19:51:08 +03:00
(base-1)->value.i = GETARG_U(i);
if (L->linehook) {
L->top = top;
2000-02-14 19:51:08 +03:00
luaD_lineHook(L, base-2, GETARG_U(i));
}
1997-09-16 23:25:59 +04:00
break;
}
}
1997-09-16 23:25:59 +04:00
}