1 var arrayCopy = require('./arrayCopy'),
2 composeArgs = require('./composeArgs'),
3 composeArgsRight = require('./composeArgsRight'),
4 replaceHolders = require('./replaceHolders');
6 /** Used to compose bitmasks for wrapper metadata. */
13 /** Used as the internal argument placeholder. */
14 var PLACEHOLDER = '__lodash_placeholder__';
16 /* Native 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 required 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 `_.rearg`
25 * augment function arguments, making the order in which they are executed important,
26 * preventing the merging of metadata. However, we make an exception for a safe
27 * common case where curried functions have `_.ary` and or `_.rearg` applied.
30 * @param {Array} data The destination metadata.
31 * @param {Array} source The source metadata.
32 * @returns {Array} Returns `data`.
34 function mergeData(data, source) {
35 var bitmask = data[1],
36 srcBitmask = source[1],
37 newBitmask = bitmask | srcBitmask,
38 isCommon = newBitmask < ARY_FLAG;
41 (srcBitmask == ARY_FLAG && bitmask == CURRY_FLAG) ||
42 (srcBitmask == ARY_FLAG && bitmask == REARG_FLAG && data[7].length <= source[8]) ||
43 (srcBitmask == (ARY_FLAG | REARG_FLAG) && bitmask == CURRY_FLAG);
45 // Exit early if metadata can't be merged.
46 if (!(isCommon || isCombo)) {
49 // Use source `thisArg` if available.
50 if (srcBitmask & BIND_FLAG) {
52 // Set when currying a bound function.
53 newBitmask |= (bitmask & BIND_FLAG) ? 0 : CURRY_BOUND_FLAG;
55 // Compose partial arguments.
56 var value = source[3];
58 var partials = data[3];
59 data[3] = partials ? composeArgs(partials, value, source[4]) : arrayCopy(value);
60 data[4] = partials ? replaceHolders(data[3], PLACEHOLDER) : arrayCopy(source[4]);
62 // Compose partial right arguments.
66 data[5] = partials ? composeArgsRight(partials, value, source[6]) : arrayCopy(value);
67 data[6] = partials ? replaceHolders(data[5], PLACEHOLDER) : arrayCopy(source[6]);
69 // Use source `argPos` if available.
72 data[7] = arrayCopy(value);
74 // Use source `ary` if it's smaller.
75 if (srcBitmask & ARY_FLAG) {
76 data[8] = data[8] == null ? source[8] : nativeMin(data[8], source[8]);
78 // Use source `arity` if one is not provided.
79 if (data[9] == null) {
82 // Use source `func` and merge bitmasks.
89 module.exports = mergeData;