From 987a2b88ec98cbbc77b472473610a5e0f2b5ec9a Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sun, 26 Jan 2014 03:28:36 -0500 Subject: [PATCH] Classes can now access trait protected members Slight oversight in the original commit. --- lib/Trait.js | 9 ++++++--- test/Trait/ScopeTest.js | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/Trait.js b/lib/Trait.js index b4e2fe4..eeb340b 100644 --- a/lib/Trait.js +++ b/lib/Trait.js @@ -268,9 +268,12 @@ function tctor() T = tc[ t ][ 1 ], C = T.__ccls || ( T.__ccls = createConcrete( T.__acls ) ); - // TODO: pass protected visibility object once we create - // trait class ctors - this[ f ] = C(); + // instantiate the trait, providing it with our protected visibility + // object so that it has access to our public and protected members + // (but not private); in return, we will use its own protected + // visibility object to gain access to its protected members...quite + // the intimate relationship + this[ f ] = C( this.___$$vis$$ ).___$$vis$$; } }; diff --git a/test/Trait/ScopeTest.js b/test/Trait/ScopeTest.js index b39ada2..e5ba614 100644 --- a/test/Trait/ScopeTest.js +++ b/test/Trait/ScopeTest.js @@ -126,4 +126,27 @@ require( 'common' ).testCase( inst.invokePriv(); }, Error ); }, + + + /** + * If this seems odd at first, consider this: traits provide + * copy/paste-style functionality, meaning they need to be able to + * provide public methods. However, we may not always want to mix trait + * features into a public API; therefore, we need the ability to mix in + * protected members. + */ + 'Classes can access protected trait members': function() + { + var T = this.Sut( { 'protected foo': function() {} } ); + + var _self = this; + this.assertDoesNotThrow( function() + { + _self.Class.use( T ).extend( + { + // invokes protected trait method + 'public callFoo': function() { this.foo(); } + } )().callFoo(); + } ); + }, } );