From af53fe81b4aebea7ca2e9149ee2c8ce7e1634239 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 5 Apr 2011 00:08:39 -0400 Subject: [PATCH] Added support for public static getters/setters --- lib/class_builder.js | 15 ++++---- test/test-class_builder-static.js | 63 +++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/lib/class_builder.js b/lib/class_builder.js index 5896172..280ac81 100644 --- a/lib/class_builder.js +++ b/lib/class_builder.js @@ -420,15 +420,19 @@ function buildMembers( getter: function( name, value, keywords ) { + var dest = ( keywords[ 'static' ] ) ? static_members : members; + member_builder.buildGetter( - members, null, name, value, keywords + dest, null, name, value, keywords ); }, setter: function( name, value, keywords ) { + var dest = ( keywords[ 'static' ] ) ? static_members : members; + member_builder.buildSetter( - members, null, name, value, keywords + dest, null, name, value, keywords ); }, @@ -547,13 +551,8 @@ function attachPropInit( prototype, properties, members, cid ) */ function attachStatic( ctor, members ) { - var pub = members[ 'public' ]; - // copy over public static members - for ( name in pub ) - { - ctor[ name ] = pub[ name ]; - } + util.copyTo( ctor, members[ 'public' ] ); } diff --git a/test/test-class_builder-static.js b/test/test-class_builder-static.js index 48a8c64..6fd2d89 100644 --- a/test/test-class_builder-static.js +++ b/test/test-class_builder-static.js @@ -87,3 +87,66 @@ var common = require( './common' ), ); } )(); + +/** + * Same as above, but with getters/setters. We can only run this test if + * getters/setters are supported by the engine running it. + */ +( function testPublicStaticGettersSettersAreAccessibleViaClassDefinitionOnly() +{ + // if unsupported, don't bother with the test + if ( fallback ) + { + return; + } + + // we must define in this manner so older engines won't blow up due to + // syntax errors + var def = {}, + val = 'baz' + called = []; + + Object.defineProperty( def, 'public static foo', { + get: function() { return val; }, + set: function() { called[ 0 ] = true; }, + + enumerable: true, + } ); + + // should be public by default if not specified + Object.defineProperty( def, 'static bar', { + get: function() { return val; }, + set: function() { called[ 1 ] = true; }, + + enumerable: true, + } ); + + // define the class + var Foo = builder.build( def ); + + assert.equal( Foo.foo, val, + "Public static getters are accessible via class definition" + ); + + Foo.foo = 'moo'; + assert.equal( called[ 0 ], true, + "Public static setters are accessible via class definition" + ); + + assert.equal( Foo.bar, val, + "Static getters are public by default" + ); + + Foo.bar = 'moo'; + assert.equal( called[ 1 ], true, + "Static setters are public by default" + ); + + // none of these should be available on the prototype + assert.equal( Foo.prototype.foo, undefined, + "Public static getters/getters are unavailable on prototype (0)" + ); + assert.equal( Foo.prototype.bar, undefined, + "Public static getters/getters are unavailable on prototype (1)" + ); +} )();