1 var composeArgs = require('./_composeArgs'),
2 composeArgsRight = require('./_composeArgsRight'),
3 replaceHolders = require('./_replaceHolders');
5 /** Used as the internal argument placeholder. */
6 var PLACEHOLDER = '__lodash_placeholder__';
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,
14 WRAP_REARG_FLAG = 256;
16 /* Built-in method references for those with the same name as other `lodash` methods. */
17 var nativeMin = Math.min;
20 * Merges the function metadata of `source` into `data`.
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.
31 * @param {Array} data The destination metadata.
32 * @param {Array} source The source metadata.
33 * @returns {Array} Returns `data`.
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);
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));
46 // Exit early if metadata can't be merged.
47 if (!(isCommon || isCombo)) {
50 // Use source `thisArg` if available.
51 if (srcBitmask & WRAP_BIND_FLAG) {
53 // Set when currying a bound function.
54 newBitmask |= bitmask & WRAP_BIND_FLAG ? 0 : WRAP_CURRY_BOUND_FLAG;
56 // Compose partial arguments.
57 var value = source[3];
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];
63 // Compose partial right arguments.
67 data[5] = partials ? composeArgsRight(partials, value, source[6]) : value;
68 data[6] = partials ? replaceHolders(data[5], PLACEHOLDER) : source[6];
70 // Use source `argPos` if available.
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]);
79 // Use source `arity` if one is not provided.
80 if (data[9] == null) {
83 // Use source `func` and merge bitmasks.
90 module.exports = mergeData;