1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var getBuiltIn = require('../internals/get-built-in');
|
---|
4 | var apply = require('../internals/function-apply');
|
---|
5 | var bind = require('../internals/function-bind');
|
---|
6 | var aConstructor = require('../internals/a-constructor');
|
---|
7 | var anObject = require('../internals/an-object');
|
---|
8 | var isObject = require('../internals/is-object');
|
---|
9 | var create = require('../internals/object-create');
|
---|
10 | var fails = require('../internals/fails');
|
---|
11 |
|
---|
12 | var nativeConstruct = getBuiltIn('Reflect', 'construct');
|
---|
13 | var ObjectPrototype = Object.prototype;
|
---|
14 | var push = [].push;
|
---|
15 |
|
---|
16 | // `Reflect.construct` method
|
---|
17 | // https://tc39.es/ecma262/#sec-reflect.construct
|
---|
18 | // MS Edge supports only 2 arguments and argumentsList argument is optional
|
---|
19 | // FF Nightly sets third argument as `new.target`, but does not create `this` from it
|
---|
20 | var NEW_TARGET_BUG = fails(function () {
|
---|
21 | function F() { /* empty */ }
|
---|
22 | return !(nativeConstruct(function () { /* empty */ }, [], F) instanceof F);
|
---|
23 | });
|
---|
24 |
|
---|
25 | var ARGS_BUG = !fails(function () {
|
---|
26 | nativeConstruct(function () { /* empty */ });
|
---|
27 | });
|
---|
28 |
|
---|
29 | var FORCED = NEW_TARGET_BUG || ARGS_BUG;
|
---|
30 |
|
---|
31 | $({ target: 'Reflect', stat: true, forced: FORCED, sham: FORCED }, {
|
---|
32 | construct: function construct(Target, args /* , newTarget */) {
|
---|
33 | aConstructor(Target);
|
---|
34 | anObject(args);
|
---|
35 | var newTarget = arguments.length < 3 ? Target : aConstructor(arguments[2]);
|
---|
36 | if (ARGS_BUG && !NEW_TARGET_BUG) return nativeConstruct(Target, args, newTarget);
|
---|
37 | if (Target === newTarget) {
|
---|
38 | // w/o altered newTarget, optimization for 0-4 arguments
|
---|
39 | switch (args.length) {
|
---|
40 | case 0: return new Target();
|
---|
41 | case 1: return new Target(args[0]);
|
---|
42 | case 2: return new Target(args[0], args[1]);
|
---|
43 | case 3: return new Target(args[0], args[1], args[2]);
|
---|
44 | case 4: return new Target(args[0], args[1], args[2], args[3]);
|
---|
45 | }
|
---|
46 | // w/o altered newTarget, lot of arguments case
|
---|
47 | var $args = [null];
|
---|
48 | apply(push, $args, args);
|
---|
49 | return new (apply(bind, Target, $args))();
|
---|
50 | }
|
---|
51 | // with altered newTarget, not support built-in constructors
|
---|
52 | var proto = newTarget.prototype;
|
---|
53 | var instance = create(isObject(proto) ? proto : ObjectPrototype);
|
---|
54 | var result = apply(Target, instance, args);
|
---|
55 | return isObject(result) ? result : instance;
|
---|
56 | }
|
---|
57 | });
|
---|