1
0
Fork 0

Class ___$$meta$$ moved into private symbol field

protolib
Mike Gerwitz 2014-07-06 23:10:03 -04:00
parent e0866db313
commit a35a9c6ed3
1 changed files with 32 additions and 11 deletions

View File

@ -26,6 +26,7 @@
var util = require( './util' ), var util = require( './util' ),
Warning = require( './warn' ).Warning, Warning = require( './warn' ).Warning,
Symbol = require( './util/Symbol' ),
hasOwn = Object.prototype.hasOwnProperty, hasOwn = Object.prototype.hasOwnProperty,
@ -73,7 +74,18 @@ var util = require( './util' ),
'__construct': true, '__construct': true,
'toString': true, 'toString': true,
'__toString': true, '__toString': true,
}; },
/**
* Symbol used to encapsulate internal data
*
* Note that this is intentionally generated *outside* the ClassBuilder
* instance; this ensures that it is properly encapsulated and will not
* be exposed on the Classbuilder instance (which would defeat the
* purpose).
*/
_priv = Symbol()
;
/** /**
@ -225,7 +237,7 @@ exports.getForcedPublicMethods = function()
*/ */
exports.getMeta = function( cls ) exports.getMeta = function( cls )
{ {
return cls.___$$meta$$ || {}; return ( cls[ _priv ] || {} ).meta || {};
} }
@ -342,7 +354,7 @@ exports.prototype.build = function extend( _, __ )
{ {
throw Error( throw Error(
"Cannot extend final class " + "Cannot extend final class " +
( base.___$$meta$$.name || '(anonymous)' ) ( base[ _priv ].meta.name || '(anonymous)' )
); );
} }
@ -799,16 +811,19 @@ function validateAbstract( ctor, cname, abstract_methods, auto )
*/ */
exports.prototype.createCtor = function( cname, abstract_methods, members ) exports.prototype.createCtor = function( cname, abstract_methods, members )
{ {
// concrete class var new_class;
if ( abstract_methods.__length === 0 ) if ( abstract_methods.__length === 0 )
{ {
return this.createConcreteCtor( cname, members ); new_class = this.createConcreteCtor( cname, members );
} }
// abstract class
else else
{ {
return this.createAbstractCtor( cname ); new_class = this.createAbstractCtor( cname );
} }
new_class[ _priv ] = {};
return new_class;
} }
@ -1239,21 +1254,27 @@ function createMeta( func, cparent )
// copy the parent prototype's metadata if it exists (inherit metadata) // copy the parent prototype's metadata if it exists (inherit metadata)
if ( parent_meta ) if ( parent_meta )
{ {
func.___$$meta$$ = util.clone( parent_meta, true ); func[ _priv ].meta = util.clone( parent_meta, true );
} }
else else
{ {
// create empty // create empty
func.___$$meta$$ = { func[ _priv ].meta = {
implemented: [], implemented: [],
}; };
} }
// TODO: this should be done elsewhere
if ( !func.prototype[ _priv ] )
{
func.prototype[ _priv ] = {};
}
// store the metadata in the prototype as well (inconsiderable overhead; // store the metadata in the prototype as well (inconsiderable overhead;
// it's just a reference) // it's just a reference)
func.prototype.___$$meta$$ = func.___$$meta$$; func.prototype[ _priv ].meta = func[ _priv ].meta;
return func.___$$meta$$; return func[ _priv ].meta;
} }