first implementation of centralized global state.

This commit is contained in:
Roberto Ierusalimschy
1997-11-19 15:29:23 -02:00
parent 9cdeb275e7
commit 592a3f289b
25 changed files with 782 additions and 829 deletions

179
lapi.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 1.3 1997/10/24 17:17:24 roberto Exp roberto $
** $Id: lapi.c,v 1.4 1997/11/04 15:27:53 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@@ -10,13 +10,12 @@
#include "lapi.h"
#include "lauxlib.h"
#include "lbuiltin.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "llex.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
@@ -38,41 +37,41 @@ TObject *luaA_Address (lua_Object o)
void luaA_packresults (void)
{
luaV_pack(luaD_Cstack.lua2C, luaD_Cstack.num, luaD_stack.top);
luaV_pack(L->Cstack.lua2C, L->Cstack.num, L->stack.top);
incr_top;
}
int luaA_passresults (void)
{
luaD_checkstack(luaD_Cstack.num);
memcpy(luaD_stack.top, luaD_Cstack.lua2C+luaD_stack.stack,
luaD_Cstack.num*sizeof(TObject));
luaD_stack.top += luaD_Cstack.num;
return luaD_Cstack.num;
luaD_checkstack(L->Cstack.num);
memcpy(L->stack.top, L->Cstack.lua2C+L->stack.stack,
L->Cstack.num*sizeof(TObject));
L->stack.top += L->Cstack.num;
return L->Cstack.num;
}
static void checkCparams (int nParams)
{
if (luaD_stack.top-luaD_stack.stack < luaD_Cstack.base+nParams)
if (L->stack.top-L->stack.stack < L->Cstack.base+nParams)
lua_error("API error - wrong number of arguments in C2lua stack");
}
static lua_Object put_luaObject (TObject *o)
{
luaD_openstack((luaD_stack.top-luaD_stack.stack)-luaD_Cstack.base);
luaD_stack.stack[luaD_Cstack.base++] = *o;
return luaD_Cstack.base; /* this is +1 real position (see Ref) */
luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
L->stack.stack[L->Cstack.base++] = *o;
return L->Cstack.base; /* this is +1 real position (see Ref) */
}
static lua_Object put_luaObjectonTop (void)
{
luaD_openstack((luaD_stack.top-luaD_stack.stack)-luaD_Cstack.base);
luaD_stack.stack[luaD_Cstack.base++] = *(--luaD_stack.top);
return luaD_Cstack.base; /* this is +1 real position (see Ref) */
luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
L->stack.stack[L->Cstack.base++] = *(--L->stack.top);
return L->Cstack.base; /* this is +1 real position (see Ref) */
}
@@ -89,20 +88,20 @@ lua_Object lua_pop (void)
*/
lua_Object lua_lua2C (int number)
{
if (number <= 0 || number > luaD_Cstack.num) return LUA_NOOBJECT;
/* Ref(luaD_stack.stack+(luaD_Cstack.lua2C+number-1)) ==
luaD_stack.stack+(luaD_Cstack.lua2C+number-1)-luaD_stack.stack+1 == */
return luaD_Cstack.lua2C+number;
if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT;
/* Ref(L->stack.stack+(L->Cstack.lua2C+number-1)) ==
L->stack.stack+(L->Cstack.lua2C+number-1)-L->stack.stack+1 == */
return L->Cstack.lua2C+number;
}
lua_Object lua_upvalue (int n)
{
TObject *f = luaD_stack.stack+luaD_Cstack.lua2C-1;
TObject *f = L->stack.stack+L->Cstack.lua2C-1;
if (ttype(f) != LUA_T_MARK || n <= 0 || n > clvalue(f)->nelems)
return LUA_NOOBJECT;
if (ttype(protovalue(f)) != LUA_T_CPROTO) lua_error("BAD!!");
*luaD_stack.top = clvalue(f)->consts[n];
*L->stack.top = clvalue(f)->consts[n];
incr_top;
return put_luaObjectonTop();
}
@@ -113,8 +112,8 @@ int lua_callfunction (lua_Object function)
if (function == LUA_NOOBJECT)
return 1;
else {
luaD_openstack((luaD_stack.top-luaD_stack.stack)-luaD_Cstack.base);
luaD_stack.stack[luaD_Cstack.base] = *Address(function);
luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
L->stack.stack[L->Cstack.base] = *Address(function);
return luaD_protectedrun(MULT_RET);
}
}
@@ -129,16 +128,16 @@ lua_Object lua_gettagmethod (int tag, char *event)
lua_Object lua_settagmethod (int tag, char *event)
{
checkCparams(1);
luaT_settagmethod(tag, event, luaD_stack.top-1);
luaT_settagmethod(tag, event, L->stack.top-1);
return put_luaObjectonTop();
}
lua_Object lua_seterrormethod (void)
{
TObject temp = luaD_errorim;
TObject temp = L->errorim;
checkCparams(1);
luaD_errorim = *(--luaD_stack.top);
L->errorim = *(--L->stack.top);
return put_luaObject(&temp);
}
@@ -154,15 +153,15 @@ lua_Object lua_gettable (void)
lua_Object lua_rawgettable (void)
{
checkCparams(2);
if (ttype(luaD_stack.top-2) != LUA_T_ARRAY)
if (ttype(L->stack.top-2) != LUA_T_ARRAY)
lua_error("indexed expression not a table in raw gettable");
else {
TObject *h = luaH_get(avalue(luaD_stack.top-2), luaD_stack.top-1);
--luaD_stack.top;
TObject *h = luaH_get(avalue(L->stack.top-2), L->stack.top-1);
--L->stack.top;
if (h != NULL)
*(luaD_stack.top-1) = *h;
*(L->stack.top-1) = *h;
else
ttype(luaD_stack.top-1) = LUA_T_NIL;
ttype(L->stack.top-1) = LUA_T_NIL;
}
return put_luaObjectonTop();
}
@@ -171,14 +170,14 @@ lua_Object lua_rawgettable (void)
void lua_settable (void)
{
checkCparams(3);
luaV_settable(luaD_stack.top-3, 1);
luaV_settable(L->stack.top-3, 1);
}
void lua_rawsettable (void)
{
checkCparams(3);
luaV_settable(luaD_stack.top-3, 0);
luaV_settable(L->stack.top-3, 0);
}
@@ -192,6 +191,12 @@ lua_Object lua_createtable (void)
}
lua_Object lua_globalbag (void)
{
return put_luaObject(&L->globalbag);
}
lua_Object lua_getglobal (char *name)
{
luaD_checkstack(2); /* may need that to call T.M. */
@@ -219,7 +224,7 @@ void lua_rawsetglobal (char *name)
{
TaggedString *ts = luaS_new(name);
checkCparams(1);
luaS_rawsetglobal(ts, --luaD_stack.top);
luaS_rawsetglobal(ts, --L->stack.top);
}
@@ -293,24 +298,24 @@ lua_CFunction lua_getcfunction (lua_Object object)
void lua_pushnil (void)
{
ttype(luaD_stack.top) = LUA_T_NIL;
ttype(L->stack.top) = LUA_T_NIL;
incr_top;
}
void lua_pushnumber (real n)
{
ttype(luaD_stack.top) = LUA_T_NUMBER;
nvalue(luaD_stack.top) = n;
ttype(L->stack.top) = LUA_T_NUMBER;
nvalue(L->stack.top) = n;
incr_top;
}
void lua_pushstring (char *s)
{
if (s == NULL)
ttype(luaD_stack.top) = LUA_T_NIL;
ttype(L->stack.top) = LUA_T_NIL;
else {
tsvalue(luaD_stack.top) = luaS_new(s);
ttype(luaD_stack.top) = LUA_T_STRING;
tsvalue(L->stack.top) = luaS_new(s);
ttype(L->stack.top) = LUA_T_STRING;
}
incr_top;
luaC_checkGC();
@@ -319,13 +324,13 @@ void lua_pushstring (char *s)
void lua_pushCclosure (lua_CFunction fn, int n)
{
if (fn == NULL) {
ttype(luaD_stack.top) = LUA_T_NIL;
ttype(L->stack.top) = LUA_T_NIL;
incr_top;
}
else {
checkCparams(n);
ttype(luaD_stack.top) = LUA_T_CPROTO;
fvalue(luaD_stack.top) = fn;
ttype(L->stack.top) = LUA_T_CPROTO;
fvalue(L->stack.top) = fn;
incr_top;
luaV_closure(n);
}
@@ -335,15 +340,15 @@ void lua_pushusertag (void *u, int tag)
{
if (tag < 0 && tag != LUA_ANYTAG)
luaT_realtag(tag); /* error if tag is not valid */
tsvalue(luaD_stack.top) = luaS_createudata(u, tag);
ttype(luaD_stack.top) = LUA_T_USERDATA;
tsvalue(L->stack.top) = luaS_createudata(u, tag);
ttype(L->stack.top) = LUA_T_USERDATA;
incr_top;
luaC_checkGC();
}
void luaA_pushobject (TObject *o)
{
*luaD_stack.top = *o;
*L->stack.top = *o;
incr_top;
}
@@ -351,9 +356,9 @@ void lua_pushobject (lua_Object o)
{
if (o == LUA_NOOBJECT)
lua_error("API error - attempt to push a NOOBJECT");
*luaD_stack.top = *Address(o);
if (ttype(luaD_stack.top) == LUA_T_MARK)
ttype(luaD_stack.top) = LUA_T_FUNCTION;
*L->stack.top = *Address(o);
if (ttype(L->stack.top) == LUA_T_MARK)
ttype(L->stack.top) = LUA_T_FUNCTION;
incr_top;
}
@@ -376,18 +381,18 @@ void lua_settag (int tag)
{
checkCparams(1);
luaT_realtag(tag);
switch (ttype(luaD_stack.top-1)) {
switch (ttype(L->stack.top-1)) {
case LUA_T_ARRAY:
(luaD_stack.top-1)->value.a->htag = tag;
(L->stack.top-1)->value.a->htag = tag;
break;
case LUA_T_USERDATA:
(luaD_stack.top-1)->value.ts->u.d.tag = tag;
(L->stack.top-1)->value.ts->u.d.tag = tag;
break;
default:
luaL_verror("cannot change the tag of a %s",
luaO_typenames[-ttype((luaD_stack.top-1))]);
luaO_typenames[-ttype((L->stack.top-1))]);
}
luaD_stack.top--;
L->stack.top--;
}
@@ -406,10 +411,10 @@ lua_LHFunction lua_linehook = NULL;
lua_Function lua_stackedfunction (int level)
{
StkId i;
for (i = (luaD_stack.top-1)-luaD_stack.stack; i>=0; i--)
if (luaD_stack.stack[i].ttype == LUA_T_MARK)
for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--)
if (L->stack.stack[i].ttype == LUA_T_MARK)
if (level-- == 0)
return Ref(luaD_stack.stack+i);
return Ref(L->stack.stack+i);
return LUA_NOOBJECT;
}
@@ -417,7 +422,7 @@ lua_Function lua_stackedfunction (int level)
int lua_currentline (lua_Function func)
{
TObject *f = Address(func);
return (f+1 < luaD_stack.top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
return (f+1 < L->stack.top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
}
@@ -447,11 +452,11 @@ int lua_setlocal (lua_Function func, int local_number)
TProtoFunc *fp = protovalue(f)->value.tf;
char *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
checkCparams(1);
--luaD_stack.top;
--L->stack.top;
if (name) {
/* if "name", there must be a LUA_T_LINE */
/* therefore, f+2 points to function base */
*((f+2)+(local_number-1)) = *luaD_stack.top;
*((f+2)+(local_number-1)) = *L->stack.top;
return 1;
}
else
@@ -478,19 +483,18 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
}
static TObject *functofind;
static int checkfunc (TObject *o)
{
return (o->ttype == LUA_T_FUNCTION) &&
((functofind->ttype == LUA_T_FUNCTION) ||
(functofind->ttype == LUA_T_MARK)) &&
(functofind->value.cl == o->value.cl);
((L->functofind->ttype == LUA_T_FUNCTION) ||
(L->functofind->ttype == LUA_T_MARK)) &&
(L->functofind->value.cl == o->value.cl);
}
char *lua_getobjname (lua_Object o, char **name)
{ /* try to find a name for given function */
functofind = Address(o);
L->functofind = Address(o);
if ((*name = luaT_travtagmethods(checkfunc)) != NULL)
return "tag-method";
else if ((*name = luaS_travsymbol(checkfunc)) != NULL)
@@ -504,36 +508,30 @@ char *lua_getobjname (lua_Object o, char **name)
** =======================================================
*/
#define MAX_C_BLOCKS 10
static int numCblocks = 0;
static struct C_Lua_Stack Cblocks[MAX_C_BLOCKS];
void lua_beginblock (void)
{
if (numCblocks >= MAX_C_BLOCKS)
if (L->numCblocks >= MAX_C_BLOCKS)
lua_error("`lua_beginblock': too many nested blocks");
Cblocks[numCblocks] = luaD_Cstack;
numCblocks++;
L->Cblocks[L->numCblocks] = L->Cstack;
L->numCblocks++;
}
void lua_endblock (void)
{
--numCblocks;
luaD_Cstack = Cblocks[numCblocks];
luaD_adjusttop(luaD_Cstack.base);
--L->numCblocks;
L->Cstack = L->Cblocks[L->numCblocks];
luaD_adjusttop(L->Cstack.base);
}
int lua_ref (int lock)
{
int ref;
checkCparams(1);
ref = luaC_ref(luaD_stack.top-1, lock);
luaD_stack.top--;
ref = luaC_ref(L->stack.top-1, lock);
L->stack.top--;
return ref;
}
@@ -546,19 +544,6 @@ lua_Object lua_getref (int ref)
}
void lua_open (void)
{
static int firsttime = 1;
if (!firsttime) return;
firsttime = 0;
luaS_init();
luaX_init();
luaT_init();
luaD_init();
luaB_predefine();
}
#if LUA_COMPAT2_5
/*
@@ -567,11 +552,11 @@ void lua_open (void)
static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
{
StkId base = (luaD_stack.top-luaD_stack.stack)-nParams;
StkId base = (L->stack.top-L->stack.stack)-nParams;
luaD_openstack(nParams);
luaD_stack.stack[base].ttype = LUA_T_CPROTO;
luaD_stack.stack[base].value.f = f;
luaF_simpleclosure(luaD_stack.stack+base);
L->stack.stack[base].ttype = LUA_T_CPROTO;
L->stack.stack[base].value.f = f;
luaF_simpleclosure(L->stack.stack+base);
luaD_call(base+1, nResults);
}