/** * Creates a NodeJS Runtime * * @return The NodeJS runtime. * * May throw an UnsupportedOperationException if node.js integration has not * been compiled for your platform. */ public static NodeJS createNodeJS() { return createNodeJS(null); }
final NodeJS node = new NodeJS(v8); v8.registerJavaMethod(new JavaVoidCallback() { File startupScript = createTemporaryScriptFile(STARTUP_SCRIPT, STARTUP_SCRIPT_NAME); try { v8.createNodeRuntime(startupScript.getAbsolutePath()); node.exec(file);
/** * Execute a NodeJS script. This will load the script and execute it on the * next tick. This is the same as how NodeJS executes scripts at startup. Since * the script won't actually run until the next tick, this method does not return * a result. * * @param file The script to execute. */ public void exec(final File file) { V8Function scriptExecution = createScriptExecutionCallback(file); V8Object process = null; V8Array parameters = null; try { process = v8.getObject(PROCESS); parameters = new V8Array(v8); parameters.push(scriptExecution); process.executeObjectFunction(NEXT_TICK, parameters); } finally { safeRelease(process); safeRelease(parameters); safeRelease(scriptExecution); } }
/** * Creates a new {@link Nodejs}. * * @param basedir Base dir where to deploy a library. * @param loader Class loader to use. */ public Nodejs(final File basedir, final ClassLoader loader) { this.basedir = requireNonNull(basedir, "Basedir required."); this.loader = requireNonNull(loader, "ClassLoader required."); this.node = NodeJS.createNodeJS(); V8 v8 = node.getRuntime(); this.scope = new MemoryManager(v8); }
/** * Unpack and execute a nodejs library. Once unpack this method will execute one of these scripts * in the following order: 1) [library].js; 2) main.js; 3) index.js. * * The first file found will be executed. * * @param library Library to unpack and execute this library. * @throws Throwable If something goes wrong. */ public void exec(final String library, final Throwing.Consumer<V8> callback) throws Throwable { Path basedir = deploy(library); List<String> candidates = Arrays.asList( basedir.getFileName().toString() + ".js", "main.js", "index.js"); Path main = candidates.stream() .map(basedir::resolve) .filter(Files::exists) .findFirst() .orElseThrow(() -> new FileNotFoundException(candidates.toString())); callback.accept(node.getRuntime()); node.exec(main.toFile()); while (node.isRunning()) { node.handleMessage(); } }
@Test public void testExecNodeScript() throws IOException { assumeFalse(skipMessage, skipTest()); // conditional skip nodeJS.release(); File testScript = createTemporaryScriptFile("global.passed = true;", "testScript"); nodeJS = NodeJS.createNodeJS(); nodeJS.exec(testScript); runMessageLoop(); assertEquals(true, nodeJS.getRuntime().getBoolean("passed")); testScript.delete(); }
@Test public void testExecuteNodeScript_viaRequire() throws IOException { assumeFalse(skipMessage, skipTest()); // conditional skip nodeJS.release(); File testScript = createTemporaryScriptFile("global.passed = true;", "testScript"); nodeJS = NodeJS.createNodeJS(); nodeJS.require(testScript).close(); runMessageLoop(); assertEquals(true, nodeJS.getRuntime().getBoolean("passed")); testScript.delete(); }
@Test public void testExecuteNodeScript_Startup() throws IOException { assumeFalse(skipMessage, skipTest()); // conditional skip nodeJS.release(); File testScript = createTemporaryScriptFile("global.passed = true;", "testScript"); nodeJS = NodeJS.createNodeJS(testScript); runMessageLoop(); assertEquals(true, nodeJS.getRuntime().getBoolean("passed")); testScript.delete(); }
@Test public void testExports() throws IOException { assumeFalse(skipMessage, skipTest()); // conditional skip nodeJS.release(); File testScript = createTemporaryScriptFile("exports.foo=7", "testScript"); nodeJS = NodeJS.createNodeJS(); V8Object exports = nodeJS.require(testScript); runMessageLoop(); assertEquals(7, exports.getInteger("foo")); exports.close(); }
/** * 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; }
private void runMessageLoop() { while (nodeJS.isRunning()) { nodeJS.handleMessage(); } }
@Override public void run() { try { nodeJS.isRunning(); } catch (Error e) { result[0] = e.getMessage().contains("Invalid V8 thread access"); } } });
@Override public void run() { try { nodeJS.handleMessage(); } catch (Error e) { result[0] = e.getMessage().contains("Invalid V8 thread access"); } } });
/** * Unpack and execute a nodejs library. Once unpack this method will execute one of these scripts * in the following order: 1) [library].js; 2) main.js; 3) index.js. * * The first file found will be executed. * * @param library Library to unpack and execute this library. * @throws Throwable If something goes wrong. */ public void exec(final String library, final Throwing.Consumer<V8> callback) throws Throwable { Path basedir = deploy(library); List<String> candidates = Arrays.asList( basedir.getFileName().toString() + ".js", "main.js", "index.js"); Path main = candidates.stream() .map(basedir::resolve) .filter(Files::exists) .findFirst() .orElseThrow(() -> new FileNotFoundException(candidates.toString())); callback.accept(node.getRuntime()); node.exec(main.toFile()); while (node.isRunning()) { node.handleMessage(); } }
/** * Creates a new {@link Nodejs}. * * @param basedir Base dir where to deploy a library. * @param loader Class loader to use. */ public Nodejs(final File basedir, final ClassLoader loader) { this.basedir = requireNonNull(basedir, "Basedir required."); this.loader = requireNonNull(loader, "ClassLoader required."); this.node = NodeJS.createNodeJS(); V8 v8 = node.getRuntime(); this.scope = new MemoryManager(v8); }
/** * 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; }
final NodeJS node = new NodeJS(v8); v8.registerJavaMethod(new JavaVoidCallback() { File startupScript = createTemporaryScriptFile(STARTUP_SCRIPT, STARTUP_SCRIPT_NAME); try { v8.createNodeRuntime(startupScript.getAbsolutePath()); node.exec(file);
/** * Execute a NodeJS script. This will load the script and execute it on the * next tick. This is the same as how NodeJS executes scripts at startup. Since * the script won't actually run until the next tick, this method does not return * a result. * * @param file The script to execute. */ public void exec(final File file) { V8Function scriptExecution = createScriptExecutionCallback(file); V8Object process = null; V8Array parameters = null; try { process = v8.getObject(PROCESS); parameters = new V8Array(v8); parameters.push(scriptExecution); process.executeObjectFunction(NEXT_TICK, parameters); } finally { safeRelease(process); safeRelease(parameters); safeRelease(scriptExecution); } }
/** * Creates a NodeJS Runtime * * @return The NodeJS runtime. * * May throw an UnsupportedOperationException if node.js integration has not * been compiled for your platform. */ public static NodeJS createNodeJS() { return createNodeJS(null); }
final NodeJS node = new NodeJS(v8); v8.registerJavaMethod(new JavaVoidCallback() { File startupScript = createTemporaryScriptFile(STARTUP_SCRIPT, STARTUP_SCRIPT_NAME); try { v8.createNodeRuntime(startupScript.getAbsolutePath()); node.exec(file);