From 841b5ac5a5574a48f7739837442942d99fbc13d4 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 7 Mar 2011 22:44:47 -0500 Subject: [PATCH 1/3] Replaed all __{define,lookup}[GS]etter__'s with defineProperty/getOwnPropertyDescriptor --- lib/member_builder.js | 24 +++++++++- lib/util.js | 19 ++++---- test/test-class-gettersetter.js | 66 ++++++++++++++++----------- test/test-interface-extend.js | 14 ++++-- test/test-util-prop-parse-keywords.js | 13 ++++-- test/test-util-prop-parse.js | 10 ++-- 6 files changed, 96 insertions(+), 50 deletions(-) diff --git a/lib/member_builder.js b/lib/member_builder.js index ac56890..4bc2665 100644 --- a/lib/member_builder.js +++ b/lib/member_builder.js @@ -157,7 +157,17 @@ exports.buildProp = function( members, meta, name, value, keywords ) */ exports.buildGetter = function( members, meta, name, value, keywords ) { - getMemberVisibility( members, keywords ).__defineGetter__( name, value ); + Object.defineProperty( + getMemberVisibility( members, keywords ), + name, + { + get: value, + enumerable: true, + + // otherwise we can't add a setter to this + configurable: true, + } + ); }; @@ -177,7 +187,17 @@ exports.buildGetter = function( members, meta, name, value, keywords ) */ exports.buildSetter = function( members, meta, name, value, keywords ) { - getMemberVisibility( members, keywords ).__defineSetter__( name, value ); + Object.defineProperty( + getMemberVisibility( members, keywords ), + name, + { + set: value, + enumerable: true, + + // otherwise we can't add a getter to this + configurable: true, + } + ); }; diff --git a/lib/util.js b/lib/util.js index fffab55..9403d05 100644 --- a/lib/util.js +++ b/lib/util.js @@ -25,15 +25,6 @@ var propParseKeywords = require( './prop_parser' ).parseKeywords; -/** - * Whether getters/setters are supported - * @var {boolean} - */ -var getset = ( Object.prototype.__defineGetter__ === undefined ) - ? false - : true -; - /** * Whether we can actually define properties, or we need to fall back * @@ -220,9 +211,15 @@ exports.propParse = function( data, options ) continue; } + // retrieve getters/setters, if supported + if ( can_define_prop ) + { + var prop_desc = Object.getOwnPropertyDescriptor( data, prop ); + getter = prop_desc.get; + setter = prop_desc.set; + } + value = data[ prop ]; - getter = ( ( getset ) ? data.__lookupGetter__( prop ) : null ); - setter = ( ( getset ) ? data.__lookupSetter__( prop ) : null ); parse_data = keywordParser( prop ) || {}; name = parse_data.name || prop; diff --git a/test/test-class-gettersetter.js b/test/test-class-gettersetter.js index ccee24d..720efa3 100644 --- a/test/test-class-gettersetter.js +++ b/test/test-class-gettersetter.js @@ -22,46 +22,60 @@ * @package test */ -// don't perform these tests if getters/setters are unsupported -if ( Object.prototype.__defineGetter__ === undefined ) -{ - return; -} - var common = require( './common' ), assert = require( 'assert' ), Class = common.require( 'class' ), + util = common.require( 'util' ), foo_def = {}, sub_foo_def = {} ; +// don't perform these tests if getters/setters are unsupported +if ( util.definePropertyFallback() ) +{ + return; +} + // to prevent syntax errors in browsers that do not support getters/setters in // object notation -foo_def.__defineGetter__( 'foo', function () -{ - return this._foo; -} ); -foo_def.__defineSetter__( 'foo', function ( val ) -{ - this._foo = ''+( val ); -} ); -foo_def.__defineGetter__( 'bar', function () -{ - return 'durp'; -} ); -foo_def.__defineSetter__( 'bar', function ( val ) -{ +Object.defineProperty( foo_def, 'foo', { + get: function () + { + return this._foo; + }, + set: function ( val ) + { + this._foo = ''+( val ); + }, + + enumerable: true, } ); -sub_foo_def.__defineGetter__( 'bar', function () -{ - return this.bar2; +Object.defineProperty( foo_def, 'bar', { + get: function () + { + return 'durp'; + }, + set: function ( val ) + { + }, + + enumerable: true, } ); -sub_foo_def.__defineSetter__( 'bar', function ( val ) -{ - this.bar2 = val; + +Object.defineProperty( sub_foo_def, 'bar', { + get: function () + { + return this.bar2; + }, + set: function ( val ) + { + this.bar2 = val; + }, + + enumerable: true, } ); var Foo = Class.extend( foo_def ), diff --git a/test/test-interface-extend.js b/test/test-interface-extend.js index f9197c5..96901c8 100644 --- a/test/test-interface-extend.js +++ b/test/test-interface-extend.js @@ -24,7 +24,9 @@ var common = require( './common' ), assert = require( 'assert' ), - Interface = common.require( 'interface' ); + Interface = common.require( 'interface' ), + util = common.require( 'util' ) +; ( function testPropertiesNotPermittedWithinInterfaces() @@ -43,7 +45,7 @@ var common = require( './common' ), ( function testGettersAndSettersNotPermittedWithinInterfaces() { // don't perform this test if unsupported by environment - if ( Object.prototype.__defineGetter__ === undefined ) + if ( util.definePropertyFallback() ) { return; } @@ -51,8 +53,12 @@ var common = require( './common' ), // so we don't break browsers that do not support getters/setters in object // notation var data = {}; - data.__defineGetter__( 'foo', function() {} ); - data.__defineSetter__( 'foo', function() {} ); + Object.defineProperty( data, 'foo', { + get: function() {}, + set: function() {}, + + enumerable: true, + } ); assert.throws( function() { diff --git a/test/test-util-prop-parse-keywords.js b/test/test-util-prop-parse-keywords.js index 7322dd2..04da770 100644 --- a/test/test-util-prop-parse-keywords.js +++ b/test/test-util-prop-parse-keywords.js @@ -24,7 +24,8 @@ var common = require( './common' ), assert = require( 'assert' ), - util = common.require( 'util' ); + util = common.require( 'util' ) +; ( function testAbstractKeywordDesignatesMethodAsAbstract() @@ -173,15 +174,19 @@ var common = require( './common' ), // for browsers that support it - if ( Object.prototype.__defineGetter__ !== undefined ) + if ( util.definePropertyFallback() === false ) { data = {}; parsed_keywords = {}; // to prevent syntax errors for environments that don't support // getters/setters in object notation - data.__defineGetter__( 'public foo', function() {} ); - data.__defineSetter__( 'public foo', function() {} ); + Object.defineProperty( data, 'public foo', { + get: function() {}, + set: function() {}, + + enumerable: true, + } ); util.propParse( data, { diff --git a/test/test-util-prop-parse.js b/test/test-util-prop-parse.js index 0debfa8..807a102 100644 --- a/test/test-util-prop-parse.js +++ b/test/test-util-prop-parse.js @@ -26,7 +26,7 @@ var common = require( './common' ), assert = require( 'assert' ), util = common.require( 'util' ), - get_set = ( ( Object.prototype.__defineGetter__ ) ? true : false ) + get_set = !( util.definePropertyFallback() ) ; var data = { @@ -52,8 +52,12 @@ var data = { // only add getter/setter if it's supported by our engine if ( get_set ) { - data.__defineGetter__( 'someFoo', function () {} ); - data.__defineSetter__( 'someFoo', function () {} ); + Object.defineProperty( data, 'someFoo', { + get: function () {}, + set: function () {}, + + enumerable: true, + } ); } From ab8faad53f59d28c62581bf4e7f36c16ff5e55f5 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 7 Mar 2011 22:54:59 -0500 Subject: [PATCH 2/3] /bin/{bash => sh} to support system with only sh installed - e.g. FreeBSD - Nothing in the scripts actually needs BASH anyway --- test/test-rmtrail | 2 +- tools/combine | 2 +- tools/rmtrail | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test-rmtrail b/test/test-rmtrail index d02c609..e845429 100755 --- a/test/test-rmtrail +++ b/test/test-rmtrail @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # # Copyright (C) 2010 Mike Gerwitz # diff --git a/tools/combine b/tools/combine index 50b01b7..ccc05e6 100755 --- a/tools/combine +++ b/tools/combine @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # # Copyright (C) 2010 Mike Gerwitz # diff --git a/tools/rmtrail b/tools/rmtrail index 1e69603..bb001d0 100755 --- a/tools/rmtrail +++ b/tools/rmtrail @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # # Removes trailing commas from array and object declarations (certain browsers, # such as earlier versions of IE, do not parse trailing commas correctly) From 8de036ec46cfd5b4169245c7a6983e81125a4df5 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 7 Mar 2011 23:08:01 -0500 Subject: [PATCH 3/3] Basic package.json --- package.json | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..2ccc412 --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "easejs", + "description": "A basic collection of CommonJS modules intended to \"ease\" the transition into Javascript from other Object-Oriented languages", + "version": "0.1.0-pre", + "author": "Mike Gerwitz ", + + "repository": { + "type": "git", + "url": "git://github.com/mikegerwitz/easejs.git" + }, + + "tags": [ + "object oriented", + "oo", + "util", + "utility", + "lib", + "library", + "framework", + "classes", + "interfaces", + "protected members", + "inheritance" + ] +}