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);
/**
* @}
* @}
@@ -0,0 +1,33 @@
// Copyright 2015 Samsung Electronics Co., Ltd.
// Copyright 2015 University of Szeged.
//
// 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.
function Function_A() { }
function Function_B() { }
Function_B.prototype = new Function_A();
function Function_C() { }
Function_C.prototype = new Function_B();
function Function_D() { }
Function_D.prototype = new Function_C();
var d_instance = new Function_D();
assert (Function_A.prototype.isPrototypeOf (d_instance) === true)
assert (Function_A.prototype.isPrototypeOf (Array) === false)
assert (Function_A.prototype.isPrototypeOf.call(0, 0) === false);
assert (Function_A.prototype.isPrototypeOf.call(Function_A, 0) === false);