1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/script/nosecurity/JDK-8067215.js Fri Apr 10 14:18:31 2015 +0200 1.3 @@ -0,0 +1,104 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/** 1.28 + * JDK-8067215: Disable dual fields when not using optimistic types 1.29 + * 1.30 + * @test 1.31 + * @run 1.32 + * @option -Dnashorn.debug=true 1.33 + * @fork 1.34 + */ 1.35 + 1.36 +var intType = Java.type("int"); 1.37 +var doubleType = Java.type("double"); 1.38 +var longType = Java.type("long"); 1.39 +var objectType = Java.type("java.lang.Object"); 1.40 + 1.41 +var Context = Java.type("jdk.nashorn.internal.runtime.Context"); 1.42 +var JSType = Java.type("jdk.nashorn.internal.runtime.JSType"); 1.43 + 1.44 +var context = Context.getContext(); 1.45 +var dualFields = context.useDualFields(); 1.46 +var optimisticTypes = context.getEnv()._optimistic_types; 1.47 + 1.48 +if (dualFields != optimisticTypes) { 1.49 + throw new Error("Wrong dual fields setting"); 1.50 +} 1.51 + 1.52 +function testMap(obj) { 1.53 + obj.x = "foo"; 1.54 + obj["y"] = 0; 1.55 + Object.defineProperty(obj, "z", {value: 0.5}); 1.56 + var map = Debug.map(obj); 1.57 + for (var key in obj) { 1.58 + var prop = map.findProperty(key); 1.59 + if (prop.hasDualFields() !== dualFields) { 1.60 + throw new Error("Wrong property flags: " + prop); 1.61 + } 1.62 + if (prop.getType() != getExpectedType(obj[key])) { 1.63 + throw new Error("Wrong property type: " + prop.getType() + " // " + getExpectedType(obj[key])); 1.64 + 1.65 + } 1.66 + } 1.67 +} 1.68 + 1.69 +function getExpectedType(value) { 1.70 + if (!dualFields) { 1.71 + return objectType.class; 1.72 + } 1.73 + if (JSType.isRepresentableAsInt(value)) { 1.74 + return intType.class; 1.75 + } 1.76 + if (JSType.isRepresentableAsLong(value)) { 1.77 + return longType.class; 1.78 + } 1.79 + if (JSType.isNumber(value)) { 1.80 + return doubleType.class; 1.81 + } 1.82 + return objectType.class; 1.83 +} 1.84 + 1.85 +var o = { 1.86 + a: 1, 1.87 + b: 2.5, 1.88 + c: 0x10000000000, 1.89 + d: true 1.90 +}; 1.91 + 1.92 +function C() { 1.93 + this.a = 1; 1.94 + this.b = 2.5; 1.95 + this.c = 0x10000000000; 1.96 + this.d = true; 1.97 +} 1.98 + 1.99 +var a = 1; 1.100 +var b = 2.5; 1.101 +var c = 0x10000000000; 1.102 +var d = true; 1.103 + 1.104 +testMap(o); 1.105 +testMap(new C()); 1.106 +testMap(JSON.parse('{ "a": 1, "b": 2.5, "c": 1099511627776, "d": true }')); 1.107 +testMap(this);