SlideShare a Scribd company logo
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
模块
Java scriptcore brief introduction
JavaScript – Running Environment
• VM
• Global Object
• Execution Context
– Exec State
Java scriptcore brief introduction
Property Access
Execution Context
.Global Code
.Function Code
.Eval Code
Java scriptcore brief introduction
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
前端功能组
后端功能组
Java scriptcore brief introduction
Parser
Lexer & Parser
i=3;
Variable Equal Constant End
To assign 3 to variable i.
Java scriptcore brief introduction
Java scriptcore brief introduction
Java scriptcore brief introduction
Java scriptcore brief introduction
Java scriptcore brief introduction
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
Java scriptcore brief introduction
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
Java scriptcore brief introduction
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
Java scriptcore brief introduction
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

PDF
Why Grails?
PPT
ELK stack at weibo.com
PDF
iOS Development with RubyMotion
PPTX
More kibana
PDF
Logging logs with Logstash - Devops MK 10-02-2016
PDF
Managing GraphQL servers with AWS Fargate & Prisma Cloud
PDF
GraalVMの紹介とTruffleでPHPぽい言語を実装したら爆速だった話
PPTX
Attack monitoring using ElasticSearch Logstash and Kibana
Why Grails?
ELK stack at weibo.com
iOS Development with RubyMotion
More kibana
Logging logs with Logstash - Devops MK 10-02-2016
Managing GraphQL servers with AWS Fargate & Prisma Cloud
GraalVMの紹介とTruffleでPHPぽい言語を実装したら爆速だった話
Attack monitoring using ElasticSearch Logstash and Kibana

Viewers also liked (19)

PDF
Design in construction
PDF
Integration
PDF
Code tuning techniques
PDF
Defencive programming
PDF
MOST_OpenFoundry_version control system_Git
PDF
Coding Style
PPT
代码大全(内训)
PDF
程序员发展漫谈
PPTX
程序员实践之路
PDF
高品質軟體的基本動作 101 + 102 for NUU
PDF
Design in construction
PPT
A Guideline to Test Your Own Code - Developer Testing
PPTX
Variables
PDF
Code tuning strategies
PDF
高品質軟體的基本動作 101 for NTHU
PPT
Code Tuning
PDF
The pseudocode
PPT
Rm 1 Intro Types Research Process
Design in construction
Integration
Code tuning techniques
Defencive programming
MOST_OpenFoundry_version control system_Git
Coding Style
代码大全(内训)
程序员发展漫谈
程序员实践之路
高品質軟體的基本動作 101 + 102 for NUU
Design in construction
A Guideline to Test Your Own Code - Developer Testing
Variables
Code tuning strategies
高品質軟體的基本動作 101 for NTHU
Code Tuning
The pseudocode
Rm 1 Intro Types Research Process
Ad

More from Horky Chen (7)

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

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Encapsulation theory and applications.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Cloud computing and distributed systems.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Review of recent advances in non-invasive hemoglobin estimation
Mobile App Security Testing_ A Comprehensive Guide.pdf
Empathic Computing: Creating Shared Understanding
NewMind AI Weekly Chronicles - August'25-Week II
MIND Revenue Release Quarter 2 2025 Press Release
The Rise and Fall of 3GPP – Time for a Sabbatical?
Reach Out and Touch Someone: Haptics and Empathic Computing
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Diabetes mellitus diagnosis method based random forest with bat algorithm
A comparative analysis of optical character recognition models for extracting...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Machine learning based COVID-19 study performance prediction
Unlocking AI with Model Context Protocol (MCP)
Encapsulation theory and applications.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Electronic commerce courselecture one. Pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Cloud computing and distributed systems.
Digital-Transformation-Roadmap-for-Companies.pptx

Java scriptcore brief introduction