Browse Source

first version of Cclosures.

Roberto Ierusalimschy 28 years ago
parent
commit
e78cf96c97
15 changed files with 177 additions and 149 deletions
  1. 62 53
      lapi.c
  2. 6 6
      lbuiltin.c
  3. 51 44
      ldo.c
  4. 11 1
      lfunc.c
  5. 2 1
      lfunc.h
  6. 3 3
      lgc.c
  7. 3 4
      lobject.c
  8. 6 6
      lobject.h
  9. 3 3
      lopcodes.h
  10. 1 4
      ltable.c
  11. 16 11
      ltm.c
  12. 4 2
      lua.h
  13. 2 2
      lua.stx
  14. 3 3
      lvm.c
  15. 4 6
      makefile

+ 62 - 53
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;
+    incr_top;
+  }
   else {
-    ttype(luaD_stack.top) = LUA_T_CFUNCTION;
+    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);
 }
 

+ 6 - 6
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; i<INTFUNCSIZE; i++) {
     ts = luaS_new(int_funcs[i].name);
     fvalue(&o) = int_funcs[i].func;
+    ttype(&o) = LUA_T_CPROTO;
+    luaF_simpleclosure(&o);
     luaS_rawsetglobal(ts, &o);
   }
   ts = luaS_new("_VERSION");

+ 51 - 44
ldo.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.c,v 1.3 1997/10/16 10:59:34 roberto Exp roberto $
+** $Id: ldo.c,v 1.4 1997/10/23 16:26:37 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -11,6 +11,7 @@
 
 #include "lbuiltin.h"
 #include "ldo.h"
+#include "lfunc.h"
 #include "lgc.h"
 #include "lmem.h"
 #include "lobject.h"
@@ -31,7 +32,7 @@
 
 static TObject initial_stack;
 
-struct Stack luaD_stack = {&initial_stack+1, &initial_stack, &initial_stack};
+struct Stack luaD_stack = {&initial_stack, &initial_stack, &initial_stack};
 
 
 struct C_Lua_Stack luaD_Cstack = {0, 0, 0};
@@ -40,6 +41,40 @@ static  jmp_buf *errorJmp = NULL; /* current error recover point */
 
 
 
+/*
+** 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 femergencyerror (void)
+{
+  auxerrorim("THERE WAS AN ERROR INSIDE AN ERROR METHOD:\n%s\n");
+}
+
+
+static void stderrorim (void)
+{
+  auxerrorim("lua: %s\n");
+}
+
+
+TObject luaD_errorim;
+static TObject emergencyerror;
+
+
+static void initCfunc (TObject *o, lua_CFunction f)
+{
+  ttype(o) = LUA_T_CPROTO;
+  fvalue(o) = f;
+  luaF_simpleclosure(o);
+}
 
 
 #define STACK_EXTRA 	32
@@ -50,8 +85,10 @@ static void initstack (int n)
   luaD_stack.stack = luaM_newvector(maxstack, TObject);
   luaD_stack.last = luaD_stack.stack+(maxstack-1);
   luaD_stack.top = luaD_stack.stack;
-  *(luaD_stack.top++) = initial_stack;
+  *luaD_stack.stack = initial_stack;
   luaB_predefine();
+  initCfunc(&luaD_errorim, stderrorim);
+  initCfunc(&emergencyerror, femergencyerror);
 }
 
 
@@ -79,7 +116,6 @@ void luaD_checkstack (int n)
 }
 
 
-
 /*
 ** Adjust stack. Set top to the given value, pushing NILs if needed.
 */
@@ -128,9 +164,9 @@ void luaD_callHook (StkId base, lua_Type type, int isreturn)
     (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
   else {
     TObject *f = luaD_stack.stack+base-1;
-    if (type == LUA_T_MARK)
-      (*lua_callhook)(Ref(f), f->value.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)
 {

+ 11 - 1
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);

+ 2 - 1
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);
 

+ 3 - 3
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;
 }

+ 3 - 4
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 */

+ 6 - 6
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

+ 3 - 3
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)  */

+ 1 - 4
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;

+ 16 - 11
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 */
 }
 
 

+ 4 - 2
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: [email protected]
@@ -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)
 
 
 

+ 2 - 2
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; i<nupvalues; i++)
     lua_pushvar((currState+1)->upvalues[i]);
+  code_constant(c);
   code_oparg(CLOSURE, 2, nupvalues, -nupvalues);
 }
 

+ 3 - 3
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;

+ 4 - 6
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