author | sundar |
Mon, 11 May 2015 22:57:24 +0530 | |
changeset 30512 | aa86a08c6625 |
parent 30395 | 352bc6fe8406 |
child 30830 | 9d3a0827accd |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.internal.objects; |
|
27 |
||
24720 | 28 |
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
29537
e6615d669728
8066217: ArrayBuffer constructor was erroneous with zero args
lagergren
parents:
27363
diff
changeset
|
29 |
|
23374 | 30 |
import java.nio.ByteBuffer; |
30512
aa86a08c6625
8080042: can't build nashorn.jar from jdk9-dev/nashorn using jdk8 installation as JAVA_HOME
sundar
parents:
30395
diff
changeset
|
31 |
|
16147 | 32 |
import jdk.nashorn.internal.objects.annotations.Attribute; |
33 |
import jdk.nashorn.internal.objects.annotations.Constructor; |
|
34 |
import jdk.nashorn.internal.objects.annotations.Function; |
|
35 |
import jdk.nashorn.internal.objects.annotations.Getter; |
|
36 |
import jdk.nashorn.internal.objects.annotations.ScriptClass; |
|
24720 | 37 |
import jdk.nashorn.internal.objects.annotations.SpecializedFunction; |
27363
9722f0db287b
8061959: ArrayBuffer lacked static isViewMethod
lagergren
parents:
25865
diff
changeset
|
38 |
import jdk.nashorn.internal.objects.annotations.Where; |
16147 | 39 |
import jdk.nashorn.internal.runtime.JSType; |
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
16226
diff
changeset
|
40 |
import jdk.nashorn.internal.runtime.PropertyMap; |
16147 | 41 |
import jdk.nashorn.internal.runtime.ScriptObject; |
42 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
43 |
||
24720 | 44 |
/** |
45 |
* NativeArrayBuffer - ArrayBuffer as described in the JS typed |
|
46 |
* array spec |
|
47 |
*/ |
|
16147 | 48 |
@ScriptClass("ArrayBuffer") |
24720 | 49 |
public final class NativeArrayBuffer extends ScriptObject { |
50 |
private final ByteBuffer nb; |
|
16147 | 51 |
|
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
16226
diff
changeset
|
52 |
// initialized by nasgen |
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
16226
diff
changeset
|
53 |
private static PropertyMap $nasgenmap$; |
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
16226
diff
changeset
|
54 |
|
24720 | 55 |
/** |
56 |
* Constructor |
|
57 |
* @param nb native byte buffer to wrap |
|
58 |
* @param global global instance |
|
59 |
*/ |
|
60 |
protected NativeArrayBuffer(final ByteBuffer nb, final Global global) { |
|
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23374
diff
changeset
|
61 |
super(global.getArrayBufferPrototype(), $nasgenmap$); |
24720 | 62 |
this.nb = nb; |
63 |
} |
|
64 |
||
65 |
/** |
|
66 |
* Constructor |
|
67 |
* @param nb native byte buffer to wrap |
|
68 |
*/ |
|
69 |
protected NativeArrayBuffer(final ByteBuffer nb) { |
|
70 |
this(nb, Global.instance()); |
|
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
71 |
} |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
72 |
|
24720 | 73 |
/** |
74 |
* Constructor |
|
75 |
* @param byteLength byteLength for buffer |
|
76 |
*/ |
|
77 |
protected NativeArrayBuffer(final int byteLength) { |
|
78 |
this(ByteBuffer.allocateDirect(byteLength)); |
|
16147 | 79 |
} |
80 |
||
24720 | 81 |
/** |
82 |
* Clone constructor |
|
83 |
* Used only for slice |
|
84 |
* @param other original buffer |
|
85 |
* @param begin begin byte index |
|
86 |
* @param end end byte index |
|
87 |
*/ |
|
88 |
protected NativeArrayBuffer(final NativeArrayBuffer other, final int begin, final int end) { |
|
89 |
this(cloneBuffer(other.getNioBuffer(), begin, end)); |
|
16147 | 90 |
} |
91 |
||
24731
ab0c8fc915ae
8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents:
24727
diff
changeset
|
92 |
/** |
ab0c8fc915ae
8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents:
24727
diff
changeset
|
93 |
* Constructor |
ab0c8fc915ae
8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents:
24727
diff
changeset
|
94 |
* @param newObj is this invoked with new |
ab0c8fc915ae
8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents:
24727
diff
changeset
|
95 |
* @param self self reference |
ab0c8fc915ae
8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents:
24727
diff
changeset
|
96 |
* @param args arguments to constructor |
ab0c8fc915ae
8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents:
24727
diff
changeset
|
97 |
* @return new NativeArrayBuffer |
ab0c8fc915ae
8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents:
24727
diff
changeset
|
98 |
*/ |
16147 | 99 |
@Constructor(arity = 1) |
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
100 |
public static NativeArrayBuffer constructor(final boolean newObj, final Object self, final Object... args) { |
24731
ab0c8fc915ae
8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents:
24727
diff
changeset
|
101 |
if (!newObj) { |
ab0c8fc915ae
8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents:
24727
diff
changeset
|
102 |
throw typeError("constructor.requires.new", "ArrayBuffer"); |
ab0c8fc915ae
8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents:
24727
diff
changeset
|
103 |
} |
ab0c8fc915ae
8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents:
24727
diff
changeset
|
104 |
|
16147 | 105 |
if (args.length == 0) { |
29537
e6615d669728
8066217: ArrayBuffer constructor was erroneous with zero args
lagergren
parents:
27363
diff
changeset
|
106 |
return new NativeArrayBuffer(0); |
16147 | 107 |
} |
108 |
||
109 |
return new NativeArrayBuffer(JSType.toInt32(args[0])); |
|
110 |
} |
|
111 |
||
24731
ab0c8fc915ae
8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents:
24727
diff
changeset
|
112 |
private static ByteBuffer cloneBuffer(final ByteBuffer original, final int begin, final int end) { |
24720 | 113 |
final ByteBuffer clone = ByteBuffer.allocateDirect(original.capacity()); |
114 |
original.rewind();//copy from the beginning |
|
115 |
clone.put(original); |
|
116 |
original.rewind(); |
|
117 |
clone.flip(); |
|
118 |
clone.position(begin); |
|
119 |
clone.limit(end); |
|
120 |
return clone.slice(); |
|
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
121 |
} |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
122 |
|
24720 | 123 |
ByteBuffer getNioBuffer() { |
124 |
return nb; |
|
16147 | 125 |
} |
126 |
||
19638
bf08be79957a
8023784: Object.prototype.toString should contain the class name for all instances
sundar
parents:
18851
diff
changeset
|
127 |
@Override |
bf08be79957a
8023784: Object.prototype.toString should contain the class name for all instances
sundar
parents:
18851
diff
changeset
|
128 |
public String getClassName() { |
bf08be79957a
8023784: Object.prototype.toString should contain the class name for all instances
sundar
parents:
18851
diff
changeset
|
129 |
return "ArrayBuffer"; |
bf08be79957a
8023784: Object.prototype.toString should contain the class name for all instances
sundar
parents:
18851
diff
changeset
|
130 |
} |
bf08be79957a
8023784: Object.prototype.toString should contain the class name for all instances
sundar
parents:
18851
diff
changeset
|
131 |
|
24720 | 132 |
/** |
133 |
* Byte length for native array buffer |
|
134 |
* @param self native array buffer |
|
135 |
* @return byte length |
|
136 |
*/ |
|
16147 | 137 |
@Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE) |
24720 | 138 |
public static int byteLength(final Object self) { |
139 |
return ((NativeArrayBuffer)self).getByteLength(); |
|
16147 | 140 |
} |
141 |
||
24720 | 142 |
/** |
27363
9722f0db287b
8061959: ArrayBuffer lacked static isViewMethod
lagergren
parents:
25865
diff
changeset
|
143 |
* Returns true if an object is an ArrayBufferView |
9722f0db287b
8061959: ArrayBuffer lacked static isViewMethod
lagergren
parents:
25865
diff
changeset
|
144 |
* |
9722f0db287b
8061959: ArrayBuffer lacked static isViewMethod
lagergren
parents:
25865
diff
changeset
|
145 |
* @param self self |
9722f0db287b
8061959: ArrayBuffer lacked static isViewMethod
lagergren
parents:
25865
diff
changeset
|
146 |
* @param obj object to check |
9722f0db287b
8061959: ArrayBuffer lacked static isViewMethod
lagergren
parents:
25865
diff
changeset
|
147 |
* |
9722f0db287b
8061959: ArrayBuffer lacked static isViewMethod
lagergren
parents:
25865
diff
changeset
|
148 |
* @return true if obj is an ArrayBufferView |
9722f0db287b
8061959: ArrayBuffer lacked static isViewMethod
lagergren
parents:
25865
diff
changeset
|
149 |
*/ |
9722f0db287b
8061959: ArrayBuffer lacked static isViewMethod
lagergren
parents:
25865
diff
changeset
|
150 |
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) |
9722f0db287b
8061959: ArrayBuffer lacked static isViewMethod
lagergren
parents:
25865
diff
changeset
|
151 |
public static boolean isView(final Object self, final Object obj) { |
9722f0db287b
8061959: ArrayBuffer lacked static isViewMethod
lagergren
parents:
25865
diff
changeset
|
152 |
return obj instanceof ArrayBufferView; |
9722f0db287b
8061959: ArrayBuffer lacked static isViewMethod
lagergren
parents:
25865
diff
changeset
|
153 |
} |
9722f0db287b
8061959: ArrayBuffer lacked static isViewMethod
lagergren
parents:
25865
diff
changeset
|
154 |
|
9722f0db287b
8061959: ArrayBuffer lacked static isViewMethod
lagergren
parents:
25865
diff
changeset
|
155 |
/** |
24720 | 156 |
* Slice function |
157 |
* @param self native array buffer |
|
158 |
* @param begin0 start byte index |
|
159 |
* @param end0 end byte index |
|
160 |
* @return new array buffer, sliced |
|
161 |
*/ |
|
16147 | 162 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
163 |
public static NativeArrayBuffer slice(final Object self, final Object begin0, final Object end0) { |
16147 | 164 |
final NativeArrayBuffer arrayBuffer = (NativeArrayBuffer)self; |
24720 | 165 |
final int byteLength = arrayBuffer.getByteLength(); |
166 |
final int begin = adjustIndex(JSType.toInt32(begin0), byteLength); |
|
167 |
final int end = adjustIndex(end0 != ScriptRuntime.UNDEFINED ? JSType.toInt32(end0) : byteLength, byteLength); |
|
168 |
return new NativeArrayBuffer(arrayBuffer, begin, Math.max(end, begin)); |
|
169 |
} |
|
170 |
||
171 |
/** |
|
172 |
* Specialized slice function |
|
173 |
* @param self native array buffer |
|
174 |
* @param begin start byte index |
|
175 |
* @param end end byte index |
|
176 |
* @return new array buffer, sliced |
|
177 |
*/ |
|
178 |
@SpecializedFunction |
|
179 |
public static Object slice(final Object self, final int begin, final int end) { |
|
180 |
final NativeArrayBuffer arrayBuffer = (NativeArrayBuffer)self; |
|
181 |
final int byteLength = arrayBuffer.getByteLength(); |
|
182 |
return new NativeArrayBuffer(arrayBuffer, adjustIndex(begin, byteLength), Math.max(adjustIndex(end, byteLength), begin)); |
|
183 |
} |
|
184 |
||
185 |
/** |
|
186 |
* Specialized slice function |
|
187 |
* @param self native array buffer |
|
188 |
* @param begin start byte index |
|
189 |
* @return new array buffer, sliced |
|
190 |
*/ |
|
191 |
@SpecializedFunction |
|
192 |
public static Object slice(final Object self, final int begin) { |
|
193 |
return slice(self, begin, ((NativeArrayBuffer)self).getByteLength()); |
|
16147 | 194 |
} |
195 |
||
196 |
/** |
|
197 |
* If index is negative, it refers to an index from the end of the array, as |
|
198 |
* opposed to from the beginning. The index is clamped to the valid index |
|
199 |
* range for the array. |
|
200 |
* |
|
201 |
* @param index The index. |
|
202 |
* @param length The length of the array. |
|
203 |
* @return valid index index in the range [0, length). |
|
204 |
*/ |
|
205 |
static int adjustIndex(final int index, final int length) { |
|
24720 | 206 |
return index < 0 ? clamp(index + length, length) : clamp(index, length); |
16147 | 207 |
} |
208 |
||
209 |
/** |
|
210 |
* Clamp index into the range [0, length). |
|
211 |
*/ |
|
212 |
private static int clamp(final int index, final int length) { |
|
213 |
if (index < 0) { |
|
214 |
return 0; |
|
215 |
} else if (index > length) { |
|
216 |
return length; |
|
217 |
} |
|
218 |
return index; |
|
219 |
} |
|
220 |
||
24720 | 221 |
int getByteLength() { |
222 |
return nb.limit(); |
|
16147 | 223 |
} |
23374 | 224 |
|
225 |
ByteBuffer getBuffer() { |
|
24727 | 226 |
return nb; |
23374 | 227 |
} |
228 |
||
229 |
ByteBuffer getBuffer(final int offset) { |
|
30512
aa86a08c6625
8080042: can't build nashorn.jar from jdk9-dev/nashorn using jdk8 installation as JAVA_HOME
sundar
parents:
30395
diff
changeset
|
230 |
return (ByteBuffer)nb.duplicate().position(offset); |
23374 | 231 |
} |
232 |
||
233 |
ByteBuffer getBuffer(final int offset, final int length) { |
|
30512
aa86a08c6625
8080042: can't build nashorn.jar from jdk9-dev/nashorn using jdk8 installation as JAVA_HOME
sundar
parents:
30395
diff
changeset
|
234 |
return (ByteBuffer)getBuffer(offset).limit(length); |
23374 | 235 |
} |
16147 | 236 |
} |