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  * @fork
  31  */
  32 
  33 var intType    = Java.type("int");
  34 var doubleType = Java.type("double");
  35 var objectType = Java.type("java.lang.Object");
  36 
  37 var Context = Java.type("jdk.nashorn.internal.runtime.Context");
  38 var JSType  = Java.type("jdk.nashorn.internal.runtime.JSType");
  39 
  40 var context = Context.getContext();
  41 var dualFields = context.useDualFields();
  42 var optimisticTypes = context.getEnv()._optimistic_types;
  43 
  44 if (dualFields != optimisticTypes) {
  45     throw new Error("Wrong dual fields setting");
  46 }
  47 
  48 function testMap(obj) {
  49     obj.x = "foo";
  50     obj["y"] = 0;
  51     Object.defineProperty(obj, "z", {value: 0.5});
  52     var map = Debug.map(obj);
  53     for (var key in obj) {
  54         var prop = map.findProperty(key);
  55         if (prop.hasDualFields() !== dualFields) {
  56             throw new Error("Wrong property flags: " + prop);
  57         }
  58         if (prop.getType() != getExpectedType(obj[key])) {
  59             throw new Error("Wrong property type: " + prop.getType() + " // " + getExpectedType(obj[key]));
  60         }
  61     }
  62 }
  63 
  64 function getExpectedType(value) {
  65     if (!dualFields) {
  66         return objectType.class;
  67     }
  68     if (typeof value === "number") {
  69         return JSType.isRepresentableAsInt(value) ? intType.class : doubleType.class;
  70     }
  71     return objectType.class;
  72 }
  73 
  74 var o = {
  75     a: 1,
  76     b: 2.5,
  77     c: 0x10000000000,
  78     d: true
  79 };
  80 
  81 function C() {
  82     this.a = 1;
  83     this.b = 2.5;
  84     this.c = 0x10000000000;
  85     this.d = true;
  86 }
  87 
  88 var a = 1;
  89 var b = 2.5;
  90 var c = 0x10000000000;
  91 var d = true;
  92 
  93 testMap(o);
  94 testMap(new C());
  95 testMap(JSON.parse('{ "a": 1, "b": 2.5, "c": 1099511627776, "d": true }'));
  96 testMap(this);