Implement Object.prototype.isPrototypeOf

JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
This commit is contained in:
Peter Gal
2015-05-13 15:48:19 +02:00
parent eac7372f41
commit 7d703040d0
4 changed files with 96 additions and 1 deletions
@@ -252,7 +252,37 @@ static ecma_completion_value_t
ecma_builtin_object_prototype_object_is_prototype_of (ecma_value_t this_arg, /**< this argument */
ecma_value_t arg) /**< routine's first argument */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
/* 1. Is the argument an object? */
if (!ecma_is_value_object (arg))
{
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE);
}
ecma_completion_value_t return_value = ecma_make_empty_completion_value ();
/* 2. ToObject(this) */
ECMA_TRY_CATCH (obj_value,
ecma_op_to_object (this_arg),
return_value);
ecma_object_t *obj_p = ecma_get_object_from_value (obj_value);
/* 3. Compare prototype to object */
ECMA_TRY_CATCH (v_obj_value,
ecma_op_to_object (arg),
return_value);
ecma_object_t *v_obj_p = ecma_get_object_from_value (v_obj_value);
bool is_prototype_of = ecma_op_object_is_prototype_of (obj_p, v_obj_p);
return_value = ecma_make_simple_completion_value (is_prototype_of
? ECMA_SIMPLE_VALUE_TRUE
: ECMA_SIMPLE_VALUE_FALSE);
ECMA_FINALIZE (v_obj_value);
ECMA_FINALIZE (obj_value);
return return_value;
} /* ecma_builtin_object_prototype_object_is_prototype_of */
/**
@@ -504,6 +504,36 @@ ecma_op_object_has_instance (ecma_object_t *obj_p, /**< the object */
JERRY_UNREACHABLE ();
} /* ecma_op_object_has_instance */
/**
* Object's isPrototypeOf operation
*
* See also:
* ECMA-262 v5, 15.2.4.6; 3
*
* @return true if the target object is prototype of the base object
* false if the target object is not prototype of the base object
*/
bool
ecma_op_object_is_prototype_of (ecma_object_t *base_p, /** < base object */
ecma_object_t *target_p) /** < target object */
{
do
{
target_p = ecma_get_object_prototype (target_p);
if (target_p == NULL)
{
return false;
}
else if (target_p == base_p)
{
return true;
}
} while (true);
} /* ecma_op_object_is_prototype_of */
/**
* @}
* @}
@@ -45,6 +45,8 @@ ecma_op_object_define_own_property (ecma_object_t *obj_p,
bool is_throw);
extern ecma_completion_value_t ecma_op_object_has_instance (ecma_object_t *obj_p,
ecma_value_t value);
extern bool ecma_op_object_is_prototype_of (ecma_object_t *base_p, ecma_object_t *target_p);
/**
* @}
* @}