Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #2134 from jridgewell/ios-jit-isArrayLike

Fix iOS JIT Bug, Part 2
commit 3101e18a5818b158e9ffc18291be4c6a167e28f8 2 parents 4c7b7e4 + 086a56d
@megawac megawac authored
Showing with 21 additions and 20 deletions.
  1. +21 −20 underscore.js
View
41 underscore.js
@@ -122,13 +122,20 @@
return result;
};
+ var property = function(key) {
+ return function(obj) {
+ return obj == null ? void 0 : obj[key];
+ };
+ };
+
// Helper for collection methods to determine whether a collection
// should be iterated as an array or as an object
// Related: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength
// Avoids a very nasty iOS 8 JIT bug on ARM-64. #2094
- var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1, LENGTH = 'length';
+ var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1;
+ var getLength = property('length');
var isArrayLike = function(collection) {
- var length = collection != null && collection[LENGTH];
+ var length = getLength(collection);
return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX;
};
@@ -482,7 +489,7 @@
// Internal implementation of a recursive `flatten` function.
var flatten = function(input, shallow, strict, startIndex) {
var output = [], idx = 0;
- for (var i = startIndex || 0, length = input && input.length; i < length; i++) {
+ for (var i = startIndex || 0, length = getLength(input); i < length; i++) {
var value = input[i];
if (isArrayLike(value) && (_.isArray(value) || _.isArguments(value))) {
//flatten current level of array or arguments object
@@ -513,7 +520,6 @@
// been sorted, you have the option of using a faster algorithm.
// Aliased as `unique`.
_.uniq = _.unique = function(array, isSorted, iteratee, context) {
- if (array == null) return [];
if (!_.isBoolean(isSorted)) {
context = iteratee;
iteratee = isSorted;
@@ -522,7 +528,7 @@
if (iteratee != null) iteratee = cb(iteratee, context);
var result = [];
var seen = [];
- for (var i = 0, length = array.length; i < length; i++) {
+ for (var i = 0, length = getLength(array); i < length; i++) {
var value = array[i],
computed = iteratee ? iteratee(value, i, array) : value;
if (isSorted) {
@@ -549,10 +555,9 @@
// Produce an array that contains every item shared between all the
// passed-in arrays.
_.intersection = function(array) {
- if (array == null) return [];
var result = [];
var argsLength = arguments.length;
- for (var i = 0, length = array.length; i < length; i++) {
+ for (var i = 0, length = getLength(array); i < length; i++) {
var item = array[i];
if (_.contains(result, item)) continue;
for (var j = 1; j < argsLength; j++) {
@@ -581,7 +586,7 @@
// Complement of _.zip. Unzip accepts an array of arrays and groups
// each array's elements on shared indices
_.unzip = function(array) {
- var length = array && _.max(array, 'length').length || 0;
+ var length = array && _.max(array, getLength).length || 0;
var result = Array(length);
for (var index = 0; index < length; index++) {
@@ -595,7 +600,7 @@
// the corresponding values.
_.object = function(list, values) {
var result = {};
- for (var i = 0, length = list && list.length; i < length; i++) {
+ for (var i = 0, length = getLength(list); i < length; i++) {
if (values) {
result[list[i]] = values[i];
} else {
@@ -609,7 +614,7 @@
function createPredicateIndexFinder(dir) {
return function(array, predicate, context) {
predicate = cb(predicate, context);
- var length = array != null && array.length;
+ var length = getLength(array);
var index = dir > 0 ? 0 : length - 1;
for (; index >= 0 && index < length; index += dir) {
if (predicate(array[index], index, array)) return index;
@@ -627,7 +632,7 @@
_.sortedIndex = function(array, obj, iteratee, context) {
iteratee = cb(iteratee, context, 1);
var value = iteratee(obj);
- var low = 0, high = array.length;
+ var low = 0, high = getLength(array);
while (low < high) {
var mid = Math.floor((low + high) / 2);
if (iteratee(array[mid]) < value) low = mid + 1; else high = mid;
@@ -638,7 +643,7 @@
// Generator function to create the indexOf and lastIndexOf functions
function createIndexFinder(dir, predicateFind, sortedIndex) {
return function(array, item, idx) {
- var i = 0, length = array ? array.length : -1;
+ var i = 0, length = getLength(array);
if (typeof idx == 'number') {
if (dir > 0) {
i = idx >= 0 ? idx : Math.max(idx + length, i);
@@ -1136,7 +1141,7 @@
}
// Assume equality for cyclic structures. The algorithm for detecting cyclic
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
-
+
// Initializing stack of traversed objects.
// It's done here since we only need them for objects and arrays comparison.
aStack = aStack || [];
@@ -1287,11 +1292,7 @@
_.noop = function(){};
- _.property = function(key) {
- return function(obj) {
- return obj == null ? void 0 : obj[key];
- };
- };
+ _.property = property;
// Generates a function for a given object that returns a given property.
_.propertyOf = function(obj) {
@@ -1300,7 +1301,7 @@
};
};
- // Returns a predicate for checking whether an object has a given set of
+ // Returns a predicate for checking whether an object has a given set of
// `key:value` pairs.
_.matcher = _.matches = function(attrs) {
attrs = _.extendOwn({}, attrs);
@@ -1527,7 +1528,7 @@
// Provide unwrapping proxy for some methods used in engine operations
// such as arithmetic and JSON stringification.
_.prototype.valueOf = _.prototype.toJSON = _.prototype.value;
-
+
_.prototype.toString = function() {
return '' + this._wrapped;
};
Please sign in to comment.
Something went wrong with that request. Please try again.