From 4be1922df5a89656f382e60ec5879e270ccbbf8f Mon Sep 17 00:00:00 2001 From: Ryan Jones Date: Mon, 28 Nov 2016 14:47:15 -0800 Subject: [PATCH 1/6] add bind polyfill --- scripts/system/libraries/utils.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/scripts/system/libraries/utils.js b/scripts/system/libraries/utils.js index b7de9b012c..6a3f20717e 100644 --- a/scripts/system/libraries/utils.js +++ b/scripts/system/libraries/utils.js @@ -6,6 +6,34 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +if (!Function.prototype.bind) { + Function.prototype.bind = function(oThis) { + if (typeof this !== 'function') { + // closest thing possible to the ECMAScript 5 + // internal IsCallable function + throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); + } + + var aArgs = Array.prototype.slice.call(arguments, 1), + fToBind = this, + fNOP = function() {}, + fBound = function() { + return fToBind.apply(this instanceof fNOP + ? this + : oThis, + aArgs.concat(Array.prototype.slice.call(arguments))); + }; + + if (this.prototype) { + // Function.prototype doesn't have a prototype property + fNOP.prototype = this.prototype; + } + fBound.prototype = new fNOP(); + + return fBound; + }; +} + vec3toStr = function(v, digits) { if (!digits) { digits = 3; } return "{ " + v.x.toFixed(digits) + ", " + v.y.toFixed(digits) + ", " + v.z.toFixed(digits)+ " }"; From 68e86da023fbc48880e123c930874c253bbb8abf Mon Sep 17 00:00:00 2001 From: Ryan Jones Date: Mon, 28 Nov 2016 17:10:59 -0800 Subject: [PATCH 2/6] adjust indentation and implement editorconfig at root of repo --- .editorconfig | 6 ++++++ scripts/developer/libraries/utils.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000000..70dee8a217 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,6 @@ +root = true + +# 4-space indentation +[*] +indent_style = space +indent_size = 4 \ No newline at end of file diff --git a/scripts/developer/libraries/utils.js b/scripts/developer/libraries/utils.js index f39f4d7913..64f3b2c9e0 100644 --- a/scripts/developer/libraries/utils.js +++ b/scripts/developer/libraries/utils.js @@ -6,6 +6,34 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +if (!Function.prototype.bind) { + Function.prototype.bind = function(oThis) { + if (typeof this !== 'function') { + // closest thing possible to the ECMAScript 5 + // internal IsCallable function + throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); + } + + var aArgs = Array.prototype.slice.call(arguments, 1), + fToBind = this, + fNOP = function() {}, + fBound = function() { + return fToBind.apply(this instanceof fNOP + ? this + : oThis, + aArgs.concat(Array.prototype.slice.call(arguments))); + }; + + if (this.prototype) { + // Function.prototype doesn't have a prototype property + fNOP.prototype = this.prototype; + } + fBound.prototype = new fNOP(); + + return fBound; + }; +} + vec3toStr = function(v, digits) { if (!digits) { digits = 3; } return "{ " + v.x.toFixed(digits) + ", " + v.y.toFixed(digits) + ", " + v.z.toFixed(digits)+ " }"; From 3961dbcf55f48be8c8b303e38ad57de7c3817d53 Mon Sep 17 00:00:00 2001 From: Ryan Jones Date: Mon, 28 Nov 2016 17:18:31 -0800 Subject: [PATCH 3/6] revert changes to dev utils, apply changes to system utils --- scripts/developer/libraries/utils.js | 28 ---------------------------- scripts/system/libraries/utils.js | 22 +++++++++++----------- 2 files changed, 11 insertions(+), 39 deletions(-) diff --git a/scripts/developer/libraries/utils.js b/scripts/developer/libraries/utils.js index 64f3b2c9e0..f39f4d7913 100644 --- a/scripts/developer/libraries/utils.js +++ b/scripts/developer/libraries/utils.js @@ -6,34 +6,6 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -if (!Function.prototype.bind) { - Function.prototype.bind = function(oThis) { - if (typeof this !== 'function') { - // closest thing possible to the ECMAScript 5 - // internal IsCallable function - throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); - } - - var aArgs = Array.prototype.slice.call(arguments, 1), - fToBind = this, - fNOP = function() {}, - fBound = function() { - return fToBind.apply(this instanceof fNOP - ? this - : oThis, - aArgs.concat(Array.prototype.slice.call(arguments))); - }; - - if (this.prototype) { - // Function.prototype doesn't have a prototype property - fNOP.prototype = this.prototype; - } - fBound.prototype = new fNOP(); - - return fBound; - }; -} - vec3toStr = function(v, digits) { if (!digits) { digits = 3; } return "{ " + v.x.toFixed(digits) + ", " + v.y.toFixed(digits) + ", " + v.z.toFixed(digits)+ " }"; diff --git a/scripts/system/libraries/utils.js b/scripts/system/libraries/utils.js index 6a3f20717e..3d03f51e78 100644 --- a/scripts/system/libraries/utils.js +++ b/scripts/system/libraries/utils.js @@ -7,31 +7,31 @@ // if (!Function.prototype.bind) { - Function.prototype.bind = function(oThis) { +Function.prototype.bind = function(oThis) { if (typeof this !== 'function') { - // closest thing possible to the ECMAScript 5 - // internal IsCallable function - throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); + // closest thing possible to the ECMAScript 5 + // internal IsCallable function + throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); } var aArgs = Array.prototype.slice.call(arguments, 1), fToBind = this, fNOP = function() {}, fBound = function() { - return fToBind.apply(this instanceof fNOP - ? this - : oThis, - aArgs.concat(Array.prototype.slice.call(arguments))); + return fToBind.apply(this instanceof fNOP + ? this + : oThis, + aArgs.concat(Array.prototype.slice.call(arguments))); }; if (this.prototype) { - // Function.prototype doesn't have a prototype property - fNOP.prototype = this.prototype; + // Function.prototype doesn't have a prototype property + fNOP.prototype = this.prototype; } fBound.prototype = new fNOP(); return fBound; - }; + }; } vec3toStr = function(v, digits) { From a59df5b04d84630f794774e0300e200dab701fb1 Mon Sep 17 00:00:00 2001 From: Ryan Jones Date: Mon, 28 Nov 2016 17:19:43 -0800 Subject: [PATCH 4/6] one last indentation fix --- scripts/system/libraries/utils.js | 42 +++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/scripts/system/libraries/utils.js b/scripts/system/libraries/utils.js index 3d03f51e78..9bd29d663a 100644 --- a/scripts/system/libraries/utils.js +++ b/scripts/system/libraries/utils.js @@ -7,30 +7,30 @@ // if (!Function.prototype.bind) { -Function.prototype.bind = function(oThis) { - if (typeof this !== 'function') { - // closest thing possible to the ECMAScript 5 - // internal IsCallable function - throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); - } + Function.prototype.bind = function(oThis) { + if (typeof this !== 'function') { + // closest thing possible to the ECMAScript 5 + // internal IsCallable function + throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); + } - var aArgs = Array.prototype.slice.call(arguments, 1), - fToBind = this, - fNOP = function() {}, - fBound = function() { - return fToBind.apply(this instanceof fNOP - ? this - : oThis, - aArgs.concat(Array.prototype.slice.call(arguments))); - }; + var aArgs = Array.prototype.slice.call(arguments, 1), + fToBind = this, + fNOP = function() {}, + fBound = function() { + return fToBind.apply(this instanceof fNOP + ? this + : oThis, + aArgs.concat(Array.prototype.slice.call(arguments))); + }; - if (this.prototype) { - // Function.prototype doesn't have a prototype property - fNOP.prototype = this.prototype; - } - fBound.prototype = new fNOP(); + if (this.prototype) { + // Function.prototype doesn't have a prototype property + fNOP.prototype = this.prototype; + } + fBound.prototype = new fNOP(); - return fBound; + return fBound; }; } From 251b070db314c5c7e13fd389f69bf78eb2adc55c Mon Sep 17 00:00:00 2001 From: Ryan Jones Date: Tue, 29 Nov 2016 11:50:11 -0800 Subject: [PATCH 5/6] add unit test --- scripts/developer/tests/bindUnitTest.js | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 scripts/developer/tests/bindUnitTest.js diff --git a/scripts/developer/tests/bindUnitTest.js b/scripts/developer/tests/bindUnitTest.js new file mode 100644 index 0000000000..38c2fb350f --- /dev/null +++ b/scripts/developer/tests/bindUnitTest.js @@ -0,0 +1,31 @@ +Script.include('../libraries/jasmine/jasmine.js'); +Script.include('../libraries/jasmine/hifi-boot.js'); +Script.include('../../system/libraries/utils.js'); + +describe('Bind', function() { + it('functions should have bind available', function() { + var foo = 'bar'; + + function callAnotherFn(anotherFn) { + return anotherFn(); + } + + function TestConstructor() { + this.foo = foo; + } + + TestConstructor.prototype.doSomething = function() { + return callAnotherFn(function() { + return this.foo; + }.bind(this)); + } + + var instance = new TestConstructor(); + + expect(typeof(instance.doSomething.bind) !== 'undefined'); + expect(instance.doSomething()).toEqual(foo); + }); +}); + +jasmine.getEnv().execute(); +Script.stop(); \ No newline at end of file From 01ae2b945248ac11f6c5f02cb2769d5fd645b2be Mon Sep 17 00:00:00 2001 From: Ryan Jones Date: Tue, 29 Nov 2016 11:56:11 -0800 Subject: [PATCH 6/6] add errant semicolon --- scripts/developer/tests/bindUnitTest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/developer/tests/bindUnitTest.js b/scripts/developer/tests/bindUnitTest.js index 38c2fb350f..95fd497916 100644 --- a/scripts/developer/tests/bindUnitTest.js +++ b/scripts/developer/tests/bindUnitTest.js @@ -18,7 +18,7 @@ describe('Bind', function() { return callAnotherFn(function() { return this.foo; }.bind(this)); - } + }; var instance = new TestConstructor();