Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What javascript engine used inside javafx?

WebView has built-in javascript engine. What engine is used for it? Is it the same for openjdk and oracle jdk? May it be used separately from WebView, just as JSR 223 engine?

like image 343
ayvango Avatar asked May 07 '15 14:05

ayvango


People also ask

Does JavaFX use JavaScript?

A JavaFX application can communicate with the web page in which it is embedded by using a JavaScript engine. The host web page can also communicate to embedded JavaFX applications using JavaScript.

What is the use of Nashorn JavaScript engine?

Nashorn: Nashorn is a JavaScript engine which is introduced in JDK 8. With the help of Nashorn, we can execute JavaScript code at Java Virtual Machine. Nashorn is introduced in JDK 8 to replace existing JavaScript engine i.e. Rhino.

Which of the following JavaScript engine was replaced with Nashorn?

Technical Article. Until Java SE 7, JDKs shipped with a JavaScript scripting engine based on Mozilla Rhino. Java SE 8 will instead ship with a new engine called Oracle Nashorn, which is based on JSR 292 and invokedynamic .

What is JavaFX WebView?

WebView is a Node that manages a WebEngine and displays its content. The associated WebEngine is created automatically at construction time and cannot be changed afterwards. WebView handles mouse and some keyboard events, and manages scrolling automatically, so there's no need to put it into a ScrollPane .


2 Answers

JavaScript Runtimes in the Oracle JRE

The full Oracle Java Runtime 8 ships with two JavaScript engines:

  1. Nashorn: "Nashorn's goal is to implement a lightweight high-performance JavaScript runtime in Java with a native JVM. This Project intends to enable Java developers embedding of JavaScript in Java applications via JSR-223 and to develop free standing JavaScript applications using the jrunscript command-line tool."
  2. JavaScriptCore: The JavaScript engine built into the WebKit implementation wrapped by WebView component the JavaFX system.

JavaScript Runtime Used by WebView and JavaFX applications

JavaFX Webkit does not use Nashorn, it uses JavaScriptCore.

You can use Nashorn to program JavaFX applications and APIs (as an alternative to programming in Java) and you can use Nashorn as a JavaFX script engine for JavaFX FXML documents, but you cannot use Nashorn as the JavaScript engine within WebView.

Background Source Information

There is some information on the JavaScript implementation in WebView provided by the JavaFX developers on the JavaFX mailing list. Quoting Richard Bair, an Oracle JavaFX developer (who in turn quotes Olivier Hunt, a WebKit Developer):

Well…. it goes like this. WebKit comes with JavaScriptCore by default, and this is the JS engine that we use. It is pretty good. My understanding is that JavaScriptCore (aka SquirrelFish aka Nitro) is the same JS engine used by Safari. When Chrome and Apple were both part of WebKit, one of the abstraction layers that Google had put into WebKit was the ability to swap out the JavaScript engine. When Google forked WebKit into Blink, the need for the WebKit project to have an abstraction for a different JS VM disappeared. As a consequence, the WebKit guys have been talking about removing those abstractions such that you won't be able to swap out the JS engine, [further info]. If/When that happens, it will be hard (or impossible) for us to switch over to Nashorn for WebView. We aren't going to fork WebKit, so we sort of have to follow along with what WebKit does.

"Supporting V8 places a considerable burden on webkit, there are a number of large, cumbersome and expensive abstractions required for to support multiple JS engines (see the original discussions on the topic from many years ago).

Additionally we will only be supporting JSC in WebKit2, so I don't think anything could convince me at least that maintaining support for multiple JS engines is good for the project." - Oliver Hunt

Disclaimer

This answer is related to Oracle Java 8 releases only; alternate and future JavaFX and Java implementations may have different internal implementations.


May it be used separately from WebView, just as JSR 223 engine?

JavaScriptCore in WebView cannot be used separately from WebView as a JSR 223 engine (as far as I know).

I think it is best to treat the JavaScriptCore implementation shipped to support WebView as an internal implementation detail of the WebView component and not treat the JavaScript implementation as a general purpose JavaScript Runtime to be used elsewhere. It is not an officially supported component outside of its specific use within WebView, and, when used there, the public WebView API shields you from the implementation details of the JavaScript runtime it uses: Such that it is deliberately opaque to users which JavaScript runtime is actually used to execute JavaScript within the WebView component.

like image 109
jewelsea Avatar answered Oct 23 '22 16:10

jewelsea


Update

Since OP is more concerned about JavaFX WebView, it uses JavaScriptCore. Please read JewelSea's answer for a better understanding.

JavaFX Webkit does not use Nashorn, it uses JavaScriptCore

Oracle Nashorn is the JavaScript engine in Java 8. Until Java 7, the JavaScript engine used was based on Mozilla Rhino.

Yes, it is same for Oracle JDK and OpenJDK.

You can find more information on Nashorn on the Oracle Site.

like image 27
ItachiUchiha Avatar answered Oct 23 '22 17:10

ItachiUchiha