From 9822894eae06097749ecf16ae2051a06e5fae365 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 11 May 2011 17:56:48 -0400 Subject: [PATCH] Protected static methods are now inherited by subtypes --- lib/class_builder.js | 4 +-- test/test-class_builder-static.js | 52 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/lib/class_builder.js b/lib/class_builder.js index 8891786..4fc0977 100644 --- a/lib/class_builder.js +++ b/lib/class_builder.js @@ -228,13 +228,13 @@ exports.build = function extend() var new_class = createCtor( cname, abstract_methods, members ); // closure to hold static initialization to be used later by subtypes + initStaticVisibilityObj( new_class, static_members ); var staticInit = function( ctor, inheriting ) { attachStatic( ctor, static_members, base, inheriting ); } staticInit( new_class, false ); - initStaticVisibilityObj( new_class, static_members ); attachPropInit( prototype, prop_init, members, new_class, class_id ); new_class.prototype = prototype; @@ -615,7 +615,6 @@ function initStaticVisibilityObj( ctor, static_members ) sobj.prototype = ctor; var sobji = new sobj(); - util.copyTo( sobji, static_members.methods[ 'protected' ] ); // override __self on the instance's visibility object, giving internal // methods access to the restricted static methods @@ -698,6 +697,7 @@ function attachStatic( ctor, members, base, inheriting ) // copy over public static methods util.copyTo( ctor, methods[ 'public' ], true ); + util.copyTo( ctor.___$$svis$$, methods[ 'protected' ], true ); } diff --git a/test/test-class_builder-static.js b/test/test-class_builder-static.js index 32b97d9..ec7c1a9 100644 --- a/test/test-class_builder-static.js +++ b/test/test-class_builder-static.js @@ -474,3 +474,55 @@ var common = require( './common' ), ); } )(); + +/** + * As usual, protected members (in this case, static) should be inherited by + * subtypes. + */ +( function testProtectedStaticMembersAreInheritedBySubtypes() +{ + var val = 'baz', + val2 = 'bazbaz', + Foo = builder.build( + { + 'protected static foo': function() + { + return val; + }, + } ), + + SubFoo = builder.build( Foo, + { + 'public static bar': function() + { + return this.foo(); + }, + + 'protected static foo2': function() + { + return val2; + }, + + 'public static bar2': function() + { + return this.foo2(); + }, + } ), + + SubSubFoo = builder.build( SubFoo, {} ) + ; + + assert.equal( SubFoo.bar(), val, + "Subtypes inherit parents' protected static methods" + ); + + assert.equal( SubFoo.bar2(), val2, + "Static methods have access to other static methods in the same class" + ); + + // for extra assurance, to ensure our recursive implementation is correct + assert.equal( SubSubFoo.bar(), val, + "Sub-subtypes inherit parents' protected static methods" + ); +} )(); +