Skip to content

Commit

Permalink
add Object.typeOfInstance(); update type_engine_init() to reset [user…
Browse files Browse the repository at this point in the history
…] namespace
  • Loading branch information
gewang committed Nov 1, 2024
1 parent 6424ffb commit 0004053
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
3 changes: 3 additions & 0 deletions VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ ChucK VERSIONS log
suffice, assuming there are no filename conflicts elsewhere in the
import paths)
******************************************************
- (added) member function in Object:
Type Object.typeOfInstance()
Get the instanced type of this object.
- (fixed) specific internal UGen connection error now handled without
exiting
- (fixed, windows) single letter filenames without extensions (e.g., "A")
Expand Down
9 changes: 9 additions & 0 deletions examples/type/type_type.ck
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,12 @@ for( int i; i < kids.size(); i++ )
{
cherr <= "UAna: " <= kids[i].name() <= IO.newline();
}

// instantiate a SinOsc; assign reference to a parent class
SinOsc theChild @=> UGen @ theParent;
// static typing: should be `SinOsc UGen`
<<< theChild.typeOf().name(),
theParent.typeOf().name() >>>;
// instanced typing: should be `SinOsc SinOsc`
<<< theChild.typeOfInstance().name(),
theParent.typeOfInstance().name() >>>;
19 changes: 18 additions & 1 deletion src/core/chuck_lang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,16 @@ t_CKBOOL init_class_object( Chuck_Env * env, Chuck_Type * type )
func->doc = "output helpful information about a class or an instance thereof.";
if( !type_engine_import_sfun( env, func ) ) goto error;

// add getType() // 1.5.0.0
// add typeOf() // 1.5.0.0
func = make_new_sfun( "Type", "typeOf", object_typeInfo );
func->doc = "get the type of this object (or class).";
if( !type_engine_import_sfun( env, func ) ) goto error;

// add typeOfInstance() // 1.5.4.0 (nshaheed, ge) added
func = make_new_mfun( "Type", "typeOfInstance", object_typeInstanceInfo );
func->doc = "get the instanced type of this object.";
if( !type_engine_import_mfun( env, func ) ) goto error;

// // add dump()
// func = make_new_mfun( "void", "dump", object_dump );
// func->doc = "output current state of the object.";
Expand Down Expand Up @@ -1633,6 +1638,18 @@ CK_DLL_SFUN( object_typeInfo )
}
}

// get the instanced type info
CK_DLL_MFUN( object_typeInstanceInfo )
{
// get the object reference
Chuck_Object * self = SELF;

// if null, return null
if( !self ) return;

// return the type
RETURN->v_object = self->type_ref;
}

// ctor
CK_DLL_CTOR( ugen_ctor )
Expand Down
1 change: 1 addition & 0 deletions src/core/chuck_lang.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ CK_DLL_MFUN( object_equals );
CK_DLL_MFUN( object_hashCode );
CK_DLL_MFUN( object_toString );
CK_DLL_MFUN( object_dump );
CK_DLL_MFUN( object_typeInstanceInfo );
CK_DLL_SFUN( object_help );
CK_DLL_SFUN( object_typeInfo );

Expand Down
18 changes: 14 additions & 4 deletions src/core/chuck_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,11 @@ void Chuck_Env::clear_user_namespace()
// clean up user namesapce
CK_SAFE_RELEASE( user_nspc );
// load new user namespace
load_user_namespace();
this->load_user_namespace();
// reset it
this->reset();
// reset operator overloads, including public (env->reset() only resets local)
op_registry.reset2public();
this->op_registry.reset2public();
// reset @import registry | 1.5.4.0 (ge) added
this->compiler()->imports()->clearAllUserImports();
}
Expand Down Expand Up @@ -564,6 +564,10 @@ t_CKBOOL type_engine_init( Chuck_Carrier * carrier )
EM_log( CK_LOG_HERALD, "adding base classes..." );
EM_pushlog();

//-------------------------
// initialize internal classes; for now these are assumed to not error out
// NOTE: alternately, could test for return values and bailing out gracefully
// however, there may be inconstencies for cleaning up ChucK::init() fails
//-------------------------
// disable array type cache until ckt_array is initialized | 1.5.4.0 (ge) added
env->arrayTypeCache()->enable( FALSE );
Expand Down Expand Up @@ -719,17 +723,23 @@ 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
// clear/create/reset [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();
env->clear_user_namespace();

// pop indent level
EM_poplog();

return TRUE;

error:
EM_error2( 0, "(internal error) during type initialization..." );
EM_error2( 0, "...bailing out; please contact the ChucK Team" );

return FALSE;
}


Expand Down
9 changes: 9 additions & 0 deletions src/test/01-Basic/255-type-of-instance.ck
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SinOsc foo @=> UGen @ bar;

// static typing
if( foo.typeOf().name() != "SinOsc" && bar.typeOf().name() != "UGen" )
{ <<< "error" >>>; me.exit(); }

// get the instanced type
if( foo.typeOfInstance().name() == "SinOsc" &&
bar.typeOfInstance().name() == "SinOsc" ) <<< "success" >>>;

0 comments on commit 0004053

Please sign in to comment.