Permalink
Browse files

Check multiple array methods for proper context argument object-boxing.

  • Loading branch information...
ljharb committed Jan 4, 2014
1 parent 55e7890 commit 06915fc820ffa72f5f947c420cf4ac813f18ed9b
Showing with 19 additions and 14 deletions.
  1. +19 −14 es5-shim.js
View
@@ -334,19 +334,24 @@ if (!Array.isArray) {
// http://es5.github.com/#x15.4.4.18
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/forEach
+
// Check failure of by-index access of string characters (IE < 9)
// and failure of `0 in boxedString` (Rhino)
-var boxedString = Object("a"),
- splitString = boxedString[0] != "a" || !(0 in boxedString);
-// Check node 0.6.21 bug where third parameter is not boxed
-var boxedForEach = true;
-if (Array.prototype.forEach) {
- Array.prototype.forEach.call("foo", function(item, i, obj) {
- if (typeof obj !== 'object') boxedForEach = false;
- });
-}
+var boxedString = Object("a");
+var splitString = boxedString[0] != "a" || !(0 in boxedString);
+
+var properlyBoxesContext = function properlyBoxed(method) {
+ // Check node 0.6.21 bug where third parameter is not boxed
+ var properlyBoxes = true;
+ if (method) {
+ method.call('foo', function (item, index, context) {
+ if (typeof context !== 'object') { properlyBoxes = false; }
+ });
+ }
+ return !!method && properlyBoxes;
+};
-if (!Array.prototype.forEach || !boxedForEach) {
+if (!Array.prototype.forEach || !properlyBoxesContext(Array.prototype.forEach)) {
Array.prototype.forEach = function forEach(fun /*, thisp*/) {
var object = toObject(this),
self = splitString && _toString(this) == "[object String]" ?
@@ -375,7 +380,7 @@ if (!Array.prototype.forEach || !boxedForEach) {
// ES5 15.4.4.19
// http://es5.github.com/#x15.4.4.19
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
-if (!Array.prototype.map) {
+if (!Array.prototype.map || !properlyBoxesContext(Array.prototype.map)) {
Array.prototype.map = function map(fun /*, thisp*/) {
var object = toObject(this),
self = splitString && _toString(this) == "[object String]" ?
@@ -401,7 +406,7 @@ if (!Array.prototype.map) {
// ES5 15.4.4.20
// http://es5.github.com/#x15.4.4.20
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter
-if (!Array.prototype.filter) {
+if (!Array.prototype.filter || !properlyBoxesContext(Array.prototype.filter)) {
Array.prototype.filter = function filter(fun /*, thisp */) {
var object = toObject(this),
self = splitString && _toString(this) == "[object String]" ?
@@ -432,7 +437,7 @@ if (!Array.prototype.filter) {
// ES5 15.4.4.16
// http://es5.github.com/#x15.4.4.16
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
-if (!Array.prototype.every) {
+if (!Array.prototype.every || !properlyBoxesContext(Array.prototype.every)) {
Array.prototype.every = function every(fun /*, thisp */) {
var object = toObject(this),
self = splitString && _toString(this) == "[object String]" ?
@@ -458,7 +463,7 @@ if (!Array.prototype.every) {
// ES5 15.4.4.17
// http://es5.github.com/#x15.4.4.17
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
-if (!Array.prototype.some) {
+if (!Array.prototype.some || !properlyBoxesContext(Array.prototype.some)) {
Array.prototype.some = function some(fun /*, thisp */) {
var object = toObject(this),
self = splitString && _toString(this) == "[object String]" ?

0 comments on commit 06915fc

Please sign in to comment.