|
| 1 | +// |
| 2 | +// JSArrayBuffer.java |
| 3 | +// AndroidJSCore project |
| 4 | +// |
| 5 | +// https://github.com/ericwlange/AndroidJSCore/ |
| 6 | +// |
| 7 | +// Created by Eric Lange |
| 8 | +// |
| 9 | +/* |
| 10 | + Copyright (c) 2014-2016 Eric Lange. All rights reserved. |
| 11 | +
|
| 12 | + Redistribution and use in source and binary forms, with or without |
| 13 | + modification, are permitted provided that the following conditions are met: |
| 14 | +
|
| 15 | + - Redistributions of source code must retain the above copyright notice, this |
| 16 | + list of conditions and the following disclaimer. |
| 17 | +
|
| 18 | + - Redistributions in binary form must reproduce the above copyright notice, |
| 19 | + this list of conditions and the following disclaimer in the documentation |
| 20 | + and/or other materials provided with the distribution. |
| 21 | +
|
| 22 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 23 | + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 | + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 26 | + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 28 | + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 30 | + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | +*/ |
1 | 33 | package org.liquidplayer.webkit.javascriptcore; |
2 | 34 |
|
3 | 35 | /** |
4 | | - * Created by Eric on 6/20/16. |
| 36 | + * A wrapper class for a JavaScript ArrayBuffer |
5 | 37 | */ |
6 | 38 | public class JSArrayBuffer { |
| 39 | + /** |
| 40 | + * Creates a new array buffer of 'length' bytes |
| 41 | + * @param ctx the JSContext in which to create the ArrayBuffer |
| 42 | + * @param length the length in bytes of the ArrayBuffer |
| 43 | + */ |
7 | 44 | public JSArrayBuffer(JSContext ctx, int length) { |
8 | 45 | JSFunction constructor = new JSFunction(ctx,"_ArrayBuffer",new String[] {"length"}, |
9 | 46 | "return new ArrayBuffer(length);", |
10 | 47 | null, 0); |
11 | 48 | mArrayBuffer = constructor.call(null,length).toObject(); |
12 | 49 | } |
| 50 | + |
| 51 | + /** |
| 52 | + * Treats an existing JSObject as an ArrayBuffer. It is up to the user to ensure the |
| 53 | + * underlying JSObject is actually an ArrayBuffer. |
| 54 | + * @param buffer The ArrayBuffer JSObject to wrap |
| 55 | + */ |
13 | 56 | public JSArrayBuffer(JSObject buffer) { |
14 | 57 | mArrayBuffer = buffer; |
15 | 58 | } |
16 | 59 | private final JSObject mArrayBuffer; |
17 | 60 |
|
| 61 | + /** |
| 62 | + * Gets underlying JSObject |
| 63 | + * @return JSObject representing the ArrayBuffer |
| 64 | + */ |
18 | 65 | public JSObject getJSObject() { |
19 | 66 | return mArrayBuffer; |
20 | 67 | } |
21 | 68 |
|
22 | | - public int length() { |
23 | | - return mArrayBuffer.property("length").toNumber().intValue(); |
| 69 | + /** |
| 70 | + * JavaScript: ArrayBuffer.prototype.byteLength, see: |
| 71 | + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/byteLength |
| 72 | + * @return length of ArrayBuffer in bytes |
| 73 | + */ |
| 74 | + public int byteLength() { |
| 75 | + return mArrayBuffer.property("byteLength").toNumber().intValue(); |
24 | 76 | } |
25 | 77 |
|
| 78 | + /** |
| 79 | + * JavaScript: ArrayBuffer.isView(), see: |
| 80 | + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView |
| 81 | + * @param arg the argument to be checked |
| 82 | + * @return true if arg is one of the ArrayBuffer views, such as typed array objects or |
| 83 | + * a DataView; false otherwise |
| 84 | + */ |
26 | 85 | public static boolean isView(JSValue arg) { |
27 | 86 | return arg.getContext().property("ArrayBuffer").toObject().property("isView").toFunction() |
28 | 87 | .call(null,arg).toBoolean(); |
29 | 88 | } |
30 | 89 |
|
| 90 | + /** |
| 91 | + * JavaScript: ArrayBuffer.transfer(), see: |
| 92 | + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/transfer |
| 93 | + * @param oldBuffer An ArrayBuffer object from which to transfer from |
| 94 | + * @param newByteLength The byte length of the new ArrayBuffer object |
| 95 | + * @return a new ArrayBuffer |
| 96 | + */ |
31 | 97 | public static JSArrayBuffer transfer(JSArrayBuffer oldBuffer, int newByteLength) { |
32 | 98 | return new JSArrayBuffer(oldBuffer.getJSObject().getContext().property("ArrayBuffer").toObject() |
33 | 99 | .property("transfer").toFunction().call(null,oldBuffer,newByteLength).toObject()); |
34 | 100 | } |
| 101 | + /** |
| 102 | + * JavaScript: ArrayBuffer.transfer(), see: |
| 103 | + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/transfer |
| 104 | + * @param oldBuffer An ArrayBuffer object from which to transfer from |
| 105 | + * @return a new ArrayBuffer |
| 106 | + */ |
35 | 107 | public static JSArrayBuffer transfer(JSArrayBuffer oldBuffer) { |
36 | 108 | return new JSArrayBuffer(oldBuffer.getJSObject().getContext().property("ArrayBuffer").toObject() |
37 | 109 | .property("transfer").toFunction().call(null,oldBuffer).toObject()); |
38 | 110 | } |
39 | 111 |
|
| 112 | + /** |
| 113 | + * JavaScript: ArrayBuffer.prototype.slice(), see: |
| 114 | + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/slice |
| 115 | + * @param begin Zero-based byte index at which to begin slicing |
| 116 | + * @param end Byte index to end slicing |
| 117 | + * @return new ArrayBuffer with sliced contents copied |
| 118 | + */ |
40 | 119 | public JSArrayBuffer slice(int begin, int end) { |
41 | 120 | return new JSArrayBuffer( |
42 | 121 | mArrayBuffer.property("slice").toFunction().call(null,begin,end).toObject()); |
43 | 122 | } |
| 123 | + /** |
| 124 | + * JavaScript: ArrayBuffer.prototype.slice(), see: |
| 125 | + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/slice |
| 126 | + * @param begin Zero-based byte index at which to begin slicing |
| 127 | + * @return new ArrayBuffer with sliced contents copied |
| 128 | + */ |
44 | 129 | public JSArrayBuffer slice(int begin) { |
45 | 130 | return new JSArrayBuffer( |
46 | 131 | mArrayBuffer.property("slice").toFunction().call(null,begin).toObject()); |
|
0 commit comments