Began refactoring into class_builder module
- Sloppy thusfar. Baby steps.closure/master
parent
30d10ff9d7
commit
af8f0b1566
96
lib/class.js
96
lib/class.js
|
@ -23,6 +23,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var util = require( './util' ),
|
var util = require( './util' ),
|
||||||
|
class_builder = require( './class_builder' ),
|
||||||
member_builder = require( './member_builder' ),
|
member_builder = require( './member_builder' ),
|
||||||
propobj = require( './propobj' )
|
propobj = require( './propobj' )
|
||||||
;
|
;
|
||||||
|
@ -439,12 +440,9 @@ var extend = ( function( extending )
|
||||||
prototype = new base(),
|
prototype = new base(),
|
||||||
cname = '',
|
cname = '',
|
||||||
|
|
||||||
hasOwn = Array.prototype.hasOwnProperty,
|
|
||||||
|
|
||||||
properties = {},
|
properties = {},
|
||||||
prop_init = member_builder.initMembers(),
|
prop_init = member_builder.initMembers(),
|
||||||
members = member_builder.initMembers( prototype ),
|
members = member_builder.initMembers( prototype ),
|
||||||
defs = {},
|
|
||||||
|
|
||||||
abstract_methods =
|
abstract_methods =
|
||||||
util.clone( getMeta( base ).abstractMethods )
|
util.clone( getMeta( base ).abstractMethods )
|
||||||
|
@ -470,89 +468,17 @@ var extend = ( function( extending )
|
||||||
// increment class identifier
|
// increment class identifier
|
||||||
class_id++;
|
class_id++;
|
||||||
|
|
||||||
util.propParse( props, {
|
// build the various class components (xxx: this is temporary; needs
|
||||||
each: function( name, value, keywords )
|
// refactoring)
|
||||||
{
|
class_builder.build( props,
|
||||||
// disallow use of our internal __initProps() method
|
class_id,
|
||||||
if ( name === '__initProps' )
|
base,
|
||||||
{
|
prop_init,
|
||||||
throw new Error(
|
abstract_methods,
|
||||||
( ( cname ) ? cname + '::' : '' ) +
|
properties,
|
||||||
"__initProps is a reserved method"
|
members,
|
||||||
|
getMethodInstance
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
// if a member was defined multiple times in the same class
|
|
||||||
// declaration, throw an error
|
|
||||||
if ( hasOwn.call( defs, name ) )
|
|
||||||
{
|
|
||||||
throw Error(
|
|
||||||
"Cannot redefine method '" + name + "' in same declaration"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// keep track of the definitions (only during class declaration)
|
|
||||||
// to catch duplicates
|
|
||||||
defs[ name ] = 1;
|
|
||||||
},
|
|
||||||
|
|
||||||
property: function( name, value, keywords )
|
|
||||||
{
|
|
||||||
properties[ name ] = value;
|
|
||||||
|
|
||||||
// build a new property, passing in the other members to compare
|
|
||||||
// against for preventing nonsensical overrides
|
|
||||||
member_builder.buildProp(
|
|
||||||
prop_init, null, name, value, keywords, base
|
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
getter: function( name, value, keywords )
|
|
||||||
{
|
|
||||||
member_builder.buildGetter(
|
|
||||||
members, null, name, value, keywords
|
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
setter: function( name, value, keywords )
|
|
||||||
{
|
|
||||||
member_builder.buildSetter(
|
|
||||||
members, null, name, value, keywords
|
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
method: function( name, func, is_abstract, keywords )
|
|
||||||
{
|
|
||||||
// constructor check
|
|
||||||
if ( name === '__construct' )
|
|
||||||
{
|
|
||||||
if ( keywords[ 'protected' ] || keywords[ 'private' ] )
|
|
||||||
{
|
|
||||||
throw TypeError( "Constructor must be public" );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
member_builder.buildMethod(
|
|
||||||
members, null, name, func, keywords, getMethodInstance,
|
|
||||||
class_id, base
|
|
||||||
);
|
|
||||||
|
|
||||||
if ( is_abstract )
|
|
||||||
{
|
|
||||||
abstract_methods[ name ] = true;
|
|
||||||
abstract_methods.__length++;
|
|
||||||
}
|
|
||||||
else if ( ( hasOwn.call( abstract_methods, name ) )
|
|
||||||
&& ( is_abstract === false )
|
|
||||||
)
|
|
||||||
{
|
|
||||||
// if this was a concrete method, then it should no longer
|
|
||||||
// be marked as abstract
|
|
||||||
delete abstract_methods[ name ];
|
|
||||||
abstract_methods.__length--;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
} );
|
|
||||||
|
|
||||||
// reference to the parent prototype (for more experienced users)
|
// reference to the parent prototype (for more experienced users)
|
||||||
prototype.___$$parent$$ = base.prototype;
|
prototype.___$$parent$$ = base.prototype;
|
||||||
|
|
|
@ -0,0 +1,121 @@
|
||||||
|
/**
|
||||||
|
* Handles building of classes
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 Mike Gerwitz
|
||||||
|
*
|
||||||
|
* This file is part of ease.js.
|
||||||
|
*
|
||||||
|
* ease.js is free software: you can redistribute it and/or modify it under the
|
||||||
|
* terms of the GNU Lesser General Public License as published by the Free
|
||||||
|
* Software Foundation, either version 3 of the License, or (at your option)
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* @author Mike Gerwitz
|
||||||
|
* @package core
|
||||||
|
*/
|
||||||
|
|
||||||
|
var util = require( './util' ),
|
||||||
|
member_builder = require( './member_builder' )
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
exports.build = function(
|
||||||
|
props, class_id, base, prop_init, abstract_methods, properties, members,
|
||||||
|
getMethodInstance
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var hasOwn = Array.prototype.hasOwnProperty,
|
||||||
|
defs = {};
|
||||||
|
|
||||||
|
util.propParse( props, {
|
||||||
|
each: function( name, value, keywords )
|
||||||
|
{
|
||||||
|
// disallow use of our internal __initProps() method
|
||||||
|
if ( name === '__initProps' )
|
||||||
|
{
|
||||||
|
throw new Error(
|
||||||
|
( ( cname ) ? cname + '::' : '' ) +
|
||||||
|
"__initProps is a reserved method"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// if a member was defined multiple times in the same class
|
||||||
|
// declaration, throw an error
|
||||||
|
if ( hasOwn.call( defs, name ) )
|
||||||
|
{
|
||||||
|
throw Error(
|
||||||
|
"Cannot redefine method '" + name + "' in same declaration"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// keep track of the definitions (only during class declaration)
|
||||||
|
// to catch duplicates
|
||||||
|
defs[ name ] = 1;
|
||||||
|
},
|
||||||
|
|
||||||
|
property: function( name, value, keywords )
|
||||||
|
{
|
||||||
|
properties[ name ] = value;
|
||||||
|
|
||||||
|
// build a new property, passing in the other members to compare
|
||||||
|
// against for preventing nonsensical overrides
|
||||||
|
member_builder.buildProp(
|
||||||
|
prop_init, null, name, value, keywords, base
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
getter: function( name, value, keywords )
|
||||||
|
{
|
||||||
|
member_builder.buildGetter(
|
||||||
|
members, null, name, value, keywords
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
setter: function( name, value, keywords )
|
||||||
|
{
|
||||||
|
member_builder.buildSetter(
|
||||||
|
members, null, name, value, keywords
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
method: function( name, func, is_abstract, keywords )
|
||||||
|
{
|
||||||
|
// constructor check
|
||||||
|
if ( name === '__construct' )
|
||||||
|
{
|
||||||
|
if ( keywords[ 'protected' ] || keywords[ 'private' ] )
|
||||||
|
{
|
||||||
|
throw TypeError( "Constructor must be public" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
member_builder.buildMethod(
|
||||||
|
members, null, name, func, keywords, getMethodInstance,
|
||||||
|
class_id, base
|
||||||
|
);
|
||||||
|
|
||||||
|
if ( is_abstract )
|
||||||
|
{
|
||||||
|
abstract_methods[ name ] = true;
|
||||||
|
abstract_methods.__length++;
|
||||||
|
}
|
||||||
|
else if ( ( hasOwn.call( abstract_methods, name ) )
|
||||||
|
&& ( is_abstract === false )
|
||||||
|
)
|
||||||
|
{
|
||||||
|
// if this was a concrete method, then it should no longer
|
||||||
|
// be marked as abstract
|
||||||
|
delete abstract_methods[ name ];
|
||||||
|
abstract_methods.__length--;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
} );
|
||||||
|
};
|
|
@ -28,7 +28,8 @@ TPL_VAR='/**{CONTENT}**/'
|
||||||
RMTRAIL="$PATH_TOOLS/rmtrail"
|
RMTRAIL="$PATH_TOOLS/rmtrail"
|
||||||
|
|
||||||
# order matters
|
# order matters
|
||||||
CAT_MODULES="prop_parser util member_builder propobj class interface"
|
CAT_MODULES="prop_parser util member_builder class_builder propobj"
|
||||||
|
CAT_MODULES="$CAT_MODULES class interface"
|
||||||
|
|
||||||
##
|
##
|
||||||
# Output template header
|
# Output template header
|
||||||
|
|
Loading…
Reference in New Issue