nashorn/test/script/basic/JDK-8016618.js
author psandoz
Tue, 13 Aug 2013 11:16:37 +0200
changeset 19424 c4524285927f
parent 18334 47413e8d71b5
child 24738 be2026c9717c
permissions -rw-r--r--
8022797: Clarify spliterator characteristics for collections containing no elements Reviewed-by: alanb, mduigou
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.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
     4
 * 
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.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
     8
 * 
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).
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    14
 * 
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.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    18
 * 
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)
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    41
}; 
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
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    57
global.load({ 
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
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    64
    global.func.apply(obj, arguments);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    65
}
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
f(23, "hello");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    68
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    69
var fObject = global.eval("Object");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    70
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    71
// instanceof on mirrors
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    72
print("global instanceof fObject? " + (global instanceof fObject));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    73
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    74
// Object API on mirrors
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    75
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    76
var desc = Object.getOwnPropertyDescriptor(global, "x");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    77
print("x is wriable ? " + desc.writable);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    78
print("x value = " + desc.value);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    79
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    80
var proto = Object.getPrototypeOf(global);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    81
print("global's __proto__ " + proto);
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
var obj = global.obj;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    84
var keys = Object.keys(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    85
print("Object.keys on obj");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    86
for (var i in keys) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    87
    print(keys[i]);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    88
}
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    89
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    90
print("Object.getOwnProperties on obj");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    91
var keys = Object.getOwnPropertyNames(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    92
for (var i in keys) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    93
  print(keys[i]);
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
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    96
// mirror array access
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    97
var array = global.eval("[334, 55, 65]");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    98
Array.prototype.forEach.call(array, function(elem) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
    99
    print("forEach " + elem)
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("reduceRight " + Array.prototype.reduceRight.call(array,
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   103
    function(previousValue, currentValue, index, array) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   104
        print("reduceRight cur value " + currentValue);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   105
        return previousValue + currentValue;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   106
}, 0));
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
print("reduce " + Array.prototype.reduce.call(array,
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   109
    function(previousValue, currentValue, index, array) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   110
        print("reduce cur value " + currentValue);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   111
        return previousValue + currentValue;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   112
}, 0));
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("forEach");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   115
Array.prototype.forEach.call(array, function(o) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   116
   print(o);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   117
});
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   118
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   119
print("Array.isArray(array)? " + Array.isArray(array));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   120
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   121
// try to write to a non-writable property of mirror
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   122
try {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   123
   obj.bar = 33;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   124
} catch (e) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   125
   print(e);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   126
}
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   127
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   128
// mirror function called with local callback
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   129
print("forEach on mirror");
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   130
array.forEach(function(toto) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   131
    print(toto);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents:
diff changeset
   132
});