@SuppressWarnings("unchecked") private List<String> filter(final V8Array args, final int i) { if (i < args.length()) { Object value = V8ObjectUtils.getValue(args, i); if (value == V8.getUndefined()) { return Collections.emptyList(); } List<String> filter = new ArrayList<>(); if (value instanceof Collection) { filter.addAll((Collection<? extends String>) value); } else { filter.add(value.toString()); } return filter; } return Collections.emptyList(); } }
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 Object getValue(final Object value, final int valueType, final V8Map<Object> cache, final TypeAdapter adapter) { Object adapterResult = adapter.adapt(valueType, value); if (TypeAdapter.DEFAULT != adapterResult) { return adapterResult; } switch (valueType) { case V8Value.INTEGER: case V8Value.DOUBLE: case V8Value.BOOLEAN: case V8Value.STRING: return value; case V8Value.V8_FUNCTION: return IGNORE; case V8Value.V8_ARRAY_BUFFER: return new ArrayBuffer((V8ArrayBuffer) value); case V8Value.V8_TYPED_ARRAY: return new TypedArray((V8TypedArray) value); case V8Value.V8_ARRAY: return toList((V8Array) value, cache, adapter); case V8Value.V8_OBJECT: return toMap((V8Object) value, cache, adapter); case V8Value.NULL: return null; case V8Value.UNDEFINED: return V8.getUndefined(); default: throw new IllegalStateException("Cannot convert type " + V8Value.getStringRepresentation(valueType)); } }
/** * 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; }
/** * Pushes a String value to the next available spot in the Array. In * particular, this[length] = value; * * @param value The value to push to the array. * * @return The receiver. */ public V8Array push(final String value) { v8.checkThread(); checkReleased(); if (value == null) { v8.addArrayNullItem(v8.getV8RuntimePtr(), getHandle()); } else if (value.equals(V8.getUndefined())) { v8.addArrayUndefinedItem(v8.getV8RuntimePtr(), getHandle()); } else { v8.addArrayStringItem(v8.getV8RuntimePtr(), getHandle(), value); } return this; }
private Object getArrayItem(final V8Array array, final int index) { try { int type = array.getType(index); switch (type) { case INTEGER: return array.getInteger(index); case DOUBLE: return array.getDouble(index); case BOOLEAN: return array.getBoolean(index); case STRING: return array.getString(index); case V8_ARRAY: case V8_TYPED_ARRAY: return array.getArray(index); case V8_OBJECT: return array.getObject(index); case V8_FUNCTION: return array.getObject(index); case V8_ARRAY_BUFFER: return array.get(index); case UNDEFINED: return V8.getUndefined(); } } catch (V8ResultUndefined e) { // do nothing } return null; }
/** * Adds a key value pair to the receiver where the value is a V8Value. * * @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 V8Value value) { v8.checkThread(); checkReleased(); v8.checkRuntime(value); if (value == null) { v8.addNull(v8.getV8RuntimePtr(), objectHandle, key); } else if (value.equals(V8.getUndefined())) { v8.addUndefined(v8.getV8RuntimePtr(), objectHandle, key); } else { v8.addObject(v8.getV8RuntimePtr(), objectHandle, key, value.getHandle()); } return this; }
/** * Pushes a V8Value to the next available spot in the Array. In * particular, this[length] = value; * * @param value The value to push to the array. * * @return The receiver. */ public V8Array push(final V8Value value) { v8.checkThread(); checkReleased(); v8.checkRuntime(value); if (value == null) { v8.addArrayNullItem(v8.getV8RuntimePtr(), getHandle()); } else if (value.equals(V8.getUndefined())) { v8.addArrayUndefinedItem(v8.getV8RuntimePtr(), getHandle()); } else { v8.addArrayObjectItem(v8.getV8RuntimePtr(), getHandle(), value.getHandle()); } return this; }
} else if (value.equals(V8.getUndefined())) { v8.addArrayUndefinedItem(v8.getV8RuntimePtr(), getHandle()); } else {
@Override public Object adapt(final Object value) { return V8.getUndefined(); } });
@Test public void testGetV8ResultUndefined() { Object result = V8ObjectUtils.getV8Result(v8, V8.getUndefined()); assertEquals(V8.getUndefined(), result); }
@Test public void testV8ObjectMethodReturnsUndefined() { ICallback callback = mock(ICallback.class); doReturn(V8.getUndefined()).when(callback).v8ObjectMethodNoParameters(); v8.registerJavaMethod(callback, "v8ObjectMethodNoParameters", "foo", new Class<?>[0]); boolean result = v8.executeBooleanScript("typeof foo() === 'undefined'"); assertTrue(result); }
@Test public void testGenericPushUndefined() { V8Array array = new V8Array(v8); array.push((Object) V8.getUndefined()); assertEquals(V8.getUndefined(), array.get(0)); array.close(); }
@Test public void testFunctionCallWithUndefinedReturn() { v8.executeVoidScript("function getFoo() {return undefined;}"); Object result = v8.executeFunction("getFoo", null); assertEquals(V8.getUndefined(), result); }
@Test public void testFunctionCallWithNoReturn() { v8.executeVoidScript("function getAge(first, last, age) {}"); Object result = v8.executeFunction("getAge", null); assertEquals(V8.getUndefined(), result); }
@Test public void testExecuteJSFunction_undefined() { v8.executeVoidScript("function test(p1) { if (!p1) { return 'passed';} }"); String result = (String) v8.executeJSFunction("test", V8.getUndefined()); assertEquals("passed", result); }
@Test public void testGetIsUndefined() { V8Object object = v8.executeObjectScript("foo = {}"); Object result = object.get("key"); assertEquals(V8.getUndefined(), result); object.close(); }
@Test public void testGetIsUndefined() { V8Array array = v8.executeArrayScript("foo = []"); Object result = array.get(0); assertEquals(V8.getUndefined(), result); array.close(); }
@Test public void testAddUndefinedIsUndefined() { V8Object object = new V8Object(v8); object.add("foo", V8.getUndefined()); assertEquals(UNDEFINED, object.getType("foo")); object.close(); }
@Test public void testTwinIsUndefined() { V8Object v8Object = (V8Object) V8.getUndefined(); V8Value twin = v8Object.twin(); assertTrue(twin.isUndefined()); v8Object.close(); twin.close(); }