[vlc-devel] commit: Do not store p_this in a table value visible from lua code. ( Antoine Cellerier )
git version control
git at videolan.org
Sat Feb 27 22:05:40 CET 2010
vlc | branch: master | Antoine Cellerier <dionoea at videolan.org> | Fri Feb 26 11:56:32 2010 +0100| [2c24456389d6518d0d429c0c4f41e5bf52c2d793] | committer: Antoine Cellerier
Do not store p_this in a table value visible from lua code.
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=2c24456389d6518d0d429c0c4f41e5bf52c2d793
---
modules/misc/lua/demux.c | 3 +--
modules/misc/lua/extension.c | 5 ++---
modules/misc/lua/intf.c | 6 ++----
modules/misc/lua/libs/misc.c | 16 +++++++++++-----
modules/misc/lua/meta.c | 5 ++---
modules/misc/lua/services_discovery.c | 3 +--
modules/misc/lua/vlc.h | 2 ++
7 files changed, 21 insertions(+), 19 deletions(-)
diff --git a/modules/misc/lua/demux.c b/modules/misc/lua/demux.c
index 601287f..c3d3595 100644
--- a/modules/misc/lua/demux.c
+++ b/modules/misc/lua/demux.c
@@ -135,14 +135,13 @@ static int probe_luascript( vlc_object_t *p_this, const char * psz_filename,
/* Load Lua libraries */
luaL_openlibs( L ); /* FIXME: Don't open all the libs? */
+ vlclua_set_this( L, p_demux );
luaL_register( L, "vlc", p_reg );
luaopen_msg( L );
luaopen_strings( L );
luaopen_stream( L );
luaopen_xml( L );
luaopen_md5( L );
- lua_pushlightuserdata( L, p_demux );
- lua_setfield( L, -2, "private" );
lua_pushstring( L, p_demux->psz_path );
lua_setfield( L, -2, "path" );
lua_pushstring( L, p_demux->psz_access );
diff --git a/modules/misc/lua/extension.c b/modules/misc/lua/extension.c
index a6fa103..174d18d 100644
--- a/modules/misc/lua/extension.c
+++ b/modules/misc/lua/extension.c
@@ -719,13 +719,12 @@ static lua_State* GetLuaState( extensions_manager_t *p_mgr,
msg_Err( p_mgr, "Could not create new Lua State" );
return NULL;
}
+ vlclua_set_this( L, p_mgr );
+
luaL_openlibs( L );
luaL_register( L, "vlc", p_reg );
luaopen_msg( L );
- lua_pushlightuserdata( L, p_mgr );
- lua_setfield( L, -2, "private" );
-
lua_pushlightuserdata( L, p_ext );
lua_setfield( L, -2, "extension" );
diff --git a/modules/misc/lua/intf.c b/modules/misc/lua/intf.c
index 90bb782..3415907 100644
--- a/modules/misc/lua/intf.c
+++ b/modules/misc/lua/intf.c
@@ -168,15 +168,13 @@ int Open_LuaIntf( vlc_object_t *p_this )
goto error;
}
+ vlclua_set_this( L, p_intf );
+
luaL_openlibs( L );
/* register our functions */
luaL_register( L, "vlc", p_reg );
- /* store a pointer to p_intf (FIXME: user could overwrite this) */
- lua_pushlightuserdata( L, p_intf );
- lua_setfield( L, -2, "private" );
-
/* register submodules */
luaopen_acl( L );
luaopen_config( L );
diff --git a/modules/misc/lua/libs/misc.c b/modules/misc/lua/libs/misc.c
index 5e5e806..444092d 100644
--- a/modules/misc/lua/libs/misc.c
+++ b/modules/misc/lua/libs/misc.c
@@ -50,13 +50,19 @@
/*****************************************************************************
* Internal lua<->vlc utils
*****************************************************************************/
+void __vlclua_set_this( lua_State *L, vlc_object_t *p_this )
+{
+ lua_pushlightuserdata( L, __vlclua_set_this );
+ lua_pushlightuserdata( L, p_this );
+ lua_rawset( L, LUA_REGISTRYINDEX );
+}
+
vlc_object_t * vlclua_get_this( lua_State *L )
{
- vlc_object_t * p_this;
- lua_getglobal( L, "vlc" );
- lua_getfield( L, -1, "private" );
- p_this = (vlc_object_t*)lua_topointer( L, lua_gettop( L ) );
- lua_pop( L, 2 );
+ lua_pushlightuserdata( L, __vlclua_set_this );
+ lua_rawget( L, LUA_REGISTRYINDEX );
+ vlc_object_t *p_this = (vlc_object_t*)lua_topointer( L, -1 );
+ lua_pop( L, 1 );
return p_this;
}
diff --git a/modules/misc/lua/meta.c b/modules/misc/lua/meta.c
index b61ec4e..a56fd09 100644
--- a/modules/misc/lua/meta.c
+++ b/modules/misc/lua/meta.c
@@ -60,6 +60,8 @@ static lua_State * init( vlc_object_t *p_this, input_item_t * p_item, const char
return NULL;
}
+ vlclua_set_this( L, p_this );
+
/* Load Lua libraries */
luaL_openlibs( L ); /* XXX: Don't open all the libs? */
@@ -75,9 +77,6 @@ static lua_State * init( vlc_object_t *p_this, input_item_t * p_item, const char
luaopen_md5( L );
luaopen_input_item( L, p_item );
- lua_pushlightuserdata( L, p_this );
- lua_setfield( L, -2, "private" );
-
if( vlclua_add_modules_path( p_this, L, psz_filename ) )
{
msg_Warn( p_this, "Error while setting the module search path for %s",
diff --git a/modules/misc/lua/services_discovery.c b/modules/misc/lua/services_discovery.c
index f5e21fd..d5f3649 100644
--- a/modules/misc/lua/services_discovery.c
+++ b/modules/misc/lua/services_discovery.c
@@ -94,10 +94,9 @@ int Open_LuaSD( vlc_object_t *p_this )
msg_Err( p_sd, "Could not create new Lua State" );
goto error;
}
+ vlclua_set_this( L, p_sd );
luaL_openlibs( L );
luaL_register( L, "vlc", p_reg );
- lua_pushlightuserdata( L, p_sd );
- lua_setfield( L, -2, "private" );
luaopen_input( L );
luaopen_msg( L );
luaopen_misc( L );
diff --git a/modules/misc/lua/vlc.h b/modules/misc/lua/vlc.h
index e6e13d2..20b19fd 100644
--- a/modules/misc/lua/vlc.h
+++ b/modules/misc/lua/vlc.h
@@ -91,6 +91,8 @@ static inline const char *luaL_nilorcheckstring( lua_State *L, int narg )
return luaL_checkstring( L, narg );
}
+#define vlclua_set_this(a, b) __vlclua_set_this(a, VLC_OBJECT(b))
+void __vlclua_set_this( lua_State *, vlc_object_t * );
vlc_object_t * vlclua_get_this( lua_State * );
/*****************************************************************************
More information about the vlc-devel
mailing list