Skip to content

Commit

Permalink
re-work import path into system, user, packages; add namespace to eac…
Browse files Browse the repository at this point in the history
…h chugin @import (#473)

* separate chugin paths into system, user, package

* integration of new search paths with CLI chuck

* add @[chugin-import] namespace for @imported chugins
  • Loading branch information
gewang authored Oct 30, 2024
1 parent a0ce8c2 commit 3cd84b4
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 88 deletions.
70 changes: 31 additions & 39 deletions src/core/chuck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,11 @@
#define CHUCK_PARAM_TTY_WIDTH_HINT_DEFAULT "80"
// chugin-relate param defaults
#define CHUCK_PARAM_CHUGIN_ENABLE_DEFAULT "1"
#ifndef __PLATFORM_WINDOWS__
// 1.4.1.0 (ge) changed to ""; was "/usr/local/lib/chuck"
// redundant with g_default_chugin_path, which already contains
#define CHUCK_PARAM_CHUGIN_DIRECTORY_DEFAULT ""
#else // __PLATFORM_WINDOWS__
// 1.4.1.0 (ge) changed to ""; "C:\\Program Files\\ChucK\\chugins"
// redundant with g_default_chugin_path, which already contains
#define CHUCK_PARAM_CHUGIN_DIRECTORY_DEFAULT ""
#endif // __PLATFORM_WINDOWS__
#define CHUCK_PARAM_CHUGIN_LIST_USER_DEFAULT std::list<std::string>()
#define CHUCK_PARAM_CHUGIN_LIST_USER_DIR_DEFAULT std::list<std::string>()

#define CHUCK_PARAM_USER_CHUGINS_DEFAULT std::list<std::string>()
// import search paths defaults
#define CHUCK_PARAM_IMPORT_PATH_SYSTEM_DEFAULT std::list<std::string>()
#define CHUCK_PARAM_IMPORT_PATH_USER_DEFAULT std::list<std::string>()
#define CHUCK_PARAM_IMPORT_PATH_PACKAGES_DEFAULT std::list<std::string>()



Expand Down Expand Up @@ -205,18 +198,22 @@ void ChucK::initDefaultParams()
initParam( CHUCK_PARAM_AUTO_DEPEND, CHUCK_PARAM_AUTO_DEPEND_DEFAULT, ck_param_int );
initParam( CHUCK_PARAM_DEPRECATE_LEVEL, CHUCK_PARAM_DEPRECATE_LEVEL_DEFAULT, ck_param_int );
initParam( CHUCK_PARAM_WORKING_DIRECTORY, CHUCK_PARAM_WORKING_DIRECTORY_DEFAULT, ck_param_string );
initParam( CHUCK_PARAM_CHUGIN_DIRECTORY, CHUCK_PARAM_CHUGIN_DIRECTORY_DEFAULT, ck_param_string );
initParam( CHUCK_PARAM_CHUGIN_ENABLE, CHUCK_PARAM_CHUGIN_ENABLE_DEFAULT, ck_param_int );
initParam( CHUCK_PARAM_IS_REALTIME_AUDIO_HINT, CHUCK_PARAM_IS_REALTIME_AUDIO_HINT_DEFAULT, ck_param_int );
initParam( CHUCK_PARAM_COMPILER_HIGHLIGHT_ON_ERROR, CHUCK_PARAM_COMPILER_HIGHLIGHT_ON_ERROR_DEFAULT, ck_param_int );
initParam( CHUCK_PARAM_TTY_COLOR, CHUCK_PARAM_TTY_COLOR_DEFAULT, ck_param_int );
initParam( CHUCK_PARAM_TTY_WIDTH_HINT, CHUCK_PARAM_TTY_WIDTH_HINT_DEFAULT, ck_param_int );

// initialize list params manually (take care to use tolower())
m_listParams[tolower(CHUCK_PARAM_CHUGIN_LIST_USER)] = CHUCK_PARAM_CHUGIN_LIST_USER_DEFAULT;
m_param_types[tolower(CHUCK_PARAM_CHUGIN_LIST_USER)] = ck_param_string_list;
m_listParams[tolower(CHUCK_PARAM_CHUGIN_LIST_IMPORT_PATHS)] = CHUCK_PARAM_CHUGIN_LIST_USER_DIR_DEFAULT;
m_param_types[tolower(CHUCK_PARAM_CHUGIN_LIST_IMPORT_PATHS)] = ck_param_string_list;
m_listParams[tolower(CHUCK_PARAM_USER_CHUGINS)] = CHUCK_PARAM_USER_CHUGINS_DEFAULT;
m_param_types[tolower(CHUCK_PARAM_USER_CHUGINS)] = ck_param_string_list;
// import search paths
m_listParams[tolower(CHUCK_PARAM_IMPORT_PATH_SYSTEM)] = CHUCK_PARAM_IMPORT_PATH_SYSTEM_DEFAULT;
m_param_types[tolower(CHUCK_PARAM_IMPORT_PATH_SYSTEM)] = ck_param_string_list;
m_listParams[tolower(CHUCK_PARAM_IMPORT_PATH_USER)] = CHUCK_PARAM_IMPORT_PATH_USER_DEFAULT;
m_param_types[tolower(CHUCK_PARAM_IMPORT_PATH_USER)] = ck_param_string_list;
m_listParams[tolower(CHUCK_PARAM_IMPORT_PATH_PACKAGES)] = CHUCK_PARAM_IMPORT_PATH_PACKAGES_DEFAULT;
m_param_types[tolower(CHUCK_PARAM_IMPORT_PATH_PACKAGES)] = ck_param_string_list;
}


