_mergeData.js (3135B)
1 var composeArgs = require('./_composeArgs'), 2 composeArgsRight = require('./_composeArgsRight'), 3 replaceHolders = require('./_replaceHolders'); 4 5 /** Used as the internal argument placeholder. */ 6 var PLACEHOLDER = '__lodash_placeholder__'; 7 8 /** Used to compose bitmasks for function metadata. */ 9 var WRAP_BIND_FLAG = 1, 10 WRAP_BIND_KEY_FLAG = 2, 11 WRAP_CURRY_BOUND_FLAG = 4, 12 WRAP_CURRY_FLAG = 8, 13 WRAP_ARY_FLAG = 128, 14 WRAP_REARG_FLAG = 256; 15 16 /* Built-in method references for those with the same name as other `lodash` methods. */ 17 var nativeMin = Math.min; 18 19 /** 20 * Merges the function metadata of `source` into `data`. 21 * 22 * Merging metadata reduces the number of wrappers used to invoke a function. 23 * This is possible because methods like `_.bind`, `_.curry`, and `_.partial` 24 * may be applied regardless of execution order. Methods like `_.ary` and 25 * `_.rearg` modify function arguments, making the order in which they are 26 * executed important, preventing the merging of metadata. However, we make 27 * an exception for a safe combined case where curried functions have `_.ary` 28 * and or `_.rearg` applied. 29 * 30 * @private 31 * @param {Array} data The destination metadata. 32 * @param {Array} source The source metadata. 33 * @returns {Array} Returns `data`. 34 */ 35 function mergeData(data, source) { 36 var bitmask = data[1], 37 srcBitmask = source[1], 38 newBitmask = bitmask | srcBitmask, 39 isCommon = newBitmask < (WRAP_BIND_FLAG | WRAP_BIND_KEY_FLAG | WRAP_ARY_FLAG); 40 41 var isCombo = 42 ((srcBitmask == WRAP_ARY_FLAG) && (bitmask == WRAP_CURRY_FLAG)) || 43 ((srcBitmask == WRAP_ARY_FLAG) && (bitmask == WRAP_REARG_FLAG) && (data[7].length <= source[8])) || 44 ((srcBitmask == (WRAP_ARY_FLAG | WRAP_REARG_FLAG)) && (source[7].length <= source[8]) && (bitmask == WRAP_CURRY_FLAG)); 45 46 // Exit early if metadata can't be merged. 47 if (!(isCommon || isCombo)) { 48 return data; 49 } 50 // Use source `thisArg` if available. 51 if (srcBitmask & WRAP_BIND_FLAG) { 52 data[2] = source[2]; 53 // Set when currying a bound function. 54 newBitmask |= bitmask & WRAP_BIND_FLAG ? 0 : WRAP_CURRY_BOUND_FLAG; 55 } 56 // Compose partial arguments. 57 var value = source[3]; 58 if (value) { 59 var partials = data[3]; 60 data[3] = partials ? composeArgs(partials, value, source[4]) : value; 61 data[4] = partials ? replaceHolders(data[3], PLACEHOLDER) : source[4]; 62 } 63 // Compose partial right arguments. 64 value = source[5]; 65 if (value) { 66 partials = data[5]; 67 data[5] = partials ? composeArgsRight(partials, value, source[6]) : value; 68 data[6] = partials ? replaceHolders(data[5], PLACEHOLDER) : source[6]; 69 } 70 // Use source `argPos` if available. 71 value = source[7]; 72 if (value) { 73 data[7] = value; 74 } 75 // Use source `ary` if it's smaller. 76 if (srcBitmask & WRAP_ARY_FLAG) { 77 data[8] = data[8] == null ? source[8] : nativeMin(data[8], source[8]); 78 } 79 // Use source `arity` if one is not provided. 80 if (data[9] == null) { 81 data[9] = source[9]; 82 } 83 // Use source `func` and merge bitmasks. 84 data[0] = source[0]; 85 data[1] = newBitmask; 86 87 return data; 88 } 89 90 module.exports = mergeData;