mirror of
https://github.com/lua/lua.git
synced 2025-01-14 05:43:00 +08:00
small optimization in the access to i.m. table.
This commit is contained in:
parent
209602ac31
commit
369dd65318
44
fallback.c
44
fallback.c
@ -3,7 +3,7 @@
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_fallback="$Id: fallback.c,v 2.3 1997/04/06 14:08:08 roberto Exp roberto $";
|
||||
char *rcs_fallback="$Id: fallback.c,v 2.4 1997/04/07 14:48:53 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@ -111,9 +111,7 @@ static int luaI_checkevent (char *name, char *list[])
|
||||
}
|
||||
|
||||
|
||||
static struct IM {
|
||||
TObject int_method[IM_N];
|
||||
} *luaI_IMtable = NULL;
|
||||
struct IM *luaI_IMtable = NULL;
|
||||
|
||||
static int IMtable_size = 0;
|
||||
static int last_tag = LUA_T_NIL; /* ORDER LUA_T */
|
||||
@ -141,7 +139,7 @@ static void init_entry (int tag)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<IM_N; i++)
|
||||
luaI_IMtable[-tag].int_method[i].ttype = LUA_T_NIL;
|
||||
ttype(luaI_getim(tag, i)) = LUA_T_NIL;
|
||||
}
|
||||
|
||||
void luaI_initfallbacks (void)
|
||||
@ -194,32 +192,26 @@ void luaI_settag (int tag, TObject *o)
|
||||
}
|
||||
|
||||
|
||||
int luaI_tag (TObject *o)
|
||||
int luaI_efectivetag (TObject *o)
|
||||
{
|
||||
lua_Type t = ttype(o);
|
||||
if (t == LUA_T_USERDATA)
|
||||
return o->value.ts->tag;
|
||||
if (t == LUA_T_USERDATA) {
|
||||
int tag = o->value.ts->tag;
|
||||
return (tag >= 0) ? LUA_T_USERDATA : tag;
|
||||
}
|
||||
else if (t == LUA_T_ARRAY)
|
||||
return o->value.a->htag;
|
||||
else return t;
|
||||
}
|
||||
|
||||
|
||||
TObject *luaI_getim (int tag, IMS event)
|
||||
{
|
||||
if (tag > LUA_T_USERDATA)
|
||||
tag = LUA_T_USERDATA; /* default for non-registered tags */
|
||||
return &luaI_IMtable[-tag].int_method[event];
|
||||
}
|
||||
|
||||
|
||||
void luaI_gettagmethod (void)
|
||||
{
|
||||
int t = (int)luaL_check_number(1);
|
||||
int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
|
||||
checktag(t);
|
||||
if (validevent(t, e))
|
||||
luaI_pushobject(&luaI_IMtable[-t].int_method[e]);
|
||||
luaI_pushobject(luaI_getim(t,e));
|
||||
}
|
||||
|
||||
|
||||
@ -234,8 +226,8 @@ void luaI_settagmethod (void)
|
||||
luaI_eventname[e], t);
|
||||
luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
|
||||
3, "function expected");
|
||||
luaI_pushobject(&luaI_IMtable[-t].int_method[e]);
|
||||
luaI_IMtable[-t].int_method[e] = *luaI_Address(func);
|
||||
luaI_pushobject(luaI_getim(t,e));
|
||||
*luaI_getim(t, e) = *luaI_Address(func);
|
||||
}
|
||||
|
||||
|
||||
@ -264,7 +256,7 @@ char *luaI_travfallbacks (int (*fn)(TObject *))
|
||||
for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
|
||||
int t;
|
||||
for (t=0; t>=last_tag; t--)
|
||||
if (fn(&luaI_IMtable[-t].int_method[e]))
|
||||
if (fn(luaI_getim(t,e)))
|
||||
return luaI_eventname[e];
|
||||
}
|
||||
return NULL;
|
||||
@ -301,7 +293,7 @@ static void fillvalids (IMS e, TObject *func)
|
||||
int t;
|
||||
for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++)
|
||||
if (validevent(t, e))
|
||||
luaI_IMtable[-t].int_method[e] = *func;
|
||||
*luaI_getim(t, e) = *func;
|
||||
}
|
||||
|
||||
void luaI_setfallback (void)
|
||||
@ -320,13 +312,13 @@ void luaI_setfallback (void)
|
||||
replace = errorFB;
|
||||
break;
|
||||
case 1: /* 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);
|
||||
oldfunc = *luaI_getim(LUA_T_NIL, IM_GETGLOBAL);
|
||||
*luaI_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaI_Address(func);
|
||||
replace = nilFB;
|
||||
break;
|
||||
case 2: { /* old arith fallback */
|
||||
int i;
|
||||
oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_POW];
|
||||
oldfunc = *luaI_getim(LUA_T_USERDATA, IM_POW);
|
||||
for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
|
||||
fillvalids(i, luaI_Address(func));
|
||||
replace = typeFB;
|
||||
@ -334,7 +326,7 @@ void luaI_setfallback (void)
|
||||
}
|
||||
case 3: { /* old order fallback */
|
||||
int i;
|
||||
oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_LT];
|
||||
oldfunc = *luaI_getim(LUA_T_USERDATA, IM_LT);
|
||||
for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
|
||||
fillvalids(i, luaI_Address(func));
|
||||
replace = typeFB;
|
||||
@ -343,7 +335,7 @@ void luaI_setfallback (void)
|
||||
default: {
|
||||
int e;
|
||||
if ((e = luaI_findstring(name, luaI_eventname)) >= 0) {
|
||||
oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[e];
|
||||
oldfunc = *luaI_getim(LUA_T_USERDATA, e);
|
||||
fillvalids(e, luaI_Address(func));
|
||||
replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
|
||||
}
|
||||
|
13
fallback.h
13
fallback.h
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: fallback.h,v 1.21 1997/04/02 23:04:12 roberto Exp roberto $
|
||||
** $Id: fallback.h,v 1.22 1997/04/04 22:24:51 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef fallback_h
|
||||
@ -35,8 +35,15 @@ typedef enum {
|
||||
|
||||
#define IM_N 18
|
||||
|
||||
|
||||
extern struct IM {
|
||||
TObject int_method[IM_N];
|
||||
} *luaI_IMtable;
|
||||
|
||||
extern char *luaI_eventname[];
|
||||
|
||||
#define luaI_getim(tag,event) (&luaI_IMtable[-(tag)].int_method[event])
|
||||
#define luaI_getimbyObj(o,e) (luaI_getim(luaI_efectivetag(o),(e)))
|
||||
|
||||
void luaI_setfallback (void);
|
||||
int luaI_ref (TObject *object, int lock);
|
||||
@ -47,10 +54,8 @@ char *luaI_travfallbacks (int (*fn)(TObject *));
|
||||
|
||||
void luaI_settag (int tag, TObject *o);
|
||||
void luaI_realtag (int tag);
|
||||
TObject *luaI_getim (int tag, IMS event);
|
||||
#define luaI_getimbyObj(o,e) (luaI_getim(luaI_tag(o),(e)))
|
||||
TObject *luaI_geterrorim (void);
|
||||
int luaI_tag (TObject *o);
|
||||
int luaI_efectivetag (TObject *o);
|
||||
void luaI_settagmethod (void);
|
||||
void luaI_gettagmethod (void);
|
||||
void luaI_seterrormethod (void);
|
||||
|
32
opcode.c
32
opcode.c
@ -3,7 +3,7 @@
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_opcode="$Id: opcode.c,v 4.2 1997/04/04 22:24:51 roberto Exp roberto $";
|
||||
char *rcs_opcode="$Id: opcode.c,v 4.3 1997/04/15 17:32:47 roberto Exp roberto $";
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <stdio.h>
|
||||
@ -180,11 +180,14 @@ static int lua_tostring (TObject *obj)
|
||||
*/
|
||||
static void adjust_top (StkId newtop)
|
||||
{
|
||||
TObject *nt;
|
||||
lua_checkstack(stack+newtop);
|
||||
nt = stack+newtop; /* warning: previous call may change stack */
|
||||
while (top < nt) ttype(top++) = LUA_T_NIL;
|
||||
top = nt; /* top could be bigger than newtop */
|
||||
if (newtop <= top-stack) /* int arith, since newtop may be out of stack */
|
||||
top = stack+newtop;
|
||||
else {
|
||||
TObject *nt;
|
||||
lua_checkstack(stack+newtop);
|
||||
nt = stack+newtop; /* warning: previous call may change stack */
|
||||
while (top < nt) ttype(top++) = LUA_T_NIL;
|
||||
}
|
||||
}
|
||||
|
||||
#define adjustC(nParams) adjust_top(CLS_current.base+nParams)
|
||||
@ -300,7 +303,7 @@ static void do_call (StkId base, int nResults)
|
||||
return;
|
||||
}
|
||||
/* adjust the number of results */
|
||||
if (nResults != MULT_RET && top - (stack+firstResult) != nResults)
|
||||
if (nResults != MULT_RET)
|
||||
adjust_top(firstResult+nResults);
|
||||
/* move results to base-1 (to erase parameters and function) */
|
||||
base--;
|
||||
@ -317,7 +320,7 @@ static void do_call (StkId base, int nResults)
|
||||
*/
|
||||
static void pushsubscript (void)
|
||||
{
|
||||
int tg = luaI_tag(top-2);
|
||||
int tg = luaI_efectivetag(top-2);
|
||||
TObject *im = luaI_getim(tg, IM_GETTABLE);
|
||||
if (ttype(top-2) == LUA_T_ARRAY && ttype(im) == LUA_T_NIL) {
|
||||
TObject *h = lua_hashget(avalue(top-2), top-1);
|
||||
@ -1033,9 +1036,18 @@ void lua_pushobject (lua_Object o)
|
||||
incr_top;
|
||||
}
|
||||
|
||||
int lua_tag (lua_Object o)
|
||||
int lua_tag (lua_Object lo)
|
||||
{
|
||||
return (o == LUA_NOOBJECT) ? LUA_T_NIL : luaI_tag(Address(o));
|
||||
if (lo == LUA_NOOBJECT) return LUA_T_NIL;
|
||||
else {
|
||||
TObject *o = Address(lo);
|
||||
lua_Type t = ttype(o);
|
||||
if (t == LUA_T_USERDATA)
|
||||
return o->value.ts->tag;
|
||||
else if (t == LUA_T_ARRAY)
|
||||
return o->value.a->htag;
|
||||
else return t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user