From e78cf96c971234ea25e35a9672ef00ea389d843f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 24 Oct 1997 15:17:24 -0200 Subject: [PATCH] first version of Cclosures. --- lapi.c | 119 ++++++++++++++++++++++++++++------------------------- lbuiltin.c | 12 +++--- ldo.c | 95 ++++++++++++++++++++++-------------------- lfunc.c | 12 +++++- lfunc.h | 3 +- lgc.c | 6 +-- lobject.c | 7 ++-- lobject.h | 12 +++--- lopcodes.h | 6 +-- ltable.c | 5 +-- ltm.c | 27 +++++++----- lua.h | 6 ++- lua.stx | 4 +- lvm.c | 6 +-- makefile | 10 ++--- 15 files changed, 179 insertions(+), 151 deletions(-) diff --git a/lapi.c b/lapi.c index d67ecd45..c3c4af08 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ +** $Id: lapi.c,v 1.2 1997/09/26 15:02:26 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -94,6 +94,18 @@ lua_Object lua_lua2C (int number) } +lua_Object lua_upvalue (int n) +{ + TObject *f = luaD_stack.stack+luaD_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]; + incr_top; + return put_luaObjectonTop(); +} + + int lua_callfunction (lua_Object function) { if (function == LUA_NOOBJECT) @@ -227,8 +239,7 @@ int lua_isuserdata (lua_Object o) int lua_iscfunction (lua_Object o) { - int t = lua_tag(o); - return (t == LUA_T_CMARK) || (t == LUA_T_CFUNCTION); + return (o != LUA_NOOBJECT) && (lua_tag(o) == LUA_T_CPROTO); } int lua_isnumber (lua_Object o) @@ -244,9 +255,8 @@ int lua_isstring (lua_Object o) int lua_isfunction (lua_Object o) { - int t = lua_tag(o); - return (t == LUA_T_FUNCTION) || (t == LUA_T_CFUNCTION) || - (t == LUA_T_MARK) || (t == LUA_T_CMARK); + return (o != LUA_NOOBJECT) && ((ttype(Address(o)) == LUA_T_FUNCTION) || + (ttype(Address(o)) == LUA_T_MARK)); } @@ -273,10 +283,9 @@ void *lua_getuserdata (lua_Object object) lua_CFunction lua_getcfunction (lua_Object object) { - if (object == LUA_NOOBJECT || ((ttype(Address(object)) != LUA_T_CFUNCTION) && - (ttype(Address(object)) != LUA_T_CMARK))) - return NULL; - else return (fvalue(Address(object))); + if (!lua_iscfunction(object)) + return NULL; + else return fvalue(protovalue(Address(object))); } @@ -305,15 +314,19 @@ void lua_pushstring (char *s) luaC_checkGC(); } -void lua_pushcfunction (lua_CFunction fn) +void lua_pushCclosure (lua_CFunction fn, int n) { - if (fn == NULL) + if (fn == NULL) { ttype(luaD_stack.top) = LUA_T_NIL; - else { - ttype(luaD_stack.top) = LUA_T_CFUNCTION; - fvalue(luaD_stack.top) = fn; + incr_top; + } + else { + checkCparams(n); + ttype(luaD_stack.top) = LUA_T_CPROTO; + fvalue(luaD_stack.top) = fn; + incr_top; + luaV_closure(n); } - incr_top; } void lua_pushusertag (void *u, int tag) @@ -339,8 +352,6 @@ void lua_pushobject (lua_Object o) *luaD_stack.top = *Address(o); if (ttype(luaD_stack.top) == LUA_T_MARK) ttype(luaD_stack.top) = LUA_T_FUNCTION; - else if (ttype(luaD_stack.top) == LUA_T_CMARK) - ttype(luaD_stack.top) = LUA_T_CFUNCTION; incr_top; } @@ -350,12 +361,11 @@ int lua_tag (lua_Object lo) if (lo == LUA_NOOBJECT) return LUA_T_NIL; else { TObject *o = Address(lo); - lua_Type t = ttype(o); + int t = luaT_efectivetag(o); if (t == LUA_T_USERDATA) return o->value.ts->u.d.tag; - else if (t == LUA_T_ARRAY) - return o->value.a->htag; - else return t; + else + return t; } } @@ -395,7 +405,7 @@ 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 || luaD_stack.stack[i].ttype == LUA_T_CMARK) + if (luaD_stack.stack[i].ttype == LUA_T_MARK) if (level-- == 0) return Ref(luaD_stack.stack+i); return LUA_NOOBJECT; @@ -413,24 +423,27 @@ lua_Object lua_getlocal (lua_Function func, int local_number, char **name) { TObject *f = luaA_Address(func); /* check whether func is a Lua function */ - if (ttype(f) != LUA_T_MARK && ttype(f) != LUA_T_FUNCTION) + if (!(f->ttype == LUA_T_MARK || f->ttype == LUA_T_FUNCTION)) return LUA_NOOBJECT; - *name = luaF_getlocalname(f->value.tf, local_number, lua_currentline(func)); - if (*name) { - /* if "*name", there must be a LUA_T_LINE */ - /* therefore, f+2 points to function base */ - return Ref((f+2)+(local_number-1)); + else { + TProtoFunc *fp = protovalue(f)->value.tf; + *name = luaF_getlocalname(fp, local_number, lua_currentline(func)); + if (*name) { + /* if "*name", there must be a LUA_T_LINE */ + /* therefore, f+2 points to function base */ + return Ref((f+2)+(local_number-1)); + } + else + return LUA_NOOBJECT; } - else - return LUA_NOOBJECT; } int lua_setlocal (lua_Function func, int local_number) { TObject *f = Address(func); - char *name = luaF_getlocalname(f->value.tf, local_number, - lua_currentline(func)); + TProtoFunc *fp = protovalue(f)->value.tf; + char *name = luaF_getlocalname(fp, local_number, lua_currentline(func)); checkCparams(1); --luaD_stack.top; if (name) { @@ -447,16 +460,18 @@ int lua_setlocal (lua_Function func, int local_number) void lua_funcinfo (lua_Object func, char **filename, int *linedefined) { TObject *f = Address(func); - if (f->ttype == LUA_T_MARK || f->ttype == LUA_T_FUNCTION) - { - TProtoFunc *fp = f->value.cl->consts[0].value.tf; - *filename = fp->fileName->str; - *linedefined = fp->lineDefined; - } - else if (f->ttype == LUA_T_CMARK || f->ttype == LUA_T_CFUNCTION) - { - *filename = "(C)"; - *linedefined = -1; + if (!(ttype(f) == LUA_T_MARK || ttype(f) == LUA_T_FUNCTION)) + lua_error("API - `funcinfo' called with a non-function value"); + else { + f = protovalue(f); + if (ttype(f) == LUA_T_PROTO) { + *filename = tfvalue(f)->fileName->str; + *linedefined = tfvalue(f)->lineDefined; + } + else { + *filename = "(C)"; + *linedefined = -1; + } } } @@ -464,17 +479,10 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined) static TObject *functofind; static int checkfunc (TObject *o) { - if (o->ttype == LUA_T_FUNCTION) - return - ((functofind->ttype == LUA_T_FUNCTION || - functofind->ttype == LUA_T_MARK) && - (functofind->value.cl == o->value.cl)); - else if (o->ttype == LUA_T_CFUNCTION) - return - ((functofind->ttype == LUA_T_CFUNCTION || - functofind->ttype == LUA_T_CMARK) && - (functofind->value.f == o->value.f)); - else return 0; + return (o->ttype == LUA_T_FUNCTION) && + ((functofind->ttype == LUA_T_FUNCTION) || + (functofind->ttype == LUA_T_MARK)) && + (functofind->value.cl == o->value.cl); } @@ -548,8 +556,9 @@ static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults) { StkId base = (luaD_stack.top-luaD_stack.stack)-nParams; luaD_openstack(nParams); - luaD_stack.stack[base].ttype = LUA_T_CFUNCTION; + luaD_stack.stack[base].ttype = LUA_T_CPROTO; luaD_stack.stack[base].value.f = f; + luaF_simpleclosure(luaD_stack.stack+base); luaD_call(base+1, nResults); } diff --git a/lbuiltin.c b/lbuiltin.c index 902fe124..6cfd3a37 100644 --- a/lbuiltin.c +++ b/lbuiltin.c @@ -1,5 +1,5 @@ /* -** $Id: lbuiltin.c,v 1.3 1997/10/18 16:33:36 roberto Exp roberto $ +** $Id: lbuiltin.c,v 1.4 1997/10/23 16:28:48 roberto Exp roberto $ ** Built-in functions ** See Copyright Notice in lua.h */ @@ -12,6 +12,7 @@ #include "lauxlib.h" #include "lbuiltin.h" #include "ldo.h" +#include "lfunc.h" #include "lmem.h" #include "lobject.h" #include "lstring.h" @@ -164,10 +165,6 @@ static char *to_string (lua_Object obj) sprintf(buff, "function: %p", o->value.cl); return buff; } - case LUA_T_CFUNCTION: { - sprintf(buff, "cfunction: %p", o->value.f); - return buff; - } case LUA_T_USERDATA: { sprintf(buff, "userdata: %p", o->value.ts->u.d.v); return buff; @@ -382,6 +379,7 @@ static void testC (void) break; case 'c': reg[getnum(s)] = lua_createtable(); break; + case 'C': lua_pushCclosure(testC, getnum(s)); break; case 'P': reg[getnum(s)] = lua_pop(); break; case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; } case 'G': { int n = getnum(s); @@ -401,6 +399,7 @@ static void testC (void) case 'I': reg[getnum(s)] = lua_rawgettable(); break; case 't': lua_settable(); break; case 'T': lua_rawsettable(); break; + case 'U': { int n=getnum(s); reg[n]=lua_upvalue(getnum(s)); break; } default: luaL_verror("unknown command in `testC': %c", *(s-1)); } if (*s == 0) return; @@ -462,10 +461,11 @@ void luaB_predefine (void) /* pre-register mem error messages, to avoid loop when error arises */ luaS_newfixedstring(tableEM); luaS_newfixedstring(memEM); - o.ttype = LUA_T_CFUNCTION; for (i=0; ivalue.tf->fileName->str, - f->value.tf->lineDefined); + if (type == LUA_T_PROTO) + (*lua_callhook)(Ref(f), tfvalue(protovalue(f))->fileName->str, + tfvalue(protovalue(f))->lineDefined); else (*lua_callhook)(Ref(f), "(C)", -1); } @@ -153,10 +189,10 @@ static StkId callC (lua_CFunction func, StkId base) luaD_Cstack.lua2C = base; luaD_Cstack.base = base+luaD_Cstack.num; /* == top-stack */ if (lua_callhook) - luaD_callHook(base, LUA_T_CMARK, 0); + luaD_callHook(base, LUA_T_CPROTO, 0); (*func)(); if (lua_callhook) /* func may have changed lua_callhook */ - luaD_callHook(base, LUA_T_CMARK, 1); + luaD_callHook(base, LUA_T_CPROTO, 1); firstResult = luaD_Cstack.base; luaD_Cstack = oldCLS; return firstResult; @@ -182,13 +218,11 @@ void luaD_call (StkId base, int nResults) StkId firstResult; TObject *func = luaD_stack.stack+base-1; int i; - if (ttype(func) == LUA_T_CFUNCTION) { - ttype(func) = LUA_T_CMARK; - firstResult = callC(fvalue(func), base); - } - else if (ttype(func) == LUA_T_FUNCTION) { + if (ttype(func) == LUA_T_FUNCTION) { + TObject *proto = protovalue(func); ttype(func) = LUA_T_MARK; - firstResult = luaV_execute(func->value.cl, base); + firstResult = (ttype(proto) == LUA_T_CPROTO) ? callC(fvalue(proto), base) + : luaV_execute(func->value.cl, base); } else { /* func is not a function */ /* Check the tag method for invalid functions */ @@ -222,39 +256,12 @@ void luaD_travstack (int (*fn)(TObject *)) } -/* -** Error messages -*/ - -static void auxerrorim (char *form) -{ - lua_Object s = lua_getparam(1); - if (lua_isstring(s)) - fprintf(stderr, form, lua_getstring(s)); -} - - -static void emergencyerrorf (void) -{ - auxerrorim("THERE WAS AN ERROR INSIDE AN ERROR METHOD:\n%s\n"); -} - - -static void stderrorim (void) -{ - auxerrorim("lua: %s\n"); -} - - -TObject luaD_errorim = {LUA_T_CFUNCTION, {stderrorim}}; - static void message (char *s) { TObject im = luaD_errorim; if (ttype(&im) != LUA_T_NIL) { - luaD_errorim.ttype = LUA_T_CFUNCTION; - luaD_errorim.value.f = emergencyerrorf; + luaD_errorim = emergencyerror; lua_pushstring(s); luaD_callTM(&im, 1, 0); luaD_errorim = im; @@ -291,7 +298,7 @@ static void do_callinc (int nResults) /* ** Execute a protected call. Assumes that function is at luaD_Cstack.base and -** parameters are on luaD_stack.top of it. Leave nResults on the luaD_stack.stack. +** parameters are on top of it. Leave nResults on the stack. */ int luaD_protectedrun (int nResults) { diff --git a/lfunc.c b/lfunc.c index a29894e2..7270e980 100644 --- a/lfunc.c +++ b/lfunc.c @@ -1,5 +1,5 @@ /* -** $Id: lfunc.c,v 1.3 1997/10/16 10:59:34 roberto Exp roberto $ +** $Id: lfunc.c,v 1.4 1997/10/23 16:26:37 roberto Exp roberto $ ** Lua Funcion auxiliar ** See Copyright Notice in lua.h */ @@ -23,10 +23,20 @@ Closure *luaF_newclosure (int nelems) Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject)); luaO_insertlist(&luaF_rootcl, (GCnode *)c); luaO_nblocks += gcsizeclosure(c); + c->nelems = nelems; return c; } +void luaF_simpleclosure (TObject *o) +{ + Closure *c = luaF_newclosure(0); + c->consts[0] = *o; + ttype(o) = LUA_T_FUNCTION; + clvalue(o) = c; +} + + TProtoFunc *luaF_newproto (void) { TProtoFunc *f = luaM_new(TProtoFunc); diff --git a/lfunc.h b/lfunc.h index 4a7a6ac9..b6e14e26 100644 --- a/lfunc.h +++ b/lfunc.h @@ -1,5 +1,5 @@ /* -** $Id: lfunc.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ +** $Id: lfunc.h,v 1.2 1997/09/26 16:46:20 roberto Exp roberto $ ** Lua Function structures ** See Copyright Notice in lua.h */ @@ -19,6 +19,7 @@ TProtoFunc *luaF_newproto (void); Closure *luaF_newclosure (int nelems); void luaF_freeproto (TProtoFunc *l); void luaF_freeclosure (Closure *l); +void luaF_simpleclosure (TObject *o); char *luaF_getlocalname (TProtoFunc *func, int local_number, int line); diff --git a/lgc.c b/lgc.c index 0ea948f6..aafc4cb8 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 1.4 1997/10/16 10:59:34 roberto Exp roberto $ +** $Id: lgc.c,v 1.5 1997/10/23 16:26:37 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -97,7 +97,7 @@ static int ismarked (TObject *o) return o->value.tf->head.marked; case LUA_T_ARRAY: return o->value.a->head.marked; - default: /* nil, number or cfunction */ + default: /* nil, number or cproto */ return 1; } } @@ -234,7 +234,7 @@ static int markobject (TObject *o) case LUA_T_PROTO: protomark(o->value.tf); break; - default: break; /* numbers, cfunctions, etc */ + default: break; /* numbers, cprotos, etc */ } return 0; } diff --git a/lobject.c b/lobject.c index 71579156..8c3c11f7 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 1.3 1997/10/16 20:07:40 roberto Exp roberto $ +** $Id: lobject.c,v 1.4 1997/10/23 16:26:37 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -12,8 +12,8 @@ char *luaO_typenames[] = { /* ORDER LUA_T */ - "userdata", "number", "string", "table", "function", "function", - "nil", "prototype", "mark", "cmark", "line", NULL + "userdata", "number", "string", "table", "prototype", "cprototype", + "nil", "function", "mark", "cmark", "line", NULL }; @@ -48,7 +48,6 @@ int luaO_equalObj (TObject *t1, TObject *t2) case LUA_T_STRING: case LUA_T_USERDATA: return svalue(t1) == svalue(t2); case LUA_T_ARRAY: return avalue(t1) == avalue(t2); case LUA_T_FUNCTION: return t1->value.cl == t2->value.cl; - case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2); default: lua_error("internal error in `lua_equalObj'"); return 0; /* UNREACHEABLE */ diff --git a/lobject.h b/lobject.h index ee97f8cd..7e9afd62 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 1.5 1997/10/16 20:07:40 roberto Exp roberto $ +** $Id: lobject.h,v 1.6 1997/10/23 16:26:37 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -39,12 +39,11 @@ typedef enum { LUA_T_NUMBER = -1, /* fixed tag for numbers */ LUA_T_STRING = -2, /* fixed tag for strings */ LUA_T_ARRAY = -3, /* tag default for tables (or arrays) */ - LUA_T_FUNCTION = -4, /* fixed tag for functions */ - LUA_T_CFUNCTION= -5, /* fixed tag for Cfunctions */ + LUA_T_PROTO = -4, /* fixed tag for functions */ + LUA_T_CPROTO = -5, /* fixed tag for Cfunctions */ LUA_T_NIL = -6, /* last "pre-defined" tag */ - LUA_T_PROTO = -7, + LUA_T_FUNCTION = -7, LUA_T_MARK = -8, - LUA_T_CMARK = -9, LUA_T_LINE = -10 } lua_Type; @@ -53,7 +52,7 @@ typedef enum { typedef union { - lua_CFunction f; /* LUA_T_CFUNCTION, LUA_T_CMARK */ + lua_CFunction f; /* LUA_T_CPROTO */ real n; /* LUA_T_NUMBER */ struct TaggedString *ts; /* LUA_T_STRING, LUA_T_USERDATA */ struct TProtoFunc *tf; /* LUA_T_PROTO */ @@ -132,6 +131,7 @@ typedef struct LocVar { #define fvalue(o) ((o)->value.f) #define tfvalue(o) ((o)->value.tf) +#define protovalue(o) (&(o)->value.cl->consts[0]) /* ** Closures diff --git a/lopcodes.h b/lopcodes.h index 11a88152..ba73cec4 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.9 1997/10/16 10:59:34 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.10 1997/10/16 21:14:47 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -135,9 +135,9 @@ IFTUPJMPW,/* w x - (x!=nil)? PC-=w */ IFFUPJMP,/* b x - (x==nil)? PC-=b */ IFFUPJMPW,/* w x - (x==nil)? PC-=w */ -CLOSURE,/* b v_b...v_1 proto c(proto) */ +CLOSURE,/* b proto v_b...v_1 c(proto) */ CLOSURE0,/* - proto c(proto) */ -CLOSURE1,/* - v_1 proto c(proto) */ +CLOSURE1,/* - proto v_1 c(proto) */ CALLFUNC,/* b c v_c...v_1 f r_b...r_1 f(v1,...,v_c) */ CALLFUNC0,/* b v_b...v_1 f - f(v1,...,v_b) */ diff --git a/ltable.c b/ltable.c index 71d49dcb..4ac0caab 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 1.3 1997/10/18 16:29:15 roberto Exp roberto $ +** $Id: ltable.c,v 1.4 1997/10/23 16:26:37 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -41,9 +41,6 @@ static long int hashindex (TObject *ref) case LUA_T_FUNCTION: h = (IntPoint)clvalue(ref); break; - case LUA_T_CFUNCTION: - h = (IntPoint)fvalue(ref); - break; case LUA_T_ARRAY: h = (IntPoint)avalue(ref); break; diff --git a/ltm.c b/ltm.c index 913c4b7d..980fd4d6 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 1.2 1997/09/26 15:02:26 roberto Exp roberto $ +** $Id: ltm.c,v 1.3 1997/10/16 20:07:40 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -93,8 +93,8 @@ static char validevents[NUM_TAGS][IM_N] = { /* ORDER LUA_T, ORDER IM */ {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_NUMBER */ {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */ {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */ -{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */ -{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */ +{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_PROTO */ +{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CPROTO */ {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* LUA_T_NIL */ }; @@ -145,14 +145,19 @@ void luaT_realtag (int tag) int luaT_efectivetag (TObject *o) { - lua_Type t = ttype(o); - if (t == LUA_T_USERDATA) { - int tag = o->value.ts->u.d.tag; - return (tag >= 0) ? LUA_T_USERDATA : tag; + int t; + switch (t = ttype(o)) { + case LUA_T_USERDATA: { + int tag = o->value.ts->u.d.tag; + return (tag >= 0) ? LUA_T_USERDATA : tag; + } + case LUA_T_ARRAY: + return o->value.a->htag; + case LUA_T_FUNCTION: case LUA_T_MARK: + return o->value.cl->consts[0].ttype; + default: + return t; } - else if (t == LUA_T_ARRAY) - return o->value.a->htag; - else return t; } @@ -163,7 +168,7 @@ TObject *luaT_gettagmethod (int t, char *event) if (validevent(t, e)) return luaT_getim(t,e); else - return luaT_getim(LUA_T_CMARK, IM_GETTABLE); /* always nil */ + return luaT_getim(LUA_T_NUMBER, IM_ADD); /* always nil */ } diff --git a/lua.h b/lua.h index 1cce56c0..91d80c02 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: $ +** $Id: lua.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ ** LUA - An Extensible Extension Language ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** e-mail: lua@tecgraf.puc-rio.br @@ -72,6 +72,7 @@ void lua_endblock (void); lua_Object lua_lua2C (int number); #define lua_getparam(_) lua_lua2C(_) #define lua_getresult(_) lua_lua2C(_) +lua_Object lua_upvalue (int n); int lua_isnil (lua_Object object); int lua_istable (lua_Object object); @@ -90,7 +91,7 @@ void *lua_getuserdata (lua_Object object); void lua_pushnil (void); void lua_pushnumber (float n); void lua_pushstring (char *s); -void lua_pushcfunction (lua_CFunction fn); +void lua_pushCclosure (lua_CFunction fn, int n); void lua_pushusertag (void *u, int tag); void lua_pushobject (lua_Object object); @@ -131,6 +132,7 @@ long lua_collectgarbage (long limit); #define lua_pushuserdata(u) lua_pushusertag(u, 0) +#define lua_pushcfunction(f) lua_pushCclosure(f, 0) diff --git a/lua.stx b/lua.stx index 13549e31..559d224f 100644 --- a/lua.stx +++ b/lua.stx @@ -1,6 +1,6 @@ %{ /* -** $Id: lua.stx,v 1.11 1997/10/16 10:59:34 roberto Exp roberto $ +** $Id: lua.stx,v 1.12 1997/10/18 16:46:39 roberto Exp roberto $ ** Syntax analizer and code generator ** See Copyright Notice in lua.h */ @@ -520,9 +520,9 @@ static void func_onstack (TProtoFunc *f) int c = next_constant(currState); ttype(&currState->f->consts[c]) = LUA_T_PROTO; currState->f->consts[c].value.tf = (currState+1)->f; - code_constant(c); for (i=0; iupvalues[i]); + code_constant(c); code_oparg(CLOSURE, 2, nupvalues, -nupvalues); } diff --git a/lvm.c b/lvm.c index 4b77c3a6..2f8aaea1 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 1.9 1997/10/13 22:12:04 roberto Exp roberto $ +** $Id: lvm.c,v 1.10 1997/10/16 10:59:34 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -79,8 +79,8 @@ int luaV_tostring (TObject *obj) void luaV_closure (int nelems) { Closure *c = luaF_newclosure(nelems); - memcpy(c->consts, luaD_stack.top-(nelems+1), (nelems+1)*sizeof(TObject)); - c->nelems = nelems; + c->consts[0] = *(luaD_stack.top-1); + memcpy(&c->consts[1], luaD_stack.top-(nelems+1), nelems*sizeof(TObject)); luaD_stack.top -= nelems; ttype(luaD_stack.top-1) = LUA_T_FUNCTION; (luaD_stack.top-1)->value.cl = c; diff --git a/makefile b/makefile index d562fecf..c2b0b2cd 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,5 @@ # -## $Id: makefile,v 1.2 1997/09/26 15:02:26 roberto Exp roberto $ +## $Id: makefile,v 1.3 1997/10/13 22:10:45 roberto Exp roberto $ ## Makefile ## See Copyright Notice in lua.h # @@ -90,15 +90,13 @@ clear : %.c : RCS/%.c,v co $@ - - lapi.o: lapi.c lapi.h lua.h lobject.h lauxlib.h ldo.h lfunc.h lgc.h \ lmem.h lstring.h ltable.h ltm.h luadebug.h lvm.h lauxlib.o: lauxlib.c lauxlib.h lua.h luadebug.h lbuiltin.o: lbuiltin.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h \ - lmem.h lstring.h ltable.h ltm.h -ldo.o: ldo.c lbuiltin.h ldo.h lobject.h lua.h lgc.h lmem.h lparser.h \ - lzio.h ltm.h luadebug.h lundump.h lvm.h + ldo.h lfunc.h lmem.h lstring.h ltable.h ltm.h +ldo.o: ldo.c lbuiltin.h ldo.h lobject.h lua.h lfunc.h lgc.h lmem.h \ + lparser.h lzio.h ltm.h luadebug.h lundump.h lvm.h lfunc.o: lfunc.c lfunc.h lobject.h lua.h lmem.h lgc.o: lgc.c ldo.h lobject.h lua.h lfunc.h lgc.h lmem.h lstring.h \ ltable.h ltm.h