first version of "setglobal" and "getglobal" internal methods.

This commit is contained in:
Roberto Ierusalimschy 1997-03-31 17:59:09 -03:00
parent f8a571ee35
commit efaaf99c42
6 changed files with 115 additions and 82 deletions

View File

@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio
*/
char *rcs_fallback="$Id: fallback.c,v 1.34 1997/03/31 14:02:58 roberto Exp roberto $";
char *rcs_fallback="$Id: fallback.c,v 1.35 1997/03/31 14:17:09 roberto Exp roberto $";
#include <stdio.h>
#include <string.h>
@ -109,16 +109,13 @@ void luaI_invalidaterefs (void)
*/
char *luaI_eventname[] = { /* ORDER IM */
"gettable", "settable", "index", "add", "sub", "mul", "div",
"pow", "unm", "lt", "le", "gt", "ge", "concat", "gc", "function",
"gettable", "settable", "index", "getglobal", "setglobal", "add",
"sub", "mul", "div", "pow", "unm", "lt", "le", "gt", "ge",
"concat", "gc", "function",
NULL
};
static char *geventname[] = { /* ORDER GIM */
"error", "getglobal", "setglobal",
NULL
};
static int findstring (char *name, char *list[])
{
@ -147,16 +144,16 @@ static int IMtable_size = 0;
static int last_tag = LUA_T_NIL; /* ORDER LUA_T */
static char validevents[NUM_TYPES][IM_N] = { /* ORDER LUA_T, ORDER IM */
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_USERDATA */
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_LINE */
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_CMARK */
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */
{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1}, /* LUA_T_NUMBER */
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0} /* LUA_T_NIL */
{1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_USERDATA */
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_LINE */
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_CMARK */
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */
{1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */
{1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1}, /* LUA_T_NUMBER */
{0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0} /* LUA_T_NIL */
};
static int validevent (lua_Type t, int e)
@ -263,32 +260,29 @@ void luaI_setintmethod (void)
luaI_IMtable[-t].int_method[e] = *luaI_Address(func);
}
static TObject gmethod[GIM_N] = {
{LUA_T_NIL, {NULL}}, {LUA_T_NIL, {NULL}}, {LUA_T_NIL, {NULL}}
};
TObject *luaI_getgim (IMGS event)
static TObject errorim = {LUA_T_NIL, {NULL}};
TObject *luaI_geterrorim (void)
{
return &gmethod[event];
return &errorim;
}
void luaI_setglobalmethod (void)
void luaI_seterrormethod (void)
{
int e = luaI_checkevent(luaL_check_string(1, "setintmethod"), geventname);
lua_Object func = lua_getparam(2);
luaL_arg_check(lua_isnil(func) || lua_isfunction(func), "setintmethod",
2, "function expected");
luaI_pushobject(&gmethod[e]);
gmethod[e] = *luaI_Address(func);
lua_Object func = lua_getparam(1);
luaL_arg_check(lua_isnil(func) || lua_isfunction(func), "seterrormethod",
1, "function expected");
luaI_pushobject(&errorim);
errorim = *luaI_Address(func);
}
char *luaI_travfallbacks (int (*fn)(TObject *))
{
int e;
for (e=GIM_ERROR; e<=GIM_SETGLOBAL; e++) { /* ORDER GIM */
if (fn(&gmethod[e]))
return geventname[e];
}
if (fn(&errorim))
return "error";
for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
int t;
for (t=0; t>=last_tag; t--)
@ -339,28 +333,33 @@ void luaI_setfallback (void)
lua_CFunction replace;
char *name = luaL_check_string(1, "setfallback");
lua_Object func = lua_getparam(2);
luaI_initfallbacks();
luaL_arg_check(lua_isfunction(func), "setfallback", 2, "function expected");
e = findstring(name, geventname);
if (e >= 0) { /* global event */
oldfunc = gmethod[e];
gmethod[e] = *luaI_Address(func);
replace = (e == GIM_ERROR) ? errorFB : nilFB;
if (strcmp(name, "error") == 0) { /* old error fallback */
oldfunc = errorim;
errorim = *luaI_Address(func);
replace = errorFB;
}
else if (strcmp(name, "getglobal") == 0) { /* old getglobal fallback */
oldfunc = luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL];
luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL] = *luaI_Address(func);
replace = nilFB;
}
else if ((e = findstring(name, luaI_eventname)) >= 0) {
oldfunc = luaI_IMtable[LUA_T_USERDATA].int_method[e];
oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[e];
fillvalids(e, luaI_Address(func));
replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
}
else if (strcmp(name, "arith") == 0) { /* old arith fallback */
int i;
oldfunc = luaI_IMtable[LUA_T_USERDATA].int_method[IM_POW];
oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_POW];
for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
fillvalids(i, luaI_Address(func));
replace = typeFB;
}
else if (strcmp(name, "order") == 0) { /* old order fallback */
int i;
oldfunc = luaI_IMtable[LUA_T_USERDATA].int_method[IM_LT];
oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_LT];
for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
fillvalids(i, luaI_Address(func));
replace = typeFB;

