JDK-8067215.js revision 1643:133ea8746b37
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 * @option -scripting
31 * @fork
32 */
33
34var intType    = Java.type("int");
35var doubleType = Java.type("double");
36var objectType = Java.type("java.lang.Object");
37
38var Context = Java.type("jdk.nashorn.internal.runtime.Context");
39var JSType  = Java.type("jdk.nashorn.internal.runtime.JSType");
40var Property = Java.type("jdk.nashorn.internal.runtime.Property");
41var PropertyMap  = Java.type("jdk.nashorn.internal.runtime.PropertyMap");
42
43// Class objects
44var objectCls = Java.type("java.lang.Object").class;
45var contextCls = Context.class;
46var JSTypeCls = JSType.class;
47var propertyCls = Property.class;
48var propertyMapCls  = PropertyMap.class;
49
50// Method objects
51var getContextMethod = contextCls.getMethod("getContext");
52var isRepresentableAsIntMethod = JSTypeCls.getMethod("isRepresentableAsInt", java.lang.Double.TYPE);
53var hasDualFieldsMethod = propertyCls.getMethod("hasDualFields");
54var getTypeMethod = propertyCls.getMethod("getType");
55var findPropertyMethod = propertyMapCls.getMethod("findProperty", objectCls);
56
57var context = getContextMethod.invoke(null);
58var useDualFieldsMethod = contextCls.getMethod("useDualFields");
59var dualFields = useDualFieldsMethod.invoke(context);
60var optimisticTypes = $OPTIONS._optimistic_types;
61
62if (dualFields != optimisticTypes) {
63    throw new Error("Wrong dual fields setting");
64}
65
66function testMap(obj) {
67    obj.x = "foo";
68    obj["y"] = 0;
69    Object.defineProperty(obj, "z", {value: 0.5});
70    var map = Debug.map(obj);
71    for (var key in obj) {
72        var prop = findPropertyMethod.invoke(map, key);
73        if (hasDualFieldsMethod.invoke(prop) !== dualFields) {
74            throw new Error("Wrong property flags: " + prop);
75        }
76        if (getTypeMethod.invoke(prop) != getExpectedType(obj[key])) {
77            throw new Error("Wrong property type: " + prop.getType() + " // " + getExpectedType(obj[key]));
78        }
79    }
80}
81
82function getExpectedType(value) {
83    if (!dualFields) {
84        return objectType.class;
85    }
86    if (typeof value === "number") {
87        return isRepresentableAsIntMethod.invoke(null, value) ? intType.class : doubleType.class;
88    }
89    return objectType.class;
90}
91
92var o = {
93    a: 1,
94    b: 2.5,
95    c: 0x10000000000,
96    d: true
97};
98
99function C() {
100    this.a = 1;
101    this.b = 2.5;
102    this.c = 0x10000000000;
103    this.d = true;
104}
105
106var a = 1;
107var b = 2.5;
108var c = 0x10000000000;
109var d = true;
110
111testMap(o);
112testMap(new C());
113testMap(JSON.parse('{ "a": 1, "b": 2.5, "c": 1099511627776, "d": true }'));
114testMap(this);
115