Skip to content

Commit cefeea0

Browse files
Zsolt Borbélygalpeter
authored andcommitted
Implement Object.getPrototypeOf()
JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély [email protected]
1 parent 385b988 commit cefeea0

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,28 @@ ecma_builtin_object_dispatch_construct (const ecma_value_t *arguments_list_p, /*
114114
* Returned value must be freed with ecma_free_completion_value.
115115
*/
116116
static ecma_completion_value_t
117-
ecma_builtin_object_object_get_prototype_of (ecma_value_t this_arg, /**< 'this' argument */
117+
ecma_builtin_object_object_get_prototype_of (ecma_value_t this_arg __attr_unused___, /**< 'this' argument */
118118
ecma_value_t arg) /**< routine's argument */
119119
{
120-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
120+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
121+
122+
/* 1. */
123+
if (!ecma_is_value_object (arg))
124+
{
125+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
126+
}
127+
else
128+
{
129+
/* 2. */
130+
ecma_object_t *obj_p = ecma_get_object_from_value (arg);
131+
132+
ecma_object_t *prototype_p = ecma_get_object_prototype (obj_p);
133+
ecma_ref_object (prototype_p);
134+
135+
ret_value = ecma_make_normal_completion_value (ecma_make_object_value (prototype_p));
136+
}
137+
138+
return ret_value;
121139
} /* ecma_builtin_object_object_get_prototype_of */
122140

123141
/**

tests/jerry/object_getprototypeof.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
try {
17+
var v;
18+
Object.getPrototypeOf(v);
19+
assert (false);
20+
} catch (e) {
21+
assert (e instanceof TypeError);
22+
}
23+
24+
try {
25+
Object.getPrototypeOf("foo");
26+
assert (false);
27+
} catch (e) {
28+
assert (e instanceof TypeError);
29+
}
30+
31+
try {
32+
Object.getPrototypeOf(60);
33+
assert (false);
34+
} catch (e) {
35+
assert (e instanceof TypeError);
36+
}
37+
38+
var obj = { x : "foo" };
39+
assert (Object.getPrototypeOf(obj) === Object.prototype)
40+
41+
var constructor = function () {};
42+
constructor.prototype = obj;
43+
44+
var d_obj = new constructor();
45+
assert (Object.getPrototypeOf(d_obj) === obj);

0 commit comments

Comments
 (0)