Skip to content

Commit 7d70304

Browse files
committed
Implement Object.prototype.isPrototypeOf
JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent eac7372 commit 7d70304

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-object-prototype.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,37 @@ static ecma_completion_value_t
252252
ecma_builtin_object_prototype_object_is_prototype_of (ecma_value_t this_arg, /**< this argument */
253253
ecma_value_t arg) /**< routine's first argument */
254254
{
255-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
255+
/* 1. Is the argument an object? */
256+
if (!ecma_is_value_object (arg))
257+
{
258+
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE);
259+
}
260+
261+
ecma_completion_value_t return_value = ecma_make_empty_completion_value ();
262+
263+
/* 2. ToObject(this) */
264+
ECMA_TRY_CATCH (obj_value,
265+
ecma_op_to_object (this_arg),
266+
return_value);
267+
268+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_value);
269+
270+
/* 3. Compare prototype to object */
271+
ECMA_TRY_CATCH (v_obj_value,
272+
ecma_op_to_object (arg),
273+
return_value);
274+
275+
ecma_object_t *v_obj_p = ecma_get_object_from_value (v_obj_value);
276+
277+
bool is_prototype_of = ecma_op_object_is_prototype_of (obj_p, v_obj_p);
278+
return_value = ecma_make_simple_completion_value (is_prototype_of
279+
? ECMA_SIMPLE_VALUE_TRUE
280+
: ECMA_SIMPLE_VALUE_FALSE);
281+
ECMA_FINALIZE (v_obj_value);
282+
283+
ECMA_FINALIZE (obj_value);
284+
285+
return return_value;
256286
} /* ecma_builtin_object_prototype_object_is_prototype_of */
257287

258288
/**

jerry-core/ecma/operations/ecma-objects.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,36 @@ ecma_op_object_has_instance (ecma_object_t *obj_p, /**< the object */
504504
JERRY_UNREACHABLE ();
505505
} /* ecma_op_object_has_instance */
506506

507+
508+
509+
/**
510+
* Object's isPrototypeOf operation
511+
*
512+
* See also:
513+
* ECMA-262 v5, 15.2.4.6; 3
514+
*
515+
* @return true if the target object is prototype of the base object
516+
* false if the target object is not prototype of the base object
517+
*/
518+
bool
519+
ecma_op_object_is_prototype_of (ecma_object_t *base_p, /** < base object */
520+
ecma_object_t *target_p) /** < target object */
521+
{
522+
do
523+
{
524+
target_p = ecma_get_object_prototype (target_p);
525+
if (target_p == NULL)
526+
{
527+
return false;
528+
}
529+
else if (target_p == base_p)
530+
{
531+
return true;
532+
}
533+
} while (true);
534+
} /* ecma_op_object_is_prototype_of */
535+
536+
507537
/**
508538
* @}
509539
* @}

jerry-core/ecma/operations/ecma-objects.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ ecma_op_object_define_own_property (ecma_object_t *obj_p,
4545
bool is_throw);
4646
extern ecma_completion_value_t ecma_op_object_has_instance (ecma_object_t *obj_p,
4747
ecma_value_t value);
48+
extern bool ecma_op_object_is_prototype_of (ecma_object_t *base_p, ecma_object_t *target_p);
49+
4850
/**
4951
* @}
5052
* @}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
function Function_A() { }
17+
function Function_B() { }
18+
Function_B.prototype = new Function_A();
19+
20+
function Function_C() { }
21+
Function_C.prototype = new Function_B();
22+
23+
function Function_D() { }
24+
Function_D.prototype = new Function_C();
25+
26+
var d_instance = new Function_D();
27+
28+
assert (Function_A.prototype.isPrototypeOf (d_instance) === true)
29+
assert (Function_A.prototype.isPrototypeOf (Array) === false)
30+
31+
32+
assert (Function_A.prototype.isPrototypeOf.call(0, 0) === false);
33+
assert (Function_A.prototype.isPrototypeOf.call(Function_A, 0) === false);

0 commit comments

Comments
 (0)