Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/test/script/nosecurity/JDK-8067215.js
32281 views
/*1* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* JDK-8067215: Disable dual fields when not using optimistic types25*26* @test27* @run28* @option -Dnashorn.debug=true29* @fork30*/3132var intType = Java.type("int");33var doubleType = Java.type("double");34var objectType = Java.type("java.lang.Object");3536var Context = Java.type("jdk.nashorn.internal.runtime.Context");37var JSType = Java.type("jdk.nashorn.internal.runtime.JSType");3839var context = Context.getContext();40var dualFields = context.useDualFields();41var optimisticTypes = context.getEnv()._optimistic_types;4243if (dualFields != optimisticTypes) {44throw new Error("Wrong dual fields setting");45}4647function testMap(obj) {48obj.x = "foo";49obj["y"] = 0;50Object.defineProperty(obj, "z", {value: 0.5});51var map = Debug.map(obj);52for (var key in obj) {53var prop = map.findProperty(key);54if (prop.hasDualFields() !== dualFields) {55throw new Error("Wrong property flags: " + prop);56}57if (prop.getType() != getExpectedType(obj[key])) {58throw new Error("Wrong property type: " + prop.getType() + " // " + getExpectedType(obj[key]));59}60}61}6263function getExpectedType(value) {64if (!dualFields) {65return objectType.class;66}67if (typeof value === "number") {68return JSType.isRepresentableAsInt(value) ? intType.class : doubleType.class;69}70return objectType.class;71}7273var o = {74a: 1,75b: 2.5,76c: 0x10000000000,77d: true78};7980function C() {81this.a = 1;82this.b = 2.5;83this.c = 0x10000000000;84this.d = true;85}8687var a = 1;88var b = 2.5;89var c = 0x10000000000;90var d = true;9192testMap(o);93testMap(new C());94testMap(JSON.parse('{ "a": 1, "b": 2.5, "c": 1099511627776, "d": true }'));95testMap(this);969798