From dfcc3a098680f4fd4636774c87734bff91725a32 Mon Sep 17 00:00:00 2001 From: "e.gavrin" Date: Thu, 21 Aug 2014 20:00:12 +0400 Subject: [PATCH] Add retval opcode handler. --- src/libcoreint/interpreter.c | 2 +- src/libcoreint/opcodes.c | 26 ++++++++++++++++++++++++- tests/jerry/N.function_return.js | 33 ++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 tests/jerry/N.function_return.js diff --git a/src/libcoreint/interpreter.c b/src/libcoreint/interpreter.c index 6a224dc6d..530030617 100644 --- a/src/libcoreint/interpreter.c +++ b/src/libcoreint/interpreter.c @@ -182,7 +182,7 @@ try_get_string_by_idx (T_IDX idx, /**< literal id */ JERRY_ASSERT (str_p != NULL); FIXME (ecma_char_t strlen); - + ssize_t req_length = (ssize_t)__strlen ((const char*)str_p) + 1; if (buffer_size < req_length) diff --git a/src/libcoreint/opcodes.c b/src/libcoreint/opcodes.c index 82c592e3d..c8d48ead1 100644 --- a/src/libcoreint/opcodes.c +++ b/src/libcoreint/opcodes.c @@ -431,7 +431,6 @@ do_number_bitwise_logic (struct __int_data *int_data, /**< interpreter context * op (native_call) \ op (func_decl_n) \ op (varg_list) \ - op (retval) \ op (construct_decl) \ op (array_decl) \ op (prop) \ @@ -1894,6 +1893,31 @@ opfunc_ret (OPCODE opdata __unused, /**< operation data */ ECMA_TARGET_ID_RESERVED); } /* opfunc_ret */ +/** + * 'Return with expression' opcode handler. + * + * See also: ECMA-262 v5, 12.9 + * + * @return completion value + * Returned value is simple and so need not be freed. + * However, ecma_free_completion_value may be called for it, but it is a no-op. + */ +ecma_completion_value_t +opfunc_retval (OPCODE opdata __unused, /**< operation data */ + struct __int_data *int_data __unused) /**< interpreter context */ +{ + ecma_completion_value_t ret_value; + + ECMA_TRY_CATCH (expr_val, get_variable_value (int_data, opdata.data.retval.ret_value, false), ret_value); + + ret_value = ecma_make_completion_value (ECMA_COMPLETION_TYPE_RETURN, + expr_val.value, + ECMA_TARGET_ID_RESERVED); + + ECMA_FINALIZE (expr_val); + return ret_value; +} /* opfunc_retval */ + /** * 'Property getter' opcode handler. * diff --git a/tests/jerry/N.function_return.js b/tests/jerry/N.function_return.js new file mode 100644 index 000000000..55323597a --- /dev/null +++ b/tests/jerry/N.function_return.js @@ -0,0 +1,33 @@ +// Copyright 2014 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_empty() +{ + return; +} + +function f_42() +{ + return 42; +} + +function f_expr() +{ + var a = 5; + var b = 5; + return a + b; +} + +assert(f_expr() === 10); +assert(f_42() === 42); \ No newline at end of file