SlideShare a Scribd company logo
1 of 75
JavaScript Core 概述
陈浩
Jun/2013
Agenda
• JavaScript
• JavaScript Core & WebCore
• JavaScript VM
• JavaScript Compiler
• JavaScript JIT
• And ……
JavaScript
JavaScript
• Garbage collection
• Dynamic typing
• No classes, prototypes allow OOP
• eval, call, apply, inner functions, closures
JavaScript - OOP
JavaScript – Closure & Scope Chain
id win = [webView windowScriptObject];
id location = [win valueForKey:@"location"];
NSString *href = [location valueForKey:@"href"];
JavaScript in Objective-C
JavaScript Core and WebCore
模块
JavaScript – Running Environment
• VM
• Global Object
• Execution Context
– Exec State
Property Access
Execution Context
.Global Code
.Function Code
.Eval Code
JS Binding
• W3C Web IDL
JS Binding
JS Binding
Console
JavaScript Execution
页面解析相关
执行相关
JavaScript Execution
JavaScript Execution
Script Element
HTML Parser
JavaScript Execution
JS blocked by CSS
JavaScript Execution - async
JavaScript Execution - defer
W3C Standard
• Running a script
– <LINK>
Inside of JSC
Running in JSC
模块功能
Front-end
Back-end
前端功能组
后端功能组
Parser
Lexer & Parser
i=3;
Variable Equal Constant End
To assign 3 to variable i.
VM
• Register-Based
[ 0] enter
[ 1] mov r0, Cell: 0133FC40(@k0)
[ 4] put_by_id r0, a(@id0), Int32:
100(@k1)
[ 13] mov r0, Cell: 0133FC40(@k0)
[ 16] put_by_id r0, b(@id1), Int32:
200(@k2)
[ 25] mov r0, Cell: 0133FC40(@k0)
[ 28] put_by_id r0, c(@id2), Int32:
300(@k3)
[ 37] resolve_global r0, a(@id0)
[ 43] resolve_global r1, b(@id1)
[ 49] add r0, r0, r1
[ 54] resolve_global r1, c(@id2)
[ 60] mul r0, r0, r1
[ 65] ret r0
VM
• Stack Based 0: bipush 100
2: istore_1
3: sipush 200
6: istore_2
7: sipush 300
10: istore_3
11: iload_1
12: iload_2
13: iadd
14: iload_3
15: imul
16: ireturn
function sub(a,b)
{
return a-b;
}
function add(a,b)
{
return a+b;
}
i=0;
while(i<3000)
{
value = add(i,i+2);
i=i+1;
}
value = value + 1;
[ 0] enter
[ 1] mov r0, Undefined(@k0)
[ 4] resolve_base r1, i(@id0), 20975696, 20978944
[ 11] mov r0, Int32: 0(@k1)
[ 14] put_to_base r1, i(@id0), r0, 20978944
[ 19] resolve r1, i(@id0), 20975712
[ 24] jnless r1, Int32: 3000(@k2), 78(->102)
[ 28] loop_hint
[ 0] enter
[ 1] add r0, r-8, r-9
[ 6] ret r0
Low Level Interpreter
• 执行ByteCode
• Offlineasm
Bytecode
[ 32] put_to_base r1, s(@id0), r0, 35719584
[ 37] resolve_base r1, i(@id2), 35719328, 35719616
[ 44] mov r0, Int32: 0(@k3)
[ 47] put_to_base r1, i(@id2), r0, 35719616
[ 52] resolve r1, i(@id2), 35719344
[ 57] jnless r1, Int32: 3000(@k4), 78(->135)
[ 61] loop_hint
[ 62] resolve_base r1, value(@id3), 35719360, 35719648
[ 69] resolve_with_this r5, r2, add(@id4), 35719376
[ 75] resolve r4, i(@id2), 35719344
[ 80] resolve r6, i(@id2), 35719344
[ 0] enter
[ 1] sub r0, r-8, r-9
[ 6] ret r0
JIT – Just in time
• 编译为本地代码
• 占用执行时间
JIT - Interpreter
• Classic Interpreter
– Implementation
• while(1) { switch(…) …}
– Easy implementation
– Performance limitation
• Obsoleted in WebKit.
JIT
• ◮ Compiler generates machine code from bytecode (jit/*)
• ◮ Big switch with emit-calls
• ◮ Uses target specific macro assembler and assembler classes
• ◮ Target specific assember constructs instruction words
• ◮ Register allocation
Baseline JIT
• 与Interpreter相似, 执行ByteCode
• 转为机器码执行
DFG JIT
• Data Flow Graph JIT
• 推测优化、局部寄存器分配
• 基于数据流图的类型推测
– SSA
Traced JIT
Execution – Dynamic Compiler
Type Inference
• 对已知类型的数据操作要比查表法快得多,所以需要越早
知道数据类型,越有利于提升性能。
Type Inference
o.x * o.x + o.y * o.y
1. Type Prediction
2. Type Checks
Garbage Collection
• Mark – Sweep
– Reference Counting
– Bitmap
Forward …
Compiler
SSA
• 转为直线型代码,优化的基础
Trampolines
• 机器码函数存根
• JIT编译后,替换为真实函数的指针。
Trampolines
Inline Cache
Inline Cache
• Polymorphic Inline Cache
– Source Code:PolymorphicPutByIdList.h/.cpp
Common Sub-expression Elimination
• Value numbering
• CSE
Register Allocation
• Linear Scan
• Register Allocation
RegisterID *BytecodeGenerator::newRegister()
Generational GC
• Card marking Write Barriers
Tracer:
Marks and traces
Java Mutator:
Modifies Blue and Green objects
Write barrier on objects
Tracer:
Traces rest of graph
Tracer:
Clean blue object
Any more…
• LLVM IR to JavaScript (Emscripten)
– Python,Ruby,Lua for WebBrowser
– C++ to JavaScript
• JavaScript on LLVM
– Farbic Engine
VM/JIT
Garbage Collection
• Pause time
• Pause predictability
• CPU usage
• Memory footprint
• Virtual memory interaction
• Cache interaction
• Effects on program locality
• Compiler and runtime impact
• ECMA Harmony
– 6th version
Key Reference
• Virtual Machine & JavaScript Engine
– <LINK>
Java scriptcore brief introduction

More Related Content

Viewers also liked

程序员发展漫谈
程序员发展漫谈程序员发展漫谈
程序员发展漫谈
Horky Chen
 
Rm 1 Intro Types Research Process
Rm   1   Intro Types   Research ProcessRm   1   Intro Types   Research Process
Rm 1 Intro Types Research Process
itsvineeth209
 

Viewers also liked (19)

Design in construction
Design in constructionDesign in construction
Design in construction
 
Integration
IntegrationIntegration
Integration
 
Code tuning techniques
Code tuning techniquesCode tuning techniques
Code tuning techniques
 
Defencive programming
Defencive programmingDefencive programming
Defencive programming
 
MOST_OpenFoundry_version control system_Git
MOST_OpenFoundry_version control system_GitMOST_OpenFoundry_version control system_Git
MOST_OpenFoundry_version control system_Git
 
Coding Style
Coding StyleCoding Style
Coding Style
 
代码大全(内训)
代码大全(内训)代码大全(内训)
代码大全(内训)
 
程序员发展漫谈
程序员发展漫谈程序员发展漫谈
程序员发展漫谈
 
程序员实践之路
程序员实践之路程序员实践之路
程序员实践之路
 
高品質軟體的基本動作 101 + 102 for NUU
高品質軟體的基本動作 101 + 102 for NUU高品質軟體的基本動作 101 + 102 for NUU
高品質軟體的基本動作 101 + 102 for NUU
 
Design in construction
Design in constructionDesign in construction
Design in construction
 
A Guideline to Test Your Own Code - Developer Testing
A Guideline to Test Your Own Code - Developer TestingA Guideline to Test Your Own Code - Developer Testing
A Guideline to Test Your Own Code - Developer Testing
 
Variables
VariablesVariables
Variables
 
Code tuning strategies
Code tuning strategiesCode tuning strategies
Code tuning strategies
 
高品質軟體的基本動作 101 for NTHU
高品質軟體的基本動作 101 for NTHU高品質軟體的基本動作 101 for NTHU
高品質軟體的基本動作 101 for NTHU
 
Code Complete
Code CompleteCode Complete
Code Complete
 
Code Tuning
Code TuningCode Tuning
Code Tuning
 
The pseudocode
The pseudocodeThe pseudocode
The pseudocode
 
Rm 1 Intro Types Research Process
Rm   1   Intro Types   Research ProcessRm   1   Intro Types   Research Process
Rm 1 Intro Types Research Process
 

Similar to Java scriptcore brief introduction

Jvm operation casual talks
Jvm operation casual talksJvm operation casual talks
Jvm operation casual talks
Yusaku Watanabe
 
Spring 4-groovy
Spring 4-groovySpring 4-groovy
Spring 4-groovy
GR8Conf
 
Bh ad-12-stealing-from-thieves-saher-slides
Bh ad-12-stealing-from-thieves-saher-slidesBh ad-12-stealing-from-thieves-saher-slides
Bh ad-12-stealing-from-thieves-saher-slides
Matt Kocubinski
 
How to integrate front end tool via gruntjs
How to integrate front end tool via gruntjsHow to integrate front end tool via gruntjs
How to integrate front end tool via gruntjs
Bo-Yi Wu
 

Similar to Java scriptcore brief introduction (20)

Running JavaScript Efficiently in a Java World
Running JavaScript Efficiently in a Java WorldRunning JavaScript Efficiently in a Java World
Running JavaScript Efficiently in a Java World
 
React inter3
React inter3React inter3
React inter3
 
Js tacktalk team dev js testing performance
Js tacktalk team dev js testing performanceJs tacktalk team dev js testing performance
Js tacktalk team dev js testing performance
 
Structured Streaming with Apache Spark
Structured Streaming with Apache SparkStructured Streaming with Apache Spark
Structured Streaming with Apache Spark
 
.NET 7 Performance Improvements_10_03_2023.pdf
.NET 7 Performance Improvements_10_03_2023.pdf.NET 7 Performance Improvements_10_03_2023.pdf
.NET 7 Performance Improvements_10_03_2023.pdf
 
Hujs 总结
Hujs 总结Hujs 总结
Hujs 总结
 
How to test code with mruby
How to test code with mrubyHow to test code with mruby
How to test code with mruby
 
Jvm operation casual talks
Jvm operation casual talksJvm operation casual talks
Jvm operation casual talks
 
Hybrid Tips & Tricks
Hybrid Tips & TricksHybrid Tips & Tricks
Hybrid Tips & Tricks
 
Marvel of Annotation Preprocessing in Java by Alexey Buzdin
Marvel of Annotation Preprocessing in Java by Alexey BuzdinMarvel of Annotation Preprocessing in Java by Alexey Buzdin
Marvel of Annotation Preprocessing in Java by Alexey Buzdin
 
Introduction to Grunt.js on Taiwan JavaScript Conference
Introduction to Grunt.js on Taiwan JavaScript ConferenceIntroduction to Grunt.js on Taiwan JavaScript Conference
Introduction to Grunt.js on Taiwan JavaScript Conference
 
Scripting Oracle Develop 2007
Scripting Oracle Develop 2007Scripting Oracle Develop 2007
Scripting Oracle Develop 2007
 
Android OpenGL ES Game ImageGrabber Final Report
Android OpenGL ES Game ImageGrabber Final ReportAndroid OpenGL ES Game ImageGrabber Final Report
Android OpenGL ES Game ImageGrabber Final Report
 
Spring 4-groovy
Spring 4-groovySpring 4-groovy
Spring 4-groovy
 
Relational Database Access with Python ‘sans’ ORM
Relational Database Access with Python ‘sans’ ORM  Relational Database Access with Python ‘sans’ ORM
Relational Database Access with Python ‘sans’ ORM
 
Relational Database Access with Python
Relational Database Access with PythonRelational Database Access with Python
Relational Database Access with Python
 
Bh ad-12-stealing-from-thieves-saher-slides
Bh ad-12-stealing-from-thieves-saher-slidesBh ad-12-stealing-from-thieves-saher-slides
Bh ad-12-stealing-from-thieves-saher-slides
 
How to integrate front end tool via gruntjs
How to integrate front end tool via gruntjsHow to integrate front end tool via gruntjs
How to integrate front end tool via gruntjs
 
Padrino - the Godfather of Sinatra
Padrino - the Godfather of SinatraPadrino - the Godfather of Sinatra
Padrino - the Godfather of Sinatra
 
Run Node Run
Run Node RunRun Node Run
Run Node Run
 

More from Horky Chen (7)

编程语言与自然语言
编程语言与自然语言编程语言与自然语言
编程语言与自然语言
 
Tow points of WebKit in design
Tow points of WebKit in designTow points of WebKit in design
Tow points of WebKit in design
 
第五项修炼 (学习型组织的艺术与实践)
第五项修炼 (学习型组织的艺术与实践)第五项修炼 (学习型组织的艺术与实践)
第五项修炼 (学习型组织的艺术与实践)
 
注重实效的编程(3)
注重实效的编程(3)注重实效的编程(3)
注重实效的编程(3)
 
注重实效的编程(2)
注重实效的编程(2)注重实效的编程(2)
注重实效的编程(2)
 
注重实效的编程(1)
注重实效的编程(1)注重实效的编程(1)
注重实效的编程(1)
 
JavaScript closures
JavaScript closuresJavaScript closures
JavaScript closures
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

Java scriptcore brief introduction