From d4593725a470111f6916ed4f6f840ee49580cd84 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sun, 14 Nov 2010 21:33:13 -0500 Subject: [PATCH] If a definition is provided for an abstract method, the concrete implementation must be compatiable (proper number of arguments) --- lib/class.js | 15 +++++++++++++++ test/test-class-abstract.js | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/class.js b/lib/class.js index f038213..99acddb 100644 --- a/lib/class.js +++ b/lib/class.js @@ -64,6 +64,8 @@ exports.abstractMethod = function( definition ) }; define_secure_prop( method, 'abstractFlag', true ); + define_secure_prop( method, 'definition', definition ); + return method; } @@ -135,6 +137,19 @@ function prop_copy( props, dest, result_data ) // then the method should no longer be considered abstract if ( abstract_map[ property ] !== undefined ) { + if ( pre.definition ) + { + // ensure the concrete definition is compatible with + // that of its supertype + if ( prop.length < pre.definition.length ) + { + throw new Error( + "Declaration of " + property + " must be compatiable" + + "with that of its supertype" + ); + } + } + delete abstract_methods[ abstract_map[ property ] ]; abstract_regen = true; } diff --git a/test/test-class-abstract.js b/test/test-class-abstract.js index 29e8dfe..56b3706 100644 --- a/test/test-class-abstract.js +++ b/test/test-class-abstract.js @@ -158,3 +158,29 @@ assert.equal( "Can call constructors of abstract supertypes" ); + +assert.throws( function() +{ + AbstractFoo.extend( + { + // incorrect number of arguments + method: function() + { + }, + }); +}, Error, "Concrete methods must implement the proper number of argments" ); + +assert.doesNotThrow( + function() + { + AbstractFoo.extend( + { + second: function( foo ) + { + }, + }); + }, Error, + "Concrete methods needn't implement the proper number of arguments if " + + "no definition was provided" +); +