Skip to content

Commit

Permalink
fix public class/@import loading into [user] namespace
Browse files Browse the repository at this point in the history
previously the [user] namespace was not created on initialization, but was created only on clearVM; this led to incorrect behavior between public class definitions and @import'ed classes of the same name; also @imported classes are now correctly cleared during clearVM
  • Loading branch information
gewang committed Oct 31, 2024
1 parent 35c027b commit 99d8c49
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
4 changes: 0 additions & 4 deletions src/core/chuck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,17 +783,13 @@ t_CKBOOL ChucK::initChugins()
// pop log
EM_poplog();
---------------------------------------------------------------------*/
return true;
}
else
{
// log | 1.4.1.0 (ge) commented out; printing earlier
EM_log( CK_LOG_SYSTEM, "chugin system: OFF" );
}

// load user namespace
m_carrier->env->load_user_namespace();

// unset origin hint | 1.5.0.0 (ge) added
m_carrier->compiler->m_originHint = ckte_origin_UNKNOWN;

Expand Down
7 changes: 7 additions & 0 deletions src/core/chuck_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ t_CKBOOL Chuck_Compiler::importChugin( const string & path,
// 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
// ALSO: if the chuck file that imports the chugin contains an error,
// its context would be cleaned up, which leads to a crash on chuck exit;
// this created context prevent that crash
// ... further investigation may be beneficial to fully understand this
// mechanism
type_engine_load_context( this->carrier()->env, type_engine_make_context( NULL, "@[chugin-import]" ) );
}

Expand Down Expand Up @@ -1719,6 +1724,8 @@ t_CKBOOL Chuck_Compiler::load_external_modules( const string & extension,
// preserve all operator overloads currently in registry | 1.5.1.5
env->op_registry.preserve();

// always return true...
// a failed chugin load should not prevent chuck from starting
return TRUE;
}

Expand Down
10 changes: 9 additions & 1 deletion src/core/chuck_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,16 @@ t_CKBOOL type_engine_scan0_prog( Chuck_Env * env, a_Program prog,
// env->context->public_class_def = prog->section->class_def;
// -----------------------------------------

// make global
// -----------------------------------------
// mark this class definition to be added to [user] namespace
// which typically sits between [global] namespace and context namesapce
prog->section->class_def->home = env->user();
// -----------------------------------------
// NOTE: for legacy reasons, fallback to [global] if no [user] namespace
// this fallback mechanism was moved out of env->user() to here for clarity in 1.5.4.0 (ge)
// needs further verification, as this fallback may not be necessary
if( !prog->section->class_def->home ) prog->section->class_def->home = env->global();
// -----------------------------------------
}
// scan the class definition
ret = type_engine_scan0_class_def( env, prog->section->class_def );
Expand Down
19 changes: 16 additions & 3 deletions src/core/chuck_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ void Chuck_Env::clear_user_namespace()
this->reset();
// reset operator overloads, including public (env->reset() only resets local)
op_registry.reset2public();
// reset @import registry | 1.5.4.0 (ge) added
this->compiler()->imports()->clearAllUserImports();
}


Expand Down Expand Up @@ -418,12 +420,16 @@ Chuck_Namespace * Chuck_Env::global() { return global_nspc; }

//-----------------------------------------------------------------------------
// name: user()
// desc: get user namespace, if there is one (if not, return global)
// desc: get user namespace
//-----------------------------------------------------------------------------
Chuck_Namespace * Chuck_Env::user()
{
// always return user namespace (even NULL) | 1.5.4.0 (ge) changed for clarity
return user_nspc;

// previously:
// check if we have a user namespace here
return user_nspc != NULL ? user_nspc : global();
// return user_nspc != NULL ? user_nspc : global();
}


Expand Down Expand Up @@ -713,6 +719,13 @@ t_CKBOOL type_engine_init( Chuck_Carrier * carrier )
// initialize operator mappings
if( !type_engine_init_op_overload( env ) ) return FALSE;

// create [user] namespace | 1.5.4.0 (ge) added/moved here
// subsequent definitions (e.g., public classes) would be added
// to this namespace, unless explicitly specified;
// types added to the [user] namespace will be cleared during clearVM
// whereas types in the [global] namespace persists
env->load_user_namespace();

// pop indent level
EM_poplog();

Expand Down Expand Up @@ -8639,7 +8652,7 @@ t_CKBOOL type_engine_add_dll( Chuck_Env * env, Chuck_DLL * dll, const string & d
// desc: add the DLL using type_engine functions (added 1.3.0.0)
//-----------------------------------------------------------------------------
t_CKBOOL type_engine_add_dll2( Chuck_Env * env, Chuck_DLL * dll,
const string & )
const string & dest )
{
const Chuck_DL_Query * query = NULL;

Expand Down
2 changes: 1 addition & 1 deletion src/core/chuck_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ struct Chuck_Env : public Chuck_VM_Object
t_CKBOOL is_global();
// global namespace
Chuck_Namespace * global();
// user namespace, if there is one (if not, return global)
// user namespace
Chuck_Namespace * user();
// get namespace at top of stack
Chuck_Namespace * nspc_top();
Expand Down

0 comments on commit 99d8c49

Please sign in to comment.