private static V8ArrayBuffer toV8ArrayBuffer(final V8 v8, final ArrayBuffer arrayBuffer, final Map<Object, V8Value> cache) { if (cache.containsKey(arrayBuffer)) { return (V8ArrayBuffer) cache.get(arrayBuffer); } V8ArrayBuffer result = arrayBuffer.getV8ArrayBuffer(); cache.put(arrayBuffer, result); return result; }
/** * 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(); } }
private static V8ArrayBuffer toV8ArrayBuffer(final V8 v8, final ArrayBuffer arrayBuffer, final Map<Object, V8Value> cache) { if (cache.containsKey(arrayBuffer)) { return (V8ArrayBuffer) cache.get(arrayBuffer); } V8ArrayBuffer result = arrayBuffer.getV8ArrayBuffer(); cache.put(arrayBuffer, result); return result; }
/** * 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(); } }
@Test public void testGetV8ArrayBuffer() { ArrayBuffer arrayBuffer = new ArrayBuffer(v8, ByteBuffer.allocateDirect(8)); V8ArrayBuffer v8ArrayBuffer = arrayBuffer.getV8ArrayBuffer(); assertNotNull(v8ArrayBuffer); v8ArrayBuffer.close(); }
@Test public void testArrayBufferInList() { ArrayBuffer arrayBuffer = new ArrayBuffer(v8, ByteBuffer.allocateDirect(8)); List<Object> list = new ArrayList<Object>(); list.add(arrayBuffer); V8Array array = V8ObjectUtils.toV8Array(v8, list); V8ArrayBuffer v8ArrayBuffer = (V8ArrayBuffer) array.get(0); assertEquals(arrayBuffer.getV8ArrayBuffer().setWeak(), v8ArrayBuffer); v8ArrayBuffer.close(); array.close(); }
@Test public void testByteBufferInMap() { ArrayBuffer arrayBuffer = new ArrayBuffer(v8, ByteBuffer.allocateDirect(8)); HashMap<String, Object> map = new HashMap<String, Object>(); map.put("buffer", arrayBuffer); V8Object object = V8ObjectUtils.toV8Object(v8, map); V8ArrayBuffer v8ArrayBuffer = (V8ArrayBuffer) object.get("buffer"); assertEquals(arrayBuffer.getV8ArrayBuffer().setWeak(), v8ArrayBuffer); v8ArrayBuffer.close(); object.close(); }
@Test public void testGetArrayBuffer() { ArrayBuffer arrayBuffer = new ArrayBuffer(v8, ByteBuffer.allocateDirect(8)); V8ArrayBuffer v8ArrayBuffer = (V8ArrayBuffer) V8ObjectUtils.getV8Result(v8, arrayBuffer); assertEquals(arrayBuffer.getV8ArrayBuffer().setWeak(), v8ArrayBuffer); v8ArrayBuffer.close(); }
@Test public void testArrayBufferAsProperty() { V8Object root = v8.executeObjectScript("var buf = new ArrayBuffer(100);\n" + "var root = { 'items' : buf };\n" + "root;\n"); V8ArrayBuffer result = ((ArrayBuffer) V8ObjectUtils.getValue(root, "items")).getV8ArrayBuffer(); assertEquals(100, result.limit()); root.close(); result.close(); }
@Test public void testArrayTypedArrayValue_ArrayBuffer() { V8Array root = v8.executeArrayScript("var buf = new ArrayBuffer(100);\n" + "var root = [buf];\n" + "root;\n"); V8ArrayBuffer result = ((ArrayBuffer) V8ObjectUtils.getValue(root, 0)).getV8ArrayBuffer(); assertEquals(100, result.limit()); root.close(); result.close(); }
@Test public void testArrayBufferGetValue() { V8ArrayBuffer buf = (V8ArrayBuffer) v8.executeScript("var buf = new ArrayBuffer(100);\n" + "buf;\n"); V8ArrayBuffer result = ((ArrayBuffer) V8ObjectUtils.getValue(buf)).getV8ArrayBuffer(); assertEquals(100, result.limit()); buf.close(); result.close(); }