From 184047dfce349e148d56d1cea26423a0117e00fd Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Thu, 28 Apr 2016 00:06:56 +0700 Subject: [PATCH] `Reflect.apply` should not allow primitive as the third argument, close #194 --- library/modules/es6.reflect.apply.js | 8 +++++--- modules/es6.reflect.apply.js | 8 +++++--- tests/es.js | 8 +++++++- tests/library.js | 6 ++++++ tests/library/es6.reflect.apply.ls | 4 +++- tests/tests.js | 8 +++++++- tests/tests/es6.reflect.apply.ls | 4 +++- 7 files changed, 36 insertions(+), 10 deletions(-) diff --git a/library/modules/es6.reflect.apply.js b/library/modules/es6.reflect.apply.js index d2c167f6947d..d728e111e008 100644 --- a/library/modules/es6.reflect.apply.js +++ b/library/modules/es6.reflect.apply.js @@ -1,9 +1,11 @@ // 26.1.1 Reflect.apply(target, thisArgument, argumentsList) -var $export = require('./_export') - , _apply = Function.apply; +var $export = require('./_export') + , aFunction = require('./_a-function') + , anObject = require('./_an-object') + , _apply = Function.apply; $export($export.S, 'Reflect', { apply: function apply(target, thisArgument, argumentsList){ - return _apply.call(target, thisArgument, argumentsList); + return _apply.call(aFunction(target), thisArgument, anObject(argumentsList)); } }); \ No newline at end of file diff --git a/modules/es6.reflect.apply.js b/modules/es6.reflect.apply.js index d2c167f6947d..d728e111e008 100644 --- a/modules/es6.reflect.apply.js +++ b/modules/es6.reflect.apply.js @@ -1,9 +1,11 @@ // 26.1.1 Reflect.apply(target, thisArgument, argumentsList) -var $export = require('./_export') - , _apply = Function.apply; +var $export = require('./_export') + , aFunction = require('./_a-function') + , anObject = require('./_an-object') + , _apply = Function.apply; $export($export.S, 'Reflect', { apply: function apply(target, thisArgument, argumentsList){ - return _apply.call(target, thisArgument, argumentsList); + return _apply.call(aFunction(target), thisArgument, anObject(argumentsList)); } }); \ No newline at end of file diff --git a/tests/es.js b/tests/es.js index 2e8fd90e64ad..5d884e8e7000 100644 --- a/tests/es.js +++ b/tests/es.js @@ -4384,7 +4384,13 @@ assert.strictEqual(apply(C, null, ['foo', 'bar', 'baz']), 'foobarbaz', 'works with redefined apply'); assert.throws(function(){ apply(42, null, []); - }, TypeError, 'throws on primitive'); + }, TypeError, 'throws on primitive as first argument'); + assert.throws(function(){ + apply(function(){}, null); + }, TypeError, 'throws without third argument'); + assert.throws(function(){ + apply(function(){}, null, '123'); + }, TypeError, 'throws on primitive as third argument'); }); }).call(this); diff --git a/tests/library.js b/tests/library.js index a557ab88e9cc..afae34cf35ff 100644 --- a/tests/library.js +++ b/tests/library.js @@ -4143,6 +4143,12 @@ assert.throws(function(){ apply(42, null, []); }, TypeError, 'throws on primitive'); + assert.throws(function(){ + apply(function(){}, null); + }, TypeError, 'throws without third argument'); + assert.throws(function(){ + apply(function(){}, null, '123'); + }, TypeError, 'throws on primitive as third argument'); }); }).call(this); diff --git a/tests/library/es6.reflect.apply.ls b/tests/library/es6.reflect.apply.ls index 19bf5ba7499e..36c345847402 100644 --- a/tests/library/es6.reflect.apply.ls +++ b/tests/library/es6.reflect.apply.ls @@ -11,4 +11,6 @@ test 'Reflect.apply' (assert)!-> C = (a, b, c)-> a + b + c C.apply = 42 assert.strictEqual apply(C, null, <[foo bar baz]>), \foobarbaz, 'works with redefined apply' - assert.throws (!-> apply 42, null, []), TypeError, 'throws on primitive' \ No newline at end of file + assert.throws (!-> apply 42, null, []), TypeError, 'throws on primitive' + assert.throws (!-> apply (!->), null), TypeError, 'throws without third argument' + assert.throws (!-> apply (!->), null, \123), TypeError, 'throws on primitive as third argument' \ No newline at end of file diff --git a/tests/tests.js b/tests/tests.js index d5cc5c294c3c..7307dd7cd89e 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -5195,7 +5195,13 @@ assert.strictEqual(apply(C, null, ['foo', 'bar', 'baz']), 'foobarbaz', 'works with redefined apply'); assert.throws(function(){ apply(42, null, []); - }, TypeError, 'throws on primitive'); + }, TypeError, 'throws on primitive as first argument'); + assert.throws(function(){ + apply(function(){}, null); + }, TypeError, 'throws without third argument'); + assert.throws(function(){ + apply(function(){}, null, '123'); + }, TypeError, 'throws on primitive as third argument'); }); }).call(this); diff --git a/tests/tests/es6.reflect.apply.ls b/tests/tests/es6.reflect.apply.ls index 7323eab9d139..4405673e79a5 100644 --- a/tests/tests/es6.reflect.apply.ls +++ b/tests/tests/es6.reflect.apply.ls @@ -12,4 +12,6 @@ test 'Reflect.apply' (assert)!-> C = (a, b, c)-> a + b + c C.apply = 42 assert.strictEqual apply(C, null, <[foo bar baz]>), \foobarbaz, 'works with redefined apply' - assert.throws (!-> apply 42, null, []), TypeError, 'throws on primitive' \ No newline at end of file + assert.throws (!-> apply 42, null, []), TypeError, 'throws on primitive as first argument' + assert.throws (!-> apply (!->), null), TypeError, 'throws without third argument' + assert.throws (!-> apply (!->), null, \123), TypeError, 'throws on primitive as third argument' \ No newline at end of file