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)
/*****************************************************************************
*
*****************************************************************************/
-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 )
{
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\".",
};
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
*****************************************************************************/
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\".",
#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 */
/*****************************************************************************
*
*****************************************************************************/
+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 )
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.
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.