nashorn/test/script/nosecurity/JDK-8044798.js
author sundar
Wed, 16 Dec 2015 16:42:03 +0530
changeset 34735 0f41d334aa24
parent 32895 5a09b2d3d73a
child 36517 41a1c20eb619
permissions -rw-r--r--
8145486: jjs should support documentation key shortcut in interactive mode Reviewed-by: mhaupt, hannesw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24881
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
     1
/*
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24881
diff changeset
     4
 *
24881
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24881
diff changeset
     8
 *
24881
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    13
 * accompanied this code).
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24881
diff changeset
    14
 *
24881
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24881
diff changeset
    18
 *
24881
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    21
 * questions.
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    22
 */
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    23
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    24
/**
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    25
 * JDK-8044798: API for debugging Nashorn
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    26
 *
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    27
 * @test
27099
eceb216332cb 8050977: Java8 Javascript Nashorn exception: no current Global instance for nashorn
sundar
parents: 26067
diff changeset
    28
 * @option -Dnashorn.mirror.always=false
eceb216332cb 8050977: Java8 Javascript Nashorn exception: no current Global instance for nashorn
sundar
parents: 26067
diff changeset
    29
 * @fork
24881
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    30
 * @run
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    31
 */
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    32
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    33
// basic API exercise checks
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    34
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    35
var Arrays = Java.type("java.util.Arrays");
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    36
var CharArray = Java.type("char[]");
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    37
var DebuggerSupport = Java.type("jdk.nashorn.internal.runtime.DebuggerSupport");
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    38
var DebuggerValueDesc = Java.type("jdk.nashorn.internal.runtime.DebuggerSupport.DebuggerValueDesc");
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    39
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    40
var valueDescFields = DebuggerValueDesc.class.declaredFields;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    41
Arrays.sort(valueDescFields, function(f1, f2) f1.name.compareTo(f2.name));
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    42
for each (var f in valueDescFields) {
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    43
    f.accessible = true;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    44
}
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    45
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    46
var debuggerSupportMethods = DebuggerSupport.class.declaredMethods;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    47
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    48
// methods of DebuggerSupport that we use
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    49
var evalMethod, valueInfoMethod, valueInfosMethod;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    50
var getSourceInfoMethod, valueAsStringMethod;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    51
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    52
for each (var m in debuggerSupportMethods) {
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    53
    m.accessible = true;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    54
    switch (m.name) {
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    55
        case "eval":
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    56
            evalMethod = m;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    57
            break;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    58
        case "valueInfo":
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    59
            if (m.parameterCount == 3) {
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    60
                valueInfoMethod = m;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    61
            }
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    62
            break;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    63
        case "valueInfos":
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    64
            valueInfosMethod = m;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    65
            break;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    66
        case "valueAsString":
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    67
            valueAsStringMethod = m;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    68
            break;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    69
        case "getSourceInfo":
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    70
            getSourceInfoMethod = m;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    71
            break;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    72
    }
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    73
}
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    74
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    75
// eval
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    76
var value = evalMethod.invoke(null, null, null, "33 + 55", false);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    77
print(value);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    78
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    79
// valueInfo
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    80
var info = valueInfoMethod.invoke(null, "apply", Function, true);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    81
for each (var f in valueDescFields) {
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    82
    print(f.name, "=", f.get(info));
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    83
}
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    84
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    85
// valueInfo - user defined object
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    86
var info = valueInfoMethod.invoke(null, "foo", { foo: 343 }, true);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    87
for each (var f in valueDescFields) {
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    88
    print(f.name, "=", f.get(info));
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    89
}
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    90
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    91
// valueInfos
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    92
var infos = valueInfosMethod.invoke(null, Object, true);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    93
for each (var info in infos) {
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    94
    for each (var f in valueDescFields) {
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    95
        print(f.name, "=", f.get(info));
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24881
diff changeset
    96
    }
24881
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    97
}
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    98
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
    99
// valueInfos - user defined object
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   100
var infos = valueInfosMethod.invoke(null, { foo: 34, bar: "hello" }, true);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   101
for each (var info in infos) {
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   102
    for each (var f in valueDescFields) {
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   103
        print(f.name, "=", f.get(info));
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24881
diff changeset
   104
    }
24881
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   105
}
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   106
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   107
// valueAsString
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   108
function printValue(value) {
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   109
    print(valueAsStringMethod.invoke(null, value));
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   110
}
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   111
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   112
printValue(undefined);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   113
printValue(null);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   114
printValue("hello");
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   115
printValue(Math.PI);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   116
printValue(this);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   117
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   118
// The below are not part of DebuggerSupport. But we need these to
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   119
// test DebuggerSupport.getSourceInfo etc. which need compiled script class
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   120
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   121
var Source = Java.type("jdk.nashorn.internal.runtime.Source");
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   122
var Context = Java.type("jdk.nashorn.internal.runtime.Context");
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   123
var sourceCls = Source.class;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   124
var errorMgrCls = Java.type("jdk.nashorn.internal.runtime.ErrorManager").class;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   125
var booleanCls = Java.type("java.lang.Boolean").TYPE;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   126
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   127
// private compile method of Context class
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   128
var compileMethod = Context.class.getDeclaredMethod("compile",
32895
5a09b2d3d73a 8138882: Performance regression due to anonymous classloading
hannesw
parents: 27099
diff changeset
   129
                sourceCls, errorMgrCls, booleanCls, booleanCls);
24881
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   130
compileMethod.accessible = true;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   131
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   132
var scriptCls = compileMethod.invoke(Context.context,
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   133
    Source.sourceFor("test", "print('hello')"),
32895
5a09b2d3d73a 8138882: Performance regression due to anonymous classloading
hannesw
parents: 27099
diff changeset
   134
    new Context.ThrowErrorManager(), false, false);
24881
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   135
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   136
var SCRIPT_CLASS_NAME_PREFIX = "jdk.nashorn.internal.scripts.Script$";
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   137
print("script class name pattern satisfied? " +
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   138
    scriptCls.name.startsWith(SCRIPT_CLASS_NAME_PREFIX));
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   139
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   140
var srcInfo = getSourceInfoMethod.invoke(null, scriptCls);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   141
var srcInfoFields = srcInfo.class.declaredFields;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   142
Arrays.sort(srcInfoFields, function(f1, f2) f1.name.compareTo(f2.name));
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   143
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   144
print("Source info");
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   145
for each (var f in srcInfoFields) {
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   146
    f.accessible = true;
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   147
    var fieldValue = f.get(srcInfo);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   148
    if (fieldValue instanceof CharArray) {
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   149
        fieldValue = new java.lang.String(fieldValue);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   150
    }
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   151
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   152
    print(f.name, "=", fieldValue);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents:
diff changeset
   153
}