1
0
Fork 0

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.
perfodd
Mike Gerwitz 2014-01-23 22:24:27 -05:00
parent 93eda3c14b
commit 00c76c69df
1 changed files with 11 additions and 7 deletions

View File

@ -50,7 +50,7 @@ Trait.extend = function( dfn )
TraitType.__trait = true; TraitType.__trait = true;
TraitType.__acls = tclass; TraitType.__acls = tclass;
TraitType.__ccls = createConcrete( tclass ); TraitType.__ccls = null;
// traits are not permitted to define constructors // traits are not permitted to define constructors
if ( tclass.___$$methods$$['public'].__construct !== undefined ) if ( tclass.___$$methods$$['public'].__construct !== undefined )
@ -114,7 +114,7 @@ function mixin( trait, dfn )
methods = acls.___$$methods$$; methods = acls.___$$methods$$;
// retrieve the private member name that will contain this trait object // 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['public'], dfn, 'public', iname );
mixMethods( methods['protected'], dfn, 'protected', 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'' * Here, `tc' and `to' are understood to be, respectively, ``trait class''
* and ``trait object''. * and ``trait object''.
* *
* @param {Class} C concrete trait class * @param {Class} T trait
* @param {Object} dfn definition object of class being mixed into * @param {Object} dfn definition object of class being mixed into
* *
* @return {string} private member into which C instance shall be stored * @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$$ || [] ) ), var tc = ( dfn.___$$tc$$ = ( dfn.___$$tc$$ || [] ) ),
iname = '___$to$' + tc.length; iname = '___$to$' + tc.length;
// the trait object array will contain two values: the destination field // the trait object array will contain two values: the destination field
// and the class to instantiate // and the trait to instantiate
tc.push( [ iname, C ] ); tc.push( [ iname, T ] );
// we must also add the private field to the definition object to // we must also add the private field to the definition object to
// support the object assignment indicated by TC // support the object assignment indicated by TC
@ -213,6 +213,9 @@ function addTraitInst( C, dfn )
* resulting objects assigned to their rsepective pre-determined field * resulting objects assigned to their rsepective pre-determined field
* names. * 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} * @return {undefined}
*/ */
function tctor() function tctor()
@ -223,7 +226,8 @@ function tctor()
for ( var t in tc ) for ( var t in tc )
{ {
var f = tc[ t ][ 0 ], 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 // TODO: pass protected visibility object once we create
// trait class ctors // trait class ctors