Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility of replacing J2V8 with Javet #246

Open
daredevil2033 opened this issue Apr 29, 2023 · 1 comment
Open

Possibility of replacing J2V8 with Javet #246

daredevil2033 opened this issue Apr 29, 2023 · 1 comment

Comments

@daredevil2033
Copy link

J2V8 is severely outdated and is no longer maintained. The unofficial v6.2.0 build doesn't support Node.js which, if I understood correctly, is needed for viz.js but already provides a significant perfomance improvement. The tradeoff would be losing Mac support but at this point wouldn't GraalJS + GraalVM provide a similar perfomance compared to the 2016 verison of V8?

@lihaoyi
Copy link

lihaoyi commented Aug 30, 2024

Here's an implementation I ended up using to integrate Javet

class V8JavascriptEngine() extends AbstractJavascriptEngine {
  val LOG = LoggerFactory.getLogger(classOf[V8JavascriptEngine])
  val v8Runtime: V8Runtime = V8Host.getV8Instance().createV8Runtime()
  LOG.info("Starting V8 runtime...")
  LOG.info("Started V8 runtime. Initializing javascript...")
  val resultHandler = new ResultHandler
  val javetStandardConsoleInterceptor = new JavetStandardConsoleInterceptor(v8Runtime)
  javetStandardConsoleInterceptor.register(v8Runtime.getGlobalObject)

  class ResultHandlerInterceptor(resultHandler: ResultHandler){
    @V8Function
    def result(s: String) = resultHandler.setResult(s)

    @V8Function
    def error(s: String) = resultHandler.setError(s)

    @V8Function
    def log(s: String) = resultHandler.log(s)
  }
  val v8ValueObject = v8Runtime.createV8ValueObject
  v8Runtime.getGlobalObject.set("resultHandlerInterceptor", v8ValueObject)
  v8ValueObject.bind(new ResultHandlerInterceptor(resultHandler))

  v8Runtime.getExecutor(
    "var result = resultHandlerInterceptor.result; " +
      "var error = resultHandlerInterceptor.error; " +
      "var log = resultHandlerInterceptor.log; "
  ).execute()

  LOG.info("Initialized javascript.")

  override protected def execute(js: String): String = {
    v8Runtime.getExecutor(js).execute()
    resultHandler.waitFor
  }

  override def close(): Unit = v8Runtime.close()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants