From af092c9eaed9a25ce5f8cf07f46a663089410494 Mon Sep 17 00:00:00 2001 From: rock Date: Mon, 22 Jun 2015 02:00:45 +0600 Subject: [PATCH] add support native constructors to Reflect.construct with 2 arguments, #83 --- library/modules/es5.js | 2 +- library/modules/es6.reflect.construct.js | 8 +++++--- modules/es5.js | 2 +- modules/es6.reflect.construct.js | 8 +++++--- tests/tests-library.js | 1 + tests/tests-library/es6.reflect.construct.ls | 3 ++- tests/tests.js | 1 + tests/tests/es6.reflect.construct.ls | 3 ++- 8 files changed, 18 insertions(+), 10 deletions(-) diff --git a/library/modules/es5.js b/library/modules/es5.js index e0e645a0fd81..556eb9202f19 100644 --- a/library/modules/es5.js +++ b/library/modules/es5.js @@ -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; } diff --git a/library/modules/es6.reflect.construct.js b/library/modules/es6.reflect.construct.js index 4491cc891268..ffbd7310ac8b 100644 --- a/library/modules/es6.reflect.construct.js +++ b/library/modules/es6.reflect.construct.js @@ -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; } }); \ No newline at end of file diff --git a/modules/es5.js b/modules/es5.js index e0e645a0fd81..556eb9202f19 100644 --- a/modules/es5.js +++ b/modules/es5.js @@ -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; } diff --git a/modules/es6.reflect.construct.js b/modules/es6.reflect.construct.js index 4491cc891268..ffbd7310ac8b 100644 --- a/modules/es6.reflect.construct.js +++ b/modules/es6.reflect.construct.js @@ -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; } }); \ No newline at end of file diff --git a/tests/tests-library.js b/tests/tests-library.js index 6e870c3e41c8..342717fa786a 100644 --- a/tests/tests-library.js +++ b/tests/tests-library.js @@ -2882,6 +2882,7 @@ return false; } }())); + eq(construct(Set, [[1, 2, 3, 2, 1]]).size, 3, 'works with native constructors'); }); }).call(this); diff --git a/tests/tests-library/es6.reflect.construct.ls b/tests/tests-library/es6.reflect.construct.ls index 9c169640c71a..f71c859362d1 100644 --- a/tests/tests-library/es6.reflect.construct.ls +++ b/tests/tests-library/es6.reflect.construct.ls @@ -20,4 +20,5 @@ test '*' !-> f = (->) f:: = 42 ok try getPrototypeOf(construct f, []) is Object:: - catch => no \ No newline at end of file + catch => no + eq construct(Set, [[1, 2, 3, 2, 1]]).size, 3, 'works with native constructors' \ No newline at end of file diff --git a/tests/tests.js b/tests/tests.js index 6cc208f65d5b..14c04d61d173 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -3179,6 +3179,7 @@ return false; } }())); + eq(construct(Set, [[1, 2, 3, 2, 1]]).size, 3, 'works with native constructors'); }); }).call(this); diff --git a/tests/tests/es6.reflect.construct.ls b/tests/tests/es6.reflect.construct.ls index f2df724c5c00..f155249f44a0 100644 --- a/tests/tests/es6.reflect.construct.ls +++ b/tests/tests/es6.reflect.construct.ls @@ -21,4 +21,5 @@ test '*' !-> f = (->) f:: = 42 ok try getPrototypeOf(construct f, []) is Object:: - catch => no \ No newline at end of file + catch => no + eq construct(Set, [[1, 2, 3, 2, 1]]).size, 3, 'works with native constructors' \ No newline at end of file