From d2ebdc045bae7c8ed0e62729ca455444c076b1bb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 18 Feb 2009 14:20:56 -0300 Subject: [PATCH] new macro 'lua_checkversion' to check whether core and application are compatible --- lapi.c | 12 +++++++++++- lauxlib.c | 3 ++- lstate.c | 3 ++- lstate.h | 3 ++- lua.h | 6 +++++- 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lapi.c b/lapi.c index 9af75b17..86b591c4 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.67 2008/07/04 18:27:11 roberto Exp roberto $ +** $Id: lapi.c,v 2.68 2008/08/01 17:01:16 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -121,6 +121,16 @@ LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { } +LUA_API void lua_checkversion_ (lua_State *L, int version) { + lua_lock(L); + if (version != LUA_VERSION_NUM) + luaG_runerror(L, "application and Lua core using different Lua versions"); + if (G(L)->nilobjp != luaO_nilobject) + luaG_runerror(L, "application using two incompatible Lua VMs"); + lua_unlock(L); +} + + /* ** basic stack manipulation diff --git a/lauxlib.c b/lauxlib.c index 55ee1411..469553ae 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.180 2009/02/13 19:39:34 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.181 2009/02/17 14:31:16 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -585,6 +585,7 @@ static int libsize (const luaL_Reg *l) { LUALIB_API void luaL_register (lua_State *L, const char *libname, const luaL_Reg *l) { + lua_checkversion(L); if (libname) { /* check whether lib already exists */ luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); diff --git a/lstate.c b/lstate.c index be7eed91..e1a3237b 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.47 2008/08/26 13:27:42 roberto Exp roberto $ +** $Id: lstate.c,v 2.48 2009/02/17 19:47:58 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -175,6 +175,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { setnilvalue(registry(L)); luaZ_initbuffer(L, &g->buff); g->panic = NULL; + g->nilobjp = luaO_nilobject; g->gcstate = GCSpause; g->rootgc = obj2gco(L); g->sweepstrgc = 0; diff --git a/lstate.h b/lstate.h index c4106562..def5d7eb 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.35 2008/08/13 17:01:33 roberto Exp roberto $ +** $Id: lstate.h,v 2.36 2008/08/26 13:27:42 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -134,6 +134,7 @@ typedef struct global_State { UpVal uvhead; /* head of double-linked list of all open upvalues */ struct Table *mt[NUM_TAGS]; /* metatables for basic types */ TString *tmname[TM_N]; /* array with tag-method names */ + const TValue *nilobjp; /* pointer to nil object (to check consistency) */ } global_State; diff --git a/lua.h b/lua.h index aa3a6789..82101882 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.230 2008/07/18 19:58:10 roberto Exp roberto $ +** $Id: lua.h,v 1.231 2008/08/13 14:08:49 roberto Exp roberto $ ** Lua - An Extensible Extension Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -115,6 +115,10 @@ LUA_API lua_State *(lua_newthread) (lua_State *L); LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); +LUA_API void lua_checkversion_ (lua_State *L, int version); +#define lua_checkversion(L) (lua_checkversion_(L, LUA_VERSION_NUM)) + + /* ** basic stack manipulation */