View File

@ -1,5 +1,5 @@
/*
** $Id: fallback.h,v 1.17 1997/03/24 17:13:22 roberto Exp roberto $
** $Id: fallback.h,v 1.18 1997/03/31 14:02:58 roberto Exp roberto $
*/
#ifndef fallback_h
@ -16,6 +16,8 @@ typedef enum {
IM_GETTABLE = 0,
IM_SETTABLE,
IM_INDEX,
IM_GETGLOBAL,
IM_SETGLOBAL,
IM_ADD,
IM_SUB,
IM_MUL,
@ -31,23 +33,11 @@ typedef enum {
IM_FUNCTION
} IMS;
#define IM_N 16
#define IM_N 18
extern char *luaI_eventname[];
/*
* WARNING: if you change the order of this enumeration,
* grep "ORDER GIM"
*/
typedef enum {
GIM_ERROR = 0,
GIM_GETGLOBAL,
GIM_SETGLOBAL
} IMGS;
#define GIM_N 3
void luaI_setfallback (void);
int luaI_ref (TObject *object, int lock);
TObject *luaI_getref (int ref);
@ -60,10 +50,10 @@ void luaI_settag (int tag, TObject *o);
lua_Type luaI_typetag (int tag);
TObject *luaI_getim (int tag, IMS event);
#define luaI_getimbyObj(o,e) (luaI_getim(luaI_tag(o),(e)))
TObject *luaI_getgim (IMGS event);
TObject *luaI_geterrorim (void);
int luaI_tag (TObject *o);
void luaI_setintmethod (void);
void luaI_setglobalmethod (void);
void luaI_seterrormethod (void);
void luaI_initfallbacks (void);
#endif

30
inout.c
View File

@ -5,7 +5,7 @@
** Also provides some predefined lua functions.
*/
char *rcs_inout="$Id: inout.c,v 2.48 1997/03/20 19:20:23 roberto Exp roberto $";
char *rcs_inout="$Id: inout.c,v 2.49 1997/03/31 14:17:09 roberto Exp roberto $";
#include <stdio.h>
#include <string.h>
@ -194,11 +194,25 @@ static void luaI_setglobal (void)
lua_pushobject(value); /* return given value */
}
static void luaI_basicsetglobal (void)
{
lua_Object value = lua_getparam(2);
luaL_arg_check(value != LUA_NOOBJECT, "basicsetglobal", 2, NULL);
lua_pushobject(value);
lua_basicstoreglobal(luaL_check_string(1, "basicsetglobal"));
lua_pushobject(value); /* return given value */
}
static void luaI_getglobal (void)
{
lua_pushobject(lua_getglobal(luaL_check_string(1, "getglobal")));
}
static void luaI_basicgetglobal (void)
{
lua_pushobject(lua_basicgetglobal(luaL_check_string(1, "basicgetglobal")));
}
#define MAXPARAMS 256
static void luaI_call (void)
{
@ -281,27 +295,29 @@ static struct {
lua_CFunction func;
} int_funcs[] = {
{"assert", luaI_assert},
{"call", luaI_call},
{"basicgetglobal", luaI_basicgetglobal},
{"basicindex", basicindex},
{"basicsetglobal", luaI_basicsetglobal},
{"basicstoreindex", basicstoreindex},
{"settag", luaIl_settag},
{"call", luaI_call},
{"dofile", lua_internaldofile},
{"dostring", lua_internaldostring},
{"error", luaI_error},
{"getglobal", luaI_getglobal},
{"newtag", luaIl_newtag},
{"next", lua_next},
{"nextvar", luaI_nextvar},
{"newtag", luaIl_newtag},
{"print", luaI_print},
{"seterrormethod", luaI_seterrormethod},
{"setfallback", luaI_setfallback},
{"setintmethod", luaI_setintmethod},
{"setglobalmethod", luaI_setglobalmethod},
{"setglobal", luaI_setglobal},
{"setintmethod", luaI_setintmethod},
{"settag", luaIl_settag},
{"tonumber", lua_obj2number},
{"tostring", luaI_tostring},
{"type", luaI_type}
};
#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))

View File

@ -340,5 +340,5 @@ void iolib_open (void)
lua_tagio = lua_newtag("userdata");
lua_infile=stdin; lua_outfile=stdout;
luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
lua_setglobalmethod("error", errorfb);
lua_seterrormethod(errorfb);
}

