Commit 4417c123 authored by Antoine Cellerier's avatar Antoine Cellerier

Also load .luac files.

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)
parent e30ad750
......@@ -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\".",
......
......@@ -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\".",
......
......@@ -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.
......
......@@ -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.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment