/** * Adds a key value pair to the receiver where the value is a String. * * @param key The key to associate the value with. * @param value The value to add. * * @return The receiver. */ public V8Object add(final String key, final String value) { v8.checkThread(); checkReleased(); if (value == null) { v8.addNull(v8.getV8RuntimePtr(), objectHandle, key); } else if (value.equals(V8.getUndefined())) { v8.addUndefined(v8.getV8RuntimePtr(), objectHandle, key); } else { v8.add(v8.getV8RuntimePtr(), objectHandle, key, value); } return this; }
})); v8.add("j2v8", j2v8);
/** * Adds a key value pair to the receiver where the value is an integer. * * @param key The key to associate the value with. * @param value The value to add. * * @return The receiver. */ public V8Object add(final String key, final int value) { v8.checkThread(); checkReleased(); v8.add(v8.getV8RuntimePtr(), objectHandle, key, value); return this; }
/** * Adds a key value pair to the receiver where the value is a boolean. * * @param key The key to associate the value with. * @param value The value to add. * * @return The receiver. */ public V8Object add(final String key, final boolean value) { v8.checkThread(); checkReleased(); v8.add(v8.getV8RuntimePtr(), objectHandle, key, value); return this; }
/** * Adds a key value pair to the receiver where the value is a double. * * @param key The key to associate the value with. * @param value The value to add. * * @return The receiver. */ public V8Object add(final String key, final double value) { v8.checkThread(); checkReleased(); v8.add(v8.getV8RuntimePtr(), objectHandle, key, value); return this; }
private static void assets(Class<?> loader, final V8 v8) { V8Object assets = new V8Object(v8); v8.add("assets", assets); assets.registerJavaMethod((JavaCallback) (receiver, args) -> { try { return V8Engine.readFile(loader, args.get(0).toString()); } catch (IOException ex) { // we can't fire exceptions from Java :S return V8.getUndefined(); } }, "readFile"); assets.registerJavaMethod( (JavaCallback) (receiver, args) -> V8Engine.exists(loader, args.get(0).toString()), "exists"); assets.registerJavaMethod((JavaCallback) (receiver, args) -> { try { return load(loader, v8, args.get(0).toString()); } catch (Exception ex) { // we can't fire exceptions from Java :S return V8.getUndefined(); } }, "load"); }
private static void console(V8 v8, final String logname) { V8Object console = new V8Object(v8); Logger log = LoggerFactory.getLogger(logname); v8.add("console", console); console.registerJavaMethod(console(v8, log::info), "log"); console.registerJavaMethod(console(v8, log::info), "info"); console.registerJavaMethod(console(v8, log::error), "error"); console.registerJavaMethod(console(v8, log::debug), "debug"); console.registerJavaMethod(console(v8, log::warn), "warn"); }
@Test public void testDoesNotContainsKey() { v8.add("foo", true); boolean result = v8.contains("bar"); assertFalse(result); }
/*** Null ***/ @Test public void testStringIsNull() { v8.add("nullString", (V8Object) null); assertNull(v8.getString("nullString")); }
private int registerAndRelease(final String name, final List<? extends Object> list) { V8Array array = V8ObjectUtils.toV8Array(v8, list); v8.add(name, array); int size = array.length(); array.close(); return size; }
@Test public void testGetInt8TypedArray() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 4); v8.add("buf", buffer); V8Array array = (V8Array) v8.executeScript("new Int8Array(buf);"); assertTrue(array instanceof V8TypedArray); array.close(); buffer.close(); }
/*** Add Object ***/ @Test public void testAddObject() { V8Object v8Object = new V8Object(v8); v8.add("foo", v8Object); V8Object result = v8.executeObjectScript("foo"); assertNotNull(result); result.close(); v8Object.close(); }
/*** Add Array ***/ @Test public void testAddArray() { V8Array array = new V8Array(v8); v8.add("foo", array); V8Array result = v8.executeArrayScript("foo"); assertNotNull(result); array.close(); result.close(); }
@SuppressWarnings("unchecked") @Test public void testCloneV8Array_GetValue() { V8Array list = v8.executeArrayScript("var l = [{first:'ian', last:'bull'}, {first:'sadie', last:'bull'}]; l;"); V8Array v8Object = V8ObjectUtils.toV8Array(v8, (List<? extends Object>) V8ObjectUtils.getValue(list)); v8.add("l2", v8Object); v8.executeBooleanScript("JSON.stringify(l) === JSON.stringify(l2);"); list.close(); v8Object.close(); }
@Test public void testInt32TypedArray_CustomLength() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 8); V8TypedArray v8Int32Array = new V8TypedArray(v8, buffer, V8Value.INTEGER, 0, 1); v8.add("v8Int32Array", v8Int32Array); assertEquals(1, v8Int32Array.length()); buffer.close(); v8Int32Array.close(); }
@Test public void testAddObjectToObject() { V8Object nested = new V8Object(v8); nested.add("foo", "bar"); V8Object v8Object = new V8Object(v8); v8Object.add("nested", nested); v8.add("foo", v8Object); String result = v8.executeStringScript("foo.nested.foo"); assertEquals("bar", result); v8Object.close(); nested.close(); }
@Test public void testGetTypedRangeArrayValue_FromBackingStore() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 100); v8.add("buf", buffer); v8.executeVoidScript("var ints = new Int32Array(buf); for(var i = 0; i < 25; i++) {ints[i] = i;};"); assertEquals(25, buffer.intLimit()); for (int i = 0; i < buffer.intLimit(); i++) { assertEquals(i, buffer.getInt(i * 4)); } buffer.close(); }