diff --git a/src/libcoreint/opcodes.c b/src/libcoreint/opcodes.c index 132a77472..81c8dedb1 100644 --- a/src/libcoreint/opcodes.c +++ b/src/libcoreint/opcodes.c @@ -467,9 +467,6 @@ do_number_bitwise_logic (struct __int_data *int_data, /**< interpreter context * op (typeof) \ op (with) \ op (end_with) \ - op (logical_not) \ - op (logical_and) \ - op (logical_or) \ op (meta) \ static char __unused unimplemented_list_end @@ -2282,6 +2279,131 @@ opfunc_in (OPCODE opdata __unused, /**< operation data */ return ret_value; } /* opfunc_in */ +/** + * 'Logical NOT Operator' opcode handler. + * + * See also: ECMA-262 v5, 11.4.9 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value + */ +ecma_completion_value_t +opfunc_logical_not (OPCODE opdata, /**< operation data */ + struct __int_data *int_data) /**< interpreter context */ +{ + const T_IDX dst_var_idx = opdata.data.logical_not.dst; + const T_IDX right_var_idx = opdata.data.logical_not.var_right; + + int_data->pos++; + + ecma_completion_value_t ret_value; + + ECMA_TRY_CATCH (right_value, get_variable_value (int_data, right_var_idx, false), ret_value); + + ecma_simple_value_t old_value = ECMA_SIMPLE_VALUE_TRUE; + ecma_completion_value_t to_bool_value = ecma_op_to_boolean (right_value.value); + + if (ecma_is_value_true (to_bool_value.value)) + { + old_value = ECMA_SIMPLE_VALUE_FALSE; + } + + ret_value = set_variable_value (int_data, + dst_var_idx, + ecma_make_simple_value (old_value)); + + ECMA_FINALIZE (right_value); + + return ret_value; +} /* opfunc_logical_not */ + +/** + * 'Logical OR Operator' opcode handler. + * + * See also: ECMA-262 v5, 11.11 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value + */ +ecma_completion_value_t +opfunc_logical_or (OPCODE opdata, /**< operation data */ + struct __int_data *int_data) /**< interpreter context */ +{ + const T_IDX dst_var_idx = opdata.data.logical_or.dst; + const T_IDX left_var_idx = opdata.data.logical_or.var_left; + const T_IDX right_var_idx = opdata.data.logical_or.var_right; + + int_data->pos++; + + ecma_completion_value_t ret_value; + + ECMA_TRY_CATCH (left_value, get_variable_value (int_data, left_var_idx, false), ret_value); + ECMA_TRY_CATCH (right_value, get_variable_value (int_data, right_var_idx, false), ret_value); + + ecma_completion_value_t to_bool_value = ecma_op_to_boolean (left_value.value); + + if (ecma_is_value_true (to_bool_value.value)) + { + ret_value = set_variable_value (int_data, + dst_var_idx, + left_value.value); + } + else + { + ret_value = set_variable_value (int_data, + dst_var_idx, + right_value.value); + } + + ECMA_FINALIZE (right_value); + ECMA_FINALIZE (left_value); + + return ret_value; +} /* opfunc_logical_or */ + +/** + * 'Logical AND Operator' opcode handler. + * + * See also: ECMA-262 v5, 11.11 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value + */ +ecma_completion_value_t +opfunc_logical_and (OPCODE opdata, /**< operation data */ + struct __int_data *int_data) /**< interpreter context */ +{ + const T_IDX dst_var_idx = opdata.data.logical_and.dst; + const T_IDX left_var_idx = opdata.data.logical_and.var_left; + const T_IDX right_var_idx = opdata.data.logical_and.var_right; + + int_data->pos++; + + ecma_completion_value_t ret_value; + + ECMA_TRY_CATCH (left_value, get_variable_value (int_data, left_var_idx, false), ret_value); + ECMA_TRY_CATCH (right_value, get_variable_value (int_data, right_var_idx, false), ret_value); + + ecma_completion_value_t to_bool_value = ecma_op_to_boolean (left_value.value); + + if (ecma_is_value_true (to_bool_value.value) == false) + { + ret_value = set_variable_value (int_data, + dst_var_idx, + left_value.value); + } + else + { + ret_value = set_variable_value (int_data, + dst_var_idx, + right_value.value); + } + + ECMA_FINALIZE (right_value); + ECMA_FINALIZE (left_value); + + return ret_value; +} /* opfunc_logical_and */ /** Opcode generators. */ GETOP_IMPL_2 (is_true_jmp, value, opcode) diff --git a/tests/jerry/logical.js b/tests/jerry/logical.js new file mode 100644 index 000000000..58f395862 --- /dev/null +++ b/tests/jerry/logical.js @@ -0,0 +1,25 @@ +// 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. + +assert((!false) === true); + +assert((true || true) === true); +assert((false || true) === true); +assert((false || false) === false); +assert((true || false) === true); + +assert((true && true) === true); +assert((true && false) === false); +assert((false && true) === false); +assert((false && false) === false); \ No newline at end of file