1 /* 2 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * JDK-8067215: Disable dual fields when not using optimistic types 26 * 27 * @test 28 * @run 29 * @option -Dnashorn.debug=true 30 * @fork 31 */ 32 33 var intType = Java.type("int"); 34 var doubleType = Java.type("double"); 35 var longType = Java.type("long"); 36 var objectType = Java.type("java.lang.Object"); 37 38 var Context = Java.type("jdk.nashorn.internal.runtime.Context"); 39 var JSType = Java.type("jdk.nashorn.internal.runtime.JSType"); 40 41 var context = Context.getContext(); 42 var dualFields = context.useDualFields(); 43 var optimisticTypes = context.getEnv()._optimistic_types; 44 45 if (dualFields != optimisticTypes) { 46 throw new Error("Wrong dual fields setting"); 47 } 48 49 function testMap(obj) { 50 obj.x = "foo"; 51 obj["y"] = 0; 52 Object.defineProperty(obj, "z", {value: 0.5}); 53 var map = Debug.map(obj); 54 for (var key in obj) { 55 var prop = map.findProperty(key); 56 if (prop.hasDualFields() !== dualFields) { 57 throw new Error("Wrong property flags: " + prop); 58 } 59 if (prop.getType() != getExpectedType(obj[key])) { 60 throw new Error("Wrong property type: " + prop.getType() + " // " + getExpectedType(obj[key])); 61 62 } 63 } 64 } 65 66 function getExpectedType(value) { 67 if (!dualFields) { 68 return objectType.class; 69 } 70 if (JSType.isRepresentableAsInt(value)) { 71 return intType.class; 72 } 73 if (JSType.isRepresentableAsLong(value)) { 74 return longType.class; 75 } 76 if (JSType.isNumber(value)) { 77 return doubleType.class; 78 } 79 return objectType.class; 80 } 81 82 var o = { 83 a: 1, 84 b: 2.5, 85 c: 0x10000000000, 86 d: true 87 }; 88 89 function C() { 90 this.a = 1; 91 this.b = 2.5; 92 this.c = 0x10000000000; 93 this.d = true; 94 } 95 96 var a = 1; 97 var b = 2.5; 98 var c = 0x10000000000; 99 var d = true; 100 101 testMap(o); 102 testMap(new C()); 103 testMap(JSON.parse('{ "a": 1, "b": 2.5, "c": 1099511627776, "d": true }')); 104 testMap(this);