nashorn/test/script/nosecurity/JDK-8067215.js
author sundar
Tue, 12 Jul 2016 21:18:13 +0530
changeset 39662 e2b36a3779b9
parent 36517 41a1c20eb619
permissions -rw-r--r--
8149929: Nashorn Parser API needs to be updated for ES6 Reviewed-by: mhaupt, hannesw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29834
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
     1
/*
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
     2
 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
     4
 * 
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
     8
 * 
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    13
 * accompanied this code).
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    14
 * 
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    18
 * 
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    21
 * questions.
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    22
 */
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    23
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    24
/**
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    25
 * JDK-8067215: Disable dual fields when not using optimistic types
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    26
 *
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    27
 * @test
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    28
 * @run
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    29
 * @option -Dnashorn.debug=true
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    30
 * @option -scripting
29834
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    31
 * @fork
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    32
 */
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    33
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    34
var intType    = Java.type("int");
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    35
var doubleType = Java.type("double");
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    36
var objectType = Java.type("java.lang.Object");
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    37
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    38
var Context = Java.type("jdk.nashorn.internal.runtime.Context");
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    39
var JSType  = Java.type("jdk.nashorn.internal.runtime.JSType");
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    40
var Property = Java.type("jdk.nashorn.internal.runtime.Property");
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    41
var PropertyMap  = Java.type("jdk.nashorn.internal.runtime.PropertyMap");
29834
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    42
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    43
// Class objects
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    44
var objectCls = Java.type("java.lang.Object").class;
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    45
var contextCls = Context.class;
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    46
var JSTypeCls = JSType.class;
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    47
var propertyCls = Property.class;
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    48
var propertyMapCls  = PropertyMap.class;
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    49
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    50
// Method objects
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    51
var getContextMethod = contextCls.getMethod("getContext");
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    52
var isRepresentableAsIntMethod = JSTypeCls.getMethod("isRepresentableAsInt", java.lang.Double.TYPE);
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    53
var hasDualFieldsMethod = propertyCls.getMethod("hasDualFields");
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    54
var getTypeMethod = propertyCls.getMethod("getType");
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    55
var findPropertyMethod = propertyMapCls.getMethod("findProperty", objectCls);
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    56
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    57
var context = getContextMethod.invoke(null);
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    58
var useDualFieldsMethod = contextCls.getMethod("useDualFields");
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    59
var dualFields = useDualFieldsMethod.invoke(context);
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    60
var optimisticTypes = $OPTIONS._optimistic_types;
29834
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    61
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    62
if (dualFields != optimisticTypes) {
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    63
    throw new Error("Wrong dual fields setting");
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    64
}
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    65
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    66
function testMap(obj) {
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    67
    obj.x = "foo";
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    68
    obj["y"] = 0;
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    69
    Object.defineProperty(obj, "z", {value: 0.5});
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    70
    var map = Debug.map(obj);
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    71
    for (var key in obj) {
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    72
        var prop = findPropertyMethod.invoke(map, key);
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    73
        if (hasDualFieldsMethod.invoke(prop) !== dualFields) {
29834
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    74
            throw new Error("Wrong property flags: " + prop);
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    75
        }
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    76
        if (getTypeMethod.invoke(prop) != getExpectedType(obj[key])) {
29834
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    77
            throw new Error("Wrong property type: " + prop.getType() + " // " + getExpectedType(obj[key]));
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    78
        }
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    79
    }
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    80
}
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    81
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    82
function getExpectedType(value) {
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    83
    if (!dualFields) {
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    84
        return objectType.class;
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    85
    }
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 29834
diff changeset
    86
    if (typeof value === "number") {
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 34732
diff changeset
    87
        return isRepresentableAsIntMethod.invoke(null, value) ? intType.class : doubleType.class;
29834
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    88
    }
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    89
    return objectType.class;
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    90
}
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    91
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    92
var o = {
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    93
    a: 1,
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    94
    b: 2.5,
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    95
    c: 0x10000000000,
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    96
    d: true
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    97
};
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    98
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
    99
function C() {
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
   100
    this.a = 1;
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
   101
    this.b = 2.5;
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
   102
    this.c = 0x10000000000;
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
   103
    this.d = true;
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
   104
}
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
   105
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
   106
var a = 1;
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
   107
var b = 2.5;
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
   108
var c = 0x10000000000;
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
   109
var d = true;
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
   110
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
   111
testMap(o);
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
   112
testMap(new C());
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
   113
testMap(JSON.parse('{ "a": 1, "b": 2.5, "c": 1099511627776, "d": true }'));
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents:
diff changeset
   114
testMap(this);