Announcing J2V8 3.0

July 8, 2015 | 3 min Read

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.

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 in this blog post.

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 in this blog post.

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.

 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);

Universal get / executeScript Methods

J2V8 uses the principle primitive when possible. This 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.

  Object result = v8.executeScript("...long complicated JavaScript...");

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.

 
  com.eclipsesource.j2v8
  j2v8_linux_x86_64
  3.0.0
 

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 in this blog post and in this blog post.

Finally,  Angelo Zerr  managed to use J2V8 in Tern.js. You can now choose J2V8 as your engine for 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.

Ian Bull

Ian Bull

Ian is an Eclipse committer and EclipseSource Distinguished Engineer with a passion for developer productivity.

He leads the J2V8 project and has served on several …