/** * Register a Java method reflectively given it's name a signature. The option to include * the JS Object in the callback can be specified by setting includeReceiver true. * * @param object The Java Object on which the method is defined. * @param methodName The name of the method to register. * @param jsFunctionName The name of the JavaScript function to register the * method with. * @param parameterTypes The parameter types of the method. * @param includeReceiver True if the first parameter should include the JS Object, * false otherwise. * * @return The receiver. */ public V8Object registerJavaMethod(final Object object, final String methodName, final String jsFunctionName, final Class<?>[] parameterTypes, final boolean includeReceiver) { v8.checkThread(); checkReleased(); try { Method method = object.getClass().getMethod(methodName, parameterTypes); method.setAccessible(true); v8.registerCallback(object, method, getHandle(), jsFunctionName, includeReceiver); } catch (NoSuchMethodException e) { throw new IllegalStateException(e); } catch (SecurityException e) { throw new IllegalStateException(e); } return this; }
/** * Register a Java method as a JavaScript function. When the JS Function is invoked * the Java method will be called. * * @param callback The JavaCallback to call when the JSFunction is invoked. * @param jsFunctionName The name of the JSFunction. * * @return The receiver. */ public V8Object registerJavaMethod(final JavaCallback callback, final String jsFunctionName) { v8.checkThread(); checkReleased(); v8.registerCallback(callback, getHandle(), jsFunctionName); return this; }
/** * Register a void Java method as a JavaScript function. When the JS Function is invoked * the Java method will be called. * * @param callback The JavaVoidCallback to call when the JSFunction is invoked. * @param jsFunctionName The name of the JSFunction. * * @return The receiver. */ public V8Object registerJavaMethod(final JavaVoidCallback callback, final String jsFunctionName) { v8.checkThread(); checkReleased(); v8.registerVoidCallback(callback, getHandle(), jsFunctionName); return this; }
/** * Sets the prototype of the receiver. * * @param value The prototype to associate with this V8Object. * * @return The receiver. */ public V8Object setPrototype(final V8Object value) { v8.checkThread(); checkReleased(); v8.setPrototype(v8.getV8RuntimePtr(), objectHandle, value.getHandle()); return this; }
@Override public String toString() { if (isReleased() || v8.isReleased()) { return "[Object released]"; } v8.checkThread(); return v8.toString(v8.getV8RuntimePtr(), getHandle()); }
/** * Invoke a JavaScript function and return the result as a integer. If the * result is not an integer, or does not exist, then V8ResultUndefined is thrown. * * @param name The name of the JS Function to call. * * @param parameters The parameters to pass to the function. Parameters must be released. * * @return An integer representing the result of the function call or V8ResultUndefined * if the result is not an integer. */ public int executeIntegerFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(parameters); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeIntegerFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
/** * Invoke a JavaScript function and return the result as a double. If the * result is not a double, or does not exist, then V8ResultUndefined is thrown. * * @param name The name of the JS Function to call. * * @param parameters The parameters to pass to the function. Parameters must be released. * * @return A double representing the result of the function call or V8ResultUndefined * if the result is not a double. */ public double executeDoubleFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(parameters); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeDoubleFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
/** * Invoke a JavaScript function and return the result as a String. If the * result is not a String, or does not exist, then V8ResultUndefined is thrown. * * @param name The name of the JS Function to call. * * @param parameters The parameters to pass to the function. Parameters must be released. * * @return A String representing the result of the function call or V8ResultUndefined * if the result is not a String. */ public String executeStringFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(parameters); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeStringFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
/** * Invoke a JavaScript function and return the result as a boolean. If the * result is not a boolean, or does not exist, then V8ResultUndefined is thrown. * * @param name The name of the JS Function to call. * * @param parameters The parameters to pass to the function. Parameters must be released. * * @return A boolean representing the result of the function call or V8ResultUndefined * if the result is not a boolean. */ public boolean executeBooleanFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(parameters); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeBooleanFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
/** * Invoke the JavaScript function on the current runtime. * * @param receiver The object on which to call the function on. The * receiver will be mapped to 'this' in JavaScript. If receiver is null * or undefined, then the V8 runtime will be used instead. * @param parameters The parameters passed to the JS Function. * * @return The result of JavaScript function. */ @SuppressWarnings("resource") public Object call(V8Object receiver, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(receiver); v8.checkRuntime(parameters); receiver = receiver != null ? receiver : v8; long parametersHandle = parameters == null ? 0 : parameters.getHandle(); long receiverHandle = receiver.isUndefined() ? v8.getHandle() : receiver.getHandle(); return v8.executeFunction(v8.getV8RuntimePtr(), receiverHandle, objectHandle, parametersHandle); }
/** * Register a Java method reflectively given it's name a signature. The option to include * the JS Object in the callback can be specified by setting includeReceiver true. * * @param object The Java Object on which the method is defined. * @param methodName The name of the method to register. * @param jsFunctionName The name of the JavaScript function to register the * method with. * @param parameterTypes The parameter types of the method. * @param includeReceiver True if the first parameter should include the JS Object, * false otherwise. * * @return The receiver. */ public V8Object registerJavaMethod(final Object object, final String methodName, final String jsFunctionName, final Class<?>[] parameterTypes, final boolean includeReceiver) { v8.checkThread(); checkReleased(); try { Method method = object.getClass().getMethod(methodName, parameterTypes); method.setAccessible(true); v8.registerCallback(object, method, getHandle(), jsFunctionName, includeReceiver); } catch (NoSuchMethodException e) { throw new IllegalStateException(e); } catch (SecurityException e) { throw new IllegalStateException(e); } return this; }
/** * Register a Java method as a JavaScript function. When the JS Function is invoked * the Java method will be called. * * @param callback The JavaCallback to call when the JSFunction is invoked. * @param jsFunctionName The name of the JSFunction. * * @return The receiver. */ public V8Object registerJavaMethod(final JavaCallback callback, final String jsFunctionName) { v8.checkThread(); checkReleased(); v8.registerCallback(callback, getHandle(), jsFunctionName); return this; }
/** * Register a void Java method as a JavaScript function. When the JS Function is invoked * the Java method will be called. * * @param callback The JavaVoidCallback to call when the JSFunction is invoked. * @param jsFunctionName The name of the JSFunction. * * @return The receiver. */ public V8Object registerJavaMethod(final JavaVoidCallback callback, final String jsFunctionName) { v8.checkThread(); checkReleased(); v8.registerVoidCallback(callback, getHandle(), jsFunctionName); return this; }
/** * Sets the prototype of the receiver. * * @param value The prototype to associate with this V8Object. * * @return The receiver. */ public V8Object setPrototype(final V8Object value) { v8.checkThread(); checkReleased(); v8.setPrototype(v8.getV8RuntimePtr(), objectHandle, value.getHandle()); return this; }
@Override public String toString() { if (isReleased() || v8.isReleased()) { return "[Object released]"; } v8.checkThread(); return v8.toString(v8.getV8RuntimePtr(), getHandle()); }
/** * Invoke a JavaScript function and return the result as a double. If the * result is not a double, or does not exist, then V8ResultUndefined is thrown. * * @param name The name of the JS Function to call. * * @param parameters The parameters to pass to the function. Parameters must be released. * * @return A double representing the result of the function call or V8ResultUndefined * if the result is not a double. */ public double executeDoubleFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(parameters); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeDoubleFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
/** * Invoke a JavaScript function and return the result as a boolean. If the * result is not a boolean, or does not exist, then V8ResultUndefined is thrown. * * @param name The name of the JS Function to call. * * @param parameters The parameters to pass to the function. Parameters must be released. * * @return A boolean representing the result of the function call or V8ResultUndefined * if the result is not a boolean. */ public boolean executeBooleanFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(parameters); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeBooleanFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
/** * Invoke a JavaScript function and return the result as a integer. If the * result is not an integer, or does not exist, then V8ResultUndefined is thrown. * * @param name The name of the JS Function to call. * * @param parameters The parameters to pass to the function. Parameters must be released. * * @return An integer representing the result of the function call or V8ResultUndefined * if the result is not an integer. */ public int executeIntegerFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(parameters); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeIntegerFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
/** * Invoke a JavaScript function and return the result as a String. If the * result is not a String, or does not exist, then V8ResultUndefined is thrown. * * @param name The name of the JS Function to call. * * @param parameters The parameters to pass to the function. Parameters must be released. * * @return A String representing the result of the function call or V8ResultUndefined * if the result is not a String. */ public String executeStringFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(parameters); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeStringFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
/** * Invoke the JavaScript function on the current runtime. * * @param receiver The object on which to call the function on. The * receiver will be mapped to 'this' in JavaScript. If receiver is null * or undefined, then the V8 runtime will be used instead. * @param parameters The parameters passed to the JS Function. * * @return The result of JavaScript function. */ @SuppressWarnings("resource") public Object call(V8Object receiver, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(receiver); v8.checkRuntime(parameters); receiver = receiver != null ? receiver : v8; long parametersHandle = parameters == null ? 0 : parameters.getHandle(); long receiverHandle = receiver.isUndefined() ? v8.getHandle() : receiver.getHandle(); return v8.executeFunction(v8.getV8RuntimePtr(), receiverHandle, objectHandle, parametersHandle); }