/** * Provide access to the underlying ByteBuffer used for this TypedArray. * The V8ArrayBuffer must be released. * * @return The V8ArrayBuffer used to back this TypedArray. */ public V8ArrayBuffer getBuffer() { return (V8ArrayBuffer) get("buffer"); }
/** * Provide access to the underlying ByteBuffer used for this TypedArray. * The V8ArrayBuffer must be released. * * @return The V8ArrayBuffer used to back this TypedArray. */ public V8ArrayBuffer getBuffer() { return (V8ArrayBuffer) get("buffer"); }
@Test public void testWriteInt32TypedArrayFromArrayBuffer() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 8); V8TypedArray v8Int32Array = new V8TypedArray(v8, buffer, V8Value.INTEGER, 0, 2); v8.add("v8Int32Array", v8Int32Array); buffer.putInt(4); buffer.putInt(8); assertEquals(4, v8Int32Array.get(0)); assertEquals(8, v8Int32Array.get(1)); buffer.close(); v8Int32Array.close(); }
@Test public void testWriteInt8TypedArrayFromArrayBuffer() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 8); V8TypedArray v8Int8Array = new V8TypedArray(v8, buffer, V8Value.BYTE, 0, 2); v8.add("v8Int8Array", v8Int8Array); buffer.put((byte) 4); buffer.put((byte) 8); assertEquals((byte) 4, v8Int8Array.get(0)); assertEquals((byte) 8, v8Int8Array.get(1)); buffer.close(); v8Int8Array.close(); }
@Test public void testUInt32TypedArray_addInJava() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 8); V8TypedArray typedArray = new V8TypedArray(v8, buffer, V8Value.UNSIGNED_INT_32_ARRAY, 0, 2); typedArray.add("0", (long) (Integer.MAX_VALUE + 1)); typedArray.add("1", -1); assertEquals(2, typedArray.length()); assertEquals(2147483648L, typedArray.get(0)); assertEquals(4294967295L, typedArray.get(1)); buffer.close(); typedArray.close(); }
@Test public void testUInt8ClampedTypedArray_addInJava() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 8); V8TypedArray v8Int8Array = new V8TypedArray(v8, buffer, V8Value.UNSIGNED_INT_8_CLAMPED_ARRAY, 0, 8); v8Int8Array.add("0", (short) 256); v8Int8Array.add("1", -1); assertEquals(8, v8Int8Array.length()); assertEquals((short) 255, v8Int8Array.get(0)); assertEquals((short) 0, v8Int8Array.get(1)); buffer.close(); v8Int8Array.close(); }
@Test public void testFloat32TypedArray_addInJava() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 8); V8TypedArray typedArray = new V8TypedArray(v8, buffer, V8Value.FLOAT_32_ARRAY, 0, 2); typedArray.add("0", 123.45); typedArray.add("1", -3.14159); assertEquals(2, typedArray.length()); assertEquals(123.45, (Float) typedArray.get(0), 0.00001); assertEquals(-3.14159, (Float) typedArray.get(1), 0.0001); buffer.close(); typedArray.close(); }
@Test public void testFloat64TypedArray_addInJava() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 16); V8TypedArray typedArray = new V8TypedArray(v8, buffer, V8Value.FLOAT_64_ARRAY, 0, 2); typedArray.add("0", 123.45); typedArray.add("1", -3.14159); assertEquals(2, typedArray.length()); assertEquals(123.45, (Double) typedArray.get(0), 0.00001); assertEquals(-3.14159, (Double) typedArray.get(1), 0.0001); buffer.close(); typedArray.close(); }
@Test public void testUInt8TypedArray_addInJava() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 8); V8TypedArray typedArray = new V8TypedArray(v8, buffer, V8Value.UNSIGNED_INT_8_ARRAY, 0, 8); typedArray.add("0", (short) 255); typedArray.add("1", (short) -1); assertEquals(8, typedArray.length()); assertEquals((short) 255, typedArray.get(0)); assertEquals((short) 255, typedArray.get(1)); buffer.close(); typedArray.close(); }
@Test public void testUInt16TypedArray_addInJava() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 4); V8TypedArray typedArray = new V8TypedArray(v8, buffer, V8Value.UNSIGNED_INT_16_ARRAY, 0, 2); typedArray.add("0", Short.MAX_VALUE + 1); typedArray.add("1", -1); assertEquals(2, typedArray.length()); assertEquals(32768, typedArray.get(0)); assertEquals(65535, typedArray.get(1)); buffer.close(); typedArray.close(); }
@Test public void testTypedArrayGetValue_Uint8ClampedArray() { V8Array int8ClampedArray = v8.executeArrayScript("var buf = new ArrayBuffer(100);\n" + "var int8ClampedArray = new Uint8ClampedArray(buf);\n" + "int8ClampedArray[0] = 16;\n" + "int8ClampedArray;\n"); V8TypedArray result = ((TypedArray) V8ObjectUtils.getValue(int8ClampedArray)).getV8TypedArray(); assertEquals(100, result.length()); assertEquals((short) 16, result.get(0)); int8ClampedArray.close(); result.close(); }
@Test public void testTypedArrayGetValue_Uint16Array() { V8Array intsArray = v8.executeArrayScript("var buf = new ArrayBuffer(100);\n" + "var intsArray = new Uint16Array(buf);\n" + "intsArray[0] = 16;\n" + "intsArray;\n"); V8TypedArray result = ((TypedArray) V8ObjectUtils.getValue(intsArray)).getV8TypedArray(); assertEquals(50, result.length()); assertEquals(16, result.get(0)); intsArray.close(); result.close(); }
@Test public void testArrayTypedArrayValue_Int16Array() { V8Array root = v8.executeArrayScript("var buf = new ArrayBuffer(100);\n" + "var intsArray = new Int16Array(buf);\n" + "intsArray[0] = 16;\n" + "var root = [intsArray];\n" + "root;\n"); V8TypedArray result = ((TypedArray) V8ObjectUtils.getValue(root, 0)).getV8TypedArray(); assertEquals(50, result.length()); assertEquals((short) 16, result.get(0)); root.close(); result.close(); }
@Test public void testTypedArrayValue_Int32Array() { V8Object root = v8.executeObjectScript("var buf = new ArrayBuffer(100);\n" + "var intsArray = new Int32Array(buf);\n" + "intsArray[0] = 16;\n" + "var root = { 'items' : intsArray };\n" + "root;\n"); V8TypedArray result = ((TypedArray) V8ObjectUtils.getValue(root, "items")).getV8TypedArray(); assertEquals(25, result.length()); assertEquals(16, result.get(0)); root.close(); result.close(); }
@Test public void testTypedArrayGetValue_Float32Array() { V8Array floatsArray = v8.executeArrayScript("var buf = new ArrayBuffer(100);\n" + "var floatsArray = new Float32Array(buf);\n" + "floatsArray[0] = 16.2;\n" + "floatsArray;\n"); V8TypedArray result = ((TypedArray) V8ObjectUtils.getValue(floatsArray)).getV8TypedArray(); assertEquals(25, result.length()); assertEquals(16.2, (Float) result.get(0), 0.00001); floatsArray.close(); result.close(); }
@Test public void testTypedArrayGetValue_Int8Array() { V8Array intsArray = v8.executeArrayScript("var buf = new ArrayBuffer(100);\n" + "var intsArray = new Int8Array(buf);\n" + "intsArray[0] = 16;\n" + "intsArray;\n"); V8TypedArray result = ((TypedArray) V8ObjectUtils.getValue(intsArray)).getV8TypedArray(); assertEquals(100, result.length()); assertEquals((byte) 16, result.get(0)); intsArray.close(); result.close(); }
@Test public void testTypedArrayGetValue_Int16Array() { V8Array intsArray = v8.executeArrayScript("var buf = new ArrayBuffer(100);\n" + "var intsArray = new Int16Array(buf);\n" + "intsArray[0] = 16;\n" + "intsArray;\n"); V8TypedArray result = ((TypedArray) V8ObjectUtils.getValue(intsArray)).getV8TypedArray(); assertEquals(50, result.length()); assertEquals((short) 16, result.get(0)); intsArray.close(); result.close(); }
@Test public void testTypedArrayGetValue_Int32Array() { V8Array intsArray = v8.executeArrayScript("var buf = new ArrayBuffer(100);\n" + "var intsArray = new Int32Array(buf);\n" + "intsArray[0] = 16;\n" + "intsArray;\n"); V8TypedArray result = ((TypedArray) V8ObjectUtils.getValue(intsArray)).getV8TypedArray(); assertEquals(25, result.length()); assertEquals(16, result.get(0)); intsArray.close(); result.close(); }
@Test public void testTypedArrayGetValue_Uint32Array() { V8Array intsArray = v8.executeArrayScript("var buf = new ArrayBuffer(100);\n" + "var intsArray = new Uint32Array(buf);\n" + "intsArray[0] = 16;\n" + "intsArray;\n"); V8TypedArray result = ((TypedArray) V8ObjectUtils.getValue(intsArray)).getV8TypedArray(); assertEquals(25, result.length()); assertEquals(16L, result.get(0)); intsArray.close(); result.close(); }
@Test public void testInt8TypedArray_CustomOffset() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 8); V8TypedArray v8Int8Array = new V8TypedArray(v8, buffer, V8Value.BYTE, 1, 1); buffer.put((byte) 4); buffer.put((byte) 8); assertEquals(1, v8Int8Array.length()); assertEquals((byte) 8, v8Int8Array.get(0)); buffer.close(); v8Int8Array.close(); }