From e463d2c411911c0fb4442b6590575e67363775aa Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sun, 6 Mar 2011 18:19:19 -0500 Subject: [PATCH] Began factoring property instance object out of class module --- lib/class.js | 55 +++------------------------------- lib/propobj.js | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ tools/combine | 2 +- 3 files changed, 86 insertions(+), 52 deletions(-) create mode 100644 lib/propobj.js diff --git a/lib/class.js b/lib/class.js index 93ba5e8..a746417 100644 --- a/lib/class.js +++ b/lib/class.js @@ -23,7 +23,9 @@ */ var util = require( './util' ), - member_builder = require( './member_builder' ); + member_builder = require( './member_builder' ), + propobj = require( './propobj' ) +; /** * Stores class metadata internally (ensures data is encapsulated) @@ -772,10 +774,6 @@ function initInstance( iid, instance ) */ function attachPropInit( prototype, properties, members ) { - var prop_pub = properties[ 'public' ], - prop_prot = properties[ 'protected' ] - ; - util.defineSecureProp( prototype, '__initProps', function() { var inst_props = class_instance[ this.__iid ]; @@ -788,52 +786,7 @@ function attachPropInit( prototype, properties, members ) parent_init.call( this ); } - // initialize each of the properties for this instance to - // ensure we're not sharing prototype values - for ( prop in prop_pub ) - { - // initialize the value with a clone to ensure that they do - // not share references (and therefore, data) - this[ prop ] = util.clone( prop_pub[ prop ] ); - - ( function( prop ) - { - var inst = this; - - // public properties, when set internally, must forward to the - // actual variable - inst_props.__defineSetter__( prop, function( val ) - { - inst[ prop ] = val; - } ); - - // since we're defining a setter, we'll need to define a getter - // to return the value, or we'll simply return undefined - inst_props.__defineGetter__( prop, function() - { - return inst[ prop ]; - } ); - } ).call( this, prop ); - } - - var methods_protected = members[ 'protected' ], - hasOwn = Array.prototype.hasOwnProperty - ; - - // copy over the methods - for ( method_name in methods_protected ) - { - if ( hasOwn.call( methods_protected, method_name ) ) - { - inst_props[ method_name ] = methods_protected[ method_name ]; - } - } - - // initialize protected properties and store in instance data - for ( prop in prop_prot ) - { - inst_props[ prop ] = util.clone( prop_prot[ prop ] ); - } + propobj.setup( this, inst_props, properties, members ); }); } diff --git a/lib/propobj.js b/lib/propobj.js new file mode 100644 index 0000000..5f9d59b --- /dev/null +++ b/lib/propobj.js @@ -0,0 +1,81 @@ +/** + * Contains property object generator + * + * 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' ); + + +exports.setup = function( base, dest, properties, members ) +{ + var prop_pub = properties[ 'public' ], + prop_prot = properties[ 'protected' ] + ; + + // initialize each of the properties for this instance to + // ensure we're not sharing references to prototype values + for ( prop in prop_pub ) + { + // initialize the value with a clone to ensure that they do + // not share references (and therefore, data) + base[ prop ] = util.clone( prop_pub[ prop ] ); + + ( function( prop ) + { + var inst = this; + + // public properties, when set internally, must forward to the + // actual variable + dest.__defineSetter__( prop, function( val ) + { + inst[ prop ] = val; + } ); + + // since we're defining a setter, we'll need to define a getter + // to return the value, or we'll simply return undefined + dest.__defineGetter__( prop, function() + { + return inst[ prop ]; + } ); + } ).call( base, prop ); + } + + var methods_protected = members[ 'protected' ], + hasOwn = Array.prototype.hasOwnProperty + ; + + // copy over the methods + for ( method_name in methods_protected ) + { + if ( hasOwn.call( methods_protected, method_name ) ) + { + dest[ method_name ] = methods_protected[ method_name ]; + } + } + + // initialize protected properties and store in instance data + for ( prop in prop_prot ) + { + dest[ prop ] = util.clone( prop_prot[ prop ] ); + } +} + diff --git a/tools/combine b/tools/combine index 50b01b7..1272ccc 100755 --- a/tools/combine +++ b/tools/combine @@ -28,7 +28,7 @@ TPL_VAR='/**{CONTENT}**/' RMTRAIL="$PATH_TOOLS/rmtrail" # order matters -CAT_MODULES="prop_parser util member_builder class interface" +CAT_MODULES="prop_parser util member_builder propobj class interface" ## # Output template header