From af8f0b1566dc2fecdbcfcb73146d1bf250815739 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sun, 27 Mar 2011 01:57:17 -0400 Subject: [PATCH] Began refactoring into class_builder module - Sloppy thusfar. Baby steps. --- lib/class.js | 98 +++++------------------------------ lib/class_builder.js | 121 +++++++++++++++++++++++++++++++++++++++++++ tools/combine | 3 +- 3 files changed, 135 insertions(+), 87 deletions(-) create mode 100644 lib/class_builder.js diff --git a/lib/class.js b/lib/class.js index 8a1ecc6..5084705 100644 --- a/lib/class.js +++ b/lib/class.js @@ -23,6 +23,7 @@ */ var util = require( './util' ), + class_builder = require( './class_builder' ), member_builder = require( './member_builder' ), propobj = require( './propobj' ) ; @@ -439,12 +440,9 @@ var extend = ( function( extending ) prototype = new base(), cname = '', - hasOwn = Array.prototype.hasOwnProperty, - properties = {}, prop_init = member_builder.initMembers(), members = member_builder.initMembers( prototype ), - defs = {}, abstract_methods = util.clone( getMeta( base ).abstractMethods ) @@ -470,89 +468,17 @@ var extend = ( function( extending ) // increment class identifier class_id++; - 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--; - } - }, - } ); + // build the various class components (xxx: this is temporary; needs + // refactoring) + class_builder.build( props, + class_id, + base, + prop_init, + abstract_methods, + properties, + members, + getMethodInstance + ); // reference to the parent prototype (for more experienced users) prototype.___$$parent$$ = base.prototype; diff --git a/lib/class_builder.js b/lib/class_builder.js new file mode 100644 index 0000000..4fef945 --- /dev/null +++ b/lib/class_builder.js @@ -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 . + * + * @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--; + } + }, + } ); +}; diff --git a/tools/combine b/tools/combine index 79b49b8..f6a6cba 100755 --- a/tools/combine +++ b/tools/combine @@ -28,7 +28,8 @@ TPL_VAR='/**{CONTENT}**/' RMTRAIL="$PATH_TOOLS/rmtrail" # 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