1993-07-28 17:18:00 +04:00
|
|
|
/*
|
|
|
|
** opcode.c
|
|
|
|
** TecCGraf - PUC-Rio
|
|
|
|
*/
|
|
|
|
|
1995-10-25 16:05:51 +03:00
|
|
|
char *rcs_opcode="$Id: opcode.c,v 3.46 1995/10/17 14:30:05 roberto Exp roberto $";
|
1993-12-17 21:41:19 +03:00
|
|
|
|
1994-11-16 20:39:16 +03:00
|
|
|
#include <setjmp.h>
|
1995-02-06 22:35:09 +03:00
|
|
|
#include <stdlib.h>
|
1993-07-28 17:18:00 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
1994-10-17 22:00:40 +03:00
|
|
|
#include <math.h>
|
1993-07-28 17:18:00 +04:00
|
|
|
|
1995-10-17 17:12:45 +03:00
|
|
|
#include "luadebug.h"
|
1994-11-16 20:39:16 +03:00
|
|
|
#include "mem.h"
|
1993-07-28 17:18:00 +04:00
|
|
|
#include "opcode.h"
|
|
|
|
#include "hash.h"
|
|
|
|
#include "inout.h"
|
|
|
|
#include "table.h"
|
|
|
|
#include "lua.h"
|
1994-11-07 18:20:56 +03:00
|
|
|
#include "fallback.h"
|
1993-07-28 17:18:00 +04:00
|
|
|
|
1994-11-02 23:30:53 +03:00
|
|
|
#define tonumber(o) ((tag(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
|
|
|
|
#define tostring(o) ((tag(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
|
1993-07-28 17:18:00 +04:00
|
|
|
|
1994-07-20 01:27:18 +04:00
|
|
|
|
1995-04-11 21:56:30 +04:00
|
|
|
#define STACK_SIZE 128
|
1994-07-20 01:27:18 +04:00
|
|
|
|
1994-12-30 20:45:11 +03:00
|
|
|
typedef int StkId; /* index to stack elements */
|
1994-12-21 00:20:36 +03:00
|
|
|
|
1995-04-11 21:56:30 +04:00
|
|
|
static Object initial_stack;
|
|
|
|
|
|
|
|
static Object *stackLimit = &initial_stack+1;
|
|
|
|
static Object *stack = &initial_stack;
|
|
|
|
static Object *top = &initial_stack;
|
1994-11-02 23:30:53 +03:00
|
|
|
|
1994-11-04 13:47:49 +03:00
|
|
|
|
1994-11-07 19:34:44 +03:00
|
|
|
/* macros to convert from lua_Object to (Object *) and back */
|
|
|
|
|
|
|
|
#define Address(lo) ((lo)+stack-1)
|
|
|
|
#define Ref(st) ((st)-stack+1)
|
|
|
|
|
|
|
|
|
1995-04-11 21:56:30 +04:00
|
|
|
/* macro to increment stack top. There must be always an empty slot in
|
|
|
|
* the stack
|
|
|
|
*/
|
|
|
|
#define incr_top if (++top >= stackLimit) growstack()
|
|
|
|
|
1994-12-21 00:20:36 +03:00
|
|
|
static StkId CBase = 0; /* when Lua calls C or C calls Lua, points to */
|
|
|
|
/* the first slot after the last parameter. */
|
1994-11-02 23:30:53 +03:00
|
|
|
static int CnResults = 0; /* when Lua calls C, has the number of parameters; */
|
1994-11-04 13:47:49 +03:00
|
|
|
/* when C calls Lua, has the number of results. */
|
|
|
|
|
|
|
|
static jmp_buf *errorJmp = NULL; /* current error recover point */
|
1994-11-02 23:30:53 +03:00
|
|
|
|
|
|
|
|
1994-12-21 00:20:36 +03:00
|
|
|
static StkId lua_execute (Byte *pc, StkId base);
|
1995-10-09 16:10:20 +03:00
|
|
|
static void do_call (StkId base, int nResults);
|
1994-11-02 23:30:53 +03:00
|
|
|
|
|
|
|
|
1994-11-10 20:11:52 +03:00
|
|
|
|
1994-11-07 19:34:44 +03:00
|
|
|
Object *luaI_Address (lua_Object o)
|
|
|
|
{
|
|
|
|
return Address(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
|
|
|
|
/*
|
1994-07-20 01:27:18 +04:00
|
|
|
** Init stack
|
1993-07-28 17:18:00 +04:00
|
|
|
*/
|
1994-11-02 23:30:53 +03:00
|
|
|
static void lua_initstack (void)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1995-04-11 21:56:30 +04:00
|
|
|
Long maxstack = STACK_SIZE;
|
1994-11-16 20:39:16 +03:00
|
|
|
stack = newvector(maxstack, Object);
|
1995-04-11 21:56:30 +04:00
|
|
|
stackLimit = stack+maxstack;
|
1994-11-02 23:30:53 +03:00
|
|
|
top = stack;
|
1995-10-25 16:05:51 +03:00
|
|
|
*(top++) = initial_stack;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
1994-07-20 01:27:18 +04:00
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
/*
|
1994-07-20 01:27:18 +04:00
|
|
|
** Check stack overflow and, if necessary, realloc vector
|
|
|
|
*/
|
1995-04-11 21:56:30 +04:00
|
|
|
#define lua_checkstack(nt) if ((nt) >= stackLimit) growstack()
|
1995-01-27 20:19:06 +03:00
|
|
|
|
1995-04-11 21:56:30 +04:00
|
|
|
static void growstack (void)
|
1994-07-20 01:27:18 +04:00
|
|
|
{
|
1995-04-11 21:56:30 +04:00
|
|
|
if (stack == &initial_stack)
|
1995-01-27 20:19:06 +03:00
|
|
|
lua_initstack();
|
1995-04-11 21:56:30 +04:00
|
|
|
else
|
|
|
|
{
|
1995-10-25 16:05:51 +03:00
|
|
|
StkId t = top-stack;
|
1995-04-11 21:56:30 +04:00
|
|
|
Long maxstack = stackLimit - stack;
|
|
|
|
maxstack *= 2;
|
|
|
|
stack = growvector(stack, maxstack, Object);
|
|
|
|
stackLimit = stack+maxstack;
|
1995-10-25 16:05:51 +03:00
|
|
|
top = stack + t;
|
1995-10-09 21:45:59 +03:00
|
|
|
if (maxstack >= MAX_WORD/2)
|
|
|
|
lua_error("stack size overflow");
|
1995-04-11 21:56:30 +04:00
|
|
|
}
|
1994-07-20 01:27:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1994-11-16 19:03:48 +03:00
|
|
|
** Concatenate two given strings. Return the new string pointer.
|
1993-07-28 17:18:00 +04:00
|
|
|
*/
|
1994-07-20 01:27:18 +04:00
|
|
|
static char *lua_strconc (char *l, char *r)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-11-07 18:20:56 +03:00
|
|
|
static char *buffer = NULL;
|
|
|
|
static int buffer_size = 0;
|
1994-11-16 19:03:48 +03:00
|
|
|
int nl = strlen(l);
|
|
|
|
int n = nl+strlen(r)+1;
|
1994-11-07 18:20:56 +03:00
|
|
|
if (n > buffer_size)
|
1994-11-16 20:39:16 +03:00
|
|
|
{
|
1994-11-07 18:20:56 +03:00
|
|
|
buffer_size = n;
|
|
|
|
if (buffer != NULL)
|
1994-11-16 20:39:16 +03:00
|
|
|
luaI_free(buffer);
|
|
|
|
buffer = newvector(buffer_size, char);
|
1994-11-07 18:20:56 +03:00
|
|
|
}
|
1994-11-16 19:03:48 +03:00
|
|
|
strcpy(buffer,l);
|
|
|
|
strcpy(buffer+nl, r);
|
|
|
|
return buffer;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
1994-09-20 19:11:11 +04:00
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
/*
|
1994-09-20 19:11:11 +04:00
|
|
|
** Convert, if possible, to a number object.
|
|
|
|
** Return 0 if success, not 0 if error.
|
1994-11-02 23:30:53 +03:00
|
|
|
*/
|
1993-07-28 17:18:00 +04:00
|
|
|
static int lua_tonumber (Object *obj)
|
|
|
|
{
|
1994-11-04 13:47:49 +03:00
|
|
|
float t;
|
1994-11-22 19:02:53 +03:00
|
|
|
char c;
|
1994-11-02 23:30:53 +03:00
|
|
|
if (tag(obj) != LUA_T_STRING)
|
1994-11-04 13:47:49 +03:00
|
|
|
return 1;
|
1994-11-22 19:02:53 +03:00
|
|
|
else if (sscanf(svalue(obj), "%f %c",&t, &c) == 1)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-11-04 13:47:49 +03:00
|
|
|
nvalue(obj) = t;
|
|
|
|
tag(obj) = LUA_T_NUMBER;
|
|
|
|
return 0;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
1994-11-02 23:30:53 +03:00
|
|
|
else
|
1994-11-04 13:47:49 +03:00
|
|
|
return 2;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Convert, if possible, to a string tag
|
|
|
|
** Return 0 in success or not 0 on error.
|
1994-11-02 23:30:53 +03:00
|
|
|
*/
|
1993-07-28 17:18:00 +04:00
|
|
|
static int lua_tostring (Object *obj)
|
|
|
|
{
|
|
|
|
static char s[256];
|
1994-11-02 23:30:53 +03:00
|
|
|
if (tag(obj) != LUA_T_NUMBER)
|
1994-11-07 18:20:56 +03:00
|
|
|
return 1;
|
1993-07-28 17:18:00 +04:00
|
|
|
if ((int) nvalue(obj) == nvalue(obj))
|
1994-11-07 18:20:56 +03:00
|
|
|
sprintf (s, "%d", (int) nvalue(obj));
|
1993-07-28 17:18:00 +04:00
|
|
|
else
|
1994-11-07 18:20:56 +03:00
|
|
|
sprintf (s, "%g", nvalue(obj));
|
1994-11-23 17:32:00 +03:00
|
|
|
tsvalue(obj) = lua_createstring(s);
|
|
|
|
if (tsvalue(obj) == NULL)
|
1993-07-28 17:18:00 +04:00
|
|
|
return 1;
|
1994-11-02 23:30:53 +03:00
|
|
|
tag(obj) = LUA_T_STRING;
|
1993-07-28 17:18:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1994-11-02 23:30:53 +03:00
|
|
|
** Adjust stack. Set top to the given value, pushing NILs if needed.
|
1993-07-28 17:18:00 +04:00
|
|
|
*/
|
1994-12-21 00:20:36 +03:00
|
|
|
static void adjust_top (StkId newtop)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1995-02-10 15:51:29 +03:00
|
|
|
Object *nt;
|
1995-04-11 21:56:30 +04:00
|
|
|
lua_checkstack(stack+newtop);
|
|
|
|
nt = stack+newtop; /* warning: previous call may change stack */
|
1994-11-11 17:00:08 +03:00
|
|
|
while (top < nt) tag(top++) = LUA_T_NIL;
|
|
|
|
top = nt; /* top could be bigger than newtop */
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
1994-07-20 01:27:18 +04:00
|
|
|
|
|
|
|
|
1994-11-07 21:27:39 +03:00
|
|
|
static void adjustC (int nParams)
|
|
|
|
{
|
1994-11-11 17:00:08 +03:00
|
|
|
adjust_top(CBase+nParams);
|
1994-11-07 21:27:39 +03:00
|
|
|
}
|
|
|
|
|
1995-10-09 16:10:20 +03:00
|
|
|
/*
|
|
|
|
** Open a hole below "nelems" from the top.
|
|
|
|
*/
|
|
|
|
static void open_stack (int nelems)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<nelems; i++)
|
|
|
|
*(top-i) = *(top-i-1);
|
|
|
|
incr_top;
|
|
|
|
}
|
|
|
|
|
1994-11-07 21:27:39 +03:00
|
|
|
|
1994-11-02 23:30:53 +03:00
|
|
|
/*
|
|
|
|
** Call a C function. CBase will point to the top of the stack,
|
|
|
|
** and CnResults is the number of parameters. Returns an index
|
|
|
|
** to the first result from C.
|
|
|
|
*/
|
1994-12-21 00:20:36 +03:00
|
|
|
static StkId callC (lua_CFunction func, StkId base)
|
1994-11-02 23:30:53 +03:00
|
|
|
{
|
1994-12-21 00:20:36 +03:00
|
|
|
StkId oldBase = CBase;
|
1994-11-02 23:30:53 +03:00
|
|
|
int oldCnResults = CnResults;
|
1994-12-21 00:20:36 +03:00
|
|
|
StkId firstResult;
|
1994-11-02 23:30:53 +03:00
|
|
|
CnResults = (top-stack) - base;
|
1994-11-04 13:47:49 +03:00
|
|
|
/* incorporate parameters on the stack */
|
|
|
|
CBase = base+CnResults;
|
1994-11-02 23:30:53 +03:00
|
|
|
(*func)();
|
|
|
|
firstResult = CBase;
|
|
|
|
CBase = oldBase;
|
|
|
|
CnResults = oldCnResults;
|
|
|
|
return firstResult;
|
|
|
|
}
|
|
|
|
|
1995-10-09 16:10:20 +03:00
|
|
|
/*
|
|
|
|
** Call the specified fallback, putting it on the stack below its arguments
|
|
|
|
*/
|
|
|
|
static void callFB (int fb)
|
|
|
|
{
|
|
|
|
int nParams = luaI_fallBacks[fb].nParams;
|
|
|
|
open_stack(nParams);
|
|
|
|
*(top-nParams-1) = luaI_fallBacks[fb].function;
|
|
|
|
do_call((top-stack)-nParams, luaI_fallBacks[fb].nResults);
|
|
|
|
}
|
|
|
|
|
1994-11-02 23:30:53 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Call a function (C or Lua). The parameters must be on the stack,
|
1995-10-09 16:10:20 +03:00
|
|
|
** between [stack+base,top). The function to be called is at stack+base-1.
|
|
|
|
** When returns, the results are on the stack, between [stack+base-1,top).
|
|
|
|
** The number of results is nResults, unless nResults=MULT_RET.
|
1994-11-02 23:30:53 +03:00
|
|
|
*/
|
1995-10-09 16:10:20 +03:00
|
|
|
static void do_call (StkId base, int nResults)
|
1994-11-02 23:30:53 +03:00
|
|
|
{
|
1994-12-21 00:20:36 +03:00
|
|
|
StkId firstResult;
|
1995-10-09 16:10:20 +03:00
|
|
|
Object *func = stack+base-1;
|
1995-10-25 16:05:51 +03:00
|
|
|
int i;
|
1994-11-02 23:30:53 +03:00
|
|
|
if (tag(func) == LUA_T_CFUNCTION)
|
1995-10-17 14:58:41 +03:00
|
|
|
{
|
|
|
|
tag(func) = LUA_T_CMARK;
|
1994-11-02 23:30:53 +03:00
|
|
|
firstResult = callC(fvalue(func), base);
|
1995-10-17 14:58:41 +03:00
|
|
|
}
|
1994-11-02 23:30:53 +03:00
|
|
|
else if (tag(func) == LUA_T_FUNCTION)
|
1995-10-17 14:58:41 +03:00
|
|
|
{
|
|
|
|
tag(func) = LUA_T_MARK;
|
1995-10-04 20:13:02 +03:00
|
|
|
firstResult = lua_execute(func->value.tf->code, base);
|
1995-10-17 14:58:41 +03:00
|
|
|
}
|
1994-11-02 23:30:53 +03:00
|
|
|
else
|
1994-11-21 21:22:58 +03:00
|
|
|
{ /* func is not a function */
|
1995-10-25 16:05:51 +03:00
|
|
|
/* Call the fallback for invalid functions */
|
|
|
|
open_stack((top-stack)-(base-1));
|
|
|
|
stack[base-1] = luaI_fallBacks[FB_FUNCTION].function;
|
|
|
|
do_call(base, nResults);
|
1994-11-21 21:22:58 +03:00
|
|
|
return;
|
1994-11-09 21:13:29 +03:00
|
|
|
}
|
1994-11-02 23:30:53 +03:00
|
|
|
/* adjust the number of results */
|
|
|
|
if (nResults != MULT_RET && top - (stack+firstResult) != nResults)
|
1994-11-11 17:00:08 +03:00
|
|
|
adjust_top(firstResult+nResults);
|
1995-10-09 16:10:20 +03:00
|
|
|
/* move results to base-1 (to erase parameters and function) */
|
|
|
|
base--;
|
1995-10-25 16:05:51 +03:00
|
|
|
nResults = top - (stack+firstResult); /* actual number of results */
|
|
|
|
for (i=0; i<nResults; i++)
|
|
|
|
*(stack+base+i) = *(stack+firstResult+i);
|
|
|
|
top -= firstResult-base;
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1994-11-04 13:47:49 +03:00
|
|
|
** Function to index a table. Receives the table at top-2 and the index
|
1994-11-07 21:27:39 +03:00
|
|
|
** at top-1.
|
1994-11-02 23:30:53 +03:00
|
|
|
*/
|
1994-11-04 13:47:49 +03:00
|
|
|
static void pushsubscript (void)
|
1994-11-02 23:30:53 +03:00
|
|
|
{
|
1994-11-04 13:47:49 +03:00
|
|
|
if (tag(top-2) != LUA_T_ARRAY)
|
1995-10-09 16:10:20 +03:00
|
|
|
callFB(FB_GETTABLE);
|
1994-11-07 18:20:56 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Object *h = lua_hashget(avalue(top-2), top-1);
|
1994-12-16 19:08:34 +03:00
|
|
|
if (h == NULL || tag(h) == LUA_T_NIL)
|
1995-10-09 16:10:20 +03:00
|
|
|
callFB(FB_INDEX);
|
1994-11-07 18:20:56 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
--top;
|
|
|
|
*(top-1) = *h;
|
|
|
|
}
|
|
|
|
}
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Function to store indexed based on values at the top
|
|
|
|
*/
|
1994-11-07 18:20:56 +03:00
|
|
|
static void storesubscript (void)
|
1994-11-02 23:30:53 +03:00
|
|
|
{
|
|
|
|
if (tag(top-3) != LUA_T_ARRAY)
|
1995-10-09 16:10:20 +03:00
|
|
|
callFB(FB_SETTABLE);
|
1994-11-07 18:20:56 +03:00
|
|
|
else
|
1994-11-02 23:30:53 +03:00
|
|
|
{
|
|
|
|
Object *h = lua_hashdefine (avalue(top-3), top-2);
|
|
|
|
*h = *(top-1);
|
1994-11-07 18:20:56 +03:00
|
|
|
top -= 3;
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Traverse all objects on stack
|
|
|
|
*/
|
1995-10-17 14:58:41 +03:00
|
|
|
void lua_travstack (int (*fn)(Object *))
|
1994-11-02 23:30:53 +03:00
|
|
|
{
|
|
|
|
Object *o;
|
|
|
|
for (o = top-1; o >= stack; o--)
|
1995-10-17 14:58:41 +03:00
|
|
|
fn (o);
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1995-10-17 14:58:41 +03:00
|
|
|
** Error messages and debug functions
|
1995-10-09 16:10:20 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static void lua_message (char *s)
|
|
|
|
{
|
1995-10-17 14:58:41 +03:00
|
|
|
lua_pushstring(s);
|
1995-10-09 16:10:20 +03:00
|
|
|
callFB(FB_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Reports an error, and jumps up to the available recover label
|
|
|
|
*/
|
|
|
|
void lua_error (char *s)
|
|
|
|
{
|
|
|
|
if (s) lua_message(s);
|
|
|
|
if (errorJmp)
|
|
|
|
longjmp(*errorJmp, 1);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf (stderr, "lua: exit(1). Unable to recover\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-10-17 17:12:45 +03:00
|
|
|
lua_Object lua_stackedfunction (int level)
|
1995-10-17 14:58:41 +03:00
|
|
|
{
|
|
|
|
Object *p = top;
|
|
|
|
while (--p >= stack)
|
|
|
|
if (p->tag == LUA_T_MARK || p->tag == LUA_T_CMARK)
|
|
|
|
if (level-- == 0)
|
|
|
|
return Ref(p);
|
|
|
|
return LUA_NOOBJECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-10-17 17:12:45 +03:00
|
|
|
void lua_funcinfo (lua_Object func, char **filename, char **funcname,
|
1995-10-25 16:05:51 +03:00
|
|
|
char **objname, int *line)
|
1995-10-17 14:58:41 +03:00
|
|
|
{
|
1995-10-25 16:05:51 +03:00
|
|
|
Object *f = Address(func);
|
|
|
|
luaI_funcInfo(f, filename, funcname, objname, line);
|
|
|
|
*line = (f+1 < top && (f+1)->tag == LUA_T_LINE) ?
|
|
|
|
(f+1)->value.i : -1;
|
1995-10-17 14:58:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1995-10-09 16:10:20 +03:00
|
|
|
/*
|
|
|
|
** Execute a protected call. Assumes that function is at CBase and
|
|
|
|
** parameters are on top of it. Leave nResults on the stack.
|
1994-11-02 23:30:53 +03:00
|
|
|
*/
|
1995-10-09 16:10:20 +03:00
|
|
|
static int do_protectedrun (int nResults)
|
1994-11-02 23:30:53 +03:00
|
|
|
{
|
|
|
|
jmp_buf myErrorJmp;
|
|
|
|
int status;
|
1994-12-21 00:20:36 +03:00
|
|
|
StkId oldCBase = CBase;
|
1994-11-02 23:30:53 +03:00
|
|
|
jmp_buf *oldErr = errorJmp;
|
|
|
|
errorJmp = &myErrorJmp;
|
|
|
|
if (setjmp(myErrorJmp) == 0)
|
|
|
|
{
|
1995-10-09 16:10:20 +03:00
|
|
|
do_call(CBase+1, nResults);
|
1994-11-04 13:47:49 +03:00
|
|
|
CnResults = (top-stack) - CBase; /* number of results */
|
|
|
|
CBase += CnResults; /* incorporate results on the stack */
|
1994-11-02 23:30:53 +03:00
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
else
|
1995-10-09 16:10:20 +03:00
|
|
|
{ /* an error occurred: restore CBase and top */
|
1994-11-04 13:47:49 +03:00
|
|
|
CBase = oldCBase;
|
|
|
|
top = stack+CBase;
|
1994-11-02 23:30:53 +03:00
|
|
|
status = 1;
|
1994-11-04 13:47:49 +03:00
|
|
|
}
|
1994-11-02 23:30:53 +03:00
|
|
|
errorJmp = oldErr;
|
|
|
|
return status;
|
|
|
|
}
|
1993-07-28 17:18:00 +04:00
|
|
|
|
1994-11-09 21:13:29 +03:00
|
|
|
|
|
|
|
static int do_protectedmain (void)
|
|
|
|
{
|
1995-10-04 20:13:02 +03:00
|
|
|
TFunc tf;
|
1994-11-09 21:13:29 +03:00
|
|
|
int status;
|
|
|
|
jmp_buf myErrorJmp;
|
|
|
|
jmp_buf *oldErr = errorJmp;
|
|
|
|
errorJmp = &myErrorJmp;
|
1995-10-09 16:10:20 +03:00
|
|
|
adjustC(1); /* one slot for the pseudo-function */
|
|
|
|
stack[CBase].tag = LUA_T_FUNCTION;
|
|
|
|
stack[CBase].value.tf = &tf;
|
1995-10-17 14:58:41 +03:00
|
|
|
tf.lineDefined = 0;
|
|
|
|
tf.name1 = tf.name2 = NULL;
|
|
|
|
tf.fileName = lua_parsedfile;
|
1995-10-04 20:13:02 +03:00
|
|
|
tf.code = NULL;
|
1994-11-09 21:13:29 +03:00
|
|
|
if (setjmp(myErrorJmp) == 0)
|
|
|
|
{
|
1995-10-04 20:13:02 +03:00
|
|
|
lua_parse(&tf);
|
1995-10-09 16:10:20 +03:00
|
|
|
status = do_protectedrun(0);
|
1994-11-09 21:13:29 +03:00
|
|
|
}
|
|
|
|
else
|
1995-10-09 16:10:20 +03:00
|
|
|
{
|
1994-11-09 21:13:29 +03:00
|
|
|
status = 1;
|
1995-10-09 16:10:20 +03:00
|
|
|
adjustC(0); /* erase extra slot */
|
|
|
|
}
|
|
|
|
errorJmp = oldErr;
|
1995-10-04 20:13:02 +03:00
|
|
|
if (tf.code)
|
|
|
|
luaI_free(tf.code);
|
1994-11-09 21:13:29 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
/*
|
1994-11-04 13:47:49 +03:00
|
|
|
** Execute the given lua function. Return 0 on success or 1 on error.
|
|
|
|
*/
|
1994-11-07 19:34:44 +03:00
|
|
|
int lua_callfunction (lua_Object function)
|
1994-11-04 13:47:49 +03:00
|
|
|
{
|
1995-02-02 23:05:37 +03:00
|
|
|
if (function == LUA_NOOBJECT)
|
1994-11-04 13:47:49 +03:00
|
|
|
return 1;
|
|
|
|
else
|
1995-10-09 16:10:20 +03:00
|
|
|
{
|
|
|
|
open_stack((top-stack)-CBase);
|
|
|
|
stack[CBase] = *Address(function);
|
|
|
|
return do_protectedrun (MULT_RET);
|
|
|
|
}
|
1994-11-04 13:47:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-11-09 21:13:29 +03:00
|
|
|
int lua_call (char *funcname)
|
|
|
|
{
|
1994-12-21 00:20:36 +03:00
|
|
|
Word n = luaI_findsymbolbyname(funcname);
|
1995-10-09 16:10:20 +03:00
|
|
|
open_stack((top-stack)-CBase);
|
|
|
|
stack[CBase] = s_object(n);
|
|
|
|
return do_protectedrun(MULT_RET);
|
1994-11-09 21:13:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-11-04 13:47:49 +03:00
|
|
|
/*
|
|
|
|
** Open file, generate opcode and execute global statement. Return 0 on
|
1993-07-28 17:18:00 +04:00
|
|
|
** success or 1 on error.
|
|
|
|
*/
|
1994-11-04 13:47:49 +03:00
|
|
|
int lua_dofile (char *filename)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-11-02 23:30:53 +03:00
|
|
|
int status;
|
1994-11-04 13:47:49 +03:00
|
|
|
char *message = lua_openfile (filename);
|
|
|
|
if (message)
|
1994-11-02 23:30:53 +03:00
|
|
|
{
|
1994-11-04 13:47:49 +03:00
|
|
|
lua_message(message);
|
|
|
|
return 1;
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
1994-11-09 21:13:29 +03:00
|
|
|
status = do_protectedmain();
|
1994-11-04 13:47:49 +03:00
|
|
|
lua_closefile();
|
1994-11-02 23:30:53 +03:00
|
|
|
return status;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
1994-08-03 18:15:46 +04:00
|
|
|
/*
|
1994-11-04 13:47:49 +03:00
|
|
|
** Generate opcode stored on string and execute global statement. Return 0 on
|
|
|
|
** success or 1 on error.
|
1994-08-03 18:15:46 +04:00
|
|
|
*/
|
1994-11-04 13:47:49 +03:00
|
|
|
int lua_dostring (char *string)
|
1994-08-03 18:15:46 +04:00
|
|
|
{
|
1994-11-02 23:30:53 +03:00
|
|
|
int status;
|
1995-10-17 14:58:41 +03:00
|
|
|
lua_openstring(string);
|
1994-11-09 21:13:29 +03:00
|
|
|
status = do_protectedmain();
|
1994-11-04 13:47:49 +03:00
|
|
|
lua_closestring();
|
1994-11-02 23:30:53 +03:00
|
|
|
return status;
|
1994-08-03 18:15:46 +04:00
|
|
|
}
|
|
|
|
|
1994-11-04 13:47:49 +03:00
|
|
|
|
1994-11-07 21:27:39 +03:00
|
|
|
/*
|
|
|
|
** API: set a function as a fallback
|
|
|
|
*/
|
|
|
|
lua_Object lua_setfallback (char *name, lua_CFunction fallback)
|
|
|
|
{
|
1995-10-09 16:10:20 +03:00
|
|
|
adjustC(1); /* one slot for the pseudo-function */
|
|
|
|
stack[CBase].tag = LUA_T_CFUNCTION;
|
|
|
|
stack[CBase].value.f = luaI_setfallback;
|
1994-11-07 21:27:39 +03:00
|
|
|
lua_pushstring(name);
|
|
|
|
lua_pushcfunction(fallback);
|
1995-10-17 17:30:05 +03:00
|
|
|
if (do_protectedrun(1) == 0)
|
|
|
|
return (Ref(top-1));
|
|
|
|
else
|
|
|
|
return LUA_NOOBJECT;
|
1994-11-07 21:27:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** API: receives on the stack the table and the index.
|
|
|
|
** returns the value.
|
|
|
|
*/
|
|
|
|
lua_Object lua_getsubscript (void)
|
|
|
|
{
|
|
|
|
adjustC(2);
|
1994-12-27 23:53:15 +03:00
|
|
|
pushsubscript();
|
|
|
|
CBase++; /* incorporate object in the stack */
|
|
|
|
return (Ref(top-1));
|
1994-11-07 21:27:39 +03:00
|
|
|
}
|
|
|
|
|
1994-11-18 22:46:21 +03:00
|
|
|
|
|
|
|
#define MAX_C_BLOCKS 10
|
|
|
|
|
|
|
|
static int numCblocks = 0;
|
1994-12-21 00:20:36 +03:00
|
|
|
static StkId Cblocks[MAX_C_BLOCKS];
|
1994-11-18 22:46:21 +03:00
|
|
|
|
1994-11-17 19:41:42 +03:00
|
|
|
/*
|
|
|
|
** API: starts a new block
|
|
|
|
*/
|
1994-11-18 22:46:21 +03:00
|
|
|
void lua_beginblock (void)
|
1994-11-17 19:41:42 +03:00
|
|
|
{
|
1994-11-18 22:46:21 +03:00
|
|
|
if (numCblocks < MAX_C_BLOCKS)
|
|
|
|
Cblocks[numCblocks] = CBase;
|
|
|
|
numCblocks++;
|
1994-11-17 19:41:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** API: ends a block
|
|
|
|
*/
|
1994-11-18 22:46:21 +03:00
|
|
|
void lua_endblock (void)
|
1994-11-17 19:41:42 +03:00
|
|
|
{
|
1994-11-18 22:46:21 +03:00
|
|
|
--numCblocks;
|
|
|
|
if (numCblocks < MAX_C_BLOCKS)
|
|
|
|
{
|
|
|
|
CBase = Cblocks[numCblocks];
|
|
|
|
adjustC(0);
|
|
|
|
}
|
1994-11-17 19:41:42 +03:00
|
|
|
}
|
|
|
|
|
1994-11-07 21:27:39 +03:00
|
|
|
/*
|
|
|
|
** API: receives on the stack the table, the index, and the new value.
|
|
|
|
*/
|
1994-12-28 15:55:47 +03:00
|
|
|
void lua_storesubscript (void)
|
1994-11-07 21:27:39 +03:00
|
|
|
{
|
|
|
|
adjustC(3);
|
1994-12-27 23:53:15 +03:00
|
|
|
storesubscript();
|
1994-11-07 21:27:39 +03:00
|
|
|
}
|
|
|
|
|
1994-11-13 19:17:04 +03:00
|
|
|
/*
|
|
|
|
** API: creates a new table
|
|
|
|
*/
|
1994-12-16 18:56:45 +03:00
|
|
|
lua_Object lua_createtable (void)
|
1994-11-13 19:17:04 +03:00
|
|
|
{
|
|
|
|
adjustC(0);
|
1994-12-16 18:56:45 +03:00
|
|
|
avalue(top) = lua_createarray(0);
|
1994-11-17 16:58:57 +03:00
|
|
|
tag(top) = LUA_T_ARRAY;
|
1995-04-11 21:56:30 +04:00
|
|
|
incr_top;
|
1994-11-13 19:17:04 +03:00
|
|
|
CBase++; /* incorporate object in the stack */
|
|
|
|
return Ref(top-1);
|
|
|
|
}
|
1994-11-07 21:27:39 +03:00
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
/*
|
1994-12-16 18:56:45 +03:00
|
|
|
** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
|
1993-07-28 17:18:00 +04:00
|
|
|
** 'number' must be 1 to get the first parameter.
|
|
|
|
*/
|
1994-11-07 19:34:44 +03:00
|
|
|
lua_Object lua_getparam (int number)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-12-16 18:56:45 +03:00
|
|
|
if (number <= 0 || number > CnResults) return LUA_NOOBJECT;
|
1994-11-07 19:34:44 +03:00
|
|
|
/* Ref(stack+(CBase-CnResults+number-1)) ==
|
|
|
|
stack+(CBase-CnResults+number-1)-stack+1 == */
|
|
|
|
return CBase-CnResults+number;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Given an object handle, return its number value. On error, return 0.0.
|
|
|
|
*/
|
1994-11-07 19:34:44 +03:00
|
|
|
real lua_getnumber (lua_Object object)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1995-10-25 16:05:51 +03:00
|
|
|
if (object == LUA_NOOBJECT) return 0.0;
|
1994-11-07 19:34:44 +03:00
|
|
|
if (tonumber (Address(object))) return 0.0;
|
1995-10-25 16:05:51 +03:00
|
|
|
else return (nvalue(Address(object)));
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Given an object handle, return its string pointer. On error, return NULL.
|
|
|
|
*/
|
1994-11-07 19:34:44 +03:00
|
|
|
char *lua_getstring (lua_Object object)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1995-10-25 16:05:51 +03:00
|
|
|
if (object == LUA_NOOBJECT) return NULL;
|
1994-11-07 19:34:44 +03:00
|
|
|
if (tostring (Address(object))) return NULL;
|
|
|
|
else return (svalue(Address(object)));
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Given an object handle, return its cfuntion pointer. On error, return NULL.
|
|
|
|
*/
|
1994-11-07 19:34:44 +03:00
|
|
|
lua_CFunction lua_getcfunction (lua_Object object)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-12-16 18:56:45 +03:00
|
|
|
if (object == LUA_NOOBJECT || tag(Address(object)) != LUA_T_CFUNCTION)
|
|
|
|
return NULL;
|
1994-11-07 19:34:44 +03:00
|
|
|
else return (fvalue(Address(object)));
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Given an object handle, return its user data. On error, return NULL.
|
|
|
|
*/
|
1994-11-07 19:34:44 +03:00
|
|
|
void *lua_getuserdata (lua_Object object)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-12-16 18:56:45 +03:00
|
|
|
if (object == LUA_NOOBJECT || tag(Address(object)) < LUA_T_USERDATA)
|
|
|
|
return NULL;
|
1994-11-07 19:34:44 +03:00
|
|
|
else return (uvalue(Address(object)));
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
1994-11-08 22:56:39 +03:00
|
|
|
|
|
|
|
lua_Object lua_getlocked (int ref)
|
|
|
|
{
|
|
|
|
adjustC(0);
|
1994-11-16 20:39:16 +03:00
|
|
|
*top = *luaI_getlocked(ref);
|
1995-04-11 21:56:30 +04:00
|
|
|
incr_top;
|
1994-11-08 22:56:39 +03:00
|
|
|
CBase++; /* incorporate object in the stack */
|
|
|
|
return Ref(top-1);
|
|
|
|
}
|
|
|
|
|
1994-11-18 22:46:21 +03:00
|
|
|
|
1995-01-27 20:19:06 +03:00
|
|
|
void lua_pushlocked (int ref)
|
|
|
|
{
|
|
|
|
*top = *luaI_getlocked(ref);
|
1995-04-11 21:56:30 +04:00
|
|
|
incr_top;
|
1995-01-27 20:19:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-11-18 22:46:21 +03:00
|
|
|
int lua_lock (void)
|
|
|
|
{
|
|
|
|
adjustC(1);
|
|
|
|
return luaI_lock(--top);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-10-09 16:10:20 +03:00
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
/*
|
1995-10-09 16:10:20 +03:00
|
|
|
** Get a global object.
|
1993-07-28 17:18:00 +04:00
|
|
|
*/
|
1994-11-07 19:34:44 +03:00
|
|
|
lua_Object lua_getglobal (char *name)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-12-21 00:20:36 +03:00
|
|
|
Word n = luaI_findsymbolbyname(name);
|
1994-11-08 22:56:39 +03:00
|
|
|
adjustC(0);
|
1994-11-16 20:39:16 +03:00
|
|
|
*top = s_object(n);
|
1995-04-11 21:56:30 +04:00
|
|
|
incr_top;
|
1994-11-08 22:56:39 +03:00
|
|
|
CBase++; /* incorporate object in the stack */
|
1994-11-07 19:34:44 +03:00
|
|
|
return Ref(top-1);
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
1994-11-07 21:27:39 +03:00
|
|
|
/*
|
|
|
|
** Store top of the stack at a global variable array field.
|
|
|
|
*/
|
1994-12-28 15:55:47 +03:00
|
|
|
void lua_storeglobal (char *name)
|
1994-11-07 21:27:39 +03:00
|
|
|
{
|
1994-12-21 00:20:36 +03:00
|
|
|
Word n = luaI_findsymbolbyname(name);
|
1994-11-07 21:27:39 +03:00
|
|
|
adjustC(1);
|
|
|
|
s_object(n) = *(--top);
|
|
|
|
}
|
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
/*
|
|
|
|
** Push a nil object
|
|
|
|
*/
|
1994-12-28 15:55:47 +03:00
|
|
|
void lua_pushnil (void)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1995-04-11 21:56:30 +04:00
|
|
|
tag(top) = LUA_T_NIL;
|
|
|
|
incr_top;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-12-28 15:55:47 +03:00
|
|
|
** Push an object (tag=number) to stack.
|
1993-07-28 17:18:00 +04:00
|
|
|
*/
|
1994-12-28 15:55:47 +03:00
|
|
|
void lua_pushnumber (real n)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1995-04-11 21:56:30 +04:00
|
|
|
tag(top) = LUA_T_NUMBER; nvalue(top) = n;
|
|
|
|
incr_top;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-12-28 15:55:47 +03:00
|
|
|
** Push an object (tag=string) to stack.
|
1993-07-28 17:18:00 +04:00
|
|
|
*/
|
1994-12-28 15:55:47 +03:00
|
|
|
void lua_pushstring (char *s)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-11-23 17:32:00 +03:00
|
|
|
tsvalue(top) = lua_createstring(s);
|
1994-11-02 23:30:53 +03:00
|
|
|
tag(top) = LUA_T_STRING;
|
1995-04-11 21:56:30 +04:00
|
|
|
incr_top;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
1994-12-13 18:54:21 +03:00
|
|
|
/*
|
|
|
|
** Push an object (tag=string) on stack and register it on the constant table.
|
|
|
|
*/
|
1994-12-28 15:55:47 +03:00
|
|
|
void lua_pushliteral (char *s)
|
1994-12-13 18:54:21 +03:00
|
|
|
{
|
1995-10-13 18:16:25 +03:00
|
|
|
tsvalue(top) = lua_constant[luaI_findconstantbyname(s)];
|
1994-12-13 18:54:21 +03:00
|
|
|
tag(top) = LUA_T_STRING;
|
1995-04-11 21:56:30 +04:00
|
|
|
incr_top;
|
1994-12-13 18:54:21 +03:00
|
|
|
}
|
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
/*
|
1994-12-28 15:55:47 +03:00
|
|
|
** Push an object (tag=cfunction) to stack.
|
1993-07-28 17:18:00 +04:00
|
|
|
*/
|
1994-12-28 15:55:47 +03:00
|
|
|
void lua_pushcfunction (lua_CFunction fn)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1995-04-11 21:56:30 +04:00
|
|
|
tag(top) = LUA_T_CFUNCTION; fvalue(top) = fn;
|
|
|
|
incr_top;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-12-28 15:55:47 +03:00
|
|
|
** Push an object (tag=userdata) to stack.
|
1993-07-28 17:18:00 +04:00
|
|
|
*/
|
1994-12-28 15:55:47 +03:00
|
|
|
void lua_pushusertag (void *u, int tag)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-12-28 15:55:47 +03:00
|
|
|
if (tag < LUA_T_USERDATA) return;
|
1995-04-11 21:56:30 +04:00
|
|
|
tag(top) = tag; uvalue(top) = u;
|
|
|
|
incr_top;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-11-07 19:34:44 +03:00
|
|
|
** Push a lua_Object to stack.
|
1993-07-28 17:18:00 +04:00
|
|
|
*/
|
1994-12-28 15:55:47 +03:00
|
|
|
void lua_pushobject (lua_Object o)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1995-04-11 21:56:30 +04:00
|
|
|
*top = *Address(o);
|
|
|
|
incr_top;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
1994-11-07 19:34:44 +03:00
|
|
|
/*
|
|
|
|
** Push an object on the stack.
|
|
|
|
*/
|
|
|
|
void luaI_pushobject (Object *o)
|
|
|
|
{
|
1995-04-11 21:56:30 +04:00
|
|
|
*top = *o;
|
|
|
|
incr_top;
|
1994-11-07 19:34:44 +03:00
|
|
|
}
|
|
|
|
|
1994-11-04 13:47:49 +03:00
|
|
|
int lua_type (lua_Object o)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-12-28 15:55:47 +03:00
|
|
|
if (o == LUA_NOOBJECT)
|
1994-11-04 13:47:49 +03:00
|
|
|
return LUA_T_NIL;
|
|
|
|
else
|
1994-11-07 19:34:44 +03:00
|
|
|
return tag(Address(o));
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
1994-11-02 23:30:53 +03:00
|
|
|
|
1994-11-10 20:36:54 +03:00
|
|
|
void luaI_gcFB (Object *o)
|
|
|
|
{
|
1995-04-11 21:56:30 +04:00
|
|
|
*top = *o;
|
|
|
|
incr_top;
|
1995-10-09 16:10:20 +03:00
|
|
|
callFB(FB_GC);
|
1994-11-10 20:36:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-11-07 18:20:56 +03:00
|
|
|
static void call_arith (char *op)
|
|
|
|
{
|
|
|
|
lua_pushstring(op);
|
1995-10-09 16:10:20 +03:00
|
|
|
callFB(FB_ARITH);
|
1994-11-07 18:20:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void comparison (lua_Type tag_less, lua_Type tag_equal,
|
|
|
|
lua_Type tag_great, char *op)
|
|
|
|
{
|
|
|
|
Object *l = top-2;
|
|
|
|
Object *r = top-1;
|
|
|
|
int result;
|
|
|
|
if (tag(l) == LUA_T_NUMBER && tag(r) == LUA_T_NUMBER)
|
|
|
|
result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
|
|
|
|
else if (tostring(l) || tostring(r))
|
|
|
|
{
|
|
|
|
lua_pushstring(op);
|
1995-10-09 16:10:20 +03:00
|
|
|
callFB(FB_ORDER);
|
1994-11-07 18:20:56 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = strcmp(svalue(l), svalue(r));
|
|
|
|
top--;
|
|
|
|
nvalue(top-1) = 1;
|
|
|
|
tag(top-1) = (result < 0) ? tag_less : (result == 0) ? tag_equal : tag_great;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
/*
|
1994-11-02 23:30:53 +03:00
|
|
|
** Execute the given opcode, until a RET. Parameters are between
|
|
|
|
** [stack+base,top). Returns n such that the the results are between
|
|
|
|
** [stack+n,top).
|
1993-07-28 17:18:00 +04:00
|
|
|
*/
|
1994-12-21 00:20:36 +03:00
|
|
|
static StkId lua_execute (Byte *pc, StkId base)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-11-02 23:30:53 +03:00
|
|
|
while (1)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-11-02 23:30:53 +03:00
|
|
|
OpCode opcode;
|
|
|
|
switch (opcode = (OpCode)*pc++)
|
|
|
|
{
|
1995-04-11 21:56:30 +04:00
|
|
|
case PUSHNIL: tag(top) = LUA_T_NIL; incr_top; break;
|
1993-07-28 17:18:00 +04:00
|
|
|
|
1994-11-16 19:03:48 +03:00
|
|
|
case PUSH0: case PUSH1: case PUSH2:
|
|
|
|
tag(top) = LUA_T_NUMBER;
|
1995-04-11 21:56:30 +04:00
|
|
|
nvalue(top) = opcode-PUSH0;
|
|
|
|
incr_top;
|
1994-11-16 19:03:48 +03:00
|
|
|
break;
|
1993-12-17 21:41:19 +03:00
|
|
|
|
1995-04-11 21:56:30 +04:00
|
|
|
case PUSHBYTE:
|
|
|
|
tag(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;
|
1994-11-02 23:30:53 +03:00
|
|
|
|
|
|
|
case PUSHWORD:
|
|
|
|
{
|
|
|
|
CodeWord code;
|
|
|
|
get_word(code,pc);
|
1995-04-11 21:56:30 +04:00
|
|
|
tag(top) = LUA_T_NUMBER; nvalue(top) = code.w;
|
|
|
|
incr_top;
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PUSHFLOAT:
|
|
|
|
{
|
|
|
|
CodeFloat code;
|
|
|
|
get_float(code,pc);
|
1995-04-11 21:56:30 +04:00
|
|
|
tag(top) = LUA_T_NUMBER; nvalue(top) = code.f;
|
|
|
|
incr_top;
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PUSHSTRING:
|
|
|
|
{
|
|
|
|
CodeWord code;
|
|
|
|
get_word(code,pc);
|
1995-04-11 21:56:30 +04:00
|
|
|
tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
|
|
|
|
incr_top;
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PUSHFUNCTION:
|
|
|
|
{
|
|
|
|
CodeCode code;
|
|
|
|
get_code(code,pc);
|
1995-10-04 20:13:02 +03:00
|
|
|
luaI_insertfunction(code.tf); /* may take part in GC */
|
|
|
|
top->tag = LUA_T_FUNCTION;
|
|
|
|
top->value.tf = code.tf;
|
1995-04-11 21:56:30 +04:00
|
|
|
incr_top;
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
|
|
|
|
case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
|
|
|
|
case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
|
1995-04-11 21:56:30 +04:00
|
|
|
case PUSHLOCAL9:
|
|
|
|
*top = *((stack+base) + (int)(opcode-PUSHLOCAL0)); incr_top; break;
|
1994-11-02 23:30:53 +03:00
|
|
|
|
1995-04-11 21:56:30 +04:00
|
|
|
case PUSHLOCAL: *top = *((stack+base) + (*pc++)); incr_top; break;
|
1994-11-02 23:30:53 +03:00
|
|
|
|
|
|
|
case PUSHGLOBAL:
|
|
|
|
{
|
|
|
|
CodeWord code;
|
|
|
|
get_word(code,pc);
|
1995-04-11 21:56:30 +04:00
|
|
|
*top = s_object(code.w);
|
|
|
|
incr_top;
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PUSHINDEXED:
|
1994-11-04 13:47:49 +03:00
|
|
|
pushsubscript();
|
|
|
|
break;
|
1994-11-02 23:30:53 +03:00
|
|
|
|
|
|
|
case PUSHSELF:
|
|
|
|
{
|
1994-12-06 17:27:18 +03:00
|
|
|
Object receiver = *(top-1);
|
|
|
|
CodeWord code;
|
|
|
|
get_word(code,pc);
|
1995-04-11 21:56:30 +04:00
|
|
|
tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
|
|
|
|
incr_top;
|
1994-11-04 13:47:49 +03:00
|
|
|
pushsubscript();
|
1995-04-11 21:56:30 +04:00
|
|
|
*top = receiver;
|
|
|
|
incr_top;
|
1994-11-02 23:30:53 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
|
|
|
|
case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
|
|
|
|
case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
|
|
|
|
case STORELOCAL9:
|
|
|
|
*((stack+base) + (int)(opcode-STORELOCAL0)) = *(--top);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STORELOCAL: *((stack+base) + (*pc++)) = *(--top); break;
|
|
|
|
|
|
|
|
case STOREGLOBAL:
|
|
|
|
{
|
|
|
|
CodeWord code;
|
|
|
|
get_word(code,pc);
|
|
|
|
s_object(code.w) = *(--top);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STOREINDEXED0:
|
1994-11-07 18:20:56 +03:00
|
|
|
storesubscript();
|
|
|
|
break;
|
1994-11-02 23:30:53 +03:00
|
|
|
|
|
|
|
case STOREINDEXED:
|
|
|
|
{
|
|
|
|
int n = *pc++;
|
|
|
|
if (tag(top-3-n) != LUA_T_ARRAY)
|
1994-11-07 18:20:56 +03:00
|
|
|
{
|
1995-04-11 21:56:30 +04:00
|
|
|
lua_checkstack(top+2);
|
1994-11-07 18:20:56 +03:00
|
|
|
*(top+1) = *(top-1);
|
|
|
|
*(top) = *(top-2-n);
|
|
|
|
*(top-1) = *(top-3-n);
|
|
|
|
top += 2;
|
1995-10-09 16:10:20 +03:00
|
|
|
callFB(FB_SETTABLE);
|
1994-11-07 18:20:56 +03:00
|
|
|
}
|
|
|
|
else
|
1994-11-02 23:30:53 +03:00
|
|
|
{
|
|
|
|
Object *h = lua_hashdefine (avalue(top-3-n), top-2-n);
|
|
|
|
*h = *(top-1);
|
1994-11-07 18:20:56 +03:00
|
|
|
top--;
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STORELIST0:
|
|
|
|
case STORELIST:
|
|
|
|
{
|
|
|
|
int m, n;
|
|
|
|
Object *arr;
|
|
|
|
if (opcode == STORELIST0) m = 0;
|
|
|
|
else m = *(pc++) * FIELDS_PER_FLUSH;
|
|
|
|
n = *(pc++);
|
|
|
|
arr = top-n-1;
|
|
|
|
while (n)
|
|
|
|
{
|
|
|
|
tag(top) = LUA_T_NUMBER; nvalue(top) = n+m;
|
|
|
|
*(lua_hashdefine (avalue(arr), top)) = *(top-1);
|
|
|
|
top--;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STORERECORD:
|
|
|
|
{
|
|
|
|
int n = *(pc++);
|
|
|
|
Object *arr = top-n-1;
|
|
|
|
while (n)
|
|
|
|
{
|
|
|
|
CodeWord code;
|
|
|
|
get_word(code,pc);
|
1994-11-23 17:32:00 +03:00
|
|
|
tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
|
1994-11-02 23:30:53 +03:00
|
|
|
*(lua_hashdefine (avalue(arr), top)) = *(top-1);
|
|
|
|
top--;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ADJUST0:
|
1994-11-11 17:00:08 +03:00
|
|
|
adjust_top(base);
|
1994-11-02 23:30:53 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ADJUST:
|
1994-11-11 17:00:08 +03:00
|
|
|
adjust_top(base + *(pc++));
|
1994-11-02 23:30:53 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CREATEARRAY:
|
|
|
|
{
|
|
|
|
CodeWord size;
|
|
|
|
get_word(size,pc);
|
1994-11-16 20:39:16 +03:00
|
|
|
avalue(top) = lua_createarray(size.w);
|
1994-11-17 16:58:57 +03:00
|
|
|
tag(top) = LUA_T_ARRAY;
|
1995-04-11 21:56:30 +04:00
|
|
|
incr_top;
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EQOP:
|
|
|
|
{
|
1994-11-16 19:03:48 +03:00
|
|
|
int res = lua_equalObj(top-2, top-1);
|
1994-11-02 23:30:53 +03:00
|
|
|
--top;
|
|
|
|
tag(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
|
|
|
|
nvalue(top-1) = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1994-11-07 18:20:56 +03:00
|
|
|
case LTOP:
|
1994-11-17 22:43:34 +03:00
|
|
|
comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "lt");
|
1994-11-07 18:20:56 +03:00
|
|
|
break;
|
1994-11-02 23:30:53 +03:00
|
|
|
|
|
|
|
case LEOP:
|
1994-11-17 22:43:34 +03:00
|
|
|
comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "le");
|
1994-11-07 18:20:56 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GTOP:
|
1994-11-17 22:43:34 +03:00
|
|
|
comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, "gt");
|
1994-11-07 18:20:56 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GEOP:
|
1994-11-17 22:43:34 +03:00
|
|
|
comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, "ge");
|
1994-11-07 18:20:56 +03:00
|
|
|
break;
|
1994-11-02 23:30:53 +03:00
|
|
|
|
|
|
|
case ADDOP:
|
|
|
|
{
|
|
|
|
Object *l = top-2;
|
|
|
|
Object *r = top-1;
|
|
|
|
if (tonumber(r) || tonumber(l))
|
1994-11-17 22:43:34 +03:00
|
|
|
call_arith("add");
|
1994-11-07 18:20:56 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nvalue(l) += nvalue(r);
|
|
|
|
--top;
|
|
|
|
}
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SUBOP:
|
|
|
|
{
|
|
|
|
Object *l = top-2;
|
|
|
|
Object *r = top-1;
|
|
|
|
if (tonumber(r) || tonumber(l))
|
1994-11-17 22:43:34 +03:00
|
|
|
call_arith("sub");
|
1994-11-07 18:20:56 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nvalue(l) -= nvalue(r);
|
|
|
|
--top;
|
|
|
|
}
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MULTOP:
|
|
|
|
{
|
|
|
|
Object *l = top-2;
|
|
|
|
Object *r = top-1;
|
|
|
|
if (tonumber(r) || tonumber(l))
|
1994-11-17 22:43:34 +03:00
|
|
|
call_arith("mul");
|
1994-11-07 18:20:56 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nvalue(l) *= nvalue(r);
|
|
|
|
--top;
|
|
|
|
}
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIVOP:
|
|
|
|
{
|
|
|
|
Object *l = top-2;
|
|
|
|
Object *r = top-1;
|
|
|
|
if (tonumber(r) || tonumber(l))
|
1994-11-17 22:43:34 +03:00
|
|
|
call_arith("div");
|
1994-11-07 18:20:56 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nvalue(l) /= nvalue(r);
|
|
|
|
--top;
|
|
|
|
}
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case POWOP:
|
1994-11-17 22:43:34 +03:00
|
|
|
call_arith("pow");
|
|
|
|
break;
|
1993-12-17 21:41:19 +03:00
|
|
|
|
1994-11-02 23:30:53 +03:00
|
|
|
case CONCOP:
|
|
|
|
{
|
|
|
|
Object *l = top-2;
|
|
|
|
Object *r = top-1;
|
|
|
|
if (tostring(r) || tostring(l))
|
1995-10-09 16:10:20 +03:00
|
|
|
callFB(FB_CONCAT);
|
1994-11-07 18:20:56 +03:00
|
|
|
else
|
|
|
|
{
|
1994-11-23 17:32:00 +03:00
|
|
|
tsvalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
|
1994-11-07 18:20:56 +03:00
|
|
|
--top;
|
|
|
|
}
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MINUSOP:
|
|
|
|
if (tonumber(top-1))
|
1994-11-21 16:30:15 +03:00
|
|
|
{
|
1995-04-11 21:56:30 +04:00
|
|
|
tag(top) = LUA_T_NIL;
|
|
|
|
incr_top;
|
1994-11-21 16:30:15 +03:00
|
|
|
call_arith("unm");
|
|
|
|
}
|
1994-11-07 18:20:56 +03:00
|
|
|
else
|
|
|
|
nvalue(top-1) = - nvalue(top-1);
|
1994-11-02 23:30:53 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NOTOP:
|
1994-11-07 18:20:56 +03:00
|
|
|
tag(top-1) = (tag(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
|
1994-11-16 19:03:48 +03:00
|
|
|
nvalue(top-1) = 1;
|
1994-11-02 23:30:53 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ONTJMP:
|
|
|
|
{
|
|
|
|
CodeWord code;
|
|
|
|
get_word(code,pc);
|
|
|
|
if (tag(top-1) != LUA_T_NIL) pc += code.w;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ONFJMP:
|
|
|
|
{
|
|
|
|
CodeWord code;
|
|
|
|
get_word(code,pc);
|
|
|
|
if (tag(top-1) == LUA_T_NIL) pc += code.w;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JMP:
|
|
|
|
{
|
|
|
|
CodeWord code;
|
|
|
|
get_word(code,pc);
|
|
|
|
pc += code.w;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UPJMP:
|
|
|
|
{
|
|
|
|
CodeWord code;
|
|
|
|
get_word(code,pc);
|
|
|
|
pc -= code.w;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IFFJMP:
|
|
|
|
{
|
|
|
|
CodeWord code;
|
|
|
|
get_word(code,pc);
|
|
|
|
top--;
|
|
|
|
if (tag(top) == LUA_T_NIL) pc += code.w;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IFFUPJMP:
|
|
|
|
{
|
|
|
|
CodeWord code;
|
|
|
|
get_word(code,pc);
|
|
|
|
top--;
|
|
|
|
if (tag(top) == LUA_T_NIL) pc -= code.w;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case POP: --top; break;
|
|
|
|
|
|
|
|
case CALLFUNC:
|
|
|
|
{
|
|
|
|
int nParams = *(pc++);
|
|
|
|
int nResults = *(pc++);
|
1994-12-21 00:20:36 +03:00
|
|
|
StkId newBase = (top-stack)-nParams;
|
1995-10-09 16:10:20 +03:00
|
|
|
do_call(newBase, nResults);
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RETCODE0:
|
|
|
|
return base;
|
|
|
|
|
|
|
|
case RETCODE:
|
|
|
|
return base+*pc;
|
|
|
|
|
|
|
|
case SETLINE:
|
|
|
|
{
|
|
|
|
CodeWord code;
|
|
|
|
get_word(code,pc);
|
1995-10-25 16:05:51 +03:00
|
|
|
if ((stack+base-1)->tag != LUA_T_LINE)
|
|
|
|
{
|
|
|
|
/* open space for LINE value */
|
|
|
|
open_stack((top-stack)-base);
|
|
|
|
base++;
|
|
|
|
(stack+base-1)->tag = LUA_T_LINE;
|
|
|
|
}
|
|
|
|
(stack+base-1)->value.i = code.w;
|
|
|
|
break;
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
lua_error ("internal error - opcode doesn't match");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1993-12-17 21:41:19 +03:00
|
|
|
|
1994-11-07 19:34:44 +03:00
|
|
|
|