From 9b87e5dadcabdf4965a278a4f090c0fffbbd60b7 Mon Sep 17 00:00:00 2001 From: Attila Szegedi Date: Thu, 17 Dec 2020 10:43:08 +0000 Subject: [PATCH] 8240299: A possible bug about Object.setPrototypeOf(). Reviewed-by: sundar --- .../internal/objects/NativeObject.java | 6 ++-- test/nashorn/script/basic/JDK-8240299.js | 35 +++++++++++++++++++ .../script/basic/JDK-8240299.js.EXPECTED | 1 + 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 test/nashorn/script/basic/JDK-8240299.js create mode 100644 test/nashorn/script/basic/JDK-8240299.js.EXPECTED diff --git a/src/org.openjdk.nashorn/share/classes/org/openjdk/nashorn/internal/objects/NativeObject.java b/src/org.openjdk.nashorn/share/classes/org/openjdk/nashorn/internal/objects/NativeObject.java index feb0e071..9d3dd6b5 100644 --- a/src/org.openjdk.nashorn/share/classes/org/openjdk/nashorn/internal/objects/NativeObject.java +++ b/src/org.openjdk.nashorn/share/classes/org/openjdk/nashorn/internal/objects/NativeObject.java @@ -204,15 +204,15 @@ public static Object getPrototypeOf(final Object self, final Object obj) { */ @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) public static Object setPrototypeOf(final Object self, final Object obj, final Object proto) { + Global.checkObjectCoercible(self); + if (obj instanceof ScriptObject) { ((ScriptObject)obj).setPrototypeOf(proto); - return obj; } else if (obj instanceof ScriptObjectMirror) { ((ScriptObjectMirror)obj).setProto(proto); - return obj; } - throw notAnObject(obj); + return obj; } /** diff --git a/test/nashorn/script/basic/JDK-8240299.js b/test/nashorn/script/basic/JDK-8240299.js new file mode 100644 index 00000000..e2d8f6d4 --- /dev/null +++ b/test/nashorn/script/basic/JDK-8240299.js @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * JDK-8240299: Object.setPrototypeOf() should silently be no-op on non-objects + * + * @test + * @run + */ + +var foo = function(){ + var a = Object.setPrototypeOf("test", null); + print(a); +}; +foo(); diff --git a/test/nashorn/script/basic/JDK-8240299.js.EXPECTED b/test/nashorn/script/basic/JDK-8240299.js.EXPECTED new file mode 100644 index 00000000..9daeafb9 --- /dev/null +++ b/test/nashorn/script/basic/JDK-8240299.js.EXPECTED @@ -0,0 +1 @@ +test