4
lua.h
View File

@ -2,7 +2,7 @@
** LUA - Linguagem para Usuarios de Aplicacao
** Grupo de Tecnologia em Computacao Grafica
** TeCGraf - PUC-Rio
** $Id: lua.h,v 3.36 1997/03/17 17:01:10 roberto Exp roberto $
** $Id: lua.h,v 3.37 1997/03/19 19:41:10 roberto Exp roberto $
*/
@ -21,7 +21,7 @@ typedef unsigned int lua_Object;
lua_Object lua_setfallback (char *event, lua_CFunction fallback);
void lua_setintmethod (int tag, char *event, lua_CFunction method);
void lua_setglobalmethod (char *event, lua_CFunction method);
void lua_seterrormethod (lua_CFunction method);
int lua_newtag (char *t);
void lua_settag (int tag); /* In: object */

View File

@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio
*/
char *rcs_opcode="$Id: opcode.c,v 3.87 1997/03/31 14:02:58 roberto Exp roberto $";
char *rcs_opcode="$Id: opcode.c,v 3.88 1997/03/31 14:17:09 roberto Exp roberto $";
#include <setjmp.h>
#include <stdio.h>
@ -391,16 +391,20 @@ static void storesubscript (TObject *t, int mode)
static void getglobal (Word n)
{
*top = lua_table[n].object;
incr_top;
if (ttype(top-1) == LUA_T_NIL) { /* check i.m. */
TObject *im = luaI_getgim(GIM_GETGLOBAL);
if (ttype(im) != LUA_T_NIL) {
ttype(top-1) = LUA_T_STRING;
tsvalue(top-1) = lua_table[n].varname;
callIM(im, 1, 1);
}
TObject *value = &lua_table[n].object;
TObject *im = luaI_getimbyObj(value, IM_GETGLOBAL);
if (ttype(im) == LUA_T_NIL) { /* default behavior */
*top = *value;
incr_top;
}
else {
ttype(top) = LUA_T_STRING;
tsvalue(top) = lua_table[n].varname;
incr_top;
*top = *value;
incr_top;
callIM(im, 2, 1);
}
}
/*
@ -420,7 +424,7 @@ void lua_travstack (int (*fn)(TObject *))
static void lua_message (char *s)
{
TObject *im = luaI_getgim(GIM_ERROR);
TObject *im = luaI_geterrorim();
if (ttype(im) == LUA_T_NIL)
fprintf(stderr, "lua: %s\n", s);
else {
@ -663,11 +667,10 @@ void lua_setintmethod (int tag, char *event, lua_CFunction method)
do_unprotectedrun(luaI_setintmethod, 3, 0);
}
void lua_setglobalmethod (char *event, lua_CFunction method)
void lua_seterrormethod (lua_CFunction method)
{
lua_pushstring(event);
lua_pushcfunction (method);
do_unprotectedrun(luaI_setglobalmethod, 3, 0);
do_unprotectedrun(luaI_seterrormethod, 1, 0);
}
@ -895,7 +898,32 @@ lua_Object lua_basicgetglobal (char *name)
/*
** Store top of the stack at a global variable array field.
*/
static void storeglobal (Word n)
{
TObject *oldvalue = &lua_table[n].object;
TObject *im = luaI_getimbyObj(oldvalue, IM_SETGLOBAL);
if (ttype(im) == LUA_T_NIL) /* default behavior */
s_object(n) = *(--top);
else {
TObject newvalue = *(top-1);
ttype(top-1) = LUA_T_STRING;
tsvalue(top-1) = lua_table[n].varname;
*top = *oldvalue;
incr_top;
*top = newvalue;
incr_top;
callIM(im, 3, 0);
}
}
void lua_storeglobal (char *name)
{
adjustC(1);
storeglobal(luaI_findsymbolbyname(name));
}
void lua_basicstoreglobal (char *name)
{
Word n = luaI_findsymbolbyname(name);
adjustC(1);
@ -1201,7 +1229,7 @@ static StkId lua_execute (Byte *pc, StkId base)
{
Word w;
get_word(w,pc);
s_object(w) = *(--top);
storeglobal(w);
}
break;