Added support for public static getters/setters
parent
4d0724b85d
commit
af53fe81b4
|
@ -420,15 +420,19 @@ function buildMembers(
|
||||||
|
|
||||||
getter: function( name, value, keywords )
|
getter: function( name, value, keywords )
|
||||||
{
|
{
|
||||||
|
var dest = ( keywords[ 'static' ] ) ? static_members : members;
|
||||||
|
|
||||||
member_builder.buildGetter(
|
member_builder.buildGetter(
|
||||||
members, null, name, value, keywords
|
dest, null, name, value, keywords
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
setter: function( name, value, keywords )
|
setter: function( name, value, keywords )
|
||||||
{
|
{
|
||||||
|
var dest = ( keywords[ 'static' ] ) ? static_members : members;
|
||||||
|
|
||||||
member_builder.buildSetter(
|
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 )
|
function attachStatic( ctor, members )
|
||||||
{
|
{
|
||||||
var pub = members[ 'public' ];
|
|
||||||
|
|
||||||
// copy over public static members
|
// copy over public static members
|
||||||
for ( name in pub )
|
util.copyTo( ctor, members[ 'public' ] );
|
||||||
{
|
|
||||||
ctor[ name ] = pub[ name ];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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)"
|
||||||
|
);
|
||||||
|
} )();
|
||||||
|
|
Loading…
Reference in New Issue