From 810e3c7ae875dfe2434e91c4012161738ef5bc79 Mon Sep 17 00:00:00 2001 From: Ruben Ayrapetyan Date: Fri, 10 Apr 2015 10:24:27 +0300 Subject: [PATCH] Implementation of Function.prototype.call. --- .../ecma-builtin-function-prototype.cpp | 27 ++++++++++++++++-- tests/jerry/function.prototype.js | 28 +++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 tests/jerry/function.prototype.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-function-prototype.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-function-prototype.cpp index 24136a67d..b0b715f8c 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-function-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-function-prototype.cpp @@ -20,8 +20,7 @@ #include "ecma-gc.h" #include "ecma-globals.h" #include "ecma-helpers.h" -#include "ecma-objects.h" -#include "ecma-string-object.h" +#include "ecma-function-object.h" #include "ecma-try-catch-macro.h" #include "jrt.h" @@ -88,7 +87,29 @@ ecma_builtin_function_prototype_object_call (const ecma_value_t& this_arg, /**< const ecma_value_t* arguments_list_p, /**< list of arguments */ ecma_length_t arguments_number) /**< number of arguments */ { - ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arguments_list_p, arguments_number); + if (!ecma_op_is_callable (this_arg)) + { + return ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE)); + } + else + { + ecma_object_t *func_obj_p = ecma_get_object_from_value (this_arg); + + if (arguments_number == 0) + { + return ecma_op_function_call (func_obj_p, + ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED), + NULL, + 0); + } + else + { + return ecma_op_function_call (func_obj_p, + arguments_list_p [0], + (arguments_number == 1u) ? NULL : (arguments_list_p + 1), + (ecma_length_t) (arguments_number - 1u)); + } + } } /* ecma_builtin_function_prototype_object_call */ /** diff --git a/tests/jerry/function.prototype.js b/tests/jerry/function.prototype.js new file mode 100644 index 000000000..4142f5731 --- /dev/null +++ b/tests/jerry/function.prototype.js @@ -0,0 +1,28 @@ +// Copyright 2015 Samsung Electronics Co., Ltd. +// +// 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. + +function f (arg1, arg2, arg3) +{ + this.string = arg1; + this.number = arg2; + this.boolean = arg3; +} + +var this_arg = {}; + +f.call (this_arg, 's', 1, true, null); + +assert (this_arg.string === 's'); +assert (this_arg.number === 1); +assert (this_arg.boolean === true);