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
28 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
29 import java.nio.ByteBuffer;
30 import jdk.nashorn.internal.objects.annotations.Attribute;
31 import jdk.nashorn.internal.objects.annotations.Constructor;
32 import jdk.nashorn.internal.objects.annotations.Function;
33 import jdk.nashorn.internal.objects.annotations.Getter;
34 import jdk.nashorn.internal.objects.annotations.ScriptClass;
35 import jdk.nashorn.internal.objects.annotations.SpecializedFunction;
36 import jdk.nashorn.internal.objects.annotations.Where;
37 import jdk.nashorn.internal.runtime.JSType;
38 import jdk.nashorn.internal.runtime.PropertyMap;
39 import jdk.nashorn.internal.runtime.ScriptObject;
40 import jdk.nashorn.internal.runtime.ScriptRuntime;
41
42 /**
43 * NativeArrayBuffer - ArrayBuffer as described in the JS typed
44 * array spec
45 */
46 @ScriptClass("ArrayBuffer")
47 public final class NativeArrayBuffer extends ScriptObject {
48 private final ByteBuffer nb;
49
84 * @param end end byte index
85 */
86 protected NativeArrayBuffer(final NativeArrayBuffer other, final int begin, final int end) {
87 this(cloneBuffer(other.getNioBuffer(), begin, end));
88 }
89
90 /**
91 * Constructor
92 * @param newObj is this invoked with new
93 * @param self self reference
94 * @param args arguments to constructor
95 * @return new NativeArrayBuffer
96 */
97 @Constructor(arity = 1)
98 public static NativeArrayBuffer constructor(final boolean newObj, final Object self, final Object... args) {
99 if (!newObj) {
100 throw typeError("constructor.requires.new", "ArrayBuffer");
101 }
102
103 if (args.length == 0) {
104 throw new RuntimeException("missing length argument");
105 }
106
107 return new NativeArrayBuffer(JSType.toInt32(args[0]));
108 }
109
110 private static ByteBuffer cloneBuffer(final ByteBuffer original, final int begin, final int end) {
111 final ByteBuffer clone = ByteBuffer.allocateDirect(original.capacity());
112 original.rewind();//copy from the beginning
113 clone.put(original);
114 original.rewind();
115 clone.flip();
116 clone.position(begin);
117 clone.limit(end);
118 return clone.slice();
119 }
120
121 ByteBuffer getNioBuffer() {
122 return nb;
123 }
124
|
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
28 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
29
30 import java.nio.ByteBuffer;
31
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;
37 import jdk.nashorn.internal.objects.annotations.SpecializedFunction;
38 import jdk.nashorn.internal.objects.annotations.Where;
39 import jdk.nashorn.internal.runtime.JSType;
40 import jdk.nashorn.internal.runtime.PropertyMap;
41 import jdk.nashorn.internal.runtime.ScriptObject;
42 import jdk.nashorn.internal.runtime.ScriptRuntime;
43
44 /**
45 * NativeArrayBuffer - ArrayBuffer as described in the JS typed
46 * array spec
47 */
48 @ScriptClass("ArrayBuffer")
49 public final class NativeArrayBuffer extends ScriptObject {
50 private final ByteBuffer nb;
51
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));
90 }
91
92 /**
93 * Constructor
94 * @param newObj is this invoked with new
95 * @param self self reference
96 * @param args arguments to constructor
97 * @return new NativeArrayBuffer
98 */
99 @Constructor(arity = 1)
100 public static NativeArrayBuffer constructor(final boolean newObj, final Object self, final Object... args) {
101 if (!newObj) {
102 throw typeError("constructor.requires.new", "ArrayBuffer");
103 }
104
105 if (args.length == 0) {
106 //For ES6 rev28+ we should throw a TypeError here. None of the other runtimes
107 //currently do it, though, so I have chosen to not implement that for now.
108 return new NativeArrayBuffer(0);
109 }
110
111 return new NativeArrayBuffer(JSType.toInt32(args[0]));
112 }
113
114 private static ByteBuffer cloneBuffer(final ByteBuffer original, final int begin, final int end) {
115 final ByteBuffer clone = ByteBuffer.allocateDirect(original.capacity());
116 original.rewind();//copy from the beginning
117 clone.put(original);
118 original.rewind();
119 clone.flip();
120 clone.position(begin);
121 clone.limit(end);
122 return clone.slice();
123 }
124
125 ByteBuffer getNioBuffer() {
126 return nb;
127 }
128
|