Tue, 26 Aug 2014 15:04:48 +0200
8056052: Source.getContent() does excess Object.clone()
Reviewed-by: jlaskey, sundar
1 /*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Oracle nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
32 import jdk.nashorn.api.scripting.AbstractJSObject;
33 import java.nio.DoubleBuffer;
35 /**
36 * Simple class demonstrating pluggable script object
37 * implementation. By implementing jdk.nashorn.api.scripting.JSObject
38 * (or extending AbstractJSObject which implements it), you
39 * can supply a friendly script object. Nashorn will call
40 * 'magic' methods on such a class on 'obj.foo, obj.foo = 33,
41 * obj.bar()' etc. from script.
42 *
43 * In this example, Java nio DoubleBuffer object is wrapped
44 * as a friendly script object that provides indexed acces
45 * to buffer content and also support array-like "length"
46 * readonly property to retrieve buffer's capacity. This class
47 * also demonstrates a function valued property called "buf".
48 * On 'buf' method, we return the underlying nio buffer object
49 * that is being wrapped.
50 */
51 public class BufferArray extends AbstractJSObject {
52 // underlying nio buffer
53 private final DoubleBuffer buf;
55 public BufferArray(int size) {
56 buf = DoubleBuffer.allocate(size);
57 }
59 public BufferArray(DoubleBuffer buf) {
60 this.buf = buf;
61 }
63 // called to check if indexed property exists
64 @Override
65 public boolean hasSlot(int index) {
66 return index > 0 && index < buf.capacity();
67 }
69 // get the value from that index
70 @Override
71 public Object getSlot(int index) {
72 return buf.get(index);
73 }
75 // set the value at that index
76 @Override
77 public void setSlot(int index, Object value) {
78 buf.put(index, ((Number)value).doubleValue());
79 }
81 // do you have a property of that given name?
82 @Override
83 public boolean hasMember(String name) {
84 return "length".equals(name) || "buf".equals(name);
85 }
87 // get the value of that named property
88 @Override
89 public Object getMember(String name) {
90 switch (name) {
91 case "length":
92 return buf.capacity();
93 case "buf":
94 // return a 'function' value for this property
95 return new AbstractJSObject() {
96 @Override
97 public Object call(Object thiz, Object... args) {
98 return BufferArray.this.buf;
99 }
101 // yes, I'm a function !
102 @Override
103 public boolean isFunction() {
104 return true;
105 }
106 };
107 }
108 return null;
109 }
110 }