From 4b5cf8ad26715df0afb0dc5b01f9ed510a1854cf Mon Sep 17 00:00:00 2001 From: Zoltan Herczeg Date: Mon, 16 Jan 2017 13:43:54 +0100 Subject: [PATCH] HasInstance should accept the prototype of all functions. Fixes #1519. (#1520) The regression-test-issue-736.js is also changed, since it expects throwing an error, which does not happen anymore because the "x" variable is undefined. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com --- .../ecma/operations/ecma-function-object.c | 15 ++----- tests/jerry/function-external.js | 43 +++++++++++++++++++ tests/jerry/regression-test-issue-736.js | 2 +- 3 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 tests/jerry/function-external.js diff --git a/jerry-core/ecma/operations/ecma-function-object.c b/jerry-core/ecma/operations/ecma-function-object.c index 5df630845..63bc1218c 100644 --- a/jerry-core/ecma/operations/ecma-function-object.c +++ b/jerry-core/ecma/operations/ecma-function-object.c @@ -366,14 +366,9 @@ ecma_op_function_has_instance (ecma_object_t *func_obj_p, /**< Function object * ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); - if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_FUNCTION) + if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_FUNCTION + || ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION) { - if (ecma_get_object_is_builtin (func_obj_p) - && ecma_builtin_function_is_routine (func_obj_p)) - { - return ecma_raise_type_error (ECMA_ERR_MSG ("Function parameter cannot be built-in a function.")); - } - if (!ecma_is_value_object (value)) { return ecma_make_simple_value (ECMA_SIMPLE_VALUE_FALSE); @@ -389,7 +384,7 @@ ecma_op_function_has_instance (ecma_object_t *func_obj_p, /**< Function object * if (!ecma_is_value_object (prototype_obj_value)) { - ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Expected an object.")); + ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Object expected.")); } else { @@ -419,10 +414,6 @@ ecma_op_function_has_instance (ecma_object_t *func_obj_p, /**< Function object * ecma_deref_ecma_string (prototype_magic_string_p); } - else if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION) - { - ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Expected a function.")); - } else { JERRY_ASSERT (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_BOUND_FUNCTION); diff --git a/tests/jerry/function-external.js b/tests/jerry/function-external.js new file mode 100644 index 000000000..d6eae3922 --- /dev/null +++ b/tests/jerry/function-external.js @@ -0,0 +1,43 @@ +// Copyright JS Foundation and other contributors, http://js.foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +try { + ({} instanceof assert); + assert(false); +} catch(e) { + assert(e instanceof TypeError); +} + +assert.prototype = {} + +try { + assert(!({} instanceof assert)); +} catch(e) { + assert(false); +} + +try { + ({} instanceof Math.sin); + assert(false); +} catch(e) { + assert(e instanceof TypeError); +} + +Math.sin.prototype = {} + +try { + assert(!({} instanceof Math.sin)); +} catch(e) { + assert(false); +} diff --git a/tests/jerry/regression-test-issue-736.js b/tests/jerry/regression-test-issue-736.js index 8a1403e39..9d31f7365 100644 --- a/tests/jerry/regression-test-issue-736.js +++ b/tests/jerry/regression-test-issue-736.js @@ -31,7 +31,7 @@ try { } try { - eval("var x; x instanceof assert;"); + eval("var x = {}; x instanceof assert;"); assert(false); } catch(e) { assert(e instanceof TypeError);