void checkRuntime(final V8Value value) { if ((value == null) || value.isUndefined()) { return; } V8 runtime = value.getRuntime(); if ((runtime == null) || runtime.isReleased() || (runtime != this)) { throw new Error("Invalid target runtime"); } }
/** * Performs a JS === on the parameter and the receiver. * * @param that The Object to compare this object against. * @return Returns true iff this === that */ public boolean strictEquals(final Object that) { v8.checkThread(); checkReleased(); if (that == this) { return true; } if (that == null) { return false; } if (!(that instanceof V8Value)) { return false; } if (isUndefined() && ((V8Value) that).isUndefined()) { return true; } if (((V8Value) that).isUndefined()) { return false; } return v8.strictEquals(v8.getV8RuntimePtr(), getHandle(), ((V8Value) that).getHandle()); }
/** * Performs a JS == on the parameter and the receiver. * * @param that The Object to compare this object against. * @return Returns true iff this == that */ public boolean jsEquals(final Object that) { v8.checkThread(); checkReleased(); if (that == this) { return true; } if (that == null) { return false; } if (!(that instanceof V8Value)) { return false; } if (isUndefined() && ((V8Value) that).isUndefined()) { return true; } if (((V8Value) that).isUndefined()) { return false; } return v8.equals(v8.getV8RuntimePtr(), getHandle(), ((V8Value) that).getHandle()); }
/** * Returns the 'type' of this V8Value. The available types are defined * as constants in {@link V8Value}. Only types that inherit from * {@link V8Value} can be returned here. * * @return The 'type of this V8Value. */ public int getV8Type() { if (isUndefined()) { return UNDEFINED; } v8.checkThread(); v8.checkReleased(); return v8.getType(v8.getV8RuntimePtr(), objectHandle); }
/** * Creates a new Java object pointing at the same V8 Value * as this. If the value is mutated (by adding new members or * changing existing ones) then both the original and twin * will be updated. Twins are .equal and .strict equals, but * not == in Java. * * Twins must be closed separately since they have their own * native resources. * * @return A new Java object pointing at the same V8 Value * as this. */ public V8Value twin() { if (isUndefined()) { return this; } v8.checkThread(); v8.checkReleased(); V8Value twin = createTwin(); v8.createTwin(this, twin); return twin; }
void checkRuntime(final V8Value value) { if ((value == null) || value.isUndefined()) { return; } V8 runtime = value.getRuntime(); if ((runtime == null) || runtime.isReleased() || (runtime != this)) { throw new Error("Invalid target runtime"); } }
/** * Performs a JS === on the parameter and the receiver. * * @param that The Object to compare this object against. * @return Returns true iff this === that */ public boolean strictEquals(final Object that) { v8.checkThread(); checkReleased(); if (that == this) { return true; } if (that == null) { return false; } if (!(that instanceof V8Value)) { return false; } if (isUndefined() && ((V8Value) that).isUndefined()) { return true; } if (((V8Value) that).isUndefined()) { return false; } return v8.strictEquals(v8.getV8RuntimePtr(), getHandle(), ((V8Value) that).getHandle()); }
/** * Performs a JS == on the parameter and the receiver. * * @param that The Object to compare this object against. * @return Returns true iff this == that */ public boolean jsEquals(final Object that) { v8.checkThread(); checkReleased(); if (that == this) { return true; } if (that == null) { return false; } if (!(that instanceof V8Value)) { return false; } if (isUndefined() && ((V8Value) that).isUndefined()) { return true; } if (((V8Value) that).isUndefined()) { return false; } return v8.equals(v8.getV8RuntimePtr(), getHandle(), ((V8Value) that).getHandle()); }
/** * Returns the 'type' of this V8Value. The available types are defined * as constants in {@link V8Value}. Only types that inherit from * {@link V8Value} can be returned here. * * @return The 'type of this V8Value. */ public int getV8Type() { if (isUndefined()) { return UNDEFINED; } v8.checkThread(); v8.checkReleased(); return v8.getType(v8.getV8RuntimePtr(), objectHandle); }
@Test public void testCreateListWithUndefinedFromV8Array() { V8Array array = v8.executeArrayScript("x = [1,2,3]; x[9] = 10; x"); List<? super Object> list = V8ObjectUtils.toList(array); assertEquals(10, list.size()); assertEquals(1, list.get(0)); assertEquals(2, list.get(1)); assertEquals(3, list.get(2)); assertTrue(((V8Value) list.get(3)).isUndefined()); assertTrue(((V8Value) list.get(4)).isUndefined()); assertTrue(((V8Value) list.get(5)).isUndefined()); assertTrue(((V8Value) list.get(6)).isUndefined()); assertTrue(((V8Value) list.get(7)).isUndefined()); assertTrue(((V8Value) list.get(8)).isUndefined()); assertEquals(10, list.get(9)); array.close(); }
/** * Creates a new Java object pointing at the same V8 Value * as this. If the value is mutated (by adding new members or * changing existing ones) then both the original and twin * will be updated. Twins are .equal and .strict equals, but * not == in Java. * * Twins must be closed separately since they have their own * native resources. * * @return A new Java object pointing at the same V8 Value * as this. */ public V8Value twin() { if (isUndefined()) { return this; } v8.checkThread(); v8.checkReleased(); V8Value twin = createTwin(); v8.createTwin(this, twin); return twin; }
@SuppressWarnings("unchecked") @Test public void testCreateListWithUndefinedFromV8Array_GetValue() { V8Array array = v8.executeArrayScript("x = [1,2,3]; x[9] = 10; x"); List<? super Object> list = (List<? super Object>) V8ObjectUtils.getValue(array); assertEquals(10, list.size()); assertEquals(1, list.get(0)); assertEquals(2, list.get(1)); assertEquals(3, list.get(2)); assertTrue(((V8Value) list.get(3)).isUndefined()); assertTrue(((V8Value) list.get(4)).isUndefined()); assertTrue(((V8Value) list.get(5)).isUndefined()); assertTrue(((V8Value) list.get(6)).isUndefined()); assertTrue(((V8Value) list.get(7)).isUndefined()); assertTrue(((V8Value) list.get(8)).isUndefined()); assertEquals(10, list.get(9)); array.close(); }
@Test public void testAnyScriptReturnedUndefined() { V8Value result = (V8Value) v8.executeScript("undefined;"); assertTrue(result.isUndefined()); }
/*** Unknown Script ***/ @Test public void testAnyScriptReturnedNothing() { V8Value result = (V8Value) v8.executeScript(""); assertTrue(result.isUndefined()); }
@Test public void testObjectUndefinedTypeAdapter_Map_GetValue() { V8Object object = v8.executeObjectScript("x = {b:{a:{}}}; x"); @SuppressWarnings("resource") V8Value result = (V8Value) V8ObjectUtils.getValue(object, new SingleTypeAdapter(V8Value.V8_OBJECT) { @Override public Object adapt(final Object value) { return V8.getUndefined(); } }); assertTrue(result.isUndefined()); object.close(); }
@Test public void testTwinIsUndefined() { V8Object v8Object = (V8Object) V8.getUndefined(); V8Value twin = v8Object.twin(); assertTrue(twin.isUndefined()); v8Object.close(); twin.close(); }
/** * Performs a JS == on the parameter and the receiver. * * @param that The Object to compare this object against. * @return Returns true iff this == that */ public boolean jsEquals(final Object that) { v8.checkThread(); checkReleased(); if (that == this) { return true; } if (that == null) { return false; } if (!(that instanceof V8Value)) { return false; } if (isUndefined() && ((V8Value) that).isUndefined()) { return true; } if (((V8Value) that).isUndefined()) { return false; } return v8.equals(v8.getV8RuntimePtr(), getHandle(), ((V8Value) that).getHandle()); }
/** * Performs a JS === on the parameter and the receiver. * * @param that The Object to compare this object against. * @return Returns true iff this === that */ public boolean strictEquals(final Object that) { v8.checkThread(); checkReleased(); if (that == this) { return true; } if (that == null) { return false; } if (!(that instanceof V8Value)) { return false; } if (isUndefined() && ((V8Value) that).isUndefined()) { return true; } if (((V8Value) that).isUndefined()) { return false; } return v8.strictEquals(v8.getV8RuntimePtr(), getHandle(), ((V8Value) that).getHandle()); }
/** * Creates a new Java object pointing at the same V8 Value * as this. If the value is mutated (by adding new members or * changing existing ones) then both the original and twin * will be updated. Twins are .equal and .strict equals, but * not == in Java. * * Twins must be released separately since they have their own * native resources. * * @return A new Java object pointing at the same V8 Value * as this. */ public V8Value twin() { if (isUndefined()) { return this; } v8.checkThread(); v8.checkReleased(); V8Value twin = createTwin(); v8.createTwin(this, twin); return twin; }
/** * Creates a new Java object pointing at the same V8 Value * as this. If the value is mutated (by adding new members or * changing existing ones) then both the original and twin * will be updated. Twins are .equal and .strict equals, but * not == in Java. * * Twins must be released separately since they have their own * native resources. * * @return A new Java object pointing at the same V8 Value * as this. */ public V8Value twin() { if (isUndefined()) { return this; } v8.checkThread(); v8.checkReleased(); V8Value twin = createTwin(); v8.createTwin(this, twin); return twin; }