[d24f17c] | 1 | 'use strict';
|
---|
| 2 | var global = require('../internals/global');
|
---|
| 3 | var apply = require('../internals/function-apply');
|
---|
| 4 | var uncurryThis = require('../internals/function-uncurry-this-clause');
|
---|
| 5 | var isCallable = require('../internals/is-callable');
|
---|
| 6 | var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;
|
---|
| 7 | var isForced = require('../internals/is-forced');
|
---|
| 8 | var path = require('../internals/path');
|
---|
| 9 | var bind = require('../internals/function-bind-context');
|
---|
| 10 | var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
|
---|
| 11 | var hasOwn = require('../internals/has-own-property');
|
---|
| 12 | // add debugging info
|
---|
| 13 | require('../internals/shared-store');
|
---|
| 14 |
|
---|
| 15 | var wrapConstructor = function (NativeConstructor) {
|
---|
| 16 | var Wrapper = function (a, b, c) {
|
---|
| 17 | if (this instanceof Wrapper) {
|
---|
| 18 | switch (arguments.length) {
|
---|
| 19 | case 0: return new NativeConstructor();
|
---|
| 20 | case 1: return new NativeConstructor(a);
|
---|
| 21 | case 2: return new NativeConstructor(a, b);
|
---|
| 22 | } return new NativeConstructor(a, b, c);
|
---|
| 23 | } return apply(NativeConstructor, this, arguments);
|
---|
| 24 | };
|
---|
| 25 | Wrapper.prototype = NativeConstructor.prototype;
|
---|
| 26 | return Wrapper;
|
---|
| 27 | };
|
---|
| 28 |
|
---|
| 29 | /*
|
---|
| 30 | options.target - name of the target object
|
---|
| 31 | options.global - target is the global object
|
---|
| 32 | options.stat - export as static methods of target
|
---|
| 33 | options.proto - export as prototype methods of target
|
---|
| 34 | options.real - real prototype method for the `pure` version
|
---|
| 35 | options.forced - export even if the native feature is available
|
---|
| 36 | options.bind - bind methods to the target, required for the `pure` version
|
---|
| 37 | options.wrap - wrap constructors to preventing global pollution, required for the `pure` version
|
---|
| 38 | options.unsafe - use the simple assignment of property instead of delete + defineProperty
|
---|
| 39 | options.sham - add a flag to not completely full polyfills
|
---|
| 40 | options.enumerable - export as enumerable property
|
---|
| 41 | options.dontCallGetSet - prevent calling a getter on target
|
---|
| 42 | options.name - the .name of the function if it does not match the key
|
---|
| 43 | */
|
---|
| 44 | module.exports = function (options, source) {
|
---|
| 45 | var TARGET = options.target;
|
---|
| 46 | var GLOBAL = options.global;
|
---|
| 47 | var STATIC = options.stat;
|
---|
| 48 | var PROTO = options.proto;
|
---|
| 49 |
|
---|
| 50 | var nativeSource = GLOBAL ? global : STATIC ? global[TARGET] : global[TARGET] && global[TARGET].prototype;
|
---|
| 51 |
|
---|
| 52 | var target = GLOBAL ? path : path[TARGET] || createNonEnumerableProperty(path, TARGET, {})[TARGET];
|
---|
| 53 | var targetPrototype = target.prototype;
|
---|
| 54 |
|
---|
| 55 | var FORCED, USE_NATIVE, VIRTUAL_PROTOTYPE;
|
---|
| 56 | var key, sourceProperty, targetProperty, nativeProperty, resultProperty, descriptor;
|
---|
| 57 |
|
---|
| 58 | for (key in source) {
|
---|
| 59 | FORCED = isForced(GLOBAL ? key : TARGET + (STATIC ? '.' : '#') + key, options.forced);
|
---|
| 60 | // contains in native
|
---|
| 61 | USE_NATIVE = !FORCED && nativeSource && hasOwn(nativeSource, key);
|
---|
| 62 |
|
---|
| 63 | targetProperty = target[key];
|
---|
| 64 |
|
---|
| 65 | if (USE_NATIVE) if (options.dontCallGetSet) {
|
---|
| 66 | descriptor = getOwnPropertyDescriptor(nativeSource, key);
|
---|
| 67 | nativeProperty = descriptor && descriptor.value;
|
---|
| 68 | } else nativeProperty = nativeSource[key];
|
---|
| 69 |
|
---|
| 70 | // export native or implementation
|
---|
| 71 | sourceProperty = (USE_NATIVE && nativeProperty) ? nativeProperty : source[key];
|
---|
| 72 |
|
---|
| 73 | if (!FORCED && !PROTO && typeof targetProperty == typeof sourceProperty) continue;
|
---|
| 74 |
|
---|
| 75 | // bind methods to global for calling from export context
|
---|
| 76 | if (options.bind && USE_NATIVE) resultProperty = bind(sourceProperty, global);
|
---|
| 77 | // wrap global constructors for prevent changes in this version
|
---|
| 78 | else if (options.wrap && USE_NATIVE) resultProperty = wrapConstructor(sourceProperty);
|
---|
| 79 | // make static versions for prototype methods
|
---|
| 80 | else if (PROTO && isCallable(sourceProperty)) resultProperty = uncurryThis(sourceProperty);
|
---|
| 81 | // default case
|
---|
| 82 | else resultProperty = sourceProperty;
|
---|
| 83 |
|
---|
| 84 | // add a flag to not completely full polyfills
|
---|
| 85 | if (options.sham || (sourceProperty && sourceProperty.sham) || (targetProperty && targetProperty.sham)) {
|
---|
| 86 | createNonEnumerableProperty(resultProperty, 'sham', true);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | createNonEnumerableProperty(target, key, resultProperty);
|
---|
| 90 |
|
---|
| 91 | if (PROTO) {
|
---|
| 92 | VIRTUAL_PROTOTYPE = TARGET + 'Prototype';
|
---|
| 93 | if (!hasOwn(path, VIRTUAL_PROTOTYPE)) {
|
---|
| 94 | createNonEnumerableProperty(path, VIRTUAL_PROTOTYPE, {});
|
---|
| 95 | }
|
---|
| 96 | // export virtual prototype methods
|
---|
| 97 | createNonEnumerableProperty(path[VIRTUAL_PROTOTYPE], key, sourceProperty);
|
---|
| 98 | // export real prototype methods
|
---|
| 99 | if (options.real && targetPrototype && (FORCED || !targetPrototype[key])) {
|
---|
| 100 | createNonEnumerableProperty(targetPrototype, key, sourceProperty);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | };
|
---|