author | lana |
Sat, 26 Sep 2015 09:21:16 -0700 | |
changeset 32752 | 43c458023730 |
parent 26886 | 18c744ab4df2 |
permissions | -rw-r--r-- |
24283 | 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 |
*/ |
|
31 |
||
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
32 |
import java.nio.DoubleBuffer; |
24283 | 33 |
import jdk.nashorn.api.scripting.AbstractJSObject; |
34 |
||
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; |
|
54 |
||
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
55 |
/** |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
56 |
* Constructor |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
57 |
* @param size initial size |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
58 |
*/ |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
59 |
public BufferArray(final int size) { |
24283 | 60 |
buf = DoubleBuffer.allocate(size); |
61 |
} |
|
62 |
||
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
63 |
/** |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
64 |
* Constructur |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
65 |
* @param buf {@link DoubleBuffer} to link to |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
66 |
*/ |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
67 |
public BufferArray(final DoubleBuffer buf) { |
24283 | 68 |
this.buf = buf; |
69 |
} |
|
70 |
||
71 |
// called to check if indexed property exists |
|
72 |
@Override |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
73 |
public boolean hasSlot(final int index) { |
24283 | 74 |
return index > 0 && index < buf.capacity(); |
75 |
} |
|
76 |
||
77 |
// get the value from that index |
|
78 |
@Override |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
79 |
public Object getSlot(final int index) { |
24283 | 80 |
return buf.get(index); |
81 |
} |
|
82 |
||
83 |
// set the value at that index |
|
84 |
@Override |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
85 |
public void setSlot(final int index, final Object value) { |
24283 | 86 |
buf.put(index, ((Number)value).doubleValue()); |
87 |
} |
|
88 |
||
89 |
// do you have a property of that given name? |
|
90 |
@Override |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
91 |
public boolean hasMember(final String name) { |
24283 | 92 |
return "length".equals(name) || "buf".equals(name); |
93 |
} |
|
94 |
||
95 |
// get the value of that named property |
|
96 |
@Override |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
97 |
public Object getMember(final String name) { |
24283 | 98 |
switch (name) { |
99 |
case "length": |
|
100 |
return buf.capacity(); |
|
101 |
case "buf": |
|
102 |
// return a 'function' value for this property |
|
103 |
return new AbstractJSObject() { |
|
104 |
@Override |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
24283
diff
changeset
|
105 |
public Object call(final Object thiz, final Object... args) { |
24283 | 106 |
return BufferArray.this.buf; |
107 |
} |
|
108 |
||
109 |
// yes, I'm a function ! |
|
110 |
@Override |
|
111 |
public boolean isFunction() { |
|
112 |
return true; |
|
113 |
} |
|
114 |
}; |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
115 |
default: |
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
116 |
break; |
24283 | 117 |
} |
118 |
return null; |
|
119 |
} |
|
120 |
} |