|
| 1 | +/* globals Reflect */ |
| 2 | + |
1 | 3 | import { DEBUG } from '@glimmer/env';
|
2 | 4 |
|
3 | 5 | const setPrototypeOf = Object.setPrototypeOf;
|
| 6 | +const getPrototypeOf = Object.getPrototypeOf; |
| 7 | + |
| 8 | +const hasReflectConstruct = typeof Reflect === 'object' && typeof Reflect.construct === 'function'; |
4 | 9 |
|
5 | 10 | const nativeWrapperCache = new Map();
|
6 | 11 |
|
@@ -117,3 +122,74 @@ export function objectDestructuringEmpty(obj) {
|
117 | 122 | throw new TypeError('Cannot destructure undefined');
|
118 | 123 | }
|
119 | 124 | }
|
| 125 | + |
| 126 | +/* |
| 127 | + Differs from default implementation by checking for _any_ `Reflect.construct` |
| 128 | + (the default implementation tries to ensure that `Reflect.construct` is truly |
| 129 | + the native one). |
| 130 | +
|
| 131 | + Original source: https://github.com/babel/babel/blob/v7.9.2/packages/babel-helpers/src/helpers.js#L738-L757 |
| 132 | +*/ |
| 133 | +export function createSuper(Derived) { |
| 134 | + return function() { |
| 135 | + let Super = getPrototypeOf(Derived); |
| 136 | + let result; |
| 137 | + |
| 138 | + if (hasReflectConstruct) { |
| 139 | + // NOTE: This doesn't work if this.__proto__.constructor has been modified. |
| 140 | + let NewTarget = getPrototypeOf(this).constructor; |
| 141 | + result = Reflect.construct(Super, arguments, NewTarget); |
| 142 | + } else { |
| 143 | + result = Super.apply(this, arguments); |
| 144 | + } |
| 145 | + |
| 146 | + return possibleConstructorReturn(this, result); |
| 147 | + }; |
| 148 | +} |
| 149 | + |
| 150 | +/* |
| 151 | + Does not differ from default implementation. |
| 152 | +*/ |
| 153 | +function arrayLikeToArray(arr, len) { |
| 154 | + if (len == null || len > arr.length) len = arr.length; |
| 155 | + |
| 156 | + let arr2 = new Array(len); |
| 157 | + for (let i = 0; i < len; i++) { |
| 158 | + arr2[i] = arr[i]; |
| 159 | + } |
| 160 | + |
| 161 | + return arr2; |
| 162 | +} |
| 163 | + |
| 164 | +/* |
| 165 | + Does not differ from default implementation. |
| 166 | +*/ |
| 167 | +function unsupportedIterableToArray(o, minLen) { |
| 168 | + if (!o) return; |
| 169 | + if (typeof o === 'string') return arrayLikeToArray(o, minLen); |
| 170 | + let n = Object.prototype.toString.call(o).slice(8, -1); |
| 171 | + if (n === 'Object' && o.constructor) n = o.constructor.name; |
| 172 | + if (n === 'Map' || n === 'Set') return Array.from(n); |
| 173 | + if (n === 'Arguments' || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) |
| 174 | + return arrayLikeToArray(o, minLen); |
| 175 | +} |
| 176 | + |
| 177 | +/* |
| 178 | + Does not differ from default implementation. |
| 179 | +*/ |
| 180 | +export function createForOfIteratorHelperLoose(o) { |
| 181 | + let i = 0; |
| 182 | + if (typeof Symbol === 'undefined' || o[Symbol.iterator] == null) { |
| 183 | + // Fallback for engines without symbol support |
| 184 | + if (Array.isArray(o) || (o = unsupportedIterableToArray(o))) |
| 185 | + return function() { |
| 186 | + if (i >= o.length) return { done: true }; |
| 187 | + return { done: false, value: o[i++] }; |
| 188 | + }; |
| 189 | + throw new TypeError( |
| 190 | + 'Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.' |
| 191 | + ); |
| 192 | + } |
| 193 | + i = o[Symbol.iterator](); |
| 194 | + return i.next.bind(i); |
| 195 | +} |
0 commit comments