Migrate from J2V8 to Javet

Why Migrate from J2V8 to Javet?

  • Its Linux, Mac OS and Windows releases have been abandoned for years.
  • Its type hierarchy is inconsistent because primitive types are out of the hierarchy so that tedious if-else sentences have to be repeated all over the code base.
  • Its function registration API is kind of verbose.
  • Segfaults take place so frequently and don’t get maintainers’ attention for years.
  • Its locking mechanism heavily increases mental pressure in the code base.
  • Its V8 runtime is not multi-threaded friendly unless application adds a synchronous layer on top of it.

Migration Guides

V8 ⟶ V8Runtime

  • V8 in J2V8 is V8Runtime in Javet.
  • V8 in J2V8 carries 2 roles: 1 as the V8 runtime and 1 as the global object (globalThis or global). In Javet, V8Runtime no longer inherits from V8Value so that it literally represents the V8 runtime. V8Runtime.getGlobalObject() is dedicated to the global object.
  • V8Runtime has much richer API than V8 has. E.g. compileV8Module(), lowMemoryNotification(), terminateExecution().

Primitive Types

  • Primitive types in Javet inherit from V8ValuePrimitiveV8ValueV8Data.
  • The Javet type hierarchy is consistent so that V8Value in all supported API can represent all V8 types. This is hard in J2V8 because Object has to be used to represent all types, however, by using Object the type check during compilation doesn't work at all and that is a rich source of runtime bugs or even segfaults.

registerJavaMethod() ⟶ @V8Function

  • It is quite painful to register many functions in J2V8. Javet makes that a declarative one instead of the imperative one. Just decorate the target function with @V8Function, then call V8ValueObject.bind(javaObject) to bind that Java object, it's done.
  • In addition, Javet provides @V8Property which allows registering getters and setters in the same manner. That feature has never been delivered by J2V8.
  • Javet also allows unbinding the registration. Just call V8ValueObject.unbind(javaObject).

V8Locker

  • Javet introduced Implicit Mode which allows applications to eliminate V8Locker from the code base and still be able to share the same V8Runtime among multiple threads, because Javet does the synchronization automatically. That frees applications developers from the tedious acquire() and release() calls, and gets the rid of the runtime exceptions caused by multiple threads.
  • Javet also has Explicit Mode for performance sensitive scenarios.

Type Conversion

  • Javet has built-in JavetObjectConverter which covers the majority cases on type conversion so that the arguments of Javet API can be of any type and the converter just does the conversion transparently. That frees application developers from writing tedious type conversion code everywhere.
  • Javet also provides JavetProxyConverter which allows injecting arbitrary Java objects in V8 and polyfilling Java interfaces with JavaScript functions or objects. Especially the polyfilling feature implies hotfixing business logic without restarting the JVM.

Node.js and V8

  • Javet provides both Node.js mode and V8 mode for various usages. Each mode stays at a dedicated classloader so that both modes don’t cross each other, and are completely isolated. If the application only uses one mode, it doesn’t need to pay extra amount of memory for the other mode because the other mode is not loaded at all. Of course, both modes can be unloaded as well without shutting down the JVM.
  • In Node.js mode, all node modules can be directly used including the native modules. Please refer to Modularization for more detail.
  • In V8 mode, it is much more secure than the Node.js mode is, but lacks of some basic ES API, e.g. setTimeout(). Project Javenode is the one that aims at simulating Node.js with Java in Javet V8 mode.

ES6 Module

  • Javet supports import { *** } from '***.js' and exposes module resolve event for applications to specify where to locate the modules.

Blessing

--

--

--

Amateur programmer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Adding a custom cursor to your Next.js Website

Function Hoisting

11 webpack plugins which you should Know them

ReactJS Overview

Tackling complex concepts as a developing software engineer using JavaScript

JavaScript important core concept you want to know

React: When is a functional component NOT a functional component?

Knowing Vue Js 3

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sam Cao

Sam Cao

Amateur programmer

More from Medium

What is OAuth 2.0 — In a simple way

Article Worth Reading On What Is An API, What is A REST API And Deep Diving Into REST API.

Use cases to decide launch or async

Takamaka Java framework included in the Hotmoka project