From 99182c6872f173e8e91426a0dba1468769818681 Mon Sep 17 00:00:00 2001
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Fri, 18 Sep 2009 15:58:45 -0300
Subject: [PATCH] references must start after predefined values in the registry

---
 lauxlib.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/lauxlib.c b/lauxlib.c
index 41251c1b..bdbe576c 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lauxlib.c,v 1.189 2009/07/15 17:26:14 roberto Exp roberto $
+** $Id: lauxlib.c,v 1.190 2009/07/15 17:47:34 roberto Exp roberto $
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 */
@@ -443,9 +443,7 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
 */
 
 /* number of prereserved references (for internal use) */
-#define RESERVED_REFS   1	/* only FREELIST_REF is reserved */
-
-#define FREELIST_REF	1	/* free list of references */
+#define FREELIST_REF	(LUA_RIDX_LAST + 1)	/* free list of references */
 
 
 LUALIB_API int luaL_ref (lua_State *L, int t) {
@@ -463,10 +461,12 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
     lua_rawseti(L, t, FREELIST_REF);  /* (t[FREELIST_REF] = t[ref]) */
   }
   else {  /* no free elements */
-    ref = (int)lua_objlen(L, t);
-    if (ref < RESERVED_REFS)
-      ref = RESERVED_REFS;  /* skip reserved references */
-    ref++;  /* create new reference */
+    ref = (int)lua_objlen(L, t) + 1;  /* get a new reference */
+    if (ref == FREELIST_REF) {  /* FREELIST_REF not initialized? */
+      lua_pushinteger(L, 0);
+      lua_rawseti(L, t, FREELIST_REF);
+      ref = FREELIST_REF + 1;
+    }
   }
   lua_rawseti(L, t, ref);
   return ref;