Skip to content

Commit

Permalink
add support native constructors to Reflect.construct with 2 arguments,
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jun 21, 2015
1 parent 0738020 commit af092c9
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion library/modules/es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ $def($def.P, 'Function', {
function bound(/* args... */){
var args = partArgs.concat(_slice.call(arguments))
, constr = this instanceof bound
, ctx = constr ? $.create(fn.prototype) : that
, ctx = constr ? $.create(isObject(fn.prototype) ? fn.prototype : ObjectProto) : that
, result = invoke(fn, args, ctx);
return constr ? ctx : result;
}
Expand Down
8 changes: 5 additions & 3 deletions library/modules/es6.reflect.construct.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ var $ = require('./$')
, $def = require('./$.def')
, assertFunction = require('./$.assert').fn
, isObject = $.isObject
, _apply = Function.apply;
, apply = Function.apply
, bind = Function.bind;

$def($def.S, 'Reflect', {
construct: function construct(target, argumentsList /*, newTarget*/){
var proto = assertFunction(arguments.length < 3 ? target : arguments[2]).prototype
if(arguments.length < 3)return new (bind.apply(target, [null].concat(argumentsList)))();
var proto = assertFunction(arguments[2]).prototype
, instance = $.create(isObject(proto) ? proto : Object.prototype)
, result = _apply.call(target, instance, argumentsList);
, result = apply.call(target, instance, argumentsList);
return isObject(result) ? result : instance;
}
});
2 changes: 1 addition & 1 deletion modules/es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ $def($def.P, 'Function', {
function bound(/* args... */){
var args = partArgs.concat(_slice.call(arguments))
, constr = this instanceof bound
, ctx = constr ? $.create(fn.prototype) : that
, ctx = constr ? $.create(isObject(fn.prototype) ? fn.prototype : ObjectProto) : that
, result = invoke(fn, args, ctx);
return constr ? ctx : result;
}
Expand Down
8 changes: 5 additions & 3 deletions modules/es6.reflect.construct.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ var $ = require('./$')
, $def = require('./$.def')
, assertFunction = require('./$.assert').fn
, isObject = $.isObject
, _apply = Function.apply;
, apply = Function.apply
, bind = Function.bind;

$def($def.S, 'Reflect', {
construct: function construct(target, argumentsList /*, newTarget*/){
var proto = assertFunction(arguments.length < 3 ? target : arguments[2]).prototype
if(arguments.length < 3)return new (bind.apply(target, [null].concat(argumentsList)))();
var proto = assertFunction(arguments[2]).prototype
, instance = $.create(isObject(proto) ? proto : Object.prototype)
, result = _apply.call(target, instance, argumentsList);
, result = apply.call(target, instance, argumentsList);
return isObject(result) ? result : instance;
}
});
1 change: 1 addition & 0 deletions tests/tests-library.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/tests-library/es6.reflect.construct.ls
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ test '*' !->
f = (->)
f:: = 42
ok try getPrototypeOf(construct f, []) is Object::
catch => no
catch => no
eq construct(Set, [[1, 2, 3, 2, 1]]).size, 3, 'works with native constructors'
1 change: 1 addition & 0 deletions tests/tests.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/tests/es6.reflect.construct.ls
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ test '*' !->
f = (->)
f:: = 42
ok try getPrototypeOf(construct f, []) is Object::
catch => no
catch => no
eq construct(Set, [[1, 2, 3, 2, 1]]).size, 3, 'works with native constructors'

0 comments on commit af092c9

Please sign in to comment.