1997-09-16 23:25:59 +04:00
|
|
|
/*
|
2000-03-03 17:58:26 +03:00
|
|
|
** $Id: lvm.c,v 1.89 2000/02/22 18:12:46 roberto Exp roberto $
|
1997-09-16 23:25:59 +04:00
|
|
|
** Lua virtual machine
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1998-12-24 17:57:23 +03:00
|
|
|
#include <stdlib.h>
|
1997-09-16 23:25:59 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
#define LUA_REENTRANT
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
#include "lauxlib.h"
|
1999-12-29 19:31:15 +03:00
|
|
|
#include "ldebug.h"
|
1997-09-16 23:25:59 +04:00
|
|
|
#include "ldo.h"
|
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lgc.h"
|
1998-12-27 23:25:20 +03:00
|
|
|
#include "lobject.h"
|
1997-09-16 23:25:59 +04:00
|
|
|
#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 "lvm.h"
|
|
|
|
|
|
|
|
|
1998-01-14 16:49:15 +03:00
|
|
|
#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:
|
|
|
|
** LUA_T_LINE(1), NAME(1), TM calls(3) (plus some extra...)
|
|
|
|
*/
|
|
|
|
#define EXTRA_STACK 8
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
1999-11-30 16:06:50 +03:00
|
|
|
static TaggedString *strconc (lua_State *L, const TaggedString *l,
|
|
|
|
const TaggedString *r) {
|
1998-12-27 23:25:20 +03:00
|
|
|
long nl = l->u.s.len;
|
|
|
|
long nr = r->u.s.len;
|
1999-11-22 16:12:07 +03:00
|
|
|
char *buffer = luaL_openspace(L, nl+nr);
|
1998-03-06 19:54:42 +03:00
|
|
|
memcpy(buffer, l->str, nl);
|
|
|
|
memcpy(buffer+nl, r->str, nr);
|
1999-11-22 16:12:07 +03:00
|
|
|
return luaS_newlstr(L, buffer, nl+nr);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-13 22:09:04 +03:00
|
|
|
int luaV_tonumber (TObject *obj) { /* LUA_NUMBER */
|
1997-09-16 23:25:59 +04:00
|
|
|
if (ttype(obj) != LUA_T_STRING)
|
|
|
|
return 1;
|
1998-12-24 17:57:23 +03:00
|
|
|
else {
|
1999-09-07 00:34:18 +04:00
|
|
|
if (!luaO_str2d(svalue(obj), &nvalue(obj)))
|
1999-04-13 23:28:49 +04:00
|
|
|
return 2;
|
1997-09-16 23:25:59 +04:00
|
|
|
ttype(obj) = LUA_T_NUMBER;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
|
1997-09-16 23:25:59 +04:00
|
|
|
if (ttype(obj) != LUA_T_NUMBER)
|
|
|
|
return 1;
|
|
|
|
else {
|
1999-12-14 21:33:29 +03:00
|
|
|
char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
|
1999-01-13 22:09:04 +03:00
|
|
|
sprintf(s, "%.16g", (double)nvalue(obj));
|
1999-11-22 16:12:07 +03:00
|
|
|
tsvalue(obj) = luaS_new(L, s);
|
1997-09-16 23:25:59 +04:00
|
|
|
ttype(obj) = LUA_T_STRING;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void luaV_setn (lua_State *L, Hash *t, int val) {
|
1998-12-30 16:16:50 +03:00
|
|
|
TObject index, value;
|
1999-11-22 16:12:07 +03:00
|
|
|
ttype(&index) = LUA_T_STRING; tsvalue(&index) = luaS_new(L, "n");
|
1998-12-30 20:26:49 +03:00
|
|
|
ttype(&value) = LUA_T_NUMBER; nvalue(&value) = val;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaH_set(L, t, &index, &value);
|
1998-12-30 16:16:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void luaV_closure (lua_State *L, int nelems) {
|
1997-12-15 19:17:20 +03:00
|
|
|
if (nelems > 0) {
|
1999-11-22 16:12:07 +03:00
|
|
|
Closure *c = luaF_newclosure(L, nelems);
|
1999-12-01 22:50:08 +03:00
|
|
|
c->consts[0] = *(L->top-1);
|
|
|
|
L->top -= nelems;
|
1999-12-23 21:19:57 +03:00
|
|
|
while (nelems--)
|
|
|
|
c->consts[nelems+1] = *(L->top-1+nelems);
|
|
|
|
ttype(L->top-1) = (ttype(&c->consts[0]) == LUA_T_CPROTO) ?
|
|
|
|
LUA_T_CCLOSURE : LUA_T_LCLOSURE;
|
1999-12-01 22:50:08 +03:00
|
|
|
(L->top-1)->value.cl = c;
|
1997-12-15 19:17:20 +03:00
|
|
|
}
|
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.
|
|
|
|
*/
|
2000-01-24 23:14:07 +03:00
|
|
|
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-03 17:58:26 +03:00
|
|
|
if (ttype(table) != LUA_T_ARRAY) { /* not a table, get gettable TM */
|
1999-11-22 16:12:07 +03:00
|
|
|
im = luaT_getimbyObj(L, table, IM_GETTABLE);
|
2000-01-24 23:14:07 +03:00
|
|
|
if (ttype(im) == LUA_T_NIL) {
|
|
|
|
L->top = top;
|
1999-12-29 19:31:15 +03:00
|
|
|
luaG_indexerror(L, table);
|
2000-01-24 23:14:07 +03:00
|
|
|
}
|
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;
|
1999-11-22 16:12:07 +03:00
|
|
|
im = luaT_getim(L, tg, IM_GETTABLE);
|
2000-03-03 17:58:26 +03:00
|
|
|
if (ttype(im) == LUA_T_NIL) { /* and does not have a `gettable' TM */
|
1999-11-22 16:12:07 +03:00
|
|
|
const TObject *h = luaH_get(L, avalue(table), table+1);
|
1999-02-08 20:07:59 +03:00
|
|
|
if (ttype(h) == LUA_T_NIL &&
|
1999-11-22 16:12:07 +03:00
|
|
|
(ttype(im=luaT_getim(L, tg, IM_INDEX)) != LUA_T_NIL)) {
|
1999-12-06 14:40:55 +03:00
|
|
|
/* result is nil and there is an `index' tag method */
|
2000-01-24 23:14:07 +03:00
|
|
|
L->top = top;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaD_callTM(L, im, 2, 1); /* calls it */
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
2000-01-24 23:14:07 +03: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 */
|
2000-01-24 23:14:07 +03:00
|
|
|
L->top = top;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaD_callTM(L, im, 2, 1);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2000-01-24 23:14:07 +03:00
|
|
|
** Receives table at *t, index at *(t+1) and value at `top'.
|
1997-09-16 23:25:59 +04:00
|
|
|
*/
|
2000-01-24 23:14:07 +03:00
|
|
|
void luaV_settable (lua_State *L, StkId t, StkId top) {
|
1999-08-17 00:52:00 +04:00
|
|
|
const TObject *im;
|
1999-12-27 20:33:22 +03:00
|
|
|
if (ttype(t) != LUA_T_ARRAY) { /* not a table, get `settable' method */
|
2000-01-24 23:14:07 +03:00
|
|
|
L->top = top;
|
1999-11-22 16:12:07 +03:00
|
|
|
im = luaT_getimbyObj(L, t, IM_SETTABLE);
|
1999-02-08 20:07:59 +03:00
|
|
|
if (ttype(im) == LUA_T_NIL)
|
1999-12-29 19:31:15 +03:00
|
|
|
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... */
|
1999-11-22 16:12:07 +03:00
|
|
|
im = luaT_getim(L, avalue(t)->htag, IM_SETTABLE);
|
1999-12-27 20:33:22 +03:00
|
|
|
if (ttype(im) == LUA_T_NIL) { /* and does not have a `settable' method */
|
2000-01-24 23:14:07 +03:00
|
|
|
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 */
|
2000-02-22 21:12:46 +03:00
|
|
|
luaD_checkstack(L, 3);
|
2000-01-24 23:14:07 +03:00
|
|
|
*(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) {
|
1998-12-30 20:26:49 +03:00
|
|
|
if (ttype(t) != LUA_T_ARRAY)
|
1999-11-22 16:12:07 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-01-24 23:14:07 +03:00
|
|
|
void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top) {
|
1999-11-04 20:23:12 +03:00
|
|
|
const TObject *value = &gv->value;
|
1999-12-06 22:30:53 +03:00
|
|
|
TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
|
2000-01-13 18:56:03 +03:00
|
|
|
if (ttype(im) == LUA_T_NIL) /* is there a tag method? */
|
2000-01-24 23:14:07 +03:00
|
|
|
*top = *value; /* default behavior */
|
2000-01-13 18:56:03 +03:00
|
|
|
else { /* tag method */
|
2000-02-22 21:12:46 +03:00
|
|
|
luaD_checkstack(L, 3);
|
2000-01-24 23:14:07 +03:00
|
|
|
*top = *im;
|
|
|
|
ttype(top+1) = LUA_T_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-01-24 23:14:07 +03:00
|
|
|
void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top) {
|
1999-11-04 20:23:12 +03:00
|
|
|
const TObject *oldvalue = &gv->value;
|
1999-11-22 16:12:07 +03:00
|
|
|
const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
|
1999-01-15 16:14:24 +03:00
|
|
|
if (ttype(im) == LUA_T_NIL) /* is there a tag method? */
|
2000-01-24 23:14:07 +03:00
|
|
|
gv->value = *(top-1);
|
1997-09-16 23:25:59 +04:00
|
|
|
else {
|
2000-02-22 21:12:46 +03:00
|
|
|
luaD_checkstack(L, 3);
|
2000-01-24 23:14:07 +03:00
|
|
|
*(top+2) = *(top-1); /* new value */
|
|
|
|
*(top+1) = *oldvalue;
|
|
|
|
ttype(top) = LUA_T_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;
|
1997-09-16 23:25:59 +04:00
|
|
|
if (ttype(im) == LUA_T_NIL) {
|
1999-12-01 22:50:08 +03:00
|
|
|
im = luaT_getimbyObj(L, top-1, event); /* try second operand */
|
1997-09-16 23:25:59 +04:00
|
|
|
if (ttype(im) == LUA_T_NIL) {
|
1999-12-27 20:33:22 +03:00
|
|
|
im = luaT_getim(L, 0, event); /* try a `global' method */
|
1997-09-16 23:25:59 +04:00
|
|
|
if (ttype(im) == LUA_T_NIL)
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_error(L, msg);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
}
|
1999-11-22 16:12:07 +03: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-01-19 19:50:30 +03:00
|
|
|
static int luaV_strcomp (const TaggedString *ls, const TaggedString *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-02-22 21:12:46 +03:00
|
|
|
|
|
|
|
int luaV_lessthan (lua_State *L, TObject *l, TObject *r) {
|
1997-09-16 23:25:59 +04:00
|
|
|
if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
|
2000-02-22 21:12:46 +03:00
|
|
|
return (nvalue(l) < nvalue(r));
|
1997-09-16 23:25:59 +04:00
|
|
|
else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING)
|
2000-02-22 21:12:46 +03:00
|
|
|
return (luaV_strcomp(tsvalue(l), tsvalue(r)) < 0);
|
1997-09-16 23:25:59 +04:00
|
|
|
else {
|
2000-03-03 17:58:26 +03:00
|
|
|
/* update top and put arguments in correct order to call TM */
|
2000-02-22 21:12:46 +03:00
|
|
|
if (l<r) /* are arguments in correct order? */
|
|
|
|
L->top = r+1; /* yes; 2nd is on top */
|
|
|
|
else { /* no; exchange them */
|
|
|
|
TObject temp = *r;
|
|
|
|
*r = *l;
|
|
|
|
*l = temp;
|
|
|
|
L->top = l+1; /* 1st is on top */
|
|
|
|
}
|
2000-01-19 19:50:30 +03:00
|
|
|
call_binTM(L, L->top, IM_LT, "unexpected type in comparison");
|
2000-02-22 21:12:46 +03:00
|
|
|
L->top--;
|
|
|
|
return (ttype(L->top) != LUA_T_NIL);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
#define setbool(o,cond) if (cond) { \
|
2000-02-22 21:12:46 +03:00
|
|
|
ttype(o) = LUA_T_NUMBER; nvalue(o) = 1.0; } \
|
2000-02-14 19:51:08 +03:00
|
|
|
else ttype(o) = LUA_T_NIL
|
|
|
|
|
|
|
|
|
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;
|
1998-07-12 20:16:43 +04:00
|
|
|
Hash *htab;
|
1999-12-01 22:50:08 +03:00
|
|
|
htab = avalue(tab) = luaH_new(L, nvararg+1); /* +1 for field `n' */
|
1997-09-16 23:25:59 +04:00
|
|
|
ttype(tab) = LUA_T_ARRAY;
|
1998-07-12 20:16:43 +04:00
|
|
|
for (i=0; i<nvararg; i++)
|
1999-11-22 16:12:07 +03:00
|
|
|
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
|
|
|
*/
|
1999-11-30 16:06:50 +03:00
|
|
|
StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
|
2000-02-08 19:39:42 +03:00
|
|
|
register StkId base) {
|
1999-12-01 22:50:08 +03:00
|
|
|
register StkId top; /* keep top local, for performance */
|
2000-02-14 19:51:08 +03:00
|
|
|
register const Instruction *pc = tf->code;
|
2000-01-28 19:53:00 +03:00
|
|
|
TaggedString **kstr = tf->kstr;
|
1999-12-30 21:28:40 +03:00
|
|
|
if (L->callhook)
|
2000-01-19 15:00:45 +03:00
|
|
|
luaD_callHook(L, base-1, L->callhook, "call");
|
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);
|
1999-11-22 16:12:07 +03:00
|
|
|
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 (;;) {
|
2000-02-14 19:51:08 +03:00
|
|
|
register Instruction i = *pc++;
|
|
|
|
switch (GET_OPCODE(i)) {
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1999-03-06 00:16:07 +03:00
|
|
|
case ENDCODE:
|
1999-12-21 21:04:41 +03:00
|
|
|
return L->top; /* no results */
|
1999-12-09 23:01:48 +03:00
|
|
|
|
1999-02-02 22:41:17 +03:00
|
|
|
case RETCODE:
|
1999-12-21 21:04:41 +03:00
|
|
|
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-02-14 19:51:08 +03:00
|
|
|
case 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-02-14 19:51:08 +03:00
|
|
|
case 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);
|
1999-02-02 22:41:17 +03:00
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case PUSHNIL: {
|
|
|
|
register int n = GETARG_U(i);
|
1997-10-14 02:12:04 +04:00
|
|
|
do {
|
1999-12-01 22:50:08 +03:00
|
|
|
ttype(top++) = LUA_T_NIL;
|
2000-02-14 19:51:08 +03:00
|
|
|
} while (n--);
|
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-02-14 19:51:08 +03:00
|
|
|
case POP:
|
|
|
|
top -= GETARG_U(i);
|
1999-02-09 18:59:10 +03:00
|
|
|
break;
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case PUSHINT:
|
1999-12-01 22:50:08 +03:00
|
|
|
ttype(top) = LUA_T_NUMBER;
|
2000-02-14 19:51:08 +03:00
|
|
|
nvalue(top) = (real)GETARG_S(i);
|
1999-12-01 22:50:08 +03:00
|
|
|
top++;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case PUSHSTRING:
|
2000-01-25 16:57:18 +03:00
|
|
|
ttype(top) = LUA_T_STRING;
|
2000-02-14 19:51:08 +03:00
|
|
|
tsvalue(top) = kstr[GETARG_U(i)];
|
2000-01-25 16:57:18 +03:00
|
|
|
top++;
|
|
|
|
break;
|
|
|
|
|
2000-02-22 16:31:43 +03:00
|
|
|
case PUSHNUM:
|
2000-01-25 16:57:18 +03:00
|
|
|
ttype(top) = LUA_T_NUMBER;
|
2000-02-14 19:51:08 +03:00
|
|
|
nvalue(top) = tf->knum[GETARG_U(i)];
|
2000-01-25 16:57:18 +03:00
|
|
|
top++;
|
1999-02-02 22:41:17 +03:00
|
|
|
break;
|
|
|
|
|
2000-02-22 16:31:43 +03:00
|
|
|
case PUSHNEGNUM:
|
|
|
|
ttype(top) = LUA_T_NUMBER;
|
|
|
|
nvalue(top) = -tf->knum[GETARG_U(i)];
|
|
|
|
top++;
|
|
|
|
break;
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case PUSHUPVALUE:
|
|
|
|
*top++ = cl->consts[GETARG_U(i)+1];
|
1999-02-02 22:41:17 +03:00
|
|
|
break;
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case PUSHLOCAL:
|
|
|
|
*top++ = *(base+GETARG_U(i));
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case GETGLOBAL:
|
|
|
|
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;
|
|
|
|
|
1997-09-19 22:40:32 +04:00
|
|
|
case GETTABLE:
|
2000-01-24 23:14:07 +03:00
|
|
|
luaV_gettable(L, top);
|
1999-12-01 22:50:08 +03:00
|
|
|
top--;
|
1999-02-04 19:36:16 +03:00
|
|
|
break;
|
1997-09-16 23:25:59 +04:00
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case GETDOTTED:
|
2000-01-19 19:50:30 +03:00
|
|
|
ttype(top) = LUA_T_STRING;
|
2000-02-14 19:51:08 +03:00
|
|
|
tsvalue(top++) = kstr[GETARG_U(i)];
|
2000-01-24 23:14:07 +03:00
|
|
|
luaV_gettable(L, top);
|
1999-12-01 22:50:08 +03:00
|
|
|
top--;
|
1997-10-24 22:40:29 +04:00
|
|
|
break;
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case PUSHSELF: {
|
1999-05-21 23:41:49 +04:00
|
|
|
TObject receiver;
|
1999-12-01 22:50:08 +03:00
|
|
|
receiver = *(top-1);
|
2000-01-19 19:50:30 +03:00
|
|
|
ttype(top) = LUA_T_STRING;
|
2000-02-14 19:51:08 +03:00
|
|
|
tsvalue(top++) = kstr[GETARG_U(i)];
|
2000-01-24 23:14:07 +03:00
|
|
|
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-02-14 19:51:08 +03:00
|
|
|
case CREATETABLE:
|
1999-12-01 22:50:08 +03:00
|
|
|
L->top = top;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaC_checkGC(L);
|
2000-02-14 19:51:08 +03:00
|
|
|
avalue(top) = luaH_new(L, GETARG_U(i));
|
1999-12-01 22:50:08 +03:00
|
|
|
ttype(top) = LUA_T_ARRAY;
|
|
|
|
top++;
|
1997-09-19 22:40:32 +04:00
|
|
|
break;
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case SETLOCAL:
|
|
|
|
*(base+GETARG_U(i)) = *(--top);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case SETGLOBAL:
|
|
|
|
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;
|
|
|
|
|
1999-02-08 20:07:59 +03:00
|
|
|
case SETTABLEPOP:
|
2000-01-24 23:14:07 +03:00
|
|
|
luaV_settable(L, top-3, top);
|
1999-12-01 22:50:08 +03:00
|
|
|
top -= 3; /* pop table, index, and value */
|
1999-02-08 21:54:19 +03:00
|
|
|
break;
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1997-09-19 22:40:32 +04:00
|
|
|
case SETTABLE:
|
2000-02-14 19:51:08 +03:00
|
|
|
luaV_settable(L, top-3-GETARG_U(i), top);
|
1999-12-01 22:50:08 +03:00
|
|
|
top--; /* pop value */
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case SETLIST: {
|
|
|
|
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-02-14 19:51:08 +03:00
|
|
|
case SETMAP: {
|
|
|
|
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) */
|
1997-10-14 02:12:04 +04:00
|
|
|
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-02-14 19:51:08 +03:00
|
|
|
case NEQOP:
|
|
|
|
top--;
|
|
|
|
setbool(top-1, !luaO_equalObj(top-1, top));
|
|
|
|
break;
|
|
|
|
|
2000-01-19 19:50:30 +03:00
|
|
|
case EQOP:
|
1999-12-01 22:50:08 +03:00
|
|
|
top--;
|
2000-02-14 19:51:08 +03:00
|
|
|
setbool(top-1, luaO_equalObj(top-1, top));
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
2000-01-19 19:50:30 +03:00
|
|
|
case LTOP:
|
|
|
|
top--;
|
2000-02-22 21:12:46 +03:00
|
|
|
setbool(top-1, luaV_lessthan(L, top-1, top));
|
2000-02-14 19:51:08 +03:00
|
|
|
break;
|
1997-09-16 23:25:59 +04:00
|
|
|
|
2000-02-22 21:12:46 +03:00
|
|
|
case LEOP: /* a <= b === !(b<a) */
|
1999-12-01 22:50:08 +03:00
|
|
|
top--;
|
2000-02-22 21:12:46 +03:00
|
|
|
setbool(top-1, !luaV_lessthan(L, top, top-1));
|
2000-02-14 19:51:08 +03:00
|
|
|
break;
|
1997-09-16 23:25:59 +04:00
|
|
|
|
2000-02-22 21:12:46 +03:00
|
|
|
case GTOP: /* a > b === (b<a) */
|
1999-12-01 22:50:08 +03:00
|
|
|
top--;
|
2000-02-22 21:12:46 +03:00
|
|
|
setbool(top-1, luaV_lessthan(L, top, top-1));
|
2000-02-14 19:51:08 +03:00
|
|
|
break;
|
1997-09-16 23:25:59 +04:00
|
|
|
|
2000-02-22 21:12:46 +03:00
|
|
|
case GEOP: /* a >= b === !(a<b) */
|
1999-12-01 22:50:08 +03:00
|
|
|
top--;
|
2000-02-22 21:12:46 +03:00
|
|
|
setbool(top-1, !luaV_lessthan(L, top-1, top));
|
2000-02-14 19:51:08 +03:00
|
|
|
break;
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1999-12-01 22:50:08 +03:00
|
|
|
case ADDOP:
|
|
|
|
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-02-22 16:31:43 +03:00
|
|
|
case ADDI:
|
|
|
|
if (tonumber(top-1)) {
|
|
|
|
ttype(top) = LUA_T_NUMBER;
|
|
|
|
nvalue(top) = (real)GETARG_S(i);
|
|
|
|
call_arith(L, top+1, IM_ADD);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nvalue(top-1) += (real)GETARG_S(i);
|
|
|
|
break;
|
|
|
|
|
1999-12-01 22:50:08 +03:00
|
|
|
case SUBOP:
|
|
|
|
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;
|
|
|
|
|
1999-12-01 22:50:08 +03:00
|
|
|
case MULTOP:
|
|
|
|
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;
|
|
|
|
|
1999-12-01 22:50:08 +03:00
|
|
|
case DIVOP:
|
|
|
|
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;
|
|
|
|
|
|
|
|
case POWOP:
|
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;
|
|
|
|
|
1999-12-01 22:50:08 +03:00
|
|
|
case CONCOP:
|
|
|
|
if (tostring(L, top-2) || tostring(L, top-1))
|
|
|
|
call_binTM(L, top, IM_CONCAT, "unexpected type for concatenation");
|
|
|
|
else
|
|
|
|
tsvalue(top-2) = strconc(L, tsvalue(top-2), tsvalue(top-1));
|
1999-12-06 14:40:55 +03:00
|
|
|
top--;
|
1999-12-01 22:50:08 +03:00
|
|
|
L->top = top;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaC_checkGC(L);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MINUSOP:
|
1999-12-01 22:50:08 +03:00
|
|
|
if (tonumber(top-1)) {
|
|
|
|
ttype(top) = LUA_T_NIL;
|
|
|
|
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;
|
|
|
|
|
|
|
|
case NOTOP:
|
1999-12-01 22:50:08 +03:00
|
|
|
ttype(top-1) =
|
|
|
|
(ttype(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
|
|
|
|
nvalue(top-1) = 1;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case ONTJMP:
|
|
|
|
if (ttype(top-1) != LUA_T_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-02-14 19:51:08 +03:00
|
|
|
case ONFJMP:
|
|
|
|
if (ttype(top-1) == LUA_T_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-02-14 19:51:08 +03:00
|
|
|
case JMP:
|
|
|
|
pc += GETARG_S(i);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case IFTJMP:
|
|
|
|
if (ttype(--top) != LUA_T_NIL) pc += GETARG_S(i);
|
1997-10-02 00:05:34 +04:00
|
|
|
break;
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case IFFJMP:
|
|
|
|
if (ttype(--top) == LUA_T_NIL) pc += GETARG_S(i);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case CLOSURE:
|
2000-01-25 16:57:18 +03:00
|
|
|
ttype(top) = LUA_T_LPROTO;
|
2000-02-14 19:51:08 +03:00
|
|
|
tfvalue(top) = tf->kproto[GETARG_A(i)];
|
2000-01-25 16:57:18 +03:00
|
|
|
L->top = ++top;
|
2000-02-14 19:51:08 +03:00
|
|
|
luaV_closure(L, GETARG_B(i));
|
|
|
|
top -= GETARG_B(i);
|
1999-11-22 16:12:07 +03:00
|
|
|
luaC_checkGC(L);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
case SETLINE:
|
2000-02-11 19:52:54 +03:00
|
|
|
if ((base-1)->ttype != LUA_T_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];
|
2000-02-11 19:52:54 +03:00
|
|
|
base++;
|
|
|
|
top++;
|
|
|
|
(base-1)->ttype = LUA_T_LINE;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
2000-02-14 19:51:08 +03:00
|
|
|
(base-1)->value.i = GETARG_U(i);
|
1999-12-29 19:31:15 +03:00
|
|
|
if (L->linehook) {
|
|
|
|
L->top = top;
|
2000-02-14 19:51:08 +03:00
|
|
|
luaD_lineHook(L, base-2, GETARG_U(i));
|
1999-12-29 19:31:15 +03:00
|
|
|
}
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
1999-12-21 21:04:41 +03:00
|
|
|
}
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|