SlideShare uma empresa Scribd logo
1 de 32
Baixar para ler offline
Node4J
Running Node.js in a Java
World
Dr. R. Ian Bull
EclipseSource
@irbull
Java and JavaScript
❖ Java a successful server side language
❖ JavaScript is a client side language
❖ SWT brought performant Java UIs to the desktop
❖ Node.js brought JavaScript to the server
❖ Java and JavaScript are two of the most popular
programming languages
Polyglot Systems
❖ Single language systems are rarely an option
❖ Legacy code
❖ New frameworks and technologies
❖ Evolving enterprises
❖ JEE will be here for another 20, 30, 50 (?) years
Bridging Java and JavaScript
❖ Three common Java technologies enable JS embedding
❖ Rhino
❖ Available since JDK 6
❖ Nashorn
❖ Replacing Rhino since JDK 8
❖ More performant
❖ V8 as a separate process, String based messages
Performance
❖ 30 Runs of the Esprima parser and tokenizer
❖ Nashorn compiles to bytecode
❖ V8 compiles to native assembly
❖ Best choice for raw JavaScript execution
Node4J: Running Node.js in Java World
J2V8
❖ A set of bindings that bring V8 to Java
❖ Inspired by SWT
❖ Create a thin JNI layer
❖ Expose (some) V8 API in Java
❖ Complicated logic lives in Java
J2V8 Goals
❖ Efficient JavaScript on Android
❖ Make JavaScript shine in an enterprise Java World
❖ Standard Java APIs
❖ Efficient Java / JavaScript bindings
J2V8 — History
❖ 1.0 Released in November 2014
❖ 2.0 Released in February 2015
❖ First presented at EclipseCon 2015
❖ 3.0 Released at EnterJS — Summer 2015
J2V8 Design
❖ Each V8 Object can be referenced using a Handle
❖ Each Object is stored in a V8 Persistent Object Store
❖ Objects must be explicitly freed
❖ Primitives where possible (no wrappers)
❖ Single Thread per isolate
Two-way binding
❖ JS functions and scripts can be invoked from Java
❖ Java methods can be called from JavaScript
❖ Data can be passed back and forth using V8Objects
J2V8 In Action — Tabris.js
❖ Mobile framework
❖ Apps written in JavaScript
❖ Native iOS and Android Apps
❖ Bindings to native UI components
Shameless Plug
Example
public String someJavaMethod(final String firstName, final String lastName) {
return firstName + ", " + lastName;
}
public void start() {
V8 v8 = V8.createV8Runtime();
v8.registerJavaMethod(this,
"someJavaMethod",
"someJavaMethod",
new Class[] { String.class, String.class });
v8.executeScript("var result = someJavaMethod('Ian', ‘Bull');");
String result = v8.getString("result");
System.out.println(result);
}
J2V8 —What’s New
❖ Typed Arrays
❖ Threads & Workers
❖ ES 6
❖ ChromeDev Tools
❖ NodeJS Support
Typed Arrays
V8Array result = (V8Array) v8.executeScript(""
+ "var buf = new ArrayBuffer(100);"
+ "var ints = new Int32Array(buf); "
+ "for(var i = 0; i < 25; i++) {"
+ " ints[i] = i;"
+ "}; "
+ “ints");
int[] ints = result.getIntegers(0, 25);
❖ Native support for JS Typed Arrays
❖ Access the values efficiently from Java
Threads
❖ Every thread can have it’s own Isolate (Isolated V8
Instance)
❖ V8Thread is a Java Thread with an associated Isolate
❖ Provide an easy way to execute JavaScript
Thread t = new V8Thread(new V8Runnable() {
public void run(V8 v8) {
int result = v8.executeIntegerScript("1+2");
}
});
t.start();
Executors
❖ Long running V8Thread with a message queue and
event loop
❖ Threads can communicate via message passing
❖ Useful for implementing Web Workers / Service
Workers
ES 6
❖ Snapshot builds of J2V8 support V8 4.10 & ES 6
❖ Arrows
❖ Classes
❖ Let / Const
❖ Interators + For..Of
❖ Generators
❖ …
Debug Support
❖ V8 (and now J2V8) no longer supports the Debug Agent
❖ JavaScript based Debug API is available instead
❖ J2V8 exposes this API in Java
❖ Integrated with the Stetho tool & Chrome Dev Tools
Debug Support Demo
Node.js
❖ JavaScript Virtual Machine (V8)
❖ Modules
❖ Native
❖ JavaScript
❖ Event Loop
Node.js® is a JavaScript runtime built on Chrome's V8
JavaScript engine. Node.js uses an event-driven,
non-blocking I/O model that makes it
lightweight and efficient.
Bridging to Node.js
❖ Out of process Node & REST Services
❖ Vert.x
❖ Node engine on Nashorn / Rhino?
Node4J
❖ Dynamically link Node.js to the JVM
❖ Access Node.js context via JNI
❖ Execute Node.js modules (require)
❖ Callbacks to Java
❖ Process Node.js message queue
Node4J Demo
public static void main(final String[] args) throws Exception {
final V8 v8 = V8.createV8Runtime("global");
v8.registerJavaMethod(…);
NodeJS node = V8.createNodeJS(v8);
V8Object exports = node.requireScript(nodeCode, "http");
exports.release();
boolean running = true;
while (running) {
running = node.pumpMessageLoop();
}
}
Performance Considerations
❖ Minimize callbacks from JavaScript to Java
❖ ~4000 Per Second on my MBP
❖ Use bulk array copy to move primitives from JS to Java
❖ 60fps in our animation demo
Resources
❖ Getting started with J2V8
❖ Registering Java Callbacks with J2V8
❖ Implementing WebWorkers with J2V8
❖ Multithreaded JavaScript with J2V8
❖ Using J2V8 with Heroku
❖ All linked from our GitHub Page
Future Work
❖ Advanced exception handling between Java and JS
❖ Improved debug support
❖ Typed array access in Java
❖ You tell me?
Using J2V8
❖ J2V8 is available in Maven Central
❖ Currently 5 variants are available:
com.eclipsesource.j2v8.j2v8_win32_x86:3.1.6

com.eclipsesource.j2v8.j2v8_macosx_x86_64:3.1.6

com.eclipsesource.j2v8.j2v8:3.1.6 (aar)

com.eclipsesource.j2v8.j2v8_android_armv7l:3.1.6

com.eclipsesource.j2v8.j2v8_android_x86:3.1.6
❖ j2v8:3.1.6 (aar) contains both x86 and armv7l
4.0!
Thank-you
❖ Open Source Java bindings for V8
❖ Node4J extensions bring Node.js to Java
❖ Licensed under the EPL
❖ For J2V8 news, follow me on Twitter @irbull
https://github.com/eclipsesource/j2v8
Node4J: Running Node.js in Java World

Mais conteúdo relacionado

Último

Injection Power Cycle - The most efficient power cycle
Injection Power Cycle - The most efficient power cycleInjection Power Cycle - The most efficient power cycle
Injection Power Cycle - The most efficient power cyclemarijomiljkovic1
 
autonomous_vehicle_working_paper_01072020-_508_compliant.pdf
autonomous_vehicle_working_paper_01072020-_508_compliant.pdfautonomous_vehicle_working_paper_01072020-_508_compliant.pdf
autonomous_vehicle_working_paper_01072020-_508_compliant.pdfPandurangGurakhe
 
Governors ppt.pdf .
Governors ppt.pdf                              .Governors ppt.pdf                              .
Governors ppt.pdf .happycocoman
 
Research paper publications: Meaning of Q1 Q2 Q3 Q4 Journal
Research paper publications: Meaning of Q1 Q2 Q3 Q4 JournalResearch paper publications: Meaning of Q1 Q2 Q3 Q4 Journal
Research paper publications: Meaning of Q1 Q2 Q3 Q4 JournalDr. Manjunatha. P
 
First Review Group 1 PPT.pptx with slide
First Review Group 1 PPT.pptx with slideFirst Review Group 1 PPT.pptx with slide
First Review Group 1 PPT.pptx with slideMonika860882
 
Chapter 2 Canal Falls at Mnnit Allahabad .pptx
Chapter 2 Canal Falls at Mnnit Allahabad .pptxChapter 2 Canal Falls at Mnnit Allahabad .pptx
Chapter 2 Canal Falls at Mnnit Allahabad .pptxButcher771
 
Flutter GDE session GDSC ZHCET AMU, aligarh
Flutter GDE session GDSC ZHCET AMU, aligarhFlutter GDE session GDSC ZHCET AMU, aligarh
Flutter GDE session GDSC ZHCET AMU, aligarhjamesbond00714
 
Navigating Process Safety through Automation and Digitalization in the Oil an...
Navigating Process Safety through Automation and Digitalization in the Oil an...Navigating Process Safety through Automation and Digitalization in the Oil an...
Navigating Process Safety through Automation and Digitalization in the Oil an...soginsider
 
EJECTOR REFRIGERATION CYCLE WITH THE INJECTION OF A HIGH DENSITY FLUID INTO A...
EJECTOR REFRIGERATION CYCLE WITH THE INJECTION OF A HIGH DENSITY FLUID INTO A...EJECTOR REFRIGERATION CYCLE WITH THE INJECTION OF A HIGH DENSITY FLUID INTO A...
EJECTOR REFRIGERATION CYCLE WITH THE INJECTION OF A HIGH DENSITY FLUID INTO A...marijomiljkovic1
 
Tekom Netherlands | The evolving landscape of Simplified Technical English b...
Tekom Netherlands | The evolving landscape of Simplified Technical English  b...Tekom Netherlands | The evolving landscape of Simplified Technical English  b...
Tekom Netherlands | The evolving landscape of Simplified Technical English b...Shumin Chen
 
Support nodes for large-span coal storage structures
Support nodes for large-span coal storage structuresSupport nodes for large-span coal storage structures
Support nodes for large-span coal storage structureswendy cai
 
Conventional vs Modern method (Philosophies) of Tunneling-re.pptx
Conventional vs Modern method (Philosophies) of Tunneling-re.pptxConventional vs Modern method (Philosophies) of Tunneling-re.pptx
Conventional vs Modern method (Philosophies) of Tunneling-re.pptxSAQIB KHURSHEED WANI
 
This chapter gives an outline of the security.
This chapter gives an outline of the security.This chapter gives an outline of the security.
This chapter gives an outline of the security.RoshniIsrani1
 
0950_Rodriguez_200520_Work_done-GEOGalicia_ELAB-converted.pptx
0950_Rodriguez_200520_Work_done-GEOGalicia_ELAB-converted.pptx0950_Rodriguez_200520_Work_done-GEOGalicia_ELAB-converted.pptx
0950_Rodriguez_200520_Work_done-GEOGalicia_ELAB-converted.pptxssuser886c55
 
Advanced Additive Manufacturing by Sumanth A.pptx
Advanced Additive Manufacturing by Sumanth A.pptxAdvanced Additive Manufacturing by Sumanth A.pptx
Advanced Additive Manufacturing by Sumanth A.pptxSumanth A
 
introduction to python, fundamentals and basics
introduction to python, fundamentals and basicsintroduction to python, fundamentals and basics
introduction to python, fundamentals and basicsKNaveenKumarECE
 
Road to GDSC (Become the next GDSC lead)
Road to GDSC (Become the next GDSC lead)Road to GDSC (Become the next GDSC lead)
Road to GDSC (Become the next GDSC lead)GDSCNiT
 

Último (20)

Update on the latest research with regard to RAP
Update on the latest research with regard to RAPUpdate on the latest research with regard to RAP
Update on the latest research with regard to RAP
 
Injection Power Cycle - The most efficient power cycle
Injection Power Cycle - The most efficient power cycleInjection Power Cycle - The most efficient power cycle
Injection Power Cycle - The most efficient power cycle
 
autonomous_vehicle_working_paper_01072020-_508_compliant.pdf
autonomous_vehicle_working_paper_01072020-_508_compliant.pdfautonomous_vehicle_working_paper_01072020-_508_compliant.pdf
autonomous_vehicle_working_paper_01072020-_508_compliant.pdf
 
Governors ppt.pdf .
Governors ppt.pdf                              .Governors ppt.pdf                              .
Governors ppt.pdf .
 
FOREST FIRE USING IoT-A Visual to UG students
FOREST FIRE USING IoT-A Visual to UG studentsFOREST FIRE USING IoT-A Visual to UG students
FOREST FIRE USING IoT-A Visual to UG students
 
Research paper publications: Meaning of Q1 Q2 Q3 Q4 Journal
Research paper publications: Meaning of Q1 Q2 Q3 Q4 JournalResearch paper publications: Meaning of Q1 Q2 Q3 Q4 Journal
Research paper publications: Meaning of Q1 Q2 Q3 Q4 Journal
 
First Review Group 1 PPT.pptx with slide
First Review Group 1 PPT.pptx with slideFirst Review Group 1 PPT.pptx with slide
First Review Group 1 PPT.pptx with slide
 
Chapter 2 Canal Falls at Mnnit Allahabad .pptx
Chapter 2 Canal Falls at Mnnit Allahabad .pptxChapter 2 Canal Falls at Mnnit Allahabad .pptx
Chapter 2 Canal Falls at Mnnit Allahabad .pptx
 
Flutter GDE session GDSC ZHCET AMU, aligarh
Flutter GDE session GDSC ZHCET AMU, aligarhFlutter GDE session GDSC ZHCET AMU, aligarh
Flutter GDE session GDSC ZHCET AMU, aligarh
 
Navigating Process Safety through Automation and Digitalization in the Oil an...
Navigating Process Safety through Automation and Digitalization in the Oil an...Navigating Process Safety through Automation and Digitalization in the Oil an...
Navigating Process Safety through Automation and Digitalization in the Oil an...
 
EJECTOR REFRIGERATION CYCLE WITH THE INJECTION OF A HIGH DENSITY FLUID INTO A...
EJECTOR REFRIGERATION CYCLE WITH THE INJECTION OF A HIGH DENSITY FLUID INTO A...EJECTOR REFRIGERATION CYCLE WITH THE INJECTION OF A HIGH DENSITY FLUID INTO A...
EJECTOR REFRIGERATION CYCLE WITH THE INJECTION OF A HIGH DENSITY FLUID INTO A...
 
Tekom Netherlands | The evolving landscape of Simplified Technical English b...
Tekom Netherlands | The evolving landscape of Simplified Technical English  b...Tekom Netherlands | The evolving landscape of Simplified Technical English  b...
Tekom Netherlands | The evolving landscape of Simplified Technical English b...
 
Support nodes for large-span coal storage structures
Support nodes for large-span coal storage structuresSupport nodes for large-span coal storage structures
Support nodes for large-span coal storage structures
 
Conventional vs Modern method (Philosophies) of Tunneling-re.pptx
Conventional vs Modern method (Philosophies) of Tunneling-re.pptxConventional vs Modern method (Philosophies) of Tunneling-re.pptx
Conventional vs Modern method (Philosophies) of Tunneling-re.pptx
 
Caltrans view on recycling of in-place asphalt pavements
Caltrans view on recycling of in-place asphalt pavementsCaltrans view on recycling of in-place asphalt pavements
Caltrans view on recycling of in-place asphalt pavements
 
This chapter gives an outline of the security.
This chapter gives an outline of the security.This chapter gives an outline of the security.
This chapter gives an outline of the security.
 
0950_Rodriguez_200520_Work_done-GEOGalicia_ELAB-converted.pptx
0950_Rodriguez_200520_Work_done-GEOGalicia_ELAB-converted.pptx0950_Rodriguez_200520_Work_done-GEOGalicia_ELAB-converted.pptx
0950_Rodriguez_200520_Work_done-GEOGalicia_ELAB-converted.pptx
 
Advanced Additive Manufacturing by Sumanth A.pptx
Advanced Additive Manufacturing by Sumanth A.pptxAdvanced Additive Manufacturing by Sumanth A.pptx
Advanced Additive Manufacturing by Sumanth A.pptx
 
introduction to python, fundamentals and basics
introduction to python, fundamentals and basicsintroduction to python, fundamentals and basics
introduction to python, fundamentals and basics
 
Road to GDSC (Become the next GDSC lead)
Road to GDSC (Become the next GDSC lead)Road to GDSC (Become the next GDSC lead)
Road to GDSC (Become the next GDSC lead)
 

Destaque

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Destaque (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

Node4J: Running Node.js in Java World

  • 1. Node4J Running Node.js in a Java World Dr. R. Ian Bull EclipseSource @irbull
  • 2. Java and JavaScript ❖ Java a successful server side language ❖ JavaScript is a client side language ❖ SWT brought performant Java UIs to the desktop ❖ Node.js brought JavaScript to the server ❖ Java and JavaScript are two of the most popular programming languages
  • 3. Polyglot Systems ❖ Single language systems are rarely an option ❖ Legacy code ❖ New frameworks and technologies ❖ Evolving enterprises ❖ JEE will be here for another 20, 30, 50 (?) years
  • 4. Bridging Java and JavaScript ❖ Three common Java technologies enable JS embedding ❖ Rhino ❖ Available since JDK 6 ❖ Nashorn ❖ Replacing Rhino since JDK 8 ❖ More performant ❖ V8 as a separate process, String based messages
  • 5. Performance ❖ 30 Runs of the Esprima parser and tokenizer ❖ Nashorn compiles to bytecode ❖ V8 compiles to native assembly ❖ Best choice for raw JavaScript execution
  • 7. J2V8 ❖ A set of bindings that bring V8 to Java ❖ Inspired by SWT ❖ Create a thin JNI layer ❖ Expose (some) V8 API in Java ❖ Complicated logic lives in Java
  • 8. J2V8 Goals ❖ Efficient JavaScript on Android ❖ Make JavaScript shine in an enterprise Java World ❖ Standard Java APIs ❖ Efficient Java / JavaScript bindings
  • 9. J2V8 — History ❖ 1.0 Released in November 2014 ❖ 2.0 Released in February 2015 ❖ First presented at EclipseCon 2015 ❖ 3.0 Released at EnterJS — Summer 2015
  • 10. J2V8 Design ❖ Each V8 Object can be referenced using a Handle ❖ Each Object is stored in a V8 Persistent Object Store ❖ Objects must be explicitly freed ❖ Primitives where possible (no wrappers) ❖ Single Thread per isolate
  • 11. Two-way binding ❖ JS functions and scripts can be invoked from Java ❖ Java methods can be called from JavaScript ❖ Data can be passed back and forth using V8Objects
  • 12. J2V8 In Action — Tabris.js ❖ Mobile framework ❖ Apps written in JavaScript ❖ Native iOS and Android Apps ❖ Bindings to native UI components
  • 14. Example public String someJavaMethod(final String firstName, final String lastName) { return firstName + ", " + lastName; } public void start() { V8 v8 = V8.createV8Runtime(); v8.registerJavaMethod(this, "someJavaMethod", "someJavaMethod", new Class[] { String.class, String.class }); v8.executeScript("var result = someJavaMethod('Ian', ‘Bull');"); String result = v8.getString("result"); System.out.println(result); }
  • 15. J2V8 —What’s New ❖ Typed Arrays ❖ Threads & Workers ❖ ES 6 ❖ ChromeDev Tools ❖ NodeJS Support
  • 16. Typed Arrays V8Array result = (V8Array) v8.executeScript("" + "var buf = new ArrayBuffer(100);" + "var ints = new Int32Array(buf); " + "for(var i = 0; i < 25; i++) {" + " ints[i] = i;" + "}; " + “ints"); int[] ints = result.getIntegers(0, 25); ❖ Native support for JS Typed Arrays ❖ Access the values efficiently from Java
  • 17. Threads ❖ Every thread can have it’s own Isolate (Isolated V8 Instance) ❖ V8Thread is a Java Thread with an associated Isolate ❖ Provide an easy way to execute JavaScript Thread t = new V8Thread(new V8Runnable() { public void run(V8 v8) { int result = v8.executeIntegerScript("1+2"); } }); t.start();
  • 18. Executors ❖ Long running V8Thread with a message queue and event loop ❖ Threads can communicate via message passing ❖ Useful for implementing Web Workers / Service Workers
  • 19. ES 6 ❖ Snapshot builds of J2V8 support V8 4.10 & ES 6 ❖ Arrows ❖ Classes ❖ Let / Const ❖ Interators + For..Of ❖ Generators ❖ …
  • 20. Debug Support ❖ V8 (and now J2V8) no longer supports the Debug Agent ❖ JavaScript based Debug API is available instead ❖ J2V8 exposes this API in Java ❖ Integrated with the Stetho tool & Chrome Dev Tools
  • 22. Node.js ❖ JavaScript Virtual Machine (V8) ❖ Modules ❖ Native ❖ JavaScript ❖ Event Loop Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
  • 23. Bridging to Node.js ❖ Out of process Node & REST Services ❖ Vert.x ❖ Node engine on Nashorn / Rhino?
  • 24. Node4J ❖ Dynamically link Node.js to the JVM ❖ Access Node.js context via JNI ❖ Execute Node.js modules (require) ❖ Callbacks to Java ❖ Process Node.js message queue
  • 25. Node4J Demo public static void main(final String[] args) throws Exception { final V8 v8 = V8.createV8Runtime("global"); v8.registerJavaMethod(…); NodeJS node = V8.createNodeJS(v8); V8Object exports = node.requireScript(nodeCode, "http"); exports.release(); boolean running = true; while (running) { running = node.pumpMessageLoop(); } }
  • 26. Performance Considerations ❖ Minimize callbacks from JavaScript to Java ❖ ~4000 Per Second on my MBP ❖ Use bulk array copy to move primitives from JS to Java ❖ 60fps in our animation demo
  • 27. Resources ❖ Getting started with J2V8 ❖ Registering Java Callbacks with J2V8 ❖ Implementing WebWorkers with J2V8 ❖ Multithreaded JavaScript with J2V8 ❖ Using J2V8 with Heroku ❖ All linked from our GitHub Page
  • 28. Future Work ❖ Advanced exception handling between Java and JS ❖ Improved debug support ❖ Typed array access in Java ❖ You tell me?
  • 29. Using J2V8 ❖ J2V8 is available in Maven Central ❖ Currently 5 variants are available: com.eclipsesource.j2v8.j2v8_win32_x86:3.1.6
 com.eclipsesource.j2v8.j2v8_macosx_x86_64:3.1.6
 com.eclipsesource.j2v8.j2v8:3.1.6 (aar)
 com.eclipsesource.j2v8.j2v8_android_armv7l:3.1.6
 com.eclipsesource.j2v8.j2v8_android_x86:3.1.6 ❖ j2v8:3.1.6 (aar) contains both x86 and armv7l
  • 30. 4.0!
  • 31. Thank-you ❖ Open Source Java bindings for V8 ❖ Node4J extensions bring Node.js to Java ❖ Licensed under the EPL ❖ For J2V8 news, follow me on Twitter @irbull https://github.com/eclipsesource/j2v8