/** * Create a new TypedArray from an ArrayBuffer. * * @param v8 the V8Runtime on which to create the TypedArray * @param buffer the ArrayBuffer to use to back the TypedArray * @param type the Type of Array to create * @param offset the Offset into the ArrayBuffer in which to map the TyepdArray * @param size the Size of the TypedArray */ public TypedArray(final V8 v8, final ArrayBuffer buffer, final int type, final int offset, final int size) { V8ArrayBuffer v8ArrayBuffer = buffer.getV8ArrayBuffer(); V8TypedArray v8typedArray = new V8TypedArray(v8, v8ArrayBuffer, type, offset, size); try { typedArray = (V8TypedArray) v8typedArray.twin().setWeak(); } finally { v8ArrayBuffer.close(); v8typedArray.close(); } }
/** * 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"); }
private void checkOffset(final V8ArrayData arrayData) { if ((arrayData.offset % getStructureSize(arrayData.type)) != 0) { throw new IllegalStateException("RangeError: Start offset of Int32Array must be a multiple of " + getStructureSize(arrayData.type)); } }
private void checkArrayProperties(final V8ArrayData arrayData) { checkOffset(arrayData); checkSize(arrayData); }
@Override protected V8Value createTwin() { v8.checkThread(); checkReleased(); return new V8TypedArray(v8); }
@Override protected void initialize(final long runtimePtr, final Object data) { v8.checkThread(); if (data == null) { super.initialize(runtimePtr, data); return; } V8ArrayData arrayData = (V8ArrayData) data; checkArrayProperties(arrayData); long handle = createTypedArray(runtimePtr, arrayData); released = false; addObjectReference(handle); }
@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 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 testInt8TypedArray_TwinHasSameValues() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 4); V8TypedArray v8Int8Array1 = new V8TypedArray(v8, buffer, V8Value.BYTE, 0, 4); V8TypedArray v8Int8Array2 = (V8TypedArray) v8Int8Array1.twin(); v8Int8Array1.add("0", 7); v8Int8Array1.add("1", 8); assertEquals((byte) 7, v8Int8Array2.get(0)); assertEquals((byte) 8, v8Int8Array2.get(1)); v8Int8Array1.close(); v8Int8Array2.close(); buffer.close(); }
@Test public void testAccess64BitFloatFromTypedArray() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 8); V8TypedArray typedArray = new V8TypedArray(v8, buffer, V8Value.FLOAT_64_ARRAY, 0, 1); typedArray.add("0", 3.14159265359); assertEquals(V8Value.FLOAT_64_ARRAY, typedArray.getType()); assertEquals(3.14159265359, buffer.getDouble(), 0.000000000001); buffer.close(); typedArray.close(); }
@Test(expected = IllegalArgumentException.class) public void testCreateTypedArrayWithIllegalType_V8Object() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 10); try { new V8TypedArray(v8, buffer, V8Value.V8_OBJECT, 0, 10).close(); } finally { buffer.close(); } }
@Test public void testCreateV8Int32ArrayFromJSArrayBuffer() { V8ArrayBuffer buffer = (V8ArrayBuffer) v8.executeScript("" + "var buffer = new ArrayBuffer(8)\n" + "var array = new Int32Array(buffer);\n" + "array[0] = 7; array[1] = 9;\n" + "buffer;"); V8TypedArray array = new V8TypedArray(v8, buffer, V8Value.INTEGER, 0, 2); assertEquals(7, array.get(0)); assertEquals(9, array.get(1)); array.close(); buffer.close(); }
@Test public void testGetTypedArray() { TypedArray int8Array = new TypedArray(v8, new ArrayBuffer(v8, ByteBuffer.allocateDirect(8)), V8Value.INT_8_ARRAY, 0, 8); V8TypedArray v8TypedArray = int8Array.getV8TypedArray(); V8ArrayBuffer v8ArrayBuffer = v8TypedArray.getBuffer(); v8ArrayBuffer.put(0, (byte) 7); v8TypedArray.close(); v8ArrayBuffer.close(); V8TypedArray v8Array = (V8TypedArray) V8ObjectUtils.getV8Result(v8, int8Array); assertEquals((byte) 7, v8Array.get(0)); assertEquals(V8Value.INT_8_ARRAY, v8Array.getType()); v8Array.close(); }
@Test public void testGetArrayBuffer_Int32Array() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 8); V8TypedArray v8Int32Array = new V8TypedArray(v8, buffer, V8Value.INTEGER, 0, 2); V8ArrayBuffer result = v8Int32Array.getBuffer(); assertEquals(result, buffer); result.close(); buffer.close(); v8Int32Array.close(); }
@Test public void testInt8TypedArray_CustomLength() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 8); V8TypedArray v8Int8Array = new V8TypedArray(v8, buffer, V8Value.BYTE, 0, 1); assertEquals(1, v8Int8Array.length()); buffer.close(); v8Int8Array.close(); }
@Test public void testCreateFloat64TypedArray() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 8); V8TypedArray typedArray = new V8TypedArray(v8, buffer, V8Value.FLOAT_64_ARRAY, 0, 1); assertEquals(V8Value.FLOAT_64_ARRAY, typedArray.getType()); buffer.close(); typedArray.close(); }
@Test public void testAccessSignedValueFromUnsignedByte_Greater128() { V8TypedArray array = (V8TypedArray) v8.executeScript("var buf = new ArrayBuffer(8); var bytes = new Uint8ClampedArray(buf); bytes[0] = 240; bytes[1] = 7; bytes"); V8ArrayBuffer arrayBuffer = array.getBuffer(); short result = (short) (arrayBuffer.get() & 0xFF); assertEquals(240, result); array.close(); arrayBuffer.close(); }
@Test public void testInt32TypedArray_CustomOffset() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 8); V8TypedArray v8Int32Array = new V8TypedArray(v8, buffer, V8Value.INTEGER, 4, 1); v8.add("v8Int32Array", v8Int32Array); buffer.putInt(4); buffer.putInt(8); assertEquals(1, v8Int32Array.length()); assertEquals(8, v8Int32Array.getInteger(0)); buffer.close(); v8Int32Array.close(); }
@Test public void testCreateByteTypedArray() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 8); V8TypedArray typedArray = new V8TypedArray(v8, buffer, V8Value.BYTE, 0, 2); typedArray.add("0", 7); typedArray.add("1", 8); assertEquals(7, buffer.get()); assertEquals(8, buffer.get()); buffer.close(); typedArray.close(); }