Skip to content

Commit

Permalink
Add a generic push(Object object) to V8Array
Browse files Browse the repository at this point in the history
To allow V8Arrays to be constructed more easily, add a generic
push(Object object) method to V8Array. Push will inspect the type
of object being used, and properly add that object to the underlying
V8Array.

If a non-compatible Object is passed to push, then an
IllegalArgumentException is thrown.

This was originally contributed by @TonyRice in PR #259.

Change-Id: I193bf99a29283899f7315a28c1fe989edd87eb1e
  • Loading branch information
TonyRice authored and irbull committed May 25, 2017
1 parent c8efb4a commit e18164a
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/main/java/com/eclipsesource/v8/V8Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,46 @@ public V8Array push(final V8Value value) {
return this;
}

/**
* Pushes a Object 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 Object value) {
v8.checkThread();
checkReleased();
if (value instanceof V8Value) {
v8.checkRuntime((V8Value) value);
}
if (value == null) {
v8.addArrayNullItem(v8.getV8RuntimePtr(), getHandle());
} else if (value.equals(V8.getUndefined())) {
v8.addArrayUndefinedItem(v8.getV8RuntimePtr(), getHandle());
} else {
if (value instanceof Double) {
v8.addArrayDoubleItem(v8.getV8RuntimePtr(), getHandle(), (Double) value);
} else if (value instanceof Integer) {
v8.addArrayIntItem(v8.getV8RuntimePtr(), getHandle(), (Integer) value);
} else if (value instanceof Float) {
v8.addArrayDoubleItem(v8.getV8RuntimePtr(), getHandle(), ((Float) value).doubleValue());
} else if (value instanceof Number) {
v8.addArrayDoubleItem(v8.getV8RuntimePtr(), getHandle(), ((Number) value).doubleValue());
} else if (value instanceof Boolean) {
v8.addArrayBooleanItem(v8.getV8RuntimePtr(), getHandle(), (Boolean) value);
} else if (value instanceof String) {
v8.addArrayStringItem(v8.getV8RuntimePtr(), getHandle(), (String) value);
} else if (value instanceof V8Value) {
v8.addArrayObjectItem(v8.getV8RuntimePtr(), getHandle(), ((V8Value) value).getHandle());
} else {
throw new IllegalArgumentException();
}
}
return this;
}

/**
* Pushes null to the next available spot in the Array. In
* particular, this[length] = null;
Expand Down
92 changes: 92 additions & 0 deletions src/test/java/com/eclipsesource/v8/V8ArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,98 @@ public void testGetNullInArray() {
array.release();
}

@Test
public void testGenericPushUndefined() {
V8Array array = new V8Array(v8).push((Object) V8.getUndefined());

assertEquals(V8.getUndefined(), array.get(0));
array.release();
}

@Test(expected = IllegalArgumentException.class)
public void testGenericPush_IllegalArgument() {
V8Array array = new V8Array(v8);
try {
array.push(new Object());
} finally {
array.release();
}
}

@Test
public void testGenericPushNull() {
V8Array array = new V8Array(v8).push((Object) null);

assertNull(array.get(0));
array.release();
}

@Test
public void testGenericPushInteger() {
V8Array array = new V8Array(v8).push(new Integer(7));

assertEquals(7, array.get(0));
array.release();
}

@Test
public void testGenericPushDouble() {
V8Array array = new V8Array(v8).push(new Double(7.7777));

assertEquals(7.7777, (Double) array.get(0), 0.0001);
array.release();
}

@Test
public void testGenericPushBoolean() {
V8Array array = new V8Array(v8).push(new Boolean(true));

assertTrue((Boolean) array.get(0));
array.release();
}

@Test
public void testGenericPushObject() {
V8Object object = new V8Object(v8);
V8Array array = new V8Array(v8).push(object);

V8Value result = (V8Value) array.get(0);
assertEquals(object, result);
array.release();
object.release();
result.release();
}

@Test(expected = Error.class)
public void testGenericPushObject_WrongRuntime() {
V8 newV8 = V8.createV8Runtime();
V8Object object = new V8Object(newV8);
V8Array array = new V8Array(v8);
try {
array.push(object);
} finally {
array.release();
object.release();
newV8.release();
}
}

@Test
public void testGenericPushFloat() {
V8Array array = new V8Array(v8).push(new Float(3.14));

assertEquals(3.14, (Double) array.get(0), 0.0001);
array.release();
}

@Test
public void testGenericPushString() {
V8Array array = new V8Array(v8).push((Object) "foo");

assertEquals("foo", array.get(0));
array.release();
}

@Test
public void testAddNullAsObject() {
V8Array array = new V8Array(v8).push((V8Object) null);
Expand Down

0 comments on commit e18164a

Please sign in to comment.