From 00c76c69dfc63c01afdede9d383974c88cc782de Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Thu, 23 Jan 2014 22:24:27 -0500 Subject: [PATCH] Trait concrete class will now be lazily created on first use This just saves some time and memory in the event that the trait is never actually used, which may be the case in libraries or dynamically loaded modules. --- lib/Trait.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/Trait.js b/lib/Trait.js index 209decf..cff6c07 100644 --- a/lib/Trait.js +++ b/lib/Trait.js @@ -50,7 +50,7 @@ Trait.extend = function( dfn ) TraitType.__trait = true; TraitType.__acls = tclass; - TraitType.__ccls = createConcrete( tclass ); + TraitType.__ccls = null; // traits are not permitted to define constructors if ( tclass.___$$methods$$['public'].__construct !== undefined ) @@ -114,7 +114,7 @@ function mixin( trait, dfn ) methods = acls.___$$methods$$; // retrieve the private member name that will contain this trait object - var iname = addTraitInst( trait.__ccls, dfn ); + var iname = addTraitInst( trait, dfn ); mixMethods( methods['public'], dfn, 'public', iname ); mixMethods( methods['protected'], dfn, 'protected', iname ); @@ -177,19 +177,19 @@ function mixMethods( src, dest, vis, iname ) * Here, `tc' and `to' are understood to be, respectively, ``trait class'' * and ``trait object''. * - * @param {Class} C concrete trait class + * @param {Class} T trait * @param {Object} dfn definition object of class being mixed into * * @return {string} private member into which C instance shall be stored */ -function addTraitInst( C, dfn ) +function addTraitInst( T, dfn ) { var tc = ( dfn.___$$tc$$ = ( dfn.___$$tc$$ || [] ) ), iname = '___$to$' + tc.length; // the trait object array will contain two values: the destination field - // and the class to instantiate - tc.push( [ iname, C ] ); + // and the trait to instantiate + tc.push( [ iname, T ] ); // we must also add the private field to the definition object to // support the object assignment indicated by TC @@ -213,6 +213,9 @@ function addTraitInst( C, dfn ) * resulting objects assigned to their rsepective pre-determined field * names. * + * This will lazily create the concrete trait class if it does not already + * exist, which saves work if the trait is never used. + * * @return {undefined} */ function tctor() @@ -223,7 +226,8 @@ function tctor() for ( var t in tc ) { var f = tc[ t ][ 0 ], - C = tc[ t ][ 1 ]; + T = tc[ t ][ 1 ], + C = T.__ccls || ( T.__ccls = createConcrete( T.__acls ) ); // TODO: pass protected visibility object once we create // trait class ctors