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