/** * Executes a JS Script on this runtime and returns the result as a V8Object. * If the result is not a V8Object, then a V8ResultUndefinedException is thrown. * * @param script The script to execute. * * @return The result of the script as a V8Object, or V8ResultUndefinedException if * the result is not a V8Object. */ public V8Object executeObjectScript(final String script) { return this.executeObjectScript(script, null, 0); }
runningStateDcp = runtime.executeObjectScript("(function() {return new " + DEBUG_OBJECT_NAME + ".DebugCommandProcessor(null, true)})()");
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testCreateNestedMapFromV8Object_GetValue() { V8Object object = v8.executeObjectScript("x = { name : { first :'john', last: 'smith'}, age: 7}; x"); Map<String, ? super Object> map = (Map<String, ? super Object>) V8ObjectUtils.getValue(object); assertEquals(2, map.size()); assertEquals(7, map.get("age")); assertEquals("john", ((Map) map.get("name")).get("first")); assertEquals("smith", ((Map) map.get("name")).get("last")); object.close(); }
@Test public void testInvokeCallbackFunctionUsesReciver() { V8Object bar = v8.executeObjectScript("var bar = {}; bar;"); JavaVoidCallback callback = mock(JavaVoidCallback.class); bar.registerJavaMethod(callback, "foo"); doAnswer(constructAnswer(bar, null, 77)).when(callback).invoke(any(V8Object.class), any(V8Array.class)); bar.executeVoidFunction("foo", null); bar.close(); }
@Test public void testVoidMethodCalledFromObjectScript() { ICallback callback = mock(ICallback.class); v8.registerJavaMethod(callback, "voidMethodNoParameters", "foo", new Class<?>[0]); v8.executeObjectScript("foo(); bar={}; bar;").close(); verify(callback).voidMethodNoParameters(); }
@Test public void testDateToString() { V8Object a = v8.executeObjectScript("new Date(2014, 9, 1, 10, 0, 0, 0)"); assertTrue(a.toString().startsWith("Wed Oct 01 2014 10:00:00")); a.close(); }
@Test public void testCreateIntegerMapFromV8Object() { V8Object object = v8.executeObjectScript("x = {a:1, b:2, c:3}; x"); Map<String, ? super Object> map = V8ObjectUtils.toMap(object); assertEquals(3, map.size()); assertEquals(1, map.get("a")); assertEquals(2, map.get("b")); assertEquals(3, map.get("c")); object.close(); }
@SuppressWarnings("unchecked") @Test public void testCreateStringMapFromV8Object_GetValue() { V8Object object = v8.executeObjectScript("x = {a:'foo', b:'bar', c:'baz', d:'boo'}; x"); Map<String, ? super Object> map = (Map<String, ? super Object>) V8ObjectUtils.getValue(object); assertEquals(4, map.size()); assertEquals("foo", map.get("a")); assertEquals("bar", map.get("b")); assertEquals("baz", map.get("c")); assertEquals("boo", map.get("d")); object.close(); }
@SuppressWarnings("unchecked") @Test public void testCreateIntegerMapFromV8Object_GetValue() { V8Object object = v8.executeObjectScript("x = {a:1, b:2, c:3}; x"); Map<String, ? super Object> map = (Map<String, ? super Object>) V8ObjectUtils.getValue(object); assertEquals(3, map.size()); assertEquals(1, map.get("a")); assertEquals(2, map.get("b")); assertEquals(3, map.get("c")); object.close(); }
@Test public void testEqualHash() { v8.executeVoidScript("o = {}"); V8Object o1 = v8.executeObjectScript("o"); V8Object o2 = v8.executeObjectScript("o"); assertEquals(o1.hashCode(), o2.hashCode()); o1.close(); o2.close(); }
@Test public void testIsNull() { V8Object v8Object = v8.executeObjectScript("x = {a : null}; x;"); assertEquals(NULL, v8Object.getType("a")); v8Object.close(); }
@Test public void testToString() { V8Object a = v8.executeObjectScript("x = {a:'b'}; x;"); assertEquals("[object Object]", a.toString()); a.close(); }
@Test public void testToStringInCallback() { V8Object a = v8.executeObjectScript("x = [1, 'test', false]; x;"); v8.registerJavaMethod(this, "runMe", "runMe", new Class<?>[] { Object.class }); v8.executeVoidScript("runMe(x);"); a.close(); }
@Test public void testNotEqualsNull2() { v8.executeVoidScript("a = {};"); V8Object o1 = v8.executeObjectScript("a"); assertNotEquals(null, o1); o1.close(); }
@Test public void testV8ObjectFunctionCallOnJSObject() { ICallback callback = mock(ICallback.class); doReturn(v8.executeObjectScript("x = {first:'bob'}; x")).when(callback).v8ObjectMethodNoParameters(); V8Object v8Object = new V8Object(v8); v8Object.registerJavaMethod(callback, "v8ObjectMethodNoParameters", "foo", new Class<?>[0]); V8Object result = v8Object.executeObjectFunction("foo", null); verify(callback).v8ObjectMethodNoParameters(); assertEquals("bob", result.getString("first")); v8Object.close(); result.close(); }
@Test public void testGetIsDouble() { V8Object object = v8.executeObjectScript("foo = {key: 7.7}"); Object result = object.get("key"); assertTrue(result instanceof Double); assertEquals(7.7, result); object.close(); }
/*** Global Object Prototype Manipulation ***/ private void setupWindowAlias() { v8.close(); v8 = V8.createV8Runtime("window"); v8.executeVoidScript("function Window(){};"); V8Object prototype = v8.executeObjectScript("Window.prototype"); v8.setPrototype(prototype); prototype.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(); }
@Test public void testNestedObjectScript() { V8Object result = v8.executeObjectScript("person = {name : {first : 'john', last:'smith'} }; person;"); V8Object name = result.getObject("name"); assertEquals("john", name.getString("first")); assertEquals("smith", name.getString("last")); result.close(); name.close(); }
@Test public void testHashStable() { V8Object a = v8.executeObjectScript("a = []; a"); int hash1 = a.hashCode(); int hash2 = a.add("1", true).add("2", false).add("3", 123).hashCode(); assertEquals(hash1, hash2); a.close(); }