From d6fd33e76fdeb919fd91860aa9d0f9524e6d415a Mon Sep 17 00:00:00 2001
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Wed, 30 Jan 2002 15:26:44 -0200
Subject: `eventtable' renamed to `metatable'

---
 lapi.c     | 32 ++++++++++++++++----------------
 lbaselib.c | 11 ++++++-----
 lgc.c      | 33 ++++++++++++++++-----------------
 liolib.c   | 12 ++++++------
 lobject.h  |  4 ++--
 lstate.c   | 10 +++++-----
 lstate.h   |  4 ++--
 lstring.c  |  2 +-
 ltable.c   |  2 +-
 ltests.c   | 16 ++++++++--------
 ltm.c      |  4 ++--
 lua.h      |  4 ++--
 lvm.c      |  4 ++--
 13 files changed, 69 insertions(+), 69 deletions(-)

diff --git a/lapi.c b/lapi.c
index 94511e3d..293d09e3 100644
--- a/lapi.c
+++ b/lapi.c
@@ -402,25 +402,25 @@ LUA_API void lua_newtable (lua_State *L) {
 }
 
 
-LUA_API void lua_geteventtable (lua_State *L, int objindex) {
+LUA_API void lua_getmetatable (lua_State *L, int objindex) {
   StkId obj;
-  Table *et;
+  Table *mt;
   lua_lock(L);
   obj = luaA_indexAcceptable(L, objindex);
   switch (ttype(obj)) {
     case LUA_TTABLE:
-      et = hvalue(obj)->eventtable;
+      mt = hvalue(obj)->metatable;
       break;
     case LUA_TUSERDATA:
-      et = uvalue(obj)->uv.eventtable;
+      mt = uvalue(obj)->uv.metatable;
       break;
     default:
-      et = hvalue(defaultet(L));
+      mt = hvalue(defaultmeta(L));
   }
-  if (et == hvalue(defaultet(L)))
+  if (mt == hvalue(defaultmeta(L)))
     setnilvalue(L->top);
   else
-    sethvalue(L->top, et);
+    sethvalue(L->top, mt);
   api_incr_top(L);
   lua_unlock(L);
 }
@@ -488,24 +488,24 @@ LUA_API void lua_setglobals (lua_State *L) {
 }
 
 
-LUA_API void lua_seteventtable (lua_State *L, int objindex) {
-  StkId obj, et;
+LUA_API void lua_setmetatable (lua_State *L, int objindex) {
+  StkId obj, mt;
   lua_lock(L);
   api_checknelems(L, 1);
   obj = luaA_indexAcceptable(L, objindex);
-  et = --L->top;
-  if (ttype(et) == LUA_TNIL)
-    et = defaultet(L);
-  api_check(L, ttype(et) == LUA_TTABLE);
+  mt = --L->top;
+  if (ttype(mt) == LUA_TNIL)
+    mt = defaultmeta(L);
+  api_check(L, ttype(mt) == LUA_TTABLE);
   switch (ttype(obj)) {
     case LUA_TTABLE:
-      hvalue(obj)->eventtable = hvalue(et);
+      hvalue(obj)->metatable = hvalue(mt);
       break;
     case LUA_TUSERDATA:
-      uvalue(obj)->uv.eventtable = hvalue(et);
+      uvalue(obj)->uv.metatable = hvalue(mt);
       break;
     default:
-      luaO_verror(L, "cannot change the event table of a %.20s",
+      luaO_verror(L, "cannot change the meta table of a %.20s",
                   luaT_typenames[ttype(obj)]);
   }
   lua_unlock(L);
diff --git a/lbaselib.c b/lbaselib.c
index 87e417aa..1b1d4d96 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -135,15 +135,15 @@ static int luaB_getglobal (lua_State *L) {
 }
 
 
-static int luaB_eventtable (lua_State *L) {
+static int luaB_metatable (lua_State *L) {
   luaL_check_type(L, 1, LUA_TTABLE);
   if (lua_isnone(L, 2))
-    lua_geteventtable(L, 1);
+    lua_getmetatable(L, 1);
   else {
     int t = lua_type(L, 2);
     luaL_arg_check(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil/table expected");
     lua_settop(L, 2);
-    lua_seteventtable(L, 1);
+    lua_setmetatable(L, 1);
   }
   return 1;
 }
@@ -447,7 +447,7 @@ static int luaB_coroutine (lua_State *L) {
   lua_cobegin(NL, n-1);
   lua_newuserdatabox(L, NL);
   lua_getstr(L, LUA_REGISTRYINDEX, "Coroutine");
-  lua_seteventtable(L, -2);
+  lua_setmetatable(L, -2);
   lua_pushcclosure(L, luaB_resume, 1);
   return 1;
 }
@@ -517,6 +517,7 @@ static int luaB_tinsert (lua_State *L) {
     v = 3;  /* function may be called with more than 3 args */
     pos = luaL_check_int(L, 2);  /* 2nd argument is the position */
   }
+  if (pos > n+1) n = pos-1;
   aux_setn(L, 1, n+1);  /* t.n = n+1 */
   for (; n>=pos; n--) {
     lua_rawgeti(L, 1, n);
@@ -665,7 +666,7 @@ static const luaL_reg base_funcs[] = {
   {"dofile", luaB_dofile},
   {"dostring", luaB_dostring},
   {"error", luaB_error},
-  {"eventtable", luaB_eventtable},
+  {"metatable", luaB_metatable},
   {"foreach", luaB_foreach},
   {"foreachi", luaB_foreachi},
   {"gcinfo", luaB_gcinfo},
diff --git a/lgc.c b/lgc.c
index fe098467..facdaf0b 100644
--- a/lgc.c
+++ b/lgc.c
@@ -122,13 +122,12 @@ static void markobject (GCState *st, TObject *o) {
 }
 
 
-static void checkstacksizes (lua_State *L, StkId lim) {
+static void checkstacksizes (lua_State *L) {
   int used = L->ci - L->base_ci;  /* number of `ci' in use */
   if (4*used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci)
     luaD_reallocCI(L, L->size_ci/2);  /* still big enough... */
-  if (lim < L->top) lim = L->top;
-  used = lim - L->stack;  /* part of stack in use */
-  if (3*used < L->stacksize && 2*BASIC_STACK_SIZE < L->stacksize)
+  used = L->top - L->stack;  /* part of stack in use */
+  if (2*(used+MAXSTACK) < L->stacksize && 2*BASIC_STACK_SIZE < L->stacksize)
     luaD_reallocstack(L, L->stacksize/2);  /* still big enough... */
 }
 
@@ -148,7 +147,7 @@ static void markstacks (GCState *st) {
     lim = (L1->stack_last - L1->ci->base > MAXSTACK) ? L1->ci->base+MAXSTACK
                                                      : L1->stack_last;
     for (; o<=lim; o++) setnilvalue(o);
-    checkstacksizes(L1, lim);
+    checkstacksizes(L1);
     lua_assert(L1->previous->next == L1 && L1->next->previous == L1);
     L1 = L1->next;
   } while (L1 != st->L);
@@ -158,9 +157,9 @@ static void markstacks (GCState *st) {
 static void markudet (GCState *st) {
   Udata *u;
   for (u = G(st->L)->rootudata; u; u = u->uv.next)
-    marktable(st, u->uv.eventtable);
+    marktable(st, u->uv.metatable);
   for (u = G(st->L)->tmudata; u; u = u->uv.next)
-    marktable(st, u->uv.eventtable);
+    marktable(st, u->uv.metatable);
 }
 
 
@@ -176,8 +175,8 @@ static void traversetable (GCState *st, Table *h) {
   const TObject *mode;
   int weakkey = 0;
   int weakvalue = 0;
-  marktable(st, h->eventtable);
-  mode = fasttm(st->L, h->eventtable, TM_WEAKMODE);
+  marktable(st, h->metatable);
+  mode = fasttm(st->L, h->metatable, TM_WEAKMODE);
   if (mode) {  /* weak table? must be cleared after GC... */
     h->mark = st->toclear;  /* put in the appropriate list */
     st->toclear = h;
@@ -204,10 +203,10 @@ static void traversetable (GCState *st, Table *h) {
 
 
 static void markall (GCState *st) {
-  lua_assert(hvalue(defaultet(st->L))->flags == cast(unsigned short, ~0));
+  lua_assert(hvalue(defaultmeta(st->L))->flags == cast(unsigned short, ~0));
                                                       /* table is unchanged */
   markstacks(st); /* mark all stacks */
-  markudet(st);  /* mark userdata's event tables */
+  markudet(st);  /* mark userdata's meta tables */
   while (st->tmark) {  /* traverse marked tables */
     Table *h = st->tmark;  /* get first table from list */
     st->tmark = h->mark;  /* remove it from list */
@@ -333,7 +332,7 @@ static void collectudata (lua_State *L) {
     }
     else {
       *p = curr->uv.next;
-      if (fasttm(L, curr->uv.eventtable, TM_GC) != NULL) {  /* gc event? */
+      if (fasttm(L, curr->uv.metatable, TM_GC) != NULL) {  /* gc event? */
         curr->uv.next = NULL;  /* link `curr' at the end of `collected' list */
         *lastcollected = curr;
         lastcollected = &curr->uv.next;
@@ -384,7 +383,7 @@ static void checkMbuffer (lua_State *L) {
 
 
 static void do1gcTM (lua_State *L, Udata *udata) {
-  const TObject *tm = fasttm(L, udata->uv.eventtable, TM_GC);
+  const TObject *tm = fasttm(L, udata->uv.metatable, TM_GC);
   if (tm != NULL) {
     setobj(L->top, tm);
     setuvalue(L->top+1, udata);
@@ -405,8 +404,8 @@ static void unprotectedcallGCTM (lua_State *L, void *pu) {
     udata->uv.next = G(L)->rootudata;  /* resurect it */
     G(L)->rootudata = udata;
     do1gcTM(L, udata);
-    /* mark udata as finalized (default event table) */
-    uvalue(L->top-1)->uv.eventtable = hvalue(defaultet(L));
+    /* mark udata as finalized (default meta table) */
+    uvalue(L->top-1)->uv.metatable = hvalue(defaultmeta(L));
     unmarkud(uvalue(L->top-1));
   }
   L->top--;
@@ -420,8 +419,8 @@ static void callGCTM (lua_State *L) {
     Udata *udata;
     if (luaD_runprotected(L, unprotectedcallGCTM, &udata) != 0) {
       /* `udata' generated an error during its gc */
-      /* mark it as finalized (default event table) */
-      udata->uv.eventtable = hvalue(defaultet(L));
+      /* mark it as finalized (default meta table) */
+      udata->uv.metatable = hvalue(defaultmeta(L));
     }
   }
   L->allowhooks = oldah;  /* restore hooks */
diff --git a/liolib.c b/liolib.c
index cc572a55..ad94e6f4 100644
--- a/liolib.c
+++ b/liolib.c
@@ -72,7 +72,7 @@ static int pushresult (lua_State *L, int i) {
 
 static int checkfile (lua_State *L, int findex, const char *tname) {
   int res;
-  lua_geteventtable(L, findex);
+  lua_getmetatable(L, findex);
   lua_getstr(L, LUA_REGISTRYINDEX, tname);
   res = lua_equal(L, -1, -2);
   lua_pop(L, 2);
@@ -112,7 +112,7 @@ static FILE *getopthandle (lua_State *L, int inout) {
 static void newfile (lua_State *L, FILE *f) {
   lua_newuserdatabox(L, f);
   lua_getstr(L, LUA_REGISTRYINDEX, FILEHANDLE);
-  lua_seteventtable(L, -2);
+  lua_setmetatable(L, -2);
 }
 
 
@@ -148,7 +148,7 @@ static int io_close (lua_State *L) {
   if (f != stdin && f != stdout && f != stderr) {
     lua_settop(L, 1);  /* make sure file is on top */
     lua_getstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE);
-    lua_seteventtable(L, 1);
+    lua_setmetatable(L, 1);
     status = (CLOSEFILE(L, f) == 0);
   }
   return pushresult(L, status);
@@ -693,13 +693,13 @@ static const luaL_reg iolib[] = {
 
 
 LUALIB_API int lua_iolibopen (lua_State *L) {
-  lua_newtable(L);  /* event table for FILEHANDLE */
+  lua_newtable(L);  /* meta table for FILEHANDLE */
   /* close files when collected */
   lua_pushcfunction(L, file_collect);
   lua_setstr(L, -2, "gc");
-  /* put new eventtable into registry */
+  /* put new metatable into registry */
   lua_setstr(L, LUA_REGISTRYINDEX, FILEHANDLE);
-  /* event table for CLOSEDFILEHANDLE */
+  /* meta table for CLOSEDFILEHANDLE */
   lua_newtable(L);
   lua_setstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE);
   luaL_openl(L, iolib);
diff --git a/lobject.h b/lobject.h
index 37bb16e8..6b3a45a9 100644
--- a/lobject.h
+++ b/lobject.h
@@ -120,7 +120,7 @@ typedef union TString {
 typedef union Udata {
   union L_Umaxalign dummy;  /* ensures maximum alignment for `local' udata */
   struct {
-    struct Table *eventtable;
+    struct Table *metatable;
     void *value;
     size_t len;  /* least bit reserved for gc mark */
     union Udata *next;  /* chain for list of all udata */
@@ -221,7 +221,7 @@ typedef struct Node {
 
 
 typedef struct Table {
-  struct Table *eventtable;
+  struct Table *metatable;
   TObject *array;  /* array part */
   Node *node;
   int sizearray;  /* size of `array' array */
diff --git a/lstate.c b/lstate.c
index 15b771ee..d5984794 100644
--- a/lstate.c
+++ b/lstate.c
@@ -69,10 +69,10 @@ static void f_luaopen (lua_State *L, void *ud) {
   G(L)->tmudata = NULL;
   G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
   stack_init(L, L, so->stacksize);  /* init stack */
-  /* create default event table with a dummy table, and then close the loop */
-  sethvalue(defaultet(L), NULL);
-  sethvalue(defaultet(L), luaH_new(L, 0, 4));
-  hvalue(defaultet(L))->eventtable = hvalue(defaultet(L));
+  /* create default meta table with a dummy table, and then close the loop */
+  sethvalue(defaultmeta(L), NULL);
+  sethvalue(defaultmeta(L), luaH_new(L, 0, 4));
+  hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L));
   sethvalue(gt(L), luaH_new(L, 0, 4));  /* table of globals */
   sethvalue(registry(L), luaH_new(L, 0, 0));  /* registry */
   luaS_resize(L, 4);  /* initial size of string table */
@@ -107,7 +107,7 @@ LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
   OL->next = L;
   L->previous = OL;
   stack_init(L, OL, stacksize);  /* init stack */
-  setobj(defaultet(L), defaultet(OL));  /* share default event table */
+  setobj(defaultmeta(L), defaultmeta(OL));  /* share default meta table */
   setobj(gt(L), gt(OL));  /* share table of globals */
   setobj(registry(L), registry(OL));  /* share registry */
   lua_unlock(OL);
diff --git a/lstate.h b/lstate.h
index 684c40b4..c7528824 100644
--- a/lstate.h
+++ b/lstate.h
@@ -53,8 +53,8 @@ struct lua_longjmp;  /* defined in ldo.c */
 ** reserve init of stack to store some global values
 */
 
-/* default event table (both for tables and udata) */
-#define defaultet(L)	(L->stack)
+/* default meta table (both for tables and udata) */
+#define defaultmeta(L)	(L->stack)
 
 /* table of globals */
 #define gt(L)	(L->stack + 1)
diff --git a/lstring.c b/lstring.c
index 27e3214b..c83a4d5e 100644
--- a/lstring.c
+++ b/lstring.c
@@ -88,7 +88,7 @@ Udata *luaS_newudata (lua_State *L, size_t s) {
   if (s & 1) s++;  /* make sure size is even */
   u = cast(Udata *, luaM_malloc(L, sizeudata(s)));
   u->uv.len = s;
-  u->uv.eventtable = hvalue(defaultet(L));
+  u->uv.metatable = hvalue(defaultmeta(L));
   u->uv.value = u + 1;
   /* chain it on udata list */
   u->uv.next = G(L)->rootudata;
diff --git a/ltable.c b/ltable.c
index 618e9d0b..13a9b544 100644
--- a/ltable.c
+++ b/ltable.c
@@ -273,7 +273,7 @@ static void rehash (lua_State *L, Table *t) {
 
 Table *luaH_new (lua_State *L, int narray, int lnhash) {
   Table *t = luaM_new(L, Table);
-  t->eventtable = hvalue(defaultet(L));
+  t->metatable = hvalue(defaultmeta(L));
   t->next = G(L)->roottable;
   G(L)->roottable = t;
   t->mark = t;
diff --git a/ltests.c b/ltests.c
index 2fe4d694..f7c0638a 100644
--- a/ltests.c
+++ b/ltests.c
@@ -344,14 +344,14 @@ static int unref (lua_State *L) {
   return 0;
 }
 
-static int eventtable (lua_State *L) {
+static int metatable (lua_State *L) {
   luaL_check_any(L, 1);
   if (lua_isnone(L, 2))
-    lua_geteventtable(L, 1);
+    lua_getmetatable(L, 1);
   else {
     lua_settop(L, 2);
     luaL_check_type(L, 2, LUA_TTABLE);
-    lua_seteventtable(L, 1);
+    lua_setmetatable(L, 1);
   }
   return 1;
 }
@@ -608,11 +608,11 @@ static int testC (lua_State *L) {
     else if EQ("dostring") {
       lua_dostring(L, luaL_check_string(L, getnum));
     }
-    else if EQ("seteventtable") {
-      lua_seteventtable(L, getnum);
+    else if EQ("setmetatable") {
+      lua_setmetatable(L, getnum);
     }
-    else if EQ("geteventtable") {
-      lua_geteventtable(L, getnum);
+    else if EQ("getmetatable") {
+      lua_getmetatable(L, getnum);
     }
     else if EQ("type") {
       lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
@@ -642,7 +642,7 @@ static const struct luaL_reg tests_funcs[] = {
   {"unref", unref},
   {"d2s", d2s},
   {"s2d", s2d},
-  {"eventtable", eventtable},
+  {"metatable", metatable},
   {"newuserdata", newuserdata},
   {"newuserdatabox", newuserdatabox},
   {"udataval", udataval},
diff --git a/ltm.c b/ltm.c
index f69c6f9f..5cc0a744 100644
--- a/ltm.c
+++ b/ltm.c
@@ -57,9 +57,9 @@ const TObject *luaT_gettmbyobj (lua_State *L, const TObject *o, TMS event) {
   TString *ename = G(L)->tmname[event];
   switch (ttype(o)) {
     case LUA_TTABLE:
-      return luaH_getstr(hvalue(o)->eventtable, ename);
+      return luaH_getstr(hvalue(o)->metatable, ename);
     case LUA_TUSERDATA:
-      return luaH_getstr(uvalue(o)->uv.eventtable, ename);
+      return luaH_getstr(uvalue(o)->uv.metatable, ename);
     default:
       return &luaO_nilobject;
   }
diff --git a/lua.h b/lua.h
index 89b5818d..776f0e36 100644
--- a/lua.h
+++ b/lua.h
@@ -153,7 +153,7 @@ LUA_API void  lua_gettable (lua_State *L, int index);
 LUA_API void  lua_rawget (lua_State *L, int index);
 LUA_API void  lua_rawgeti (lua_State *L, int index, int n);
 LUA_API void  lua_newtable (lua_State *L);
-LUA_API void  lua_geteventtable (lua_State *L, int objindex);
+LUA_API void  lua_getmetatable (lua_State *L, int objindex);
 
 
 /*
@@ -164,7 +164,7 @@ LUA_API void  lua_settable (lua_State *L, int index);
 LUA_API void  lua_rawset (lua_State *L, int index);
 LUA_API void  lua_rawseti (lua_State *L, int index, int n);
 LUA_API void  lua_setglobals (lua_State *L);
-LUA_API void  lua_seteventtable (lua_State *L, int objindex);
+LUA_API void  lua_setmetatable (lua_State *L, int objindex);
 
 
 /*
diff --git a/lvm.c b/lvm.c
index 8ecf0fb8..a3ced1bc 100644
--- a/lvm.c
+++ b/lvm.c
@@ -125,7 +125,7 @@ void luaV_gettable (lua_State *L, StkId t, TObject *key, StkId res) {
   const TObject *tm;
   init:
   if (ttype(t) == LUA_TTABLE) {  /* `t' is a table? */
-    Table *et = hvalue(t)->eventtable;
+    Table *et = hvalue(t)->metatable;
     if ((tm = fasttm(L, et, TM_GETTABLE)) == NULL) {  /* no gettable TM? */
       const TObject *h = luaH_get(hvalue(t), key);  /* do a primitive get */
       /* result is no nil or there is no `index' tag method? */
@@ -158,7 +158,7 @@ void luaV_settable (lua_State *L, StkId t, TObject *key, StkId val) {
   const TObject *tm;
   init:
   if (ttype(t) == LUA_TTABLE) {  /* `t' is a table? */
-    Table *et = hvalue(t)->eventtable;
+    Table *et = hvalue(t)->metatable;
     if ((tm = fasttm(L, et, TM_SETTABLE)) == NULL) {  /* no TM? */
       luaH_set(L, hvalue(t), key, val);  /* do a primitive set */
       return;
-- 
cgit v1.2.3-55-g6feb