1997-09-16 23:25:59 +04:00
|
|
|
/*
|
1999-02-09 21:01:55 +03:00
|
|
|
** $Id: lvm.c,v 1.48 1999/02/09 15:58:45 roberto Exp roberto $
|
1997-09-16 23:25:59 +04:00
|
|
|
** Lua virtual machine
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1998-12-24 17:57:23 +03:00
|
|
|
#include <ctype.h>
|
1998-12-27 23:25:20 +03:00
|
|
|
#include <limits.h>
|
1997-09-16 23:25:59 +04:00
|
|
|
#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>
|
|
|
|
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "ldo.h"
|
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lgc.h"
|
|
|
|
#include "lmem.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 "luadebug.h"
|
|
|
|
#include "lvm.h"
|
|
|
|
|
|
|
|
|
1998-01-14 16:49:15 +03:00
|
|
|
#ifdef OLD_ANSI
|
|
|
|
#define strcoll(a,b) strcmp(a,b)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1999-02-02 20:57:49 +03:00
|
|
|
#define highbyte(x) ((x)<<8)
|
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
|
|
|
|
|
|
|
|
|
|
|
|
1998-12-27 23:25:20 +03:00
|
|
|
static TaggedString *strconc (TaggedString *l, TaggedString *r) {
|
|
|
|
long nl = l->u.s.len;
|
|
|
|
long nr = r->u.s.len;
|
|
|
|
char *buffer = luaL_openspace(nl+nr);
|
1998-03-06 19:54:42 +03:00
|
|
|
memcpy(buffer, l->str, nl);
|
|
|
|
memcpy(buffer+nl, r->str, nr);
|
|
|
|
return luaS_newlstr(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 {
|
1998-12-27 23:25:20 +03:00
|
|
|
double t;
|
|
|
|
char *e = svalue(obj);
|
|
|
|
int sig = 1;
|
|
|
|
while (isspace((unsigned char)*e)) e++;
|
|
|
|
if (*e == '+') e++;
|
|
|
|
else if (*e == '-') {
|
|
|
|
e++;
|
|
|
|
sig = -1;
|
|
|
|
}
|
|
|
|
t = luaO_str2d(e);
|
|
|
|
if (t<0) return 2;
|
|
|
|
nvalue(obj) = (real)t*sig;
|
1997-09-16 23:25:59 +04:00
|
|
|
ttype(obj) = LUA_T_NUMBER;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-13 22:09:04 +03:00
|
|
|
int luaV_tostring (TObject *obj) { /* LUA_NUMBER */
|
1997-09-16 23:25:59 +04:00
|
|
|
if (ttype(obj) != LUA_T_NUMBER)
|
|
|
|
return 1;
|
|
|
|
else {
|
1999-01-13 22:09:04 +03:00
|
|
|
char s[32]; /* 16 digits, signal, point and \0 (+ some extra...) */
|
|
|
|
sprintf(s, "%.16g", (double)nvalue(obj));
|
1997-09-16 23:25:59 +04:00
|
|
|
tsvalue(obj) = luaS_new(s);
|
|
|
|
ttype(obj) = LUA_T_STRING;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-12-30 16:16:50 +03:00
|
|
|
void luaV_setn (Hash *t, int val) {
|
|
|
|
TObject index, value;
|
1998-12-30 20:26:49 +03:00
|
|
|
ttype(&index) = LUA_T_STRING; tsvalue(&index) = luaS_new("n");
|
|
|
|
ttype(&value) = LUA_T_NUMBER; nvalue(&value) = val;
|
1999-01-25 20:39:28 +03:00
|
|
|
luaH_set(t, &index, &value);
|
1998-12-30 16:16:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-25 20:39:28 +03:00
|
|
|
void luaV_closure (int nelems) {
|
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.
|
|
|
|
*/
|
1998-12-30 20:26:49 +03:00
|
|
|
void luaV_gettable (void) {
|
1999-02-08 20:07:59 +03:00
|
|
|
TObject *table = L->stack.top-2;
|
1997-09-16 23:25:59 +04:00
|
|
|
TObject *im;
|
1999-02-08 20:07:59 +03:00
|
|
|
if (ttype(table) != LUA_T_ARRAY) { /* not a table, get gettable method */
|
|
|
|
im = luaT_getimbyObj(table, IM_GETTABLE);
|
|
|
|
if (ttype(im) == LUA_T_NIL)
|
|
|
|
lua_error("indexed expression not a table");
|
|
|
|
}
|
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;
|
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 */
|
1999-02-08 20:07:59 +03:00
|
|
|
TObject *h = luaH_get(avalue(table), table+1);
|
|
|
|
if (ttype(h) == LUA_T_NIL &&
|
|
|
|
(ttype(im=luaT_getim(tg, IM_INDEX)) != LUA_T_NIL)) {
|
|
|
|
/* result is nil and there is an "index" tag method */
|
|
|
|
luaD_callTM(im, 2, 1); /* calls it */
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
else {
|
1999-02-08 20:07:59 +03:00
|
|
|
L->stack.top--;
|
|
|
|
*table = *h; /* "push" result into table position */
|
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 */
|
1998-12-30 20:26:49 +03:00
|
|
|
luaD_callTM(im, 2, 1);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1999-02-08 20:07:59 +03:00
|
|
|
** Receives table at *t, index at *(t+1) and value at top.
|
1997-09-16 23:25:59 +04:00
|
|
|
*/
|
1999-02-08 20:07:59 +03:00
|
|
|
void luaV_settable (TObject *t) {
|
1997-11-21 22:00:46 +03:00
|
|
|
struct Stack *S = &L->stack;
|
1998-12-30 20:26:49 +03:00
|
|
|
TObject *im;
|
1999-02-08 20:07:59 +03:00
|
|
|
if (ttype(t) != LUA_T_ARRAY) { /* not a table, get "settable" method */
|
1998-12-30 20:26:49 +03:00
|
|
|
im = luaT_getimbyObj(t, IM_SETTABLE);
|
1999-02-08 20:07:59 +03:00
|
|
|
if (ttype(im) == LUA_T_NIL)
|
|
|
|
lua_error("indexed expression not a table");
|
|
|
|
}
|
1998-12-30 20:26:49 +03:00
|
|
|
else { /* object is a table... */
|
|
|
|
im = luaT_getim(avalue(t)->htag, IM_SETTABLE);
|
|
|
|
if (ttype(im) == LUA_T_NIL) { /* and does not have a "settable" method */
|
1999-01-25 20:39:28 +03:00
|
|
|
luaH_set(avalue(t), t+1, S->top-1);
|
1999-02-08 20:07:59 +03:00
|
|
|
S->top--; /* pop value */
|
1998-12-30 20:26:49 +03:00
|
|
|
return;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
1998-12-30 20:26:49 +03:00
|
|
|
/* else it has a "settable" method, go through to next command */
|
|
|
|
}
|
|
|
|
/* 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 */
|
|
|
|
*(S->top+1) = *(L->stack.top-1);
|
|
|
|
*(S->top) = *(t+1);
|
|
|
|
*(S->top-1) = *t;
|
|
|
|
S->top += 2; /* WARNING: caller must assure stack space */
|
1998-12-30 20:26:49 +03:00
|
|
|
luaD_callTM(im, 3, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaV_rawsettable (TObject *t) {
|
|
|
|
if (ttype(t) != LUA_T_ARRAY)
|
|
|
|
lua_error("indexed expression not a table");
|
|
|
|
else {
|
|
|
|
struct Stack *S = &L->stack;
|
1999-01-25 20:39:28 +03:00
|
|
|
luaH_set(avalue(t), t+1, S->top-1);
|
1998-12-30 20:26:49 +03:00
|
|
|
S->top -= 3;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-15 16:14:24 +03:00
|
|
|
void luaV_getglobal (TaggedString *ts) {
|
1999-01-20 23:22:06 +03:00
|
|
|
/* WARNING: caller must assure stack space */
|
1999-01-15 16:14:24 +03:00
|
|
|
/* only userdata, tables and nil can have getglobal tag methods */
|
|
|
|
static char valid_getglobals[] = {1, 0, 0, 1, 0, 0, 1, 0}; /* ORDER LUA_T */
|
1998-03-06 19:54:42 +03:00
|
|
|
TObject *value = &ts->u.s.globalval;
|
1999-01-15 16:14:24 +03:00
|
|
|
if (valid_getglobals[-ttype(value)]) {
|
|
|
|
TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL);
|
|
|
|
if (ttype(im) != LUA_T_NIL) { /* is there a tag method? */
|
|
|
|
struct Stack *S = &L->stack;
|
|
|
|
ttype(S->top) = LUA_T_STRING;
|
|
|
|
tsvalue(S->top) = ts;
|
|
|
|
S->top++;
|
|
|
|
*S->top++ = *value;
|
|
|
|
luaD_callTM(im, 2, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* else no tag method: go through to default behavior */
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
1999-01-15 16:14:24 +03:00
|
|
|
*L->stack.top++ = *value; /* default behavior */
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-15 16:14:24 +03:00
|
|
|
void luaV_setglobal (TaggedString *ts) {
|
1998-03-06 19:54:42 +03:00
|
|
|
TObject *oldvalue = &ts->u.s.globalval;
|
1997-09-16 23:25:59 +04:00
|
|
|
TObject *im = luaT_getimbyObj(oldvalue, IM_SETGLOBAL);
|
1999-01-15 16:14:24 +03:00
|
|
|
if (ttype(im) == LUA_T_NIL) /* is there a tag method? */
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-20 23:22:06 +03:00
|
|
|
static int luaV_strcomp (char *l, long ll, char *r, long lr)
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-07-12 20:16:43 +04:00
|
|
|
void luaV_comparison (lua_Type ttype_less, lua_Type ttype_equal,
|
1999-01-20 23:22:06 +03:00
|
|
|
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;
|
1999-01-20 23:22:06 +03:00
|
|
|
real result;
|
1997-09-16 23:25:59 +04:00
|
|
|
if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
|
1999-01-20 23:22:06 +03:00
|
|
|
result = nvalue(l)-nvalue(r);
|
1997-09-16 23:25:59 +04:00
|
|
|
else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING)
|
1999-01-20 23:22:06 +03:00
|
|
|
result = luaV_strcomp(svalue(l), tsvalue(l)->u.s.len,
|
|
|
|
svalue(r), tsvalue(r)->u.s.len);
|
1997-09-16 23:25:59 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-12-30 16:16:50 +03:00
|
|
|
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;
|
1998-07-12 20:16:43 +04:00
|
|
|
Hash *htab;
|
1997-09-16 23:25:59 +04:00
|
|
|
if (nvararg < 0) nvararg = 0;
|
1998-07-12 20:16:43 +04:00
|
|
|
htab = avalue(tab) = luaH_new(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++)
|
|
|
|
luaH_setint(htab, i+1, firstelem+i);
|
1998-12-30 16:16:50 +03:00
|
|
|
luaV_setn(htab, nvararg); /* store counter in field "n" */
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
1999-02-02 20:57:49 +03:00
|
|
|
StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) {
|
1997-11-21 22:00:46 +03:00
|
|
|
struct Stack *S = &L->stack; /* to optimize */
|
1999-02-02 20:57:49 +03:00
|
|
|
register Byte *pc = tf->code;
|
1997-12-15 19:17:20 +03:00
|
|
|
TObject *consts = tf->consts;
|
1999-02-04 20:47:59 +03:00
|
|
|
if (L->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);
|
|
|
|
}
|
1998-12-03 18:45:15 +03:00
|
|
|
for (;;) {
|
1999-02-02 20:57:49 +03:00
|
|
|
register int aux = 0;
|
1999-02-04 19:36:16 +03:00
|
|
|
switchentry:
|
1999-02-02 20:57:49 +03:00
|
|
|
switch ((OpCode)*pc++) {
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1999-02-02 22:41:17 +03:00
|
|
|
case ENDCODE: aux = 1;
|
|
|
|
S->top = S->stack + base;
|
|
|
|
/* goes through */
|
|
|
|
case RETCODE:
|
1999-02-04 20:47:59 +03:00
|
|
|
if (L->callhook)
|
1999-02-02 22:41:17 +03:00
|
|
|
luaD_callHook(base, NULL, 1);
|
|
|
|
return base + (aux ? 0 : *pc);
|
|
|
|
|
1999-02-02 20:57:49 +03:00
|
|
|
case PUSHNIL: aux = *pc++;
|
1997-10-14 02:12:04 +04:00
|
|
|
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;
|
|
|
|
|
1999-02-02 22:41:17 +03:00
|
|
|
case POP: aux = *pc++;
|
1999-02-08 21:54:19 +03:00
|
|
|
S->top -= aux;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case POPDUP: aux = *pc++;
|
|
|
|
*(S->top-aux-1) = *(S->top-1);
|
|
|
|
S->top -= aux;
|
1999-02-02 22:41:17 +03:00
|
|
|
break;
|
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case PUSHNUMBERW: aux += highbyte(*pc++);
|
1999-02-02 20:57:49 +03:00
|
|
|
case PUSHNUMBER: aux += *pc++;
|
1997-11-21 22:00:46 +03:00
|
|
|
ttype(S->top) = LUA_T_NUMBER;
|
1999-02-09 18:59:10 +03:00
|
|
|
nvalue(S->top) = aux;
|
|
|
|
S->top++;
|
|
|
|
break;
|
|
|
|
|
1999-02-09 21:01:55 +03:00
|
|
|
case PUSHNUMBERNEGW: aux += highbyte(*pc++);
|
|
|
|
case PUSHNUMBERNEG: aux += *pc++;
|
1999-02-09 18:59:10 +03:00
|
|
|
ttype(S->top) = LUA_T_NUMBER;
|
|
|
|
nvalue(S->top) = -aux;
|
1997-11-21 22:00:46 +03:00
|
|
|
S->top++;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case PUSHCONSTANTW: aux += highbyte(*pc++);
|
1999-02-02 22:41:17 +03:00
|
|
|
case PUSHCONSTANT: aux += *pc++;
|
|
|
|
*S->top++ = consts[aux];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PUSHUPVALUE: aux = *pc++;
|
|
|
|
*S->top++ = cl->consts[aux+1];
|
|
|
|
break;
|
|
|
|
|
1999-02-02 20:57:49 +03:00
|
|
|
case PUSHLOCAL: aux = *pc++;
|
1997-11-21 22:00:46 +03:00
|
|
|
*S->top++ = *((S->stack+base) + aux);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case GETGLOBALW: aux += highbyte(*pc++);
|
1999-02-02 20:57:49 +03:00
|
|
|
case GETGLOBAL: aux += *pc++;
|
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:
|
1999-02-04 19:36:16 +03:00
|
|
|
luaV_gettable();
|
|
|
|
break;
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case GETDOTTEDW: aux += highbyte(*pc++);
|
1999-02-02 20:57:49 +03:00
|
|
|
case GETDOTTED: aux += *pc++;
|
1997-11-21 22:00:46 +03:00
|
|
|
*S->top++ = consts[aux];
|
1997-10-24 22:40:29 +04:00
|
|
|
luaV_gettable();
|
|
|
|
break;
|
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case PUSHSELFW: aux += highbyte(*pc++);
|
1999-02-02 20:57:49 +03:00
|
|
|
case PUSHSELF: aux += *pc++; {
|
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;
|
|
|
|
}
|
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case CREATEARRAYW: aux += highbyte(*pc++);
|
1999-02-02 22:41:17 +03:00
|
|
|
case CREATEARRAY: aux += *pc++;
|
|
|
|
luaC_checkGC();
|
|
|
|
avalue(S->top) = luaH_new(aux);
|
|
|
|
ttype(S->top) = LUA_T_ARRAY;
|
|
|
|
S->top++;
|
1997-09-19 22:40:32 +04:00
|
|
|
break;
|
|
|
|
|
1999-02-02 20:57:49 +03:00
|
|
|
case SETLOCAL: aux = *pc++;
|
1997-11-21 22:00:46 +03:00
|
|
|
*((S->stack+base) + aux) = *(--S->top);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1999-02-02 22:41:17 +03:00
|
|
|
case SETLOCALDUP: aux = *pc++;
|
|
|
|
*((S->stack+base) + aux) = *(S->top-1);
|
|
|
|
break;
|
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case SETGLOBALW: aux += highbyte(*pc++);
|
1999-02-02 20:57:49 +03:00
|
|
|
case SETGLOBAL: aux += *pc++;
|
1997-09-26 19:02:26 +04:00
|
|
|
luaV_setglobal(tsvalue(&consts[aux]));
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case SETGLOBALDUPW: aux += highbyte(*pc++);
|
1999-02-02 22:41:17 +03:00
|
|
|
case SETGLOBALDUP: aux += *pc++;
|
|
|
|
*S->top = *(S->top-1);
|
|
|
|
S->top++;
|
|
|
|
luaV_setglobal(tsvalue(&consts[aux]));
|
|
|
|
break;
|
|
|
|
|
1999-02-08 20:07:59 +03:00
|
|
|
case SETTABLEPOP:
|
1999-02-08 21:54:19 +03:00
|
|
|
luaV_settable(S->top-3);
|
|
|
|
S->top -= 2; /* pop table and index */
|
|
|
|
break;
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1999-02-09 18:59:10 +03:00
|
|
|
case SETTABLEPOPDUP: {
|
1999-02-08 21:54:19 +03:00
|
|
|
TObject temp = *(S->top-1);
|
|
|
|
luaV_settable(S->top-3);
|
|
|
|
S->top--; /* pop index (temp goes into "table" position) */
|
|
|
|
*(S->top-1) = temp;
|
|
|
|
break;
|
|
|
|
}
|
1999-02-02 22:41:17 +03:00
|
|
|
|
1997-09-19 22:40:32 +04:00
|
|
|
case SETTABLE:
|
1999-02-08 20:07:59 +03:00
|
|
|
luaV_settable(S->top-3-(*pc++));
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1999-02-08 21:54:19 +03:00
|
|
|
case SETTABLEDUP:
|
|
|
|
*S->top = *(S->top-1);
|
|
|
|
S->top++;
|
|
|
|
luaV_settable(S->top-(3+1)-(*pc++));
|
|
|
|
break;
|
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case SETLISTW: aux += highbyte(*pc++);
|
1999-02-02 20:57:49 +03:00
|
|
|
case SETLIST: aux += *pc++; {
|
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;
|
1999-02-02 20:57:49 +03:00
|
|
|
aux *= LFIELDS_PER_FLUSH;
|
1999-01-25 20:39:28 +03:00
|
|
|
for (; n; n--)
|
|
|
|
luaH_setint(avalue(arr), n+aux, --S->top);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-02-02 20:57:49 +03:00
|
|
|
case SETMAP: aux = *pc++; {
|
1997-11-21 22:00:46 +03:00
|
|
|
TObject *arr = S->top-(2*aux)-3;
|
1997-10-14 02:12:04 +04:00
|
|
|
do {
|
1999-01-25 20:39:28 +03:00
|
|
|
luaH_set(avalue(arr), S->top-2, S->top-1);
|
1997-11-21 22:00:46 +03:00
|
|
|
S->top-=2;
|
1997-10-14 02:12:04 +04:00
|
|
|
} while (aux--);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-02-02 20:57:49 +03:00
|
|
|
case NEQOP: aux = 1;
|
|
|
|
case EQOP: {
|
1997-11-21 22:00:46 +03:00
|
|
|
int res = luaO_equalObj(S->top-2, S->top-1);
|
1999-02-02 20:57:49 +03:00
|
|
|
if (aux) res = !res;
|
1997-11-21 22:00:46 +03:00
|
|
|
S->top--;
|
|
|
|
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:
|
1998-07-12 20:16:43 +04:00
|
|
|
luaV_comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LEOP:
|
1998-07-12 20:16:43 +04:00
|
|
|
luaV_comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, IM_LE);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GTOP:
|
1998-07-12 20:16:43 +04:00
|
|
|
luaV_comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, IM_GT);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GEOP:
|
1998-07-12 20:16:43 +04:00
|
|
|
luaV_comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, IM_GE);
|
1997-09-16 23:25:59 +04:00
|
|
|
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:
|
1998-06-01 02:18:24 +04:00
|
|
|
call_binTM(IM_POW, "undefined operation");
|
1997-09-16 23:25:59 +04:00
|
|
|
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 {
|
1998-03-06 19:54:42 +03:00
|
|
|
tsvalue(l) = strconc(tsvalue(l), tsvalue(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;
|
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case ONTJMPW: aux += highbyte(*pc++);
|
1999-02-02 20:57:49 +03:00
|
|
|
case ONTJMP: aux += *pc++;
|
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
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case ONFJMPW: aux += highbyte(*pc++);
|
1999-02-02 20:57:49 +03:00
|
|
|
case ONFJMP: aux += *pc++;
|
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;
|
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case JMPW: aux += highbyte(*pc++);
|
1999-02-02 20:57:49 +03:00
|
|
|
case JMP: aux += *pc++;
|
1997-10-06 18:51:11 +04:00
|
|
|
pc += aux;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case IFFJMPW: aux += highbyte(*pc++);
|
1999-02-02 20:57:49 +03:00
|
|
|
case IFFJMP: aux += *pc++;
|
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;
|
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case IFTUPJMPW: aux += highbyte(*pc++);
|
1999-02-02 20:57:49 +03:00
|
|
|
case IFTUPJMP: aux += *pc++;
|
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;
|
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case IFFUPJMPW: aux += highbyte(*pc++);
|
1999-02-02 20:57:49 +03:00
|
|
|
case IFFUPJMP: aux += *pc++;
|
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;
|
|
|
|
|
1999-02-09 18:59:10 +03:00
|
|
|
case CLOSUREW: aux += highbyte(*pc++);
|
|
|
|
case CLOSURE: aux += *pc++;
|
1998-03-25 21:52:29 +03:00
|
|
|
*S->top++ = consts[aux];
|
|
|
|
luaV_closure(*pc++);
|
1997-09-16 23:25:59 +04:00
|
|
|
luaC_checkGC();
|
|
|
|
break;
|
|
|
|
|
1999-02-02 20:57:49 +03:00
|
|
|
case CALLFUNC: aux = *pc++; {
|
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;
|
|
|
|
}
|
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case SETLINEW: aux += highbyte(*pc++);
|
1999-02-02 20:57:49 +03:00
|
|
|
case SETLINE: aux += *pc++;
|
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;
|
1999-02-04 20:47:59 +03:00
|
|
|
if (L->linehook)
|
1997-10-14 02:12:04 +04:00
|
|
|
luaD_lineHook(aux);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
|
1999-02-04 19:36:16 +03:00
|
|
|
case LONGARG:
|
|
|
|
aux = highbyte(highbyte(*pc++));
|
|
|
|
goto switchentry; /* do not reset "aux" */
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|