]> git.sesse.net Git - vlc/commitdiff
Also load .luac files.
authorAntoine Cellerier <dionoea@videolan.org>
Sat, 6 Feb 2010 14:51:00 +0000 (15:51 +0100)
committerAntoine Cellerier <dionoea@videolan.org>
Sat, 6 Feb 2010 15:02:07 +0000 (16:02 +0100)
Lua source code can be compiled into binary files to speedup loading using the luac command line utility. VLC will first try loading the .luac module and fallback to .lua if it fails.
(And factorize code)

modules/misc/lua/intf.c
modules/misc/lua/services_discovery.c
modules/misc/lua/vlc.c
modules/misc/lua/vlc.h

index 70e590a2c7fe121390707d995e60290a7e146305..b9e1737f59b4c6b2fa9d90f9da384267b630251b 100644 (file)
@@ -59,35 +59,6 @@ static const char * const ppsz_intf_options[] = { "intf", "config", NULL };
 /*****************************************************************************
  *
  *****************************************************************************/
-static char *FindFile( vlc_object_t *p_this, const char *psz_name )
-{
-    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
-    char **ppsz_dir;
-    vlclua_dir_list( p_this, "intf", ppsz_dir_list );
-    for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
-    {
-        char *psz_filename;
-        struct stat st;
-
-        if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir,
-                      psz_name ) < 0 )
-        {
-            vlclua_dir_list_free( ppsz_dir_list );
-            return NULL;
-        }
-
-        if( utf8_stat( psz_filename, &st ) == 0
-         && S_ISREG( st.st_mode ) )
-        {
-            vlclua_dir_list_free( ppsz_dir_list );
-            return psz_filename;
-        }
-        free( psz_filename );
-    }
-    vlclua_dir_list_free( ppsz_dir_list );
-    return NULL;
-}
-
 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
                                             const luaL_Reg *l )
 {
@@ -182,7 +153,7 @@ int Open_LuaIntf( vlc_object_t *p_this )
         return VLC_ENOMEM;
     }
     p_sys = p_intf->p_sys;
-    p_sys->psz_filename = FindFile( p_this, psz_name );
+    p_sys->psz_filename = vlclua_find_file( p_this, "intf", psz_name );
     if( !p_sys->psz_filename )
     {
         msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
index d37133de265bcd7a876876efa9893922d2e63e9f..8e0216899874221a0e1eb8a69a3e991336b6caed 100644 (file)
@@ -49,37 +49,6 @@ struct services_discovery_sys_t
 };
 static const luaL_Reg p_reg[] = { { NULL, NULL } };
 
-/*****************************************************************************
- *
- *****************************************************************************/
-static char *FindFile( vlc_object_t *p_this, const char *psz_name )
-{
-    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
-    char **ppsz_dir;
-    vlclua_dir_list( p_this, "sd", ppsz_dir_list );
-    for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
-    {
-        char *psz_filename;
-        FILE *fp;
-        if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir,
-                      psz_name ) < 0 )
-        {
-            vlclua_dir_list_free( ppsz_dir_list );
-            return NULL;
-        }
-        fp = utf8_fopen( psz_filename, "r" );
-        if( fp )
-        {
-            fclose( fp );
-            vlclua_dir_list_free( ppsz_dir_list );
-            return psz_filename;
-        }
-        free( psz_filename );
-    }
-    vlclua_dir_list_free( ppsz_dir_list );
-    return NULL;
-}
-
 /*****************************************************************************
  * Open: initialize and create stuff
  *****************************************************************************/
@@ -95,7 +64,7 @@ int Open_LuaSD( vlc_object_t *p_this )
     if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
         return VLC_ENOMEM;
     p_sd->p_sys = p_sys;
-    p_sys->psz_filename = FindFile( p_this, psz_name );
+    p_sys->psz_filename = vlclua_find_file( p_this, "sd", psz_name );
     if( !p_sys->psz_filename )
     {
         msg_Err( p_sd, "Couldn't find lua services discovery script \"%s\".",
index dd1cb345d07d5ce12c51f286eb3a51557a1e73d2..591206a0e3d6d089bafad760d8448df716a09fe6 100644 (file)
@@ -41,6 +41,7 @@
 #include <vlc_charset.h>
 #include <vlc_aout.h>
 #include <vlc_services_discovery.h>
+#include <sys/stat.h>
 
 #include <lua.h>        /* Low level lua C API */
 #include <lauxlib.h>    /* Higher level C API */
@@ -129,10 +130,18 @@ vlc_module_end ()
 /*****************************************************************************
  *
  *****************************************************************************/
+static const char *ppsz_lua_exts[] = { ".luac", ".lua", NULL };
 static int file_select( const char *file )
 {
     int i = strlen( file );
-    return i > 4 && !strcmp( file+i-4, ".lua" );
+    int j;
+    for( j = 0; ppsz_lua_exts[j]; j++ )
+    {
+        int l = strlen( ppsz_lua_exts[j] );
+        if( !strcmp( file+i-l, ppsz_lua_exts[j] ) )
+            return 1;
+    }
+    return 0;
 }
 
 static int file_compare( const char **a, const char **b )
@@ -248,6 +257,37 @@ int vlclua_scripts_batch_execute( vlc_object_t *p_this,
     return i_ret;
 }
 
+char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
+{
+    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
+    char **ppsz_dir;
+    vlclua_dir_list( p_this, psz_luadirname, ppsz_dir_list );
+    for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
+    {
+        for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
+        {
+            char *psz_filename;
+            struct stat st;
+
+            if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
+                          psz_name, *ppsz_ext ) < 0 )
+            {
+                vlclua_dir_list_free( ppsz_dir_list );
+                return NULL;
+            }
+
+            if( utf8_stat( psz_filename, &st ) == 0
+                && S_ISREG( st.st_mode ) )
+            {
+                vlclua_dir_list_free( ppsz_dir_list );
+                return psz_filename;
+            }
+            free( psz_filename );
+        }
+    }
+    vlclua_dir_list_free( ppsz_dir_list );
+    return NULL;
+}
 
 /*****************************************************************************
  * Meta data setters utility.
index 8215dbcee18b5ab4aa991ad9ac005a99e72ee42c..34a08785c7632f37405495a1cacd50c322284a06 100644 (file)
@@ -110,6 +110,7 @@ int vlclua_scripts_batch_execute( vlc_object_t *p_this, const char * luadirname,
         lua_State * L, void * user_data );
 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname, char **ppsz_dir_list );
 void vlclua_dir_list_free( char **ppsz_dir_list );
+char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name );
 
 /*****************************************************************************
  * Playlist and meta data internal utilities.