JDK-8044851.js revision 1643:133ea8746b37
1219820Sjeff/*
2219820Sjeff * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
3219820Sjeff * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4219820Sjeff *
5219820Sjeff * This code is free software; you can redistribute it and/or modify it
6219820Sjeff * under the terms of the GNU General Public License version 2 only, as
7219820Sjeff * published by the Free Software Foundation.
8219820Sjeff *
9219820Sjeff * This code is distributed in the hope that it will be useful, but WITHOUT
10219820Sjeff * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11219820Sjeff * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12219820Sjeff * version 2 for more details (a copy is included in the LICENSE file that
13219820Sjeff * accompanied this code).
14219820Sjeff *
15219820Sjeff * You should have received a copy of the GNU General Public License version
16219820Sjeff * 2 along with this work; if not, write to the Free Software Foundation,
17219820Sjeff * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18219820Sjeff *
19219820Sjeff * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20219820Sjeff * or visit www.oracle.com if you need additional information or have any
21219820Sjeff * questions.
22219820Sjeff */
23219820Sjeff
24219820Sjeff/**
25219820Sjeff * JDK-8044851: nashorn properties leak memory
26219820Sjeff *
27219820Sjeff * @test
28219820Sjeff * @option -Dnashorn.debug=true
29219820Sjeff * @fork
30219820Sjeff */
31219820Sjeff
32219820Sjeffvar Property = Java.type("jdk.nashorn.internal.runtime.Property");
33219820Sjeffvar PropertyMap = Java.type("jdk.nashorn.internal.runtime.PropertyMap");
34219820Sjeff
35219820Sjeff// Class objects
36219820Sjeffvar objectCls = Java.type("java.lang.Object").class;
37219820Sjeffvar propertyCls = Property.class;
38219820Sjeffvar propertyMapCls = PropertyMap.class;
39219820Sjeff
40219820Sjeff// Method objects
41219820Sjeffvar findPropertyMethod = propertyMapCls.getMethod("findProperty", objectCls);
42219820Sjeffvar getKeyMethod = propertyCls.getMethod("getKey");
43219820Sjeffvar isSpillMethod = propertyCls.getMethod("isSpill");
44219820Sjeffvar getSlotMethod = propertyCls.getMethod("getSlot");
45219820Sjeff
46219820Sjefffunction printProperty(value, property) {
47219820Sjeff    print(value, getKeyMethod.invoke(property),
48219820Sjeff        isSpillMethod.invoke(property) ? "spill" : "field", getSlotMethod.invoke(property));
49219820Sjeff}
50219820Sjeff
51219820Sjefffunction findProperty(obj, name) {
52219820Sjeff    var map = Debug.map(obj);
53219820Sjeff    return findPropertyMethod.invoke(map, name);
54219820Sjeff}
55219820Sjeff
56219820Sjeffvar obj = {}, i, name;
57219820Sjeff
58219820Sjefffor (i = 0; i < 8; ++i) {
59219820Sjeff    name = 'property' + i;
60219820Sjeff    obj[name] = 'a' + i;
61219820Sjeff    printProperty(obj[name], findProperty(obj, name));
62219820Sjeff}
63219820Sjeffprint();
64219820Sjeff
65219820Sjefffor (i = 0; i < 8; ++i) {
66219820Sjeff    name = 'property' + i;
67219820Sjeff    delete obj[name];
68219820Sjeff}
69219820Sjeff
70219820Sjefffor (i = 0; i < 8; ++i) {
71    name = 'property' + i;
72    obj[name] = 'b' + i;
73    printProperty(obj[name], findProperty(obj, name));
74}
75print();
76
77for (i = 0; i < 8; ++i) {
78    name = 'property' + i;
79    Object.defineProperty(obj, name, {get: function() {return i;}, set: function(v) {}, configurable: true});
80    printProperty(obj[name], findProperty(obj, name));
81}
82print();
83
84for (i = 0; i < 8; ++i) {
85    name = 'property' + i;
86    delete obj[name];
87}
88
89for (i = 0; i < 8; ++i) {
90    name = 'property' + i;
91    obj[name] = 'c' + i;
92    printProperty(obj[name], findProperty(obj, name));
93}
94print();
95
96for (i = 7; i > -1; --i) {
97    name = 'property' + i;
98    delete obj[name];
99}
100
101for (i = 0; i < 8; ++i) {
102    name = 'property' + i;
103    obj[name] = 'd' + i;
104    printProperty(obj[name], findProperty(obj, name));
105}
106print();
107
108for (i = 0; i < 8; ++i) {
109    name = 'property' + i;
110    Object.defineProperty(obj, name, {get: function() {return i;}, set: function(v) {}});
111    printProperty(obj[name], findProperty(obj, name));
112}
113