aboutsummaryrefslogtreecommitdiff
path: root/test/script/basic/JDK-8016618.js
blob: 0db20a08c62865e7a555e50bca4fa9d23af06770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * JDK-8016618: script mirror object access should be improved
 *
 * @test
 * @option -scripting
 * @option -strict
 * @run
 */

var global = loadWithNewGlobal({
    name: "code",
    script: <<EOF
var x = 33;

function func(x, y) {
    print('func.x = ' + x);
    print('func.x = ' + y)
};

var obj = {
    foo: 'hello',
    bar: 42
};

Object.defineProperty(obj, "bar",
    { enumerable: false, writable: false });

// return global
this;
EOF
});

// load on mirror with local object as argument
global.load({
    name: "code",
    script: "print('x = ' + x)"
});

function f() {
    // mirror function called with local arguments
    //    global.func.apply(obj, arguments);
    global.func.apply(obj, arguments);
}

f(23, "hello");

f(24, "hello2");

var oldCall = Function.prototype.call;
Function.prototype.call = function() {
    throw "this should never happen! go back to apply!";
};

f(25, "hello3");

Function.prototype.call = oldCall;

var fObject = global.eval("Object");

// instanceof on mirrors
print("global instanceof fObject? " + (global instanceof fObject));

// Object API on mirrors

var desc = Object.getOwnPropertyDescriptor(global, "x");
print("x is wriable ? " + desc.writable);
print("x value = " + desc.value);

var proto = Object.getPrototypeOf(global);
print("global's __proto__ " + proto);

var obj = global.obj;
var keys = Object.keys(obj);
print("Object.keys on obj");
for (var i in keys) {
    print(keys[i]);
}

print("Object.getOwnProperties on obj");
var keys = Object.getOwnPropertyNames(obj);
for (var i in keys) {
  print(keys[i]);
}

// mirror array access
var array = global.eval("[334, 55, 65]");
Array.prototype.forEach.call(array, function(elem) {
    print("forEach " + elem)
});

print("reduceRight " + Array.prototype.reduceRight.call(array,
    function(previousValue, currentValue, index, array) {
        print("reduceRight cur value " + currentValue);
        return previousValue + currentValue;
}, 0));

print("reduce " + Array.prototype.reduce.call(array,
    function(previousValue, currentValue, index, array) {
        print("reduce cur value " + currentValue);
        return previousValue + currentValue;
}, 0));

print("forEach");
Array.prototype.forEach.call(array, function(o) {
   print(o);
});

print("Array.isArray(array)? " + Array.isArray(array));

// try to write to a non-writable property of mirror
try {
   obj.bar = 33;
} catch (e) {
   print(e);
}

// mirror function called with local callback
print("forEach on mirror");
array.forEach(function(toto) {
    print(toto);
});