UNPKG

5.55 kBJavaScriptView Raw
1/*!
2 * https://github.com/paulmillr/es6-shim
3 * @license es6-shim Copyright 2013-2016 by Paul Miller (http://paulmillr.com)
4 * and contributors, MIT License
5 * es6-sham: v0.35.4
6 * see https://github.com/paulmillr/es6-shim/blob/0.35.3/LICENSE
7 * Details and documentation:
8 * https://github.com/paulmillr/es6-shim/
9 */
10
11// UMD (Universal Module Definition)
12// see https://github.com/umdjs/umd/blob/master/returnExports.js
13(function (root, factory) {
14 /*global define */
15 if (typeof define === 'function' && define.amd) {
16 // AMD. Register as an anonymous module.
17 define(factory);
18 } else if (typeof exports === 'object') {
19 // Node. Does not work with strict CommonJS, but
20 // only CommonJS-like environments that support module.exports,
21 // like Node.
22 module.exports = factory();
23 } else {
24 // Browser globals (root is window)
25 root.returnExports = factory();
26 }
27}(this, function () {
28 'use strict';
29
30 /* eslint-disable no-new-func */
31 var getGlobal = new Function('return this;');
32 /* eslint-enable no-new-func */
33
34 var globals = getGlobal();
35 var Object = globals.Object;
36 var _call = Function.call.bind(Function.call);
37 var functionToString = Function.toString;
38 var _strMatch = String.prototype.match;
39
40 var throwsError = function (func) {
41 try {
42 func();
43 return false;
44 } catch (e) {
45 return true;
46 }
47 };
48 var arePropertyDescriptorsSupported = function () {
49 // if Object.defineProperty exists but throws, it's IE 8
50 return !throwsError(function () {
51 Object.defineProperty({}, 'x', { get: function () {} }); // eslint-disable-line getter-return
52 });
53 };
54 var supportsDescriptors = !!Object.defineProperty && arePropertyDescriptorsSupported();
55
56 // NOTE: This versions needs object ownership
57 // because every promoted object needs to be reassigned
58 // otherwise uncompatible browsers cannot work as expected
59 //
60 // NOTE: This might need es5-shim or polyfills upfront
61 // because it's based on ES5 API.
62 // (probably just an IE <= 8 problem)
63 //
64 // NOTE: nodejs is fine in version 0.8, 0.10, and future versions.
65 (function () {
66 if (Object.setPrototypeOf) { return; }
67
68 // @author Andrea Giammarchi - @WebReflection
69
70 var getOwnPropertyNames = Object.getOwnPropertyNames;
71 var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
72 var create = Object.create;
73 var defineProperty = Object.defineProperty;
74 var getPrototypeOf = Object.getPrototypeOf;
75 var objProto = Object.prototype;
76
77 var copyDescriptors = function (target, source) {
78 // define into target descriptors from source
79 getOwnPropertyNames(source).forEach(function (key) {
80 defineProperty(
81 target,
82 key,
83 getOwnPropertyDescriptor(source, key)
84 );
85 });
86 return target;
87 };
88 // used as fallback when no promotion is possible
89 var createAndCopy = function (origin, proto) {
90 return copyDescriptors(create(proto), origin);
91 };
92 var set, setPrototypeOf;
93 try {
94 // this might fail for various reasons
95 // ignore if Chrome cought it at runtime
96 set = getOwnPropertyDescriptor(objProto, '__proto__').set;
97 set.call({}, null);
98 // setter not poisoned, it can promote
99 // Firefox, Chrome
100 setPrototypeOf = function (origin, proto) {
101 set.call(origin, proto);
102 return origin;
103 };
104 } catch (e) {
105 // do one or more feature detections
106 set = { __proto__: null };
107 // if proto does not work, needs to fallback
108 // some Opera, Rhino, ducktape
109 if (set instanceof Object) {
110 setPrototypeOf = createAndCopy;
111 } else {
112 // verify if null objects are buggy
113 /* eslint-disable no-proto */
114 set.__proto__ = objProto;
115 /* eslint-enable no-proto */
116 // if null objects are buggy
117 // nodejs 0.8 to 0.10
118 if (set instanceof Object) {
119 setPrototypeOf = function (origin, proto) {
120 // use such bug to promote
121 /* eslint-disable no-proto */
122 origin.__proto__ = proto;
123 /* eslint-enable no-proto */
124 return origin;
125 };
126 } else {
127 // try to use proto or fallback
128 // Safari, old Firefox, many others
129 setPrototypeOf = function (origin, proto) {
130 // if proto is not null
131 if (getPrototypeOf(origin)) {
132 // use __proto__ to promote
133 /* eslint-disable no-proto */
134 origin.__proto__ = proto;
135 /* eslint-enable no-proto */
136 return origin;
137 }
138 // otherwise unable to promote: fallback
139 return createAndCopy(origin, proto);
140 };
141 }
142 }
143 }
144 Object.setPrototypeOf = setPrototypeOf;
145 }());
146
147 if (supportsDescriptors && function foo() {}.name !== 'foo') {
148 /* eslint no-extend-native: 1 */
149 Object.defineProperty(Function.prototype, 'name', {
150 configurable: true,
151 enumerable: false,
152 get: function () {
153 if (this === Function.prototype) {
154 return '';
155 }
156 var str = _call(functionToString, this);
157 var match = _call(_strMatch, str, /\s*function\s+([^(\s]*)\s*/);
158 var name = match && match[1];
159 Object.defineProperty(this, 'name', {
160 configurable: true,
161 enumerable: false,
162 writable: false,
163 value: name
164 });
165 return name;
166 }
167 });
168 }
169}));