test/nashorn/script/basic/JDK-8016618.js
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
     1
/*
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24738
diff changeset
     4
 *
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24738
diff changeset
     8
 *
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    13
 * accompanied this code).
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24738
diff changeset
    14
 *
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24738
diff changeset
    18
 *
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    21
 * questions.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    22
 */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    23
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    24
/**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    25
 * JDK-8016618: script mirror object access should be improved
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    26
 *
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    27
 * @test
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    28
 * @option -scripting
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    29
 * @option -strict
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    30
 * @run
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    31
 */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    32
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    33
var global = loadWithNewGlobal({
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    34
    name: "code",
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    35
    script: <<EOF
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    36
var x = 33;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    37
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    38
function func(x, y) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    39
    print('func.x = ' + x);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    40
    print('func.x = ' + y)
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24738
diff changeset
    41
};
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    42
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    43
var obj = {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    44
    foo: 'hello',
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    45
    bar: 42
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    46
};
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    47
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    48
Object.defineProperty(obj, "bar",
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    49
    { enumerable: false, writable: false });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    50
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    51
// return global
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    52
this;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    53
EOF
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    54
});
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    55
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    56
// load on mirror with local object as argument
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24738
diff changeset
    57
global.load({
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    58
    name: "code",
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    59
    script: "print('x = ' + x)"
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    60
});
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    61
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    62
function f() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    63
    // mirror function called with local arguments
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 18334
diff changeset
    64
    //    global.func.apply(obj, arguments);
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    65
    global.func.apply(obj, arguments);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    66
}
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    67
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    68
f(23, "hello");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    69
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 18334
diff changeset
    70
f(24, "hello2");
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 18334
diff changeset
    71
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 18334
diff changeset
    72
var oldCall = Function.prototype.call;
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 18334
diff changeset
    73
Function.prototype.call = function() {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 18334
diff changeset
    74
    throw "this should never happen! go back to apply!";
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 18334
diff changeset
    75
};
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 18334
diff changeset
    76
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 18334
diff changeset
    77
f(25, "hello3");
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 18334
diff changeset
    78
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 18334
diff changeset
    79
Function.prototype.call = oldCall;
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 18334
diff changeset
    80
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    81
var fObject = global.eval("Object");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    82
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    83
// instanceof on mirrors
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    84
print("global instanceof fObject? " + (global instanceof fObject));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    85
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    86
// Object API on mirrors
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    87
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    88
var desc = Object.getOwnPropertyDescriptor(global, "x");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    89
print("x is wriable ? " + desc.writable);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    90
print("x value = " + desc.value);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    91
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    92
var proto = Object.getPrototypeOf(global);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    93
print("global's __proto__ " + proto);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    94
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    95
var obj = global.obj;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    96
var keys = Object.keys(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    97
print("Object.keys on obj");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    98
for (var i in keys) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    99
    print(keys[i]);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   100
}
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   101
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   102
print("Object.getOwnProperties on obj");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   103
var keys = Object.getOwnPropertyNames(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   104
for (var i in keys) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   105
  print(keys[i]);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   106
}
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   107
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   108
// mirror array access
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   109
var array = global.eval("[334, 55, 65]");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   110
Array.prototype.forEach.call(array, function(elem) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   111
    print("forEach " + elem)
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   112
});
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   113
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   114
print("reduceRight " + Array.prototype.reduceRight.call(array,
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   115
    function(previousValue, currentValue, index, array) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   116
        print("reduceRight cur value " + currentValue);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   117
        return previousValue + currentValue;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   118
}, 0));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   119
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   120
print("reduce " + Array.prototype.reduce.call(array,
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   121
    function(previousValue, currentValue, index, array) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   122
        print("reduce cur value " + currentValue);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   123
        return previousValue + currentValue;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   124
}, 0));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   125
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   126
print("forEach");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   127
Array.prototype.forEach.call(array, function(o) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   128
   print(o);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   129
});
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   130
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   131
print("Array.isArray(array)? " + Array.isArray(array));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   132
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   133
// try to write to a non-writable property of mirror
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   134
try {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   135
   obj.bar = 33;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   136
} catch (e) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   137
   print(e);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   138
}
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   139
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   140
// mirror function called with local callback
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   141
print("forEach on mirror");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   142
array.forEach(function(toto) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   143
    print(toto);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   144
});