Tim van der Lippe | bc3a0b7 | 2021-11-08 15:22:37 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | var GetIntrinsic = require('../GetIntrinsic.js'); |
| 4 | |
| 5 | var $construct = GetIntrinsic('%Reflect.construct%', true); |
| 6 | |
| 7 | var DefinePropertyOrThrow = require('./DefinePropertyOrThrow'); |
| 8 | try { |
| 9 | DefinePropertyOrThrow({}, '', { '[[Get]]': function () {} }); |
| 10 | } catch (e) { |
| 11 | // Accessor properties aren't supported |
| 12 | DefinePropertyOrThrow = null; |
| 13 | } |
| 14 | |
| 15 | // https://ecma-international.org/ecma-262/6.0/#sec-isconstructor |
| 16 | |
| 17 | if (DefinePropertyOrThrow && $construct) { |
| 18 | var isConstructorMarker = {}; |
| 19 | var badArrayLike = {}; |
| 20 | DefinePropertyOrThrow(badArrayLike, 'length', { |
| 21 | '[[Get]]': function () { |
| 22 | throw isConstructorMarker; |
| 23 | }, |
| 24 | '[[Enumerable]]': true |
| 25 | }); |
| 26 | |
| 27 | module.exports = function IsConstructor(argument) { |
| 28 | try { |
| 29 | // `Reflect.construct` invokes `IsConstructor(target)` before `Get(args, 'length')`: |
| 30 | $construct(argument, badArrayLike); |
| 31 | } catch (err) { |
| 32 | return err === isConstructorMarker; |
| 33 | } |
| 34 | }; |
| 35 | } else { |
| 36 | module.exports = function IsConstructor(argument) { |
| 37 | // unfortunately there's no way to truly check this without try/catch `new argument` in old environments |
| 38 | return typeof argument === 'function' && !!argument.prototype; |
| 39 | }; |
| 40 | } |