Announcing J2V8 3.0

Last month I spent 10 days in Germany. I spent time at the EclipseSource headquarters in Karlsruhe, where I enjoyed schnitzel, good beer and a few strange sights.

IMG_20150620_185718

I also attended EnterJS, where we brought an iPhone attached to a punching bag to show just how responsive Tabris.js is. While at EnterJS, I presented J2V8 and released J2V8 3.0. J2V8 brings Google’s V8 JavaScript engine to Java. Here is the official New and Noteworthy for J2V8 3.0.

Multithread Support

J2V8 now has multithread support. Multiple V8 Runtimes can now be created, each on their own thread. This means that worker threads can be started, each with their own isolated runtime. A locker was also introduced. This allows a thread can give-up control of a V8 Runtime and another thread can take over.

You can read more about this feature here.

V8 Executors

To make multithreaded programming easier, a V8Executor class was added. This is a thread which contains its own V8Runtime. When started, it will execute the provided JavaScript on its isolated runtime and then wait for messages. The executor will continue running until it is explicitly terminated. This abstraction makes it very easy to include WebWorkers in your J2V8 application.

You can read more about this feature here.

V8Functions

A V8Function is now a first class citizen in J2V8. If a script returns a function, you can cast it to a V8Function and invoke it at a later time. This should make storing and passing around JavaScript functions much easier.

<pre lang="javascript">
 v8.executeVoidScript("function add(x, y) {return x+y;}");
 V8Function function = (V8Function) v8.getObject("add");
 V8Array parameters = new V8Array(v8).push(7).push(8);
 Object result = function.call(v8, parameters);
</pre>

Universal get / executeScript Methods

J2V8 uses the principle primitive when possibleThis means that when working with JavaScript, wrapper classes should not be created for primitives types. This helps with performance, but can cause massive code bloat as you need different cases for each possible return type. While we are sticking with this principle, we have added an additional API which uses Object and wrapper classes. If you don’t know the return type of a script, you can call executeScript and J2V8 will create wrappers as needed.

<pre lang="javascript">
  Object result = v8.executeScript("...long complicated JavaScript...");
</pre>

Linux Support

Linux support has finally be added and pushed to Maven Central. You can use the following dependency in your pom.xml to add Linux support.

<pre lang="xml">
 <dependency>
  <groupId>com.eclipsesource.j2v8</groupId>
  <artifactId>j2v8_linux_x86_64</artifactId>
  <version>3.0.0</version>
 </dependency>
</pre>

API and Getting Started Guide

JavaDocs have been written for all the public API. The JUnit tests are still your best source of examples, but you can now browse the source code for an overview of the functionality. In addition to this, a getting started guide was written to help you get started with J2V8.

You can read the 2 part getting started guide here and here.

Finally,  Angelo Zerr  managed to use J2V8 in Tern.js. You can now choose J2V8 as your engine for Tern.

tern

Thanks for the pull requests, and please let us know what you are building with J2V8.

For more J2V8 Tips & Tricks, follow me on Twitter.

1 Comment