Partially implementing ECMA 'Absract relational comparison' (11.8.5).

This commit is contained in:
Ruben Ayrapetyan
2014-07-24 20:31:57 +04:00
parent 61550f2029
commit b2ae827fbd
5 changed files with 129 additions and 75 deletions
+70
View File
@@ -14,7 +14,9 @@
*/
#include "ecma-comparison.h"
#include "ecma-conversion.h"
#include "ecma-globals.h"
#include "ecma-try-catch-macro.h"
#include "globals.h"
/** \addtogroup ecma ---TODO---
@@ -99,6 +101,74 @@ ecma_abstract_equality_compare(ecma_value_t x, /**< first operand */
}
} /* ecma_abstract_equality_compare */
/**
* ECMA abstract relational comparison routine.
*
* See also: ECMA-262 v5, 11.8.5
*
* @return completion value
* Returned value must be freed with ecma_free_completion_value
*/
ecma_completion_value_t
ecma_abstract_relational_compare(ecma_value_t x, /**< first operand */
ecma_value_t y, /**< second operand */
bool left_first) /**< 'LeftFirst' flag */
{
ecma_completion_value_t ret_value, px, py;
ecma_value_t first_converted_value = left_first ? x : y;
ecma_value_t second_converted_value = left_first ? y : x;
// 1., 2.
ECMA_TRY_CATCH( prim_first_converted_value, ecma_op_to_primitive( first_converted_value, ECMA_PREFERRED_TYPE_NUMBER), ret_value);
ECMA_TRY_CATCH( prim_second_converted_value, ecma_op_to_primitive( second_converted_value, ECMA_PREFERRED_TYPE_NUMBER), ret_value);
px = left_first ? prim_first_converted_value : prim_second_converted_value;
py = left_first ? prim_second_converted_value : prim_first_converted_value;
const bool is_px_string = ( px.value.value_type == ECMA_TYPE_STRING );
const bool is_py_string = ( py.value.value_type == ECMA_TYPE_STRING );
if ( !( is_px_string && is_py_string ) )
{ // 3.
// a.
ECMA_TRY_CATCH( nx, ecma_op_to_number( px.value), ret_value);
// b.
ECMA_TRY_CATCH( ny, ecma_op_to_number( py.value), ret_value);
ecma_number_t* num_x_p = (ecma_number_t*)ecma_get_pointer( nx.value.value);
ecma_number_t* num_y_p = (ecma_number_t*)ecma_get_pointer( ny.value.value);
TODO( /* Implement according to ECMA */ );
if ( *num_x_p >= *num_y_p )
{
ret_value = ecma_make_completion_value (ECMA_COMPLETION_TYPE_NORMAL,
ecma_make_simple_value( ECMA_SIMPLE_VALUE_FALSE),
ECMA_TARGET_ID_RESERVED);
}
else
{
ret_value = ecma_make_completion_value (ECMA_COMPLETION_TYPE_NORMAL,
ecma_make_simple_value( ECMA_SIMPLE_VALUE_TRUE),
ECMA_TARGET_ID_RESERVED);
}
ECMA_FINALIZE( ny);
ECMA_FINALIZE( nx);
}
else
{ // 4.
JERRY_UNIMPLEMENTED();
}
ECMA_FINALIZE( prim_second_converted_value);
ECMA_FINALIZE( prim_first_converted_value);
return ret_value;
} /* ecma_abstract_relational_compare */
/**
* @}
* @}
+1
View File
@@ -27,6 +27,7 @@
*/
extern bool ecma_abstract_equality_compare( ecma_value_t x, ecma_value_t y);
extern ecma_completion_value_t ecma_abstract_relational_compare(ecma_value_t x, ecma_value_t y, bool left_first);
/**
* @}
+4 -3
View File
@@ -91,7 +91,8 @@ ecma_op_check_object_coercible( ecma_value_t value) /**< ecma-value */
* Returned value must be freed with ecma_free_completion_value
*/
ecma_completion_value_t
ecma_op_to_primitive( ecma_value_t value) /**< ecma-value */
ecma_op_to_primitive( ecma_value_t value, /**< ecma-value */
ecma_preferred_type_hint preferred_type) /**< preferred type hint */
{
switch ( (ecma_type_t)value.value_type )
{
@@ -105,7 +106,7 @@ ecma_op_to_primitive( ecma_value_t value) /**< ecma-value */
}
case ECMA_TYPE_OBJECT:
{
JERRY_UNIMPLEMENTED();
JERRY_UNIMPLEMENTED_REF_UNUSED_VARS(preferred_type);
}
case ECMA_TYPE__COUNT:
{
@@ -215,7 +216,7 @@ ecma_op_to_number( ecma_value_t value) /**< ecma-value */
}
case ECMA_TYPE_OBJECT:
{
ecma_completion_value_t completion_to_primitive = ecma_op_to_primitive( value);
ecma_completion_value_t completion_to_primitive = ecma_op_to_primitive( value, ECMA_PREFERRED_TYPE_NUMBER);
JERRY_ASSERT( ecma_is_completion_value_normal( completion_to_primitive) );
ecma_completion_value_t completion_to_number = ecma_op_to_number( completion_to_primitive.value);
+15 -1
View File
@@ -26,8 +26,22 @@
* @{
*/
/**
* Second argument of 'ToPrimitive' operation that is a hint,
* specifying the preferred type of conversion result.
*/
typedef enum
{
ECMA_PREFERRED_TYPE_NO, /**< no preferred type is specified */
ECMA_PREFERRED_TYPE_UNDEFINED, /**< Undefined */
ECMA_PREFERRED_TYPE_NULL, /**< Null */
ECMA_PREFERRED_TYPE_BOOLEAN, /**< Boolean */
ECMA_PREFERRED_TYPE_NUMBER, /**< Number */
ECMA_PREFERRED_TYPE_STRING /**< String */
} ecma_preferred_type_hint;
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_primitive( ecma_value_t value, ecma_preferred_type_hint preferred_type);
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);