1
0
Fork 0

Removed try/catch from Interface.extend

Permits more aggressive optimization.
perfodd
Mike Gerwitz 2014-03-29 00:57:22 -04:00
parent 77efd515de
commit 0228de6218
2 changed files with 74 additions and 47 deletions

View File

@ -177,6 +177,23 @@ function createNamedInterface( name, def )
}
/**
* Augment an exception with interface name and then throw
*
* @param {string} iname interface name or empty string
* @param {Error} e exception to augment
*/
function _ithrow( iname, e )
{
// alter the message to include our name
e.message = "Failed to define interface " +
( ( iname ) ? iname : '(anonymous)' ) + ": " + e.message
;
throw e;
}
var extend = ( function( extending )
{
return function extend()
@ -211,50 +228,43 @@ var extend = ( function( extending )
var new_interface = createInterface( iname );
try
{
util.propParse( props, {
assumeAbstract: true,
util.propParse( props, {
assumeAbstract: true,
property: function()
// override default exceptions from parser errors
_throw: function( e )
{
_ithrow( iname, e );
},
property: function()
{
// should never get to this point because of assumeAbstract
_ithrow( iname, TypeError( "Unexpected internal error" ) );
},
getset: function()
{
// should never get to this point because of assumeAbstract
_ithrow( iname, TypeError( "Unexpected internal error" ) );
},
method: function( name, value, is_abstract, keywords )
{
// all members must be public
if ( keywords[ 'protected' ] || keywords[ 'private' ] )
{
// should never get to this point because of assumeAbstract
throw TypeError( 'Unexpected internal error' );
},
_ithrow( iname, TypeError(
"Member " + name + " must be public"
) );
}
getset: function()
{
// should never get to this point because of assumeAbstract
throw TypeError( 'Unexpected internal error' );
},
method: function( name, value, is_abstract, keywords )
{
// all members must be public
if ( keywords[ 'protected' ] || keywords[ 'private' ] )
{
throw TypeError(
iname + " member " + name + " must be public"
);
}
member_builder.buildMethod(
members, null, name, value, keywords,
null, 0, {}, vstate
);
},
} );
}
catch ( e )
{
// alter the message to include our name
e.message = "Failed to define interface " +
( ( iname ) ? iname : '(anonymous)' ) + ": " + e.message
;
// re-throw
throw e;
}
member_builder.buildMethod(
members, null, name, value, keywords,
null, 0, {}, vstate
);
},
} );
attachExtend( new_interface );
attachStringMethod( new_interface, iname );

View File

@ -246,6 +246,19 @@ exports.copyTo = function( dest, src, deep )
};
/**
* Throw an exception
*
* Yes, this function has purpose; see where it's used.
*
* @param {Error} e exception to throw
*/
function _throw( e )
{
throw e;
}
/**
* Parses object properties to determine how they should be interpreted in an
* Object Oriented manner
@ -268,6 +281,8 @@ exports.propParse = function( data, options, context )
callbackGetSet = options.getset || fvoid,
keywordParser = options.keywordParser || propParseKeywords,
throwf = options._throw || _throw,
hasOwn = Object.prototype.hasOwnProperty,
parse_data = {},
@ -315,12 +330,12 @@ exports.propParse = function( data, options, context )
if ( !( value instanceof Array ) )
{
throw TypeError(
throwf( TypeError(
"Missing parameter list for abstract method: " + name
);
) );
}
verifyAbstractNames( name, value );
verifyAbstractNames( throwf, name, value );
value = exports.createAbstractMethod.apply( this, value );
}
@ -363,22 +378,24 @@ exports.propParse = function( data, options, context )
* In the future, we may add additional functionality, so it's important to
* restrict this as much as possible for the time being.
*
* @param {function(Error)} throwf function to call with error
*
* @param {string} name name of abstract member (for error)
* @param {Object} params parameter list to check
*
* @return {undefined}
*/
function verifyAbstractNames( name, params )
function verifyAbstractNames( throwf, name, params )
{
var i = params.length;
while ( i-- )
{
if ( params[ i ].match( /^[a-z_][a-z0-9_]*$/i ) === null )
{
throw SyntaxError(
throwf( SyntaxError(
"Member " + name + " contains invalid parameter '" +
params[ i ] + "'"
);
) );
}
}
}