1
0
Fork 0

Split concrete and abstract ctor generation into two separate functions

closure/master
Mike Gerwitz 2011-03-27 23:16:19 -04:00
parent 5bb0269280
commit 8ba68b31dc
1 changed files with 238 additions and 199 deletions

View File

@ -41,7 +41,16 @@ var util = require( __dirname + '/util' ),
* Instance id counter, to be incremented on each new instance * Instance id counter, to be incremented on each new instance
* @type {number} * @type {number}
*/ */
instance_id = 0 instance_id = 0,
/**
* Set to TRUE when class is in the process of being extended to ensure that
* a constructor can be instantiated (to use as the prototype) without
* invoking the class construction logic
*
* @type {boolean}
*/
extending = false
; ;
@ -71,9 +80,7 @@ var enum_bug = (
exports.ClassBase = function Class() {}; exports.ClassBase = function Class() {};
exports.build = ( function( extending ) /**
{
/**
* Mimics class inheritance * Mimics class inheritance
* *
* This method will mimic inheritance by setting up the prototype with the * This method will mimic inheritance by setting up the prototype with the
@ -85,8 +92,8 @@ exports.build = ( function( extending )
* *
* @return {Object} extended class * @return {Object} extended class
*/ */
return function extend() exports.build = function extend()
{ {
// ensure we'll be permitted to instantiate abstract classes for the base // ensure we'll be permitted to instantiate abstract classes for the base
extending = true; extending = true;
@ -162,10 +169,10 @@ exports.build = ( function( extending )
abstractMethods: abstract_methods, abstractMethods: abstract_methods,
classId: class_id, classId: class_id,
}; };
}; };
/** /**
* Creates the constructor for a new class * Creates the constructor for a new class
* *
* This constructor will call the __constructor method for concrete classes * This constructor will call the __constructor method for concrete classes
@ -177,11 +184,34 @@ exports.build = ( function( extending )
* *
* @return {Function} constructor * @return {Function} constructor
*/ */
function createCtor( cname, abstract_methods, members ) function createCtor( cname, abstract_methods, members )
{ {
// concrete class // concrete class
if ( abstract_methods.__length === 0 ) if ( abstract_methods.__length === 0 )
{ {
return createConcreteCtor( cname, members );
}
// abstract class
else
{
return createAbstractCtor( cname );
}
}
/**
* Creates the constructor for a new concrete class
*
* This constructor will call the __constructor method of the class, if
* available.
*
* @param {string} cname class name (may be empty)
* @param {Object} members class members
*
* @return {function()} constructor
*/
function createConcreteCtor( cname, members )
{
var args = null; var args = null;
// constructor function to be returned // constructor function to be returned
@ -246,10 +276,21 @@ exports.build = ( function( extending )
; ;
return __self; return __self;
} }
// abstract class
else
{ /**
* Creates the constructor for a new abstract class
*
* Calling this constructor will cause an exception to be thrown, as abstract
* classes cannot be instantiated.
*
* @param {string} cname class name (may be empty)
*
* @return {function()} constructor
*/
function createAbstractCtor( cname )
{
var __abstract_self = function() var __abstract_self = function()
{ {
if ( !extending ) if ( !extending )
@ -273,9 +314,7 @@ exports.build = ( function( extending )
; ;
return __abstract_self; return __abstract_self;
} }
}
} )( false );
function buildMembers( function buildMembers(