hannesw@1330: /* hannesw@1330: * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved. hannesw@1330: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. hannesw@1330: * hannesw@1330: * This code is free software; you can redistribute it and/or modify it hannesw@1330: * under the terms of the GNU General Public License version 2 only, as hannesw@1330: * published by the Free Software Foundation. hannesw@1330: * hannesw@1330: * This code is distributed in the hope that it will be useful, but WITHOUT hannesw@1330: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or hannesw@1330: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License hannesw@1330: * version 2 for more details (a copy is included in the LICENSE file that hannesw@1330: * accompanied this code). hannesw@1330: * hannesw@1330: * You should have received a copy of the GNU General Public License version hannesw@1330: * 2 along with this work; if not, write to the Free Software Foundation, hannesw@1330: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. hannesw@1330: * hannesw@1330: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA hannesw@1330: * or visit www.oracle.com if you need additional information or have any hannesw@1330: * questions. hannesw@1330: */ hannesw@1330: hannesw@1330: /** hannesw@1330: * JDK-8067215: Disable dual fields when not using optimistic types hannesw@1330: * hannesw@1330: * @test hannesw@1330: * @run hannesw@1330: * @option -Dnashorn.debug=true hannesw@1330: * @fork hannesw@1330: */ hannesw@1330: hannesw@1330: var intType = Java.type("int"); hannesw@1330: var doubleType = Java.type("double"); hannesw@1330: var longType = Java.type("long"); hannesw@1330: var objectType = Java.type("java.lang.Object"); hannesw@1330: hannesw@1330: var Context = Java.type("jdk.nashorn.internal.runtime.Context"); hannesw@1330: var JSType = Java.type("jdk.nashorn.internal.runtime.JSType"); hannesw@1330: hannesw@1330: var context = Context.getContext(); hannesw@1330: var dualFields = context.useDualFields(); hannesw@1330: var optimisticTypes = context.getEnv()._optimistic_types; hannesw@1330: hannesw@1330: if (dualFields != optimisticTypes) { hannesw@1330: throw new Error("Wrong dual fields setting"); hannesw@1330: } hannesw@1330: hannesw@1330: function testMap(obj) { hannesw@1330: obj.x = "foo"; hannesw@1330: obj["y"] = 0; hannesw@1330: Object.defineProperty(obj, "z", {value: 0.5}); hannesw@1330: var map = Debug.map(obj); hannesw@1330: for (var key in obj) { hannesw@1330: var prop = map.findProperty(key); hannesw@1330: if (prop.hasDualFields() !== dualFields) { hannesw@1330: throw new Error("Wrong property flags: " + prop); hannesw@1330: } hannesw@1330: if (prop.getType() != getExpectedType(obj[key])) { hannesw@1330: throw new Error("Wrong property type: " + prop.getType() + " // " + getExpectedType(obj[key])); hannesw@1330: hannesw@1330: } hannesw@1330: } hannesw@1330: } hannesw@1330: hannesw@1330: function getExpectedType(value) { hannesw@1330: if (!dualFields) { hannesw@1330: return objectType.class; hannesw@1330: } hannesw@1330: if (JSType.isRepresentableAsInt(value)) { hannesw@1330: return intType.class; hannesw@1330: } hannesw@1330: if (JSType.isRepresentableAsLong(value)) { hannesw@1330: return longType.class; hannesw@1330: } hannesw@1330: if (JSType.isNumber(value)) { hannesw@1330: return doubleType.class; hannesw@1330: } hannesw@1330: return objectType.class; hannesw@1330: } hannesw@1330: hannesw@1330: var o = { hannesw@1330: a: 1, hannesw@1330: b: 2.5, hannesw@1330: c: 0x10000000000, hannesw@1330: d: true hannesw@1330: }; hannesw@1330: hannesw@1330: function C() { hannesw@1330: this.a = 1; hannesw@1330: this.b = 2.5; hannesw@1330: this.c = 0x10000000000; hannesw@1330: this.d = true; hannesw@1330: } hannesw@1330: hannesw@1330: var a = 1; hannesw@1330: var b = 2.5; hannesw@1330: var c = 0x10000000000; hannesw@1330: var d = true; hannesw@1330: hannesw@1330: testMap(o); hannesw@1330: testMap(new C()); hannesw@1330: testMap(JSON.parse('{ "a": 1, "b": 2.5, "c": 1099511627776, "d": true }')); hannesw@1330: testMap(this);