Expand Down Expand Up @@ -693,25 +690,19 @@ t_CKBOOL ChucK::initCompiler()
//-----------------------------------------------------------------------------
t_CKBOOL ChucK::initChugins()
{
Chuck_VM_Code * code = NULL;
Chuck_VM_Shred * shred = NULL;
// Chuck_VM_Code * code = NULL;
// Chuck_VM_Shred * shred = NULL;

// print whether chugins enabled
EM_log( CK_LOG_SYSTEM, "chugin system: %s", getParamInt( CHUCK_PARAM_CHUGIN_ENABLE ) ? "ON" : "OFF" );

// whether or not chug should be enabled (added 1.3.0.0)
if( getParamInt( CHUCK_PARAM_CHUGIN_ENABLE ) != 0 )
{
// chugin dur
std::string chuginDir = getParamString( CHUCK_PARAM_CHUGIN_DIRECTORY );
// list of search pathes (added 1.3.0.0)
std::list<std::string> dl_search_path = getParamStringList( CHUCK_PARAM_CHUGIN_LIST_IMPORT_PATHS );
if( chuginDir != std::string("") )
{
dl_search_path.push_back( chuginDir );
}
std::list<std::string> dl_search_path = getParamStringList( CHUCK_PARAM_IMPORT_PATH_SYSTEM );
// list of individually named chug-ins (added 1.3.0.0)
std::list<std::string> named_dls = getParamStringList( CHUCK_PARAM_CHUGIN_LIST_USER );
std::list<std::string> named_dls = getParamStringList( CHUCK_PARAM_USER_CHUGINS );

EM_pushlog();
// print host version
Expand All @@ -738,7 +729,9 @@ t_CKBOOL ChucK::initChugins()
goto error;
}

/*
//---------------------------------------------------------------------
// 1.5.4.0 | .ck files are no longer auto compiled; need to be @import
/*---------------------------------------------------------------------
//---------------------------------------------------------------------
// set origin hint | 1.5.0.0 (ge) added
m_carrier->compiler->m_originHint = ckte_origin_IMPORT;
Expand Down Expand Up @@ -789,8 +782,7 @@ t_CKBOOL ChucK::initChugins()
// pop log
EM_poplog();
*/

---------------------------------------------------------------------*/
return true;
}
else
Expand Down Expand Up @@ -843,17 +835,13 @@ void ChucK::probeChugins()
// pop
EM_poplog();

// chugin dur
std::string chuginDir = getParamString( CHUCK_PARAM_CHUGIN_DIRECTORY );
// list of search pathes (added 1.3.0.0)
std::list<std::string> dl_search_path = getParamStringList( CHUCK_PARAM_CHUGIN_LIST_IMPORT_PATHS );
if( chuginDir != "" )
{
// add to search path
dl_search_path.push_back( chuginDir );
}
std::list<std::string> dl_search_path = getParamStringList( CHUCK_PARAM_IMPORT_PATH_SYSTEM );
append_path_list( dl_search_path, getParamStringList( CHUCK_PARAM_IMPORT_PATH_USER) );
append_path_list( dl_search_path, getParamStringList( CHUCK_PARAM_IMPORT_PATH_PACKAGES) );

// list of individually named chug-ins (added 1.3.0.0)
std::list<std::string> named_dls = getParamStringList( CHUCK_PARAM_CHUGIN_LIST_USER );
std::list<std::string> named_dls = getParamStringList( CHUCK_PARAM_USER_CHUGINS );

// log
EM_log( CK_LOG_SYSTEM, "probing chugins (.chug)..." );
Expand All @@ -876,6 +864,9 @@ void ChucK::probeChugins()
// pop log
// EM_poplog();

//-------------------------------------------------------------------------
// 1.5.4.0 | .ck files are no longer auto compiled; need to be @import
/*-------------------------------------------------------------------------
// log
EM_log( CK_LOG_SYSTEM, "probing auto-load chuck files (.ck)..." );
EM_pushlog();
Expand All @@ -896,6 +887,7 @@ void ChucK::probeChugins()
// pop log
EM_poplog();
-------------------------------------------------------------------------*/
}


Expand Down
15 changes: 6 additions & 9 deletions src/core/chuck.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,11 @@
#define CHUCK_PARAM_TTY_WIDTH_HINT "TTY_WIDTH_HINT"
// chugin-relate param names
#define CHUCK_PARAM_CHUGIN_ENABLE "CHUGIN_ENABLE"
#define CHUCK_PARAM_CHUGIN_DIRECTORY "CHUGIN_DIRECTORY"
#define CHUCK_PARAM_CHUGIN_LIST_USER "CHUGIN_LIST_USER"
#define CHUCK_PARAM_CHUGIN_LIST_IMPORT_PATHS "CHUGIN_LIST_IMPORT_PATHS"

// legacy compatibiity (new code should use newer version of these, on the right)
#define CHUCK_PARAM_HINT_IS_REALTIME_AUDIO CHUCK_PARAM_IS_REALTIME_AUDIO_HINT
#define CHUCK_PARAM_USER_CHUGINS CHUCK_PARAM_CHUGIN_LIST_USER
#define CHUCK_PARAM_USER_CHUGIN_DIRECTORIES CHUCK_PARAM_CHUGIN_LIST_IMPORT_PATHS
#define CHUCK_PARAM_USER_CHUGINS "USER_CHUGINS"
// import paths
#define CHUCK_PARAM_IMPORT_PATH_SYSTEM "IMPORT_PATH_SYSTEM"
#define CHUCK_PARAM_IMPORT_PATH_USER "IMPORT_PATH_USER"
#define CHUCK_PARAM_IMPORT_PATH_PACKAGES "IMPORT_PATH_PACKAGES"

// code literal signifier
#define CHUCK_CODE_LITERAL_SIGNIFIER "<compiled.code>"
Expand All @@ -129,7 +126,7 @@ class ChucK
virtual ~ChucK();

public:
// set parameter by name
// set parameter by name (CHUCK_PARAM_*)
// |- (in general, parameters have reasonable defaults)
t_CKBOOL setParam( const std::string & name, t_CKINT value );
t_CKBOOL setParamFloat( const std::string & name, t_CKFLOAT value );
Expand Down
48 changes: 38 additions & 10 deletions src/core/chuck_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,31 @@ t_CKBOOL Chuck_Compiler::importCode( const string & codeLiteral,
// name: importChugin()
// desc: import a chugin by path (and optional short-hand name)
//-----------------------------------------------------------------------------
t_CKBOOL Chuck_Compiler::importChugin( const string & path, const string & name )
t_CKBOOL Chuck_Compiler::importChugin( const string & path,
t_CKBOOL createNamespace, const string & name )
{
// call internal import chugin with option
return this->import_chugin_opt( path, name );
// check if this import should be in its namespace | 1.5.4.0 (ge) added
if( createNamespace )
{
// make context for this chugin import and load it
// this is necessary to make sure the chugin is imported into its own
// namespace, to avoid being imported into say a host namespace -- e.g.,
// a chuck file that @import this chugin
type_engine_load_context( this->carrier()->env, type_engine_make_context( NULL, "@[chugin-import]" ) );
}

// call internal import chugin
t_CKBOOL ret = this->import_chugin_opt( path, name );

// if create namespace
if( createNamespace )
{
// unload the context
type_engine_unload_context( this->carrier()->env );
}

// done
return ret;
}


Expand Down Expand Up @@ -630,8 +651,13 @@ std::string Chuck_Compiler::resolveFilename( const std::string & filename,
// need for expanded search
if( !isAlreadyAbsolutePath && !hasMatch && expandSearchToGlobal )
{
// get search paths
list<string> searchPaths = this->carrier()->chuck->getParamStringList( CHUCK_PARAM_CHUGIN_LIST_IMPORT_PATHS );
// TODO: possible caching -- search for match first in registry
// match-right between each entry and fname; if found, return entry absolute path

// get search paths; order: system, user, packages
list<string> searchPaths = this->carrier()->chuck->getParamStringList( CHUCK_PARAM_IMPORT_PATH_SYSTEM );
append_path_list( searchPaths, this->carrier()->chuck->getParamStringList( CHUCK_PARAM_IMPORT_PATH_USER ) );
append_path_list( searchPaths, this->carrier()->chuck->getParamStringList( CHUCK_PARAM_IMPORT_PATH_PACKAGES ) );
// go over paths
for( list<string>::iterator it = searchPaths.begin(); it != searchPaths.end(); it++ )
{
Expand Down Expand Up @@ -798,8 +824,8 @@ t_CKBOOL type_engine_scan_import( Chuck_Env * env, a_Stmt_List stmt_list,
// test extension
if( ext == ".chug" || ext == ".wasm" )
{
// load the chugin
if( !compiler->importChugin( abs, theFile ) )
// load the chugin, in its own namespace == TRUE
if( !compiler->importChugin( abs, TRUE, theFile ) )
{
// print error (chugin loading only prints to log)
EM_error2( import->where, "cannot load chugin: '%s'...", theFile.c_str() );
Expand Down Expand Up @@ -1605,7 +1631,9 @@ t_CKBOOL Chuck_Compiler::load_external_modules_in_directory(
for( t_CKINT i = 0; i < chugins2load.size(); i++ )
{
// load module
t_CKBOOL loaded = this->importChugin( chugins2load[i].path, chugins2load[i].filename );
// ...in its own namespace == FALSE | 1.5.4.0 (ge) added
// ...since already in namespace (e.g., @[external]) from load_external_modules()
t_CKBOOL loaded = this->importChugin( chugins2load[i].path, FALSE, chugins2load[i].filename );
// if no error
if( chugins2load[i].isBundle && loaded) {
// log
Expand Down Expand Up @@ -1670,8 +1698,8 @@ t_CKBOOL Chuck_Compiler::load_external_modules( const string & extension,
// check extension, append if no match
if( !extension_matches(dl_path, extension) )
dl_path += extension;
// load the module
this->importChugin( dl_path );
// load the module, in its own namespace == FALSE
this->importChugin( dl_path, FALSE );
}

// now recurse through search paths and load any DLs or .ck files found
Expand Down
4 changes: 3 additions & 1 deletion src/core/chuck_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,9 @@ struct Chuck_Compiler
t_CKBOOL importCode( const std::string & codeLiteral,
const std::string & optFilepath );
// import a chugin by path (and optional short-hand name)
t_CKBOOL importChugin( const std::string & path, const std::string & name = "" );
t_CKBOOL importChugin( const std::string & path,
t_CKBOOL createNamespace,
const std::string & name = "" );

public:
// compile a target | 1.5.4.0 (ge)
Expand Down
30 changes: 26 additions & 4 deletions src/core/chuck_dl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,37 @@ using namespace std;



//-----------------------------------------------------------------------------
// three types of import search paths
//-----------------------------------------------------------------------------
// * system paths: for loading chugins/ck files on VM startup
// contents persists across clearVM
// * user paths: not automatically loaded on startup; @import on demand
// contents removed by clearVM
// * package path; packages installed and managed by ChuMP; @import on demand
// contents removed by clearVM | 1.5.4.0 (ge & nshaheed)
// (=v ChuMP =v ChucK Manager of Packages)
//-----------------------------------------------------------------------------
#if defined(__PLATFORM_APPLE__)
char g_default_chugin_path[] = "/usr/local/lib/chuck:/Library/Application Support/ChucK/chugins:~/Library/Application Support/ChucK/chugins:~/.chuck/lib";
char g_default_path_system[] = "/usr/local/lib/chuck:/Library/Application Support/ChucK/chugins";
char g_default_path_user[] = "~/.chuck/lib:~/Library/Application Support/ChucK/chugins";
char g_default_path_packages[] = "~/.chuck/chump";
#elif defined(__PLATFORM_WINDOWS__)
char g_default_chugin_path[] = "C:\\Windows\\system32\\ChucK;C:\\Program Files\\ChucK\\chugins;C:\\Program Files (x86)\\ChucK\\chugins;C:\\Users\\%USERNAME%\\Documents\\ChucK\\chugins";
char g_default_path_system[] = "C:\\Windows\\system32\\ChucK;C:\\Program Files\\ChucK\\chugins;C:\\Program Files (x86)\\ChucK\\chugins;";
char g_default_path_user[] = "C:\\Users\\%USERNAME%\\Documents\\ChucK\\chugins";
char g_default_path_packages[] = "~C:\\Users\\%USERNAME%\\Documents\\ChucK\\chump";
#else // Linux / Cygwin
char g_default_chugin_path[] = "/usr/local/lib/chuck:~/.chuck/lib";
char g_default_path_system[] = "/usr/local/lib/chuck";
char g_default_path_user[] = "~/.chuck/lib";
char g_default_path_packages[] = "~/.chuck/chump";
#endif

char g_chugin_path_envvar[] = "CHUCK_CHUGIN_PATH";
// environment variables
char g_envvar_path_system[] = "CHUCK_IMPORT_PATH_SYSTEM";
char g_envvar_path_user[] = "CHUCK_IMPORT_PATH_USER";
char g_envvar_path_packages[] = "CHUCK_IMPORT_PATH_PACKAGES";
// deprecated; should use the above
char g_envvar_path_deprecated[] = "CHUCK_CHUGIN_PATH";



Expand Down
15 changes: 10 additions & 5 deletions src/core/chuck_dl.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,16 @@
#define CK_DLL_VERSION_GETMINOR(v) ((v) & 0xFFFF)
#define CK_DLL_VERSION (CK_DLL_VERSION_MAKE(CK_DLL_VERSION_MAJOR, CK_DLL_VERSION_MINOR))


// string literal containing default chugin path (platform-specific)
extern char g_default_chugin_path[];
// string literal containing environment variable for chugin search path
extern char g_chugin_path_envvar[];
// string literal containing default chugin paths (platform-specific)
extern char g_default_path_system[];
extern char g_default_path_user[];
extern char g_default_path_packages[];
// string literal containing environment variable names for import search path
extern char g_envvar_path_system[];
extern char g_envvar_path_user[];
extern char g_envvar_path_packages[];
// contains deprecated env var name
extern char g_envvar_path_deprecated[];


// DL forward references
Expand Down
24 changes: 22 additions & 2 deletions src/core/util_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,9 +1010,9 @@ string dir_go_up( const string & dir, t_CKINT numUp )

//-----------------------------------------------------------------------------
// name: parse_path_list()
// desc: split "x:y:z"-style path list into {"x","y","z"}
// desc: split "x:y:z"-style path list into {"x","y","z"}, with trailing '/'
//-----------------------------------------------------------------------------
void parse_path_list( std::string & str, std::list<std::string> & lst )
void parse_path_list( const std::string & str, std::list<std::string> & lst )
{
#if defined(__PLATFORM_WINDOWS__)
const char separator = ';';
Expand All @@ -1035,6 +1035,26 @@ void parse_path_list( std::string & str, std::list<std::string> & lst )



//-----------------------------------------------------------------------------
// name: append_path_list()
// desc: append a list (appendThis) to a list (list)
//-----------------------------------------------------------------------------
void append_path_list( std::list<std::string> & list,
const std::list<std::string> & appendMe )
{
// first
std::list<std::string>::const_iterator it = appendMe.begin();
// iterate
for( ; it != appendMe.end(); it ++ )
{
// append element
list.push_back( *it );
}
}




//-----------------------------------------------------------------------------
// name: normalize_directory_separator()
// desc: unify directory separator to be consistent across platforms;
Expand Down
5 changes: 4 additions & 1 deletion src/core/util_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ std::string normalize_directory_name( const std::string & dir );
t_CKBOOL is_absolute_path( const std::string & path );

// split "x:y:z"-style path list into {"x","y","z"}
void parse_path_list( std::string & str, std::list<std::string> & lst );
void parse_path_list( const std::string & str, std::list<std::string> & lst );
// append list to list
void append_path_list( std::list<std::string> & list,
const std::list<std::string> & appendMe );

// generate auto filename (usually with date-time) with file prefix and extension
std::string autoFilename( const std::string & prefix, const std::string & extension );
Expand Down
Loading

0 comments on commit 3cd84b4

Please sign in to comment.