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

Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
FIDO Alliance
 

Recently uploaded (20)

Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptx
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 

Java scriptcore brief introduction