Skip to content

Implement the ES2015 version of Object.getPrototypeOf and add a test file for it #2256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions jerry-core/ecma/builtin-objects/ecma-builtin-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/

#include "ecma-alloc.h"
#include "ecma-array-object.h"
#include "ecma-builtin-helpers.h"
#include "ecma-builtins.h"
#include "ecma-conversion.h"
Expand Down Expand Up @@ -109,27 +108,38 @@ ecma_builtin_object_object_get_prototype_of (ecma_value_t this_arg, /**< 'this'
{
JERRY_UNUSED (this_arg);
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
bool was_object = ecma_is_value_object (arg);

/* 1. */
if (!ecma_is_value_object (arg))
if (!was_object)
{
ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Argument is not an object."));
#ifndef CONFIG_DISABLE_ES2015_BUILTIN
arg = ecma_op_to_object (arg);
if (ECMA_IS_VALUE_ERROR (arg))
{
return arg;
}
#else /* CONFIG_DISABLE_ES2015_BUILTIN */
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument is not an object."));
#endif /* !CONFIG_DISABLE_ES2015_BUILTIN */
}
/* 2. */
ecma_object_t *obj_p = ecma_get_object_from_value (arg);
ecma_object_t *prototype_p = ecma_get_object_prototype (obj_p);

if (prototype_p)
{
ret_value = ecma_make_object_value (prototype_p);
ecma_ref_object (prototype_p);
}
else
{
/* 2. */
ecma_object_t *obj_p = ecma_get_object_from_value (arg);
ecma_object_t *prototype_p = ecma_get_object_prototype (obj_p);
ret_value = ECMA_VALUE_NULL;
}

if (prototype_p)
{
ret_value = ecma_make_object_value (prototype_p);
ecma_ref_object (prototype_p);
}
else
{
ret_value = ECMA_VALUE_NULL;
}
if (!was_object)
{
ecma_free_value (arg);
}

return ret_value;
Expand Down
37 changes: 37 additions & 0 deletions tests/jerry/es2015/object-getprototypeof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright JS Foundation and other contributors, http://js.foundation
//
// 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.

var a = true;
obj = Object.getPrototypeOf(a);
assert (obj == Boolean.prototype);

a = 5;
obj = Object.getPrototypeOf(a);
assert (obj == Number.prototype);

a = "string";
obj = Object.getPrototypeOf(a);
assert (obj == String.prototype);

a = [1,2,3];
obj = Object.getPrototypeOf(a);
assert (obj == Array.prototype);

try {
a = null;
obj = Object.getPrototypeOf(a);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
2 changes: 2 additions & 0 deletions tools/run-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ def run_jerry_tests(options):

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

if options.skip_list:
skip_list.append(options.skip_list)
Expand Down