Skip to content

Commit 8392eef

Browse files
Peter MarkiLaszloLango
authored andcommitted
Implement the ES2015 version of Object.getPrototypeOf and add a test file for it (#2256)
JerryScript-DCO-1.0-Signed-off-by: Peter Marki [email protected]
1 parent 7b226f5 commit 8392eef

File tree

4 files changed

+64
-15
lines changed

4 files changed

+64
-15
lines changed

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515

1616
#include "ecma-alloc.h"
17-
#include "ecma-array-object.h"
1817
#include "ecma-builtin-helpers.h"
1918
#include "ecma-builtins.h"
2019
#include "ecma-conversion.h"
@@ -109,27 +108,38 @@ ecma_builtin_object_object_get_prototype_of (ecma_value_t this_arg, /**< 'this'
109108
{
110109
JERRY_UNUSED (this_arg);
111110
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
111+
bool was_object = ecma_is_value_object (arg);
112112

113113
/* 1. */
114-
if (!ecma_is_value_object (arg))
114+
if (!was_object)
115115
{
116-
ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Argument is not an object."));
116+
#ifndef CONFIG_DISABLE_ES2015_BUILTIN
117+
arg = ecma_op_to_object (arg);
118+
if (ECMA_IS_VALUE_ERROR (arg))
119+
{
120+
return arg;
121+
}
122+
#else /* CONFIG_DISABLE_ES2015_BUILTIN */
123+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument is not an object."));
124+
#endif /* !CONFIG_DISABLE_ES2015_BUILTIN */
125+
}
126+
/* 2. */
127+
ecma_object_t *obj_p = ecma_get_object_from_value (arg);
128+
ecma_object_t *prototype_p = ecma_get_object_prototype (obj_p);
129+
130+
if (prototype_p)
131+
{
132+
ret_value = ecma_make_object_value (prototype_p);
133+
ecma_ref_object (prototype_p);
117134
}
118135
else
119136
{
120-
/* 2. */
121-
ecma_object_t *obj_p = ecma_get_object_from_value (arg);
122-
ecma_object_t *prototype_p = ecma_get_object_prototype (obj_p);
137+
ret_value = ECMA_VALUE_NULL;
138+
}
123139

124-
if (prototype_p)
125-
{
126-
ret_value = ecma_make_object_value (prototype_p);
127-
ecma_ref_object (prototype_p);
128-
}
129-
else
130-
{
131-
ret_value = ECMA_VALUE_NULL;
132-
}
140+
if (!was_object)
141+
{
142+
ecma_free_value (arg);
133143
}
134144

135145
return ret_value;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
var a = true;
16+
obj = Object.getPrototypeOf(a);
17+
assert (obj == Boolean.prototype);
18+
19+
a = 5;
20+
obj = Object.getPrototypeOf(a);
21+
assert (obj == Number.prototype);
22+
23+
a = "string";
24+
obj = Object.getPrototypeOf(a);
25+
assert (obj == String.prototype);
26+
27+
a = [1,2,3];
28+
obj = Object.getPrototypeOf(a);
29+
assert (obj == Array.prototype);
30+
31+
try {
32+
a = null;
33+
obj = Object.getPrototypeOf(a);
34+
assert(false);
35+
} catch (e) {
36+
assert(e instanceof TypeError);
37+
}

tools/run-tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ def run_jerry_tests(options):
288288

289289
if '--profile=es2015-subset' not in job.build_args:
290290
skip_list.append(r"es2015\/")
291+
else:
292+
skip_list.append(r"es5.1\/")
291293

292294
if options.skip_list:
293295
skip_list.append(options.skip_list)

0 commit comments

Comments
 (0)