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 
  34 var intType    = Java.type("int");
  35 var doubleType = Java.type("double");
  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 var Property = Java.type("jdk.nashorn.internal.runtime.Property");
  41 var PropertyMap  = Java.type("jdk.nashorn.internal.runtime.PropertyMap");
  42 
  43 // Class objects
  44 var objectCls = Java.type("java.lang.Object").class;
  45 var contextCls = Context.class;
  46 var JSTypeCls = JSType.class;
  47 var propertyCls = Property.class;
  48 var propertyMapCls  = PropertyMap.class;
  49 
  50 // Method objects
  51 var getContextMethod = contextCls.getMethod("getContext");
  52 var isRepresentableAsIntMethod = JSTypeCls.getMethod("isRepresentableAsInt", java.lang.Double.TYPE);
  53 var hasDualFieldsMethod = propertyCls.getMethod("hasDualFields");
  54 var getTypeMethod = propertyCls.getMethod("getType");
  55 var findPropertyMethod = propertyMapCls.getMethod("findProperty", objectCls);
  56 
  57 var context = getContextMethod.invoke(null);
  58 var useDualFieldsMethod = contextCls.getMethod("useDualFields");
  59 var dualFields = useDualFieldsMethod.invoke(context);
  60 var optimisticTypes = $OPTIONS._optimistic_types;
  61 
  62 if (dualFields != optimisticTypes) {
  63     throw new Error("Wrong dual fields setting");
  64 }
  65 
  66 function 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 
  82 function 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 
  92 var o = {
  93     a: 1,
  94     b: 2.5,
  95     c: 0x10000000000,
  96     d: true
  97 };
  98 
  99 function C() {
 100     this.a = 1;
 101     this.b = 2.5;
 102     this.c = 0x10000000000;
 103     this.d = true;
 104 }
 105 
 106 var a = 1;
 107 var b = 2.5;
 108 var c = 0x10000000000;
 109 var d = true;
 110 
 111 testMap(o);
 112 testMap(new C());
 113 testMap(JSON.parse('{ "a": 1, "b": 2.5, "c": 1099511627776, "d": true }'));
 114 testMap(this);