/** * Returns the version of Node.js that is runtime is built against. * This uses process.versions.node to get the version. * * @return The version of Node.js. */ public String getNodeVersion() { if (nodeVersion != null) { return nodeVersion; } V8Object process = null; V8Object versions = null; try { process = v8.getObject(PROCESS); versions = process.getObject(VERSIONS); nodeVersion = versions.getString(NODE); } finally { safeRelease(process); safeRelease(versions); } return nodeVersion; }
public String execute(final String path, final Object... args) throws Exception { V8Function fn = register(v8, (V8Function) load(path)); V8Array arguments = array(Arrays.asList(args)); Object value = null; try { value = register(v8, fn.call(v8, arguments)); if (value instanceof String) { return value.toString(); } List<AssetProblem> problems = problems(value); if (problems.size() > 0) { throw new AssetException(id, problems); } return ((V8Object) value).getString("output"); } finally { releaseNow(value); releaseNow(arguments); releaseNow(fn); } }
@Override public Object answer(final InvocationOnMock invocation) { Object[] args = invocation.getArguments(); V8Object parameter = ((V8Object) args[0]); V8Object result = new V8Object(v8); result.add("first", parameter.getString("last")); result.add("last", parameter.getString("first")); return result; } }).when(callback).v8ObjectMethodWithObjectParameter(any(V8Object.class));
@Test(expected = UnsupportedOperationException.class) public void testGetStringUndefined() { V8Object undefined = v8.getObject("object"); undefined.getString("foo"); }
@Override public Object answer(final InvocationOnMock invocation) { Object[] args = invocation.getArguments(); assertEquals(1, args.length); assertEquals("john", ((V8Object) args[0]).getString("first")); assertEquals("smith", ((V8Object) args[0]).getString("last")); assertEquals(7, ((V8Object) args[0]).getInteger("age")); return null; } }).when(callback).voidMethodWithObjectParameter(any(V8Object.class));
@Test public void testGetV8ResultObject() { Map<String, Object> map = new HashMap<String, Object>(); map.put("foo", "bar"); Object result = V8ObjectUtils.getV8Result(v8, map); assertTrue(result instanceof V8Object); assertEquals("bar", ((V8Object) result).getString("foo")); ((V8Object) result).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 testObjectScriptWithName() { V8Object result = v8.executeObjectScript("foo = {hello:'world'}; foo;", "name", 6); assertEquals("world", result.getString("hello")); result.close(); }
@Test public void testV8ObjectMethodCalledFromScriptWithResult() { ICallback callback = mock(ICallback.class); V8Object object = new V8Object(v8); object.add("name", "john"); doReturn(object).when(callback).v8ObjectMethodNoParameters(); v8.registerJavaMethod(callback, "v8ObjectMethodNoParameters", "foo", new Class<?>[0]); V8Object result = v8.executeObjectScript("foo();"); assertEquals("john", result.getString("name")); result.close(); }
@Test public void testAnyScriptReturnsV8Object() { V8Object result = (V8Object) v8.executeScript("foo = {hello:'world'}; foo;"); assertEquals("world", result.getString("hello")); result.close(); }
@Test public void testAnyScriptWithName() { V8Object result = (V8Object) v8.executeScript("foo = {hello:'world'}; foo;", "name", 6); assertEquals("world", result.getString("hello")); result.close(); }
@Test(expected = V8ResultUndefined.class) public void testGetStringNotFound() { V8Object v8Object = new V8Object(v8); v8Object.add("hello", "string"); try { v8Object.getString("goodbye"); } finally { v8Object.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 testPushMap() { V8Array array = new V8Array(v8); Map<String, String> map = new HashMap<String, String>(); map.put("foo", "bar"); V8ObjectUtils.pushValue(v8, array, map); V8Object result = array.getObject(0); assertEquals("bar", result.getString("foo")); result.close(); array.close(); }
@Test public void testV8ObjectFunctionCallOnJSArray() { ICallback callback = mock(ICallback.class); doReturn(v8.executeObjectScript("x = {first:'bob'}; x")).when(callback).v8ObjectMethodNoParameters(); V8Array v8Array = new V8Array(v8); v8Array.registerJavaMethod(callback, "v8ObjectMethodNoParameters", "foo", new Class<?>[0]); V8Object result = v8Array.executeObjectFunction("foo", null); verify(callback).v8ObjectMethodNoParameters(); assertEquals("bob", result.getString("first")); v8Array.close(); result.close(); }
@Test public void testGetStringV8Object() { v8.executeVoidScript("foo = {bar: 'string'}"); V8Object foo = v8.getObject("foo"); assertEquals("string", foo.getString("bar")); foo.close(); }
@Test public void testObjectFunctionCallNullParameters() { v8.executeVoidScript("function foo() {return {a:'b'};}"); V8Object result = v8.executeObjectFunction("foo", null); assertEquals("b", result.getString("a")); result.close(); }
@Test public void testArrayGetObjectChangeValue() { V8Array array = v8.executeArrayScript("foo = []; foo"); v8.executeVoidScript("foo[0] = {foo:'bar'}"); V8Object obj = array.getObject(0); assertEquals("bar", obj.getString("foo")); array.close(); obj.close(); }
/**** Get Object ****/ @Test public void testArrayGetObject() { V8Array array = v8.executeArrayScript("[{name : 'joe', age : 38 }];"); V8Object object = array.getObject(0); assertEquals("joe", object.getString("name")); assertEquals(38, object.getInteger("age")); array.close(); object.close(); }