diff --git a/src/libcoreint/opcodes.c b/src/libcoreint/opcodes.c index a719a1f46..ece756af2 100644 --- a/src/libcoreint/opcodes.c +++ b/src/libcoreint/opcodes.c @@ -354,8 +354,6 @@ do_number_arithmetic (struct __int_data *int_data, /**< interpreter context */ op (b_xor) \ op (logical_and) \ op (logical_or) \ - op (equal_value_type) \ - op (not_equal_value_type) \ op (construct_0) \ op (construct_1) \ op (construct_n) \ @@ -1173,6 +1171,75 @@ opfunc_not_equal_value (OPCODE opdata, /**< operation data */ return ret_value; } /* opfunc_not_equal_value */ +/** + * 'Strict Equals' opcode handler. + * + * See also: ECMA-262 v5, 11.9.4 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value + */ +ecma_completion_value_t +opfunc_equal_value_type (OPCODE opdata, /**< operation data */ + struct __int_data *int_data) /**< interpreter context */ +{ + const T_IDX dst_var_idx = opdata.data.equal_value.dst; + const T_IDX left_var_idx = opdata.data.equal_value.var_left; + const T_IDX right_var_idx = opdata.data.equal_value.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); + + bool is_equal = ecma_op_strict_equality_compare (left_value.value, right_value.value); + + ret_value = set_variable_value (int_data, dst_var_idx, ecma_make_simple_value (is_equal ? ECMA_SIMPLE_VALUE_TRUE + : ECMA_SIMPLE_VALUE_FALSE)); + + ECMA_FINALIZE (right_value); + ECMA_FINALIZE (left_value); + + return ret_value; +} /* opfunc_equal_value_type */ + +/** + * 'Strict Does-not-equals' opcode handler. + * + * See also: ECMA-262 v5, 11.9.5 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value + */ +ecma_completion_value_t +opfunc_not_equal_value_type (OPCODE opdata, /**< operation data */ + struct __int_data *int_data) /**< interpreter context */ +{ + const T_IDX dst_var_idx = opdata.data.not_equal_value.dst; + const T_IDX left_var_idx = opdata.data.not_equal_value.var_left; + const T_IDX right_var_idx = opdata.data.not_equal_value.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); + + bool is_equal = ecma_op_strict_equality_compare (left_value.value, right_value.value); + + ret_value = set_variable_value (int_data, dst_var_idx, ecma_make_simple_value (is_equal ? ECMA_SIMPLE_VALUE_FALSE + : ECMA_SIMPLE_VALUE_TRUE)); + + + ECMA_FINALIZE (right_value); + ECMA_FINALIZE (left_value); + + return ret_value; +} /* opfunc_not_equal_value_type */ + /** * 'Less-than' opcode handler. * diff --git a/src/libecmaoperations/ecma-comparison.c b/src/libecmaoperations/ecma-comparison.c index 642f3c313..8869030bc 100644 --- a/src/libecmaoperations/ecma-comparison.c +++ b/src/libecmaoperations/ecma-comparison.c @@ -107,6 +107,82 @@ ecma_op_abstract_equality_compare (ecma_value_t x, /**< first operand */ } } /* ecma_op_abstract_equality_compare */ +/** + * ECMA strict equality comparison routine. + * + * See also: ECMA-262 v5, 11.9.6 + * + * @return true - if values are strict equal, + * false - otherwise. + */ +bool +ecma_op_strict_equality_compare (ecma_value_t x, /**< first operand */ + ecma_value_t y) /**< second operand */ +{ + // 1. If Type (x) is different from Type (y), return false. + if (x.value_type != y.value_type) + { + return false; + } + + // 2. If Type (x) is Undefined, return true. + if (ecma_is_value_undefined (x)) + { + return true; + } + + // 3. If Type (x) is Null, return true. + if (ecma_is_value_null (x)) + { + return true; + } + + // 4. If Type (x) is Number, then + if (x.value_type == ECMA_TYPE_NUMBER) + { + //a. If x is NaN, return false. + //b. If y is NaN, return false. + //c. If x is the same Number value as y, return true. + //d. If x is +0 and y is 0, return true. + //e. If x is 0 and y is +0, return true. + + ecma_number_t x_num = *(ecma_number_t*) (ECMA_GET_POINTER (x.value)); + ecma_number_t y_num = *(ecma_number_t*) (ECMA_GET_POINTER (y.value)); + + TODO (Implement according to ECMA); + + return (x_num == y_num); + + //f. Return false. + return false; + } + + // 5. If Type (x) is String, then return true if x and y are exactly the same sequence of characters + // (same length and same characters in corresponding positions); otherwise, return false. + if (x.value_type == ECMA_TYPE_STRING) + { + ecma_string_t* x_str_p = ECMA_GET_POINTER (x.value); + ecma_string_t* y_str_p = ECMA_GET_POINTER (y.value); + + return ecma_compare_ecma_string_to_ecma_string (x_str_p, y_str_p); + } + + // 6. If Type (x) is Boolean, return true if x and y are both true or both false; otherwise, return false. + if (ecma_is_value_boolean (x)) + { + return (x.value == y.value); + } + + // 7. Return true if x and y refer to the same object. + if (x.value_type == ECMA_TYPE_OBJECT) + { + return (ECMA_GET_POINTER (x.value) == ECMA_GET_POINTER (y.value)); + } + + // Otherwise, return false. + return false; +} /* ecma_op_strict_equality_compare */ + /** * ECMA abstract relational comparison routine. * diff --git a/src/libecmaoperations/ecma-comparison.h b/src/libecmaoperations/ecma-comparison.h index 52375892d..79969c062 100644 --- a/src/libecmaoperations/ecma-comparison.h +++ b/src/libecmaoperations/ecma-comparison.h @@ -27,6 +27,7 @@ */ extern bool ecma_op_abstract_equality_compare (ecma_value_t x, ecma_value_t y); +extern bool ecma_op_strict_equality_compare (ecma_value_t x, ecma_value_t y); extern ecma_completion_value_t ecma_op_abstract_relational_compare (ecma_value_t x, ecma_value_t y, bool left_first); /** diff --git a/tests/jerry/equality.js b/tests/jerry/equality.js new file mode 100644 index 000000000..d8e9e64fa --- /dev/null +++ b/tests/jerry/equality.js @@ -0,0 +1,32 @@ +// 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((5 == 5) == true); +assert((7 != 2) == true); + +var num = 0; +//var obj = new String("0"); +var str = "0"; +var b = false; + +assert(num === num); +//assert(obj === obj); +assert(str === str); + +//assert((num === obj) == false); +assert((num === str) == false); +//assert((obj === str) == false); +//assert((null === undefined) == false); +//assert((obj === null) == false); +//assert((obj === undefined) == false); diff --git a/tests/jerry/relational.js b/tests/jerry/relational.js index 320cb0e2f..6068daf52 100644 --- a/tests/jerry/relational.js +++ b/tests/jerry/relational.js @@ -12,9 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -assert((5 == 5) == true); -assert((7 != 2) == true); - assert((7 < 4) == false); assert((7 > 4) == true);