Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use V8 JavaScript engine to execute JS lib without web view

I am developing a JavaScript component which is responsible for making requests to the server and dispatching results to the UI. By doing this in JavaScript, I am able to use my component in several types of UI: Android app, iOS app, desktop app (QT), web app...

All these UI have instantiated a web view, so my component is started when the UI loads the dedicated URL (webview.load("file://myfirstWebPage.html")).

This first web page loads all the JavaScript components, and when it's done, the UI is able to make some requests to the JavaScript component, which makes a request to the server and when it has the response, it dispatches it back to the client (UI Android, UI iOS ...)

This architecture works fine, but I would like to know if there is another way to load the JavaScript component without using a web view on each client?

Can the V8 engine help me?

like image 619
wawanopoulos Avatar asked Oct 16 '13 16:10

wawanopoulos


People also ask

How does V8 execute JavaScript?

The V8 engine uses the Ignition interpreter, which takes in the Abstract Syntax Tree as the input and gives the byte code as the output, which further proceeds to the execution phase. When the code is being interpreted, the compiler tries to talk with the interpreter to optimize the code.

What is V8 JavaScript engine used for?

Chrome V8 is a JavaScript engine, which means that it executes JavaScript code. Originally, JavaScript was written to be executed by web browsers. Chrome V8, or just V8, can execute JavaScript code either within or outside of a browser, which makes server-side scripting possible.

Is V8 a JavaScript runtime engine?

V8 is Google's open source high-performance JavaScript and WebAssembly engine, written in C++. It is used in Chrome and in Node. js, among others. It implements ECMAScript and WebAssembly, and runs on Windows 7 or later, macOS 10.12+, and Linux systems that use x64, IA-32, ARM, or MIPS processors.

Does node js run on Google V8 engine?

Node. js is a very powerful JavaScript-based framework/platform built on Google Chrome's JavaScript V8 Engine.


2 Answers

If I'm understanding your question, you're looking for a way to execute JavaScript across many platforms (iOS, Android, etc.) without the use of a WebView. The solution will be platform-specific, since even the underlying WebView implementations are different for each platform.

For Android, so long as the device ships with V8, you can create a new V8 Context via its API and use that to execute your JavaScript. The device must actually ship with V8. This answer may help you further.

For iOS, which uses JavaScriptCore, recent developments in iOS7 have been made to allow you load and run arbitrary JavaScript code. Read more here.

like image 181
Andrew Avatar answered Oct 26 '22 07:10

Andrew


For the Android part. I used J2V8 JavaScript library. It is a Java wrapper of Google's V8 JavaScript engine. See here for more details.

like image 37
Angelo Avatar answered Oct 26 '22 07:10

Angelo