diff --git a/src/libcoreint/opcodes.c b/src/libcoreint/opcodes.c index 0b09a4d43..203044bf7 100644 --- a/src/libcoreint/opcodes.c +++ b/src/libcoreint/opcodes.c @@ -316,8 +316,6 @@ do_number_arithmetic(struct __int_data *int_data, /**< interpreter context */ } /* do_number_arithmetic */ #define OP_UNIMPLEMENTED_LIST(op) \ - op(is_true_jmp) \ - op(is_false_jmp) \ op(call_0) \ op(call_n) \ op(func_decl_1) \ @@ -399,7 +397,77 @@ opfunc_call_1 (OPCODE opdata __unused, struct __int_data *int_data) } /** - * Jump opcode handler. + * 'Jump if true' opcode handler. + * + * Note: + * the opcode changes current opcode position to specified opcode index + * if argument evaluates to true. + */ +ecma_completion_value_t +opfunc_is_true_jmp (OPCODE opdata, /**< operation data */ + struct __int_data *int_data) /**< interpreter context */ +{ + const T_IDX cond_var_idx = opdata.data.is_true_jmp.value; + const T_IDX dst_opcode_idx = opdata.data.is_true_jmp.opcode; + + int_data->pos = dst_opcode_idx; + + ecma_completion_value_t ret_value; + + TRY_CATCH(cond_value, get_variable_value( int_data, cond_var_idx, false), ret_value); + + ecma_completion_value_t to_bool_completion = ecma_op_to_boolean( cond_value.value); + JERRY_ASSERT( ecma_is_completion_value_normal( to_bool_completion) ); + + if ( ecma_is_value_true( to_bool_completion.value) ) + { + int_data->pos = opdata.data.jmp.opcode_idx; + } + + ret_value = ecma_make_empty_completion_value(); + + FINALIZE(cond_value); + + return ret_value; +} /* opfunc_is_true_jmp */ + +/** + * 'Jump if false' opcode handler. + * + * Note: + * the opcode changes current opcode position to specified opcode index + * if argument evaluates to false. + */ +ecma_completion_value_t +opfunc_is_false_jmp (OPCODE opdata, /**< operation data */ + struct __int_data *int_data) /**< interpreter context */ +{ + const T_IDX cond_var_idx = opdata.data.is_false_jmp.value; + const T_IDX dst_opcode_idx = opdata.data.is_false_jmp.opcode; + + int_data->pos = dst_opcode_idx; + + ecma_completion_value_t ret_value; + + TRY_CATCH(cond_value, get_variable_value( int_data, cond_var_idx, false), ret_value); + + ecma_completion_value_t to_bool_completion = ecma_op_to_boolean( cond_value.value); + JERRY_ASSERT( ecma_is_completion_value_normal( to_bool_completion) ); + + if ( !ecma_is_value_true( to_bool_completion.value) ) + { + int_data->pos = opdata.data.jmp.opcode_idx; + } + + ret_value = ecma_make_empty_completion_value(); + + FINALIZE(cond_value); + + return ret_value; +} /* opfunc_is_false_jmp */ + +/** + * 'Jump' opcode handler. * * Note: * the opcode changes current opcode position to specified opcode index @@ -414,7 +482,7 @@ opfunc_jmp (OPCODE opdata, /**< operation data */ } /* opfunc_jmp */ /** - * Jump down opcode handler. + * 'Jump down' opcode handler. * * Note: * the opcode changes adds specified value to current opcode position @@ -429,7 +497,7 @@ opfunc_jmp_down (OPCODE opdata, /**< operation data */ } /* opfunc_jmp_down */ /** - * Jump up opcode handler. + * 'Jump up' opcode handler. * * Note: * the opcode changes substracts specified value from current opcode position @@ -446,7 +514,7 @@ opfunc_jmp_up (OPCODE opdata, /**< operation data */ } /* opfunc_jmp_up */ /** - * Assignment opcode handler. + * 'Assignment' opcode handler. * * Note: * This handler implements case of assignment of a literal's or a variable's @@ -545,7 +613,7 @@ opfunc_assignment (OPCODE opdata, /**< operation data */ } /* opfunc_assignment */ /** - * Addition opcode handler. + * 'Addition' opcode handler. * * See also: ECMA-262 v5, 11.6.1 * @@ -592,7 +660,7 @@ opfunc_addition(OPCODE opdata, /**< operation data */ } /* opfunc_addition */ /** - * Substraction opcode handler. + * 'Substraction' opcode handler. * * See also: ECMA-262 v5, 11.6.2 * @@ -627,7 +695,7 @@ opfunc_substraction(OPCODE opdata, /**< operation data */ } /* opfunc_substraction */ /** - * Multiplication opcode handler. + * 'Multiplication' opcode handler. * * See also: ECMA-262 v5, 11.5, 11.5.1 * @@ -662,7 +730,7 @@ opfunc_multiplication(OPCODE opdata, /**< operation data */ } /* opfunc_multiplication */ /** - * Division opcode handler. + * 'Division' opcode handler. * * See also: ECMA-262 v5, 11.5, 11.5.2 * @@ -697,7 +765,7 @@ opfunc_division(OPCODE opdata, /**< operation data */ } /* opfunc_division */ /** - * Remainder calculation opcode handler. + * 'Remainder calculation' opcode handler. * * See also: ECMA-262 v5, 11.5, 11.5.3 * @@ -732,7 +800,7 @@ opfunc_remainder(OPCODE opdata, /**< operation data */ } /* opfunc_remainder */ /** - * Variable declaration opcode handler. + * 'Variable declaration' opcode handler. * * See also: ECMA-262 v5, 10.5 - Declaration binding instantiation (block 8). * diff --git a/src/libecmaoperations/ecma-conversion.c b/src/libecmaoperations/ecma-conversion.c index af875718e..df77f9b5a 100644 --- a/src/libecmaoperations/ecma-conversion.c +++ b/src/libecmaoperations/ecma-conversion.c @@ -116,6 +116,78 @@ ecma_op_to_primitive( ecma_value_t value) /**< ecma-value */ JERRY_UNREACHABLE(); } /* ecma_op_to_primitive */ +/** + * ToBoolean operation. + * + * See also: + * ECMA-262 v5, 9.2 + * + * @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 +ecma_op_to_boolean( ecma_value_t value) /**< ecma-value */ +{ + ecma_simple_value_t res = ECMA_SIMPLE_VALUE_EMPTY; + + switch ( (ecma_type_t)value.value_type ) + { + case ECMA_TYPE_NUMBER: + { + ecma_number_t *num_p = ecma_get_pointer( value.value); + + TODO( Implement according to ECMA ); + res = ( *num_p == 0 ) ? ECMA_SIMPLE_VALUE_FALSE: + ECMA_SIMPLE_VALUE_TRUE; + + break; + } + case ECMA_TYPE_SIMPLE: + { + if ( ecma_is_value_boolean (value ) ) + { + res = value.value; + } else if ( ecma_is_value_undefined (value) + || ecma_is_value_null( value) ) + { + res = ECMA_SIMPLE_VALUE_FALSE; + } else + { + JERRY_UNREACHABLE(); + } + + break; + } + case ECMA_TYPE_STRING: + { + ecma_array_first_chunk_t *str_p = ecma_get_pointer( value.value); + + res = ( str_p->header.unit_number == 0 ) ? ECMA_SIMPLE_VALUE_FALSE: + ECMA_SIMPLE_VALUE_TRUE; + + break; + } + case ECMA_TYPE_OBJECT: + { + res = ECMA_SIMPLE_VALUE_TRUE; + + break; + } + case ECMA_TYPE__COUNT: + { + JERRY_UNREACHABLE(); + } + } + + JERRY_ASSERT( res == ECMA_SIMPLE_VALUE_FALSE + || res == ECMA_SIMPLE_VALUE_TRUE ); + + return ecma_make_completion_value (ECMA_COMPLETION_TYPE_NORMAL, + ecma_make_simple_value( res), + ECMA_TARGET_ID_RESERVED); +} /* ecma_op_to_boolean */ + /** * ToNumber operation. * diff --git a/src/libecmaoperations/ecma-conversion.h b/src/libecmaoperations/ecma-conversion.h index d2d87ec62..eaa02eb39 100644 --- a/src/libecmaoperations/ecma-conversion.h +++ b/src/libecmaoperations/ecma-conversion.h @@ -28,6 +28,7 @@ extern ecma_completion_value_t ecma_op_check_object_coercible( ecma_value_t value); extern ecma_completion_value_t ecma_op_to_primitive( ecma_value_t value); +extern ecma_completion_value_t ecma_op_to_boolean( ecma_value_t value); extern ecma_completion_value_t ecma_op_to_number( ecma_value_t value); extern ecma_completion_value_t ecma_op_to_object( ecma_value_t value);