private static void console(V8 v8, final String logname) { V8Object console = new V8Object(v8); Logger log = LoggerFactory.getLogger(logname); v8.add("console", console); console.registerJavaMethod(console(v8, log::info), "log"); console.registerJavaMethod(console(v8, log::info), "info"); console.registerJavaMethod(console(v8, log::error), "error"); console.registerJavaMethod(console(v8, log::debug), "debug"); console.registerJavaMethod(console(v8, log::warn), "warn"); }
V8Object sourceLocation = v8Object.executeObjectFunction(SOURCE_LOCATION, null); FunctionMirror function = getFunction(); String functionScriptName = function.getScriptName(); try { String scriptName = null; V8Object scriptObject = (V8Object) sourceLocation.get(SCRIPT); if (scriptObject != null) { scriptName = scriptObject.getString(NAME); scriptObject.close(); sourceLocation.getInteger(POSITION), sourceLocation.getInteger(LINE), sourceLocation.getInteger(COLUMN), sourceLocation.getString(SOURCE_TEXT)); } finally { function.close(); sourceLocation.close();
try { params.push(false); stoppedStateDcp = execState.executeObjectFunction("debugCommandProcessor", params); } finally { params.close(); int breakId = execState.getInteger("break_id"); V8Array breakpointsHit = eventData.getArray("break_points_hit_"); V8Object event = null; params.push(breakId); params.push(breakpointsHit); event = debugObject.executeObjectFunction(MAKE_BREAK_EVENT, params); String json = event.executeStringFunction("toJSONProtocol", null); if (traceCommunication) { System.out.println("Sending event (Break):\n" + json); breakpointsHit.close(); if (event != null) { event.close(); while (isConnected() && !stoppedStateDcp.executeBooleanFunction("isRunning", null)) { try { processRequests(10); stoppedStateDcp.close(); stoppedStateDcp = null;
/** * Returns the name of the Script associated with * this FunctionMirror. * * @return The name of the script. */ public String getScriptName() { V8Object script = v8Object.executeObjectFunction(SCRIPT, null); try { return script.executeStringFunction(NAME, null); } finally { script.close(); } }
/** * Returns the value of this property. * * @return The value of this property. */ public Mirror getValue() { V8Object mirror = v8Object.executeObjectFunction("value", null); try { return createMirror(mirror); } finally { mirror.close(); } }
@Test public void testGetKeysOnObject() { V8Object v8Object = new V8Object(v8); v8Object.add("integer", 1).add("double", 1.1).add("boolean", true) .add("string", "hello, world!"); String[] keys = v8Object.getKeys(); assertEquals(4, keys.length); V8Test.arrayContains(keys, "integer", "double", "boolean", "string"); 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(); }
@Override public void release() { if ((v8Object != null) && !v8Object.isReleased()) { v8Object.release(); v8Object = null; } }
private void sendCompileEvent(final V8Object eventData) throws IOException { if (!isConnected()) { return; } //send event to debugger int type = eventData.getInteger("type_"); V8Object script = eventData.getObject("script_"); V8Object event = null; V8Array params = new V8Array(runtime); try { params.push(script); params.push(type); event = debugObject.executeObjectFunction(MAKE_COMPILE_EVENT, params); String json = event.executeStringFunction("toJSONProtocol", null); if (traceCommunication) { System.out.println("Sending event (CompileEvent):\n" + json.substring(0, Math.min(json.length(), 1000))); } if (json.length() > 0) { sendJson(json); } } finally { params.close(); script.close(); if (event != null) { event.close(); } } }
@Test public void testVoidFunctionCallOnJSObject() { ICallback callback = mock(ICallback.class); V8Object v8Object = new V8Object(v8); v8Object.registerJavaMethod(callback, "voidMethodNoParameters", "foo", new Class<?>[0]); v8Object.executeVoidFunction("foo", null); verify(callback).voidMethodNoParameters(); v8Object.close(); }
@Test public void testAddUndefined() { V8Object v8Object = new V8Object(v8); v8Object.addUndefined("foo"); assertEquals("foo", v8Object.getKeys()[0]); assertEquals(UNDEFINED, v8Object.getType("foo")); v8Object.close(); }
@Test public void testStringFunctionCallOnJSObject() { ICallback callback = mock(ICallback.class); doReturn("mystring").when(callback).stringMethodNoParameters(); V8Object v8Object = new V8Object(v8); v8Object.registerJavaMethod(callback, "stringMethodNoParameters", "foo", new Class<?>[0]); String result = v8Object.executeStringFunction("foo", null); verify(callback).stringMethodNoParameters(); assertEquals("mystring", result); v8Object.close(); }
/** * Sets a condition to be evaluated before determining if * the breakpoint event should be fired. * * @param condition A JavaScript condition to be evaluated. */ public void setCondition(final String condition) { V8Array parameters = new V8Array(v8Object.getRuntime()); parameters.push(condition); try { v8Object.executeVoidFunction(SET_CONDITION, parameters); } finally { parameters.close(); } }
private void setupBreakpointHandler() { BreakpointHandler handler = new BreakpointHandler(); debugObject.registerJavaMethod(handler, DEBUG_BREAK_HANDLER); V8Function debugHandler = null; V8Array parameters = null; try { debugHandler = (V8Function) debugObject.getObject(DEBUG_BREAK_HANDLER); parameters = new V8Array(runtime); parameters.push(debugHandler); debugObject.executeFunction(SET_LISTENER, parameters); } finally { if ((debugHandler != null) && !debugHandler.isReleased()) { debugHandler.close(); } if ((parameters != null) && !parameters.isReleased()) { parameters.close(); } } }
@SuppressWarnings("unchecked") private static Map<String, ? super Object> toMap(final V8Object v8Object, final V8Map<Object> cache, final TypeAdapter adapter) { if (v8Object == null) { return Collections.emptyMap(); } if (cache.containsKey(v8Object)) { return (Map<String, ? super Object>) cache.get(v8Object); } Map<String, ? super Object> result = new V8PropertyMap<Object>(); cache.put(v8Object, result); String[] keys = v8Object.getKeys(); for (String key : keys) { Object object = null; int type = V8Value.UNDEFINED; try { object = v8Object.get(key); type = v8Object.getType(key); Object value = getValue(object, type, cache, adapter); if (value != IGNORE) { result.put(key, value); } } finally { if (object instanceof Releasable) { ((Releasable) object).release(); } } } return result; }
@Test public void testGetKeysDoesNotIncludePrototypeKeys() { v8.executeVoidScript("function Mammal(){}; Mammal.prototype.breathe=function(){return 'breathe';};"); V8Object mammal = v8.executeObjectScript("new Mammal();"); V8Object cat = new V8Object(v8); cat.setPrototype(mammal); String[] keys = cat.getKeys(); Object object = cat.get("breathe"); assertTrue(object instanceof V8Function); assertEquals(0, keys.length); cat.close(); mammal.close(); ((Releasable) object).release(); }
/** * Returns the Frame at a given index * * @param index The stack index * @return The stack frame at a given index */ public Frame getFrame(final int index) { V8Array parameters = new V8Array(v8Object.getRuntime()); parameters.push(index); V8Object frame = null; try { frame = v8Object.executeObjectFunction(FRAME, parameters); return new Frame(frame); } finally { parameters.close(); if (frame != null) { frame.close(); } } }
@Test public void testBooleanFunctionCallOnJSObject() { ICallback callback = mock(ICallback.class); doReturn(false).when(callback).booleanMethodNoParameters(); V8Object v8Object = new V8Object(v8); v8Object.registerJavaMethod(callback, "booleanMethodNoParameters", "foo", new Class<?>[0]); boolean result = v8Object.executeBooleanFunction("foo", null); verify(callback).booleanMethodNoParameters(); assertFalse(result); v8Object.close(); }