SlideShare a Scribd company logo
1 of 43
Download to read offline
javascripts 
in the javascripts 
ffconf 2014 
andy wingo
the 
es6 
circus 
is 
coming 
to 
town 
es-discuss clownshoes 
C++ knife-jugglers 
JavaScript acrobats
building 
es.next 
in 
es.now 
Hark, an agenda: 
❧ Why? 
❧ How: JavaScriptCore 
❧ How: SpiderMonkey 
❧ How: V8
why 
implement 
js in 
js?
js is 
faster 
than 
c++
js is 
faster 
than 
c++ 
JS can optimize in ways that C++ can’t 
❧ dynamic inlining 
inline allocation (and possibly 
scalar replacement) 
❧ 
inline hard-wiring of user object 
shapes (slot offsets, getters) 
❧
js is 
faster 
than 
c++ 
No JS/C++ transition cost 
Especially important for callbacks (e.g. 
forEach)
js is 
faster 
than 
c++ 
JavaScriptCore’s Oliver Hunt, January 
2014: 
“The initial proof of concept is 
Array.prototype.every, this shows a 
65% performance improvement, and 
that improvement is significantly hurt 
by our poor optimisation of op_in.”
js 
matches 
js 
semantics 
better 
Proxies, accessors, order of effects, 
has-property versus get-property, 
user-implemented iteration protocol, 
exceptions, catch 
Terse: 
for (var x of y) z(x);
js 
more 
secure 
than 
c++ 
GC-related bugs approximately 
impossible 
❧ SM, V8; JSC immune 
No C++ knife-throwing work-related 
accidents 
❧ integer overflow, use-after-free, etc 
Cross-iframe leakage concerns 
lessened
choosy 
hackers 
choose 
js 
Goal: As much in JS as possible 
For speed, for security, for 
maintainability 
How?
simplest 
model: 
javascriptcore 
“Methods can be implemented in JS”
example Source/JavaScriptCore/builtins/ 
Array.prototype.js 
function foo() { 
return 'ahoy ffconf'; 
} 
Source/JavaScriptCore/runtime/ 
ArrayPrototype.cpp 
foo arrayProtoFuncFoo DontEnum|Function 0
weird 
js: jsc 
edition 
Function source compiled separately 
Access to globals forbidden in general 
Initial values of globals accessible via @ 
prefix, e.g. @Object 
Add @call and @apply 
http://svn.webkit.org/repository/ 
webkit/trunk@163195
more 
complicated: 
spider 
monkey 
“Self-hosted JS” files concatenated and 
evaluated – more normal model 
C++ binds functions by name to 
prototype properties
feature: 
es.next 
‘pipelines’ 
Old SpiderMonkey: 
(x*2 for (x in [0,1,2].keys())) 
Erstwhile ES6: 
(for (x of [0,1,2].keys()) x*2) 
Maybe ES7: 
[0,1,2].keys().map(x=>x*2) 
Ideally on IteratorPrototype, but 
let’s hack it
example js/src/builtin/Iterator.js 
function* IteratorMap(f) { 
for (var x of this) yield f(x); 
}
example No function* at boot-time :( 
But, ES6 object literals 
function IteratorMap(f) { 
var iter = this[std_iterator](); 
return { 
next(val) { 
var result = iter.next(val) 
return result.done ? result : { 
value: callFunction(f, iter, 
result.value), 
done: false 
}; 
}, 
[std_iterator]: IteratorIdentity, 
} 
}
example Link to C++ files; grep for surrounding 
identifiers, make similar modifications 
(e.g. in jsiter.cpp) 
js> for (var x of [1,2,3].keys().map(x=>x*2)) 
print(x) 
024
nerf 
the 
web 
forward
nerf 
the 
web 
forward 
Your search - "nerf the web forward" - 
did not match any documents.
nerf 
the 
web 
forward 
(like, nerf is like a more resilient 
polystyrene foam)
nerf 
the 
web 
forward 
(the more joke explanation slides, the 
more amusing the joke, right?)
nerf 
the 
web 
forward 
(right?)
caveats @@iterator called before or after first 
next()? 
Prototype chain of the result of map()? 
Should final result.value be 
mapped? 
%IteratorPrototype% 
No spec; spec wonkiness 
throw()? 
next() applied to different object?
v8 Story time!
languages 
are 
like 
operating 
systems 
Visit a page : Install an app 
Visit about:blank : Boot OS 
Weird self-hosted JS part of OS, not 
app
genesis In the beginning, there was the empty 
function 
and the Object function 
and its prototype property
genesis And Goog looked upon it and saw that 
it was good
genesis Then the strict mode function “maps” 
(hidden classes) 
Then the first global object 
Then Array, Number, Boolean, String, 
Symbol, Date, RegExp, JSON, 
ArrayBuffer, the TypedArrays, Map, 
Set, iterator result shapes, WeakMap, 
WeakSet, arguments object shapes, ...
genesis And Goog looked upon them and saw 
that they were good
genesis And Goog looked upon them and saw 
that they were good 
But FFS it’s a lot of C++, innit?
how 2 
js 
Problem: Need to define helpers in JS, 
but they shouldn’t be in the user’s 
scope 
Solution: Second global object for self-hosted 
JS to play in; natives mutate to 
produce a more beautiful global
builtins, 
globals 
Global: A global object, corresponding 
to a user-facing script-level scope 
builtins: The global object current 
when self-hosted JS is being defined 
In builtins, user-facing global bound 
to global 
Somewhat confusingly, in V8, “self-hosted 
JS facilities” are called “natives”
on the 
seventh 
day 
So, “natives”. That’s JavaScript y’all!
example src/generator.js 
function* GeneratorObjectMap(f) { 
for (var x of this) yield f(x); 
}
weird 
js, v8 
edition 
Verbs 
% prefix for low-level C++ runtime 
functions (--allow-natives-syntax) 
❧ 
%_ prefix for magical “inline” 
runtime functions (%_CallFunction, 
%_IsSmi) 
❧ 
❧ macros (TO_UINT32, IS_NUMBER)
weird 
js, v8 
edition 
Nouns too 
❧ global 
InternalArray (to allow builtins to 
use .push() without worrying 
about user pollution) 
❧ 
Suggested reading order 
❧ runtime.js 
❧ v8natives.js 
❧ array.js
snapshots Lots of work amirite? 
Optimization: Serialize heap of new-born 
world 
Load fresh heap from disk to “boot” 
Necessary in context of Chrome’s 
multi-process model
note: 
the 
dom is 
something 
else 
“Blink-in-JS” 
Kentaro Haro: DOM binding overhead 
is 5-15% in real web 
DOM objects live in a 1-to-N 
relationship to V8 globals 
Search for “Hardening security of 
content scripts”
but 
seriously 
Strict spec reading 
Strict spec translation (optimize later if 
ever) 
Tests (especially proxies, getters, order 
of operations) 
Patch submission 
Feature flags (in v8)
tx nerf the web forward! 
wingo@igalia.com 
http://wingolog.org/ 
. 
big kid circus, by ray forster: https:// 
www.flickr.com/photos/ 
94418464@N08/8686092191

More Related Content

What's hot

Jscex: Write Sexy JavaScript
Jscex: Write Sexy JavaScriptJscex: Write Sexy JavaScript
Jscex: Write Sexy JavaScript
jeffz
 
Jscex: Write Sexy JavaScript (中文)
Jscex: Write Sexy JavaScript (中文)Jscex: Write Sexy JavaScript (中文)
Jscex: Write Sexy JavaScript (中文)
jeffz
 
Javascript Uncommon Programming
Javascript Uncommon ProgrammingJavascript Uncommon Programming
Javascript Uncommon Programming
jeffz
 
The Evolution of Async-Programming on .NET Platform (TUP, Full)
The Evolution of Async-Programming on .NET Platform (TUP, Full)The Evolution of Async-Programming on .NET Platform (TUP, Full)
The Evolution of Async-Programming on .NET Platform (TUP, Full)
jeffz
 
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
Nick Sieger
 

What's hot (20)

Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
响应式编程及框架
响应式编程及框架响应式编程及框架
响应式编程及框架
 
ES6 is Nigh
ES6 is NighES6 is Nigh
ES6 is Nigh
 
Jscex: Write Sexy JavaScript
Jscex: Write Sexy JavaScriptJscex: Write Sexy JavaScript
Jscex: Write Sexy JavaScript
 
Jscex: Write Sexy JavaScript (中文)
Jscex: Write Sexy JavaScript (中文)Jscex: Write Sexy JavaScript (中文)
Jscex: Write Sexy JavaScript (中文)
 
Javascript tid-bits
Javascript tid-bitsJavascript tid-bits
Javascript tid-bits
 
History & Practices for UniRx(EN)
History & Practices for UniRx(EN)History & Practices for UniRx(EN)
History & Practices for UniRx(EN)
 
Streams in Node.js
Streams in Node.jsStreams in Node.js
Streams in Node.js
 
Javascript Uncommon Programming
Javascript Uncommon ProgrammingJavascript Uncommon Programming
Javascript Uncommon Programming
 
The Evolution of Async-Programming on .NET Platform (TUP, Full)
The Evolution of Async-Programming on .NET Platform (TUP, Full)The Evolution of Async-Programming on .NET Platform (TUP, Full)
The Evolution of Async-Programming on .NET Platform (TUP, Full)
 
Letusgo 2019 Summer - StringInterpolation and SwiftUI
Letusgo 2019 Summer - StringInterpolation and SwiftUILetusgo 2019 Summer - StringInterpolation and SwiftUI
Letusgo 2019 Summer - StringInterpolation and SwiftUI
 
Introduction to modern c++ principles(part 1)
Introduction to modern c++ principles(part 1)Introduction to modern c++ principles(part 1)
Introduction to modern c++ principles(part 1)
 
Modern JS with ES6
Modern JS with ES6Modern JS with ES6
Modern JS with ES6
 
PyData Berlin Meetup
PyData Berlin MeetupPyData Berlin Meetup
PyData Berlin Meetup
 
"Node.js threads for I/O-bound tasks", Timur Shemsedinov
"Node.js threads for I/O-bound tasks", Timur Shemsedinov"Node.js threads for I/O-bound tasks", Timur Shemsedinov
"Node.js threads for I/O-bound tasks", Timur Shemsedinov
 
Kotlin is charming; The reasons Java engineers should start Kotlin.
Kotlin is charming; The reasons Java engineers should start Kotlin.Kotlin is charming; The reasons Java engineers should start Kotlin.
Kotlin is charming; The reasons Java engineers should start Kotlin.
 
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
 
ES6 in Production [JSConfUY2015]
ES6 in Production [JSConfUY2015]ES6 in Production [JSConfUY2015]
ES6 in Production [JSConfUY2015]
 
JRuby @ Boulder Ruby
JRuby @ Boulder RubyJRuby @ Boulder Ruby
JRuby @ Boulder Ruby
 
Nick Sieger JRuby Concurrency EMRubyConf 2011
Nick Sieger JRuby Concurrency EMRubyConf 2011Nick Sieger JRuby Concurrency EMRubyConf 2011
Nick Sieger JRuby Concurrency EMRubyConf 2011
 

Viewers also liked

Recent Improvements in Epiphany and WebKitGTK+ (GUADEC 2015)
Recent Improvements in Epiphany and WebKitGTK+ (GUADEC 2015)Recent Improvements in Epiphany and WebKitGTK+ (GUADEC 2015)
Recent Improvements in Epiphany and WebKitGTK+ (GUADEC 2015)
Igalia
 
A Short Introduction to Servo (Web Engines Hackfest 2014)
A Short Introduction to Servo (Web Engines Hackfest 2014)A Short Introduction to Servo (Web Engines Hackfest 2014)
A Short Introduction to Servo (Web Engines Hackfest 2014)
Igalia
 
CSS Grid Layout. Specification overview. Implementation status and roadmap (B...
CSS Grid Layout. Specification overview. Implementation status and roadmap (B...CSS Grid Layout. Specification overview. Implementation status and roadmap (B...
CSS Grid Layout. Specification overview. Implementation status and roadmap (B...
Igalia
 

Viewers also liked (12)

Property-based testing an open-source compiler, pflua (FOSDEM 2015)
Property-based testing an open-source compiler, pflua (FOSDEM 2015)Property-based testing an open-source compiler, pflua (FOSDEM 2015)
Property-based testing an open-source compiler, pflua (FOSDEM 2015)
 
How to test OpenGL drivers using Free Software (FOSDEM 2015)
How to test OpenGL drivers using Free Software (FOSDEM 2015)How to test OpenGL drivers using Free Software (FOSDEM 2015)
How to test OpenGL drivers using Free Software (FOSDEM 2015)
 
Recent Improvements in Epiphany and WebKitGTK+ (GUADEC 2015)
Recent Improvements in Epiphany and WebKitGTK+ (GUADEC 2015)Recent Improvements in Epiphany and WebKitGTK+ (GUADEC 2015)
Recent Improvements in Epiphany and WebKitGTK+ (GUADEC 2015)
 
A Short Introduction to Servo (Web Engines Hackfest 2014)
A Short Introduction to Servo (Web Engines Hackfest 2014)A Short Introduction to Servo (Web Engines Hackfest 2014)
A Short Introduction to Servo (Web Engines Hackfest 2014)
 
Pairing WebKit and Wayland for Linux-Based Embedded Web Content Presentation ...
Pairing WebKit and Wayland for Linux-Based Embedded Web Content Presentation ...Pairing WebKit and Wayland for Linux-Based Embedded Web Content Presentation ...
Pairing WebKit and Wayland for Linux-Based Embedded Web Content Presentation ...
 
GStreamer support in WebKit. What's new? (GStreamer Conference 2015)
GStreamer support in WebKit. What's new? (GStreamer Conference 2015)GStreamer support in WebKit. What's new? (GStreamer Conference 2015)
GStreamer support in WebKit. What's new? (GStreamer Conference 2015)
 
CSS Grid Layout is Just Around the Corner (CSSConf US 2015)
CSS Grid Layout is Just Around the Corner (CSSConf US 2015)CSS Grid Layout is Just Around the Corner (CSSConf US 2015)
CSS Grid Layout is Just Around the Corner (CSSConf US 2015)
 
New interoperability features in LibreOffice
New interoperability features in LibreOfficeNew interoperability features in LibreOffice
New interoperability features in LibreOffice
 
CSS Grid Layout. Specification overview. Implementation status and roadmap (B...
CSS Grid Layout. Specification overview. Implementation status and roadmap (B...CSS Grid Layout. Specification overview. Implementation status and roadmap (B...
CSS Grid Layout. Specification overview. Implementation status and roadmap (B...
 
Efficient multimedia support in QtWebKit on Raspberry Pi (GStreamer Conferenc...
Efficient multimedia support in QtWebKit on Raspberry Pi (GStreamer Conferenc...Efficient multimedia support in QtWebKit on Raspberry Pi (GStreamer Conferenc...
Efficient multimedia support in QtWebKit on Raspberry Pi (GStreamer Conferenc...
 
GStreamer-VAAPI: Hardware-accelerated encoding and decoding on Intel hardware...
GStreamer-VAAPI: Hardware-accelerated encoding and decoding on Intel hardware...GStreamer-VAAPI: Hardware-accelerated encoding and decoding on Intel hardware...
GStreamer-VAAPI: Hardware-accelerated encoding and decoding on Intel hardware...
 
Spelunking through JPEG with Racket (Sixth RacketCon)
Spelunking through JPEG with Racket (Sixth RacketCon)Spelunking through JPEG with Racket (Sixth RacketCon)
Spelunking through JPEG with Racket (Sixth RacketCon)
 

Similar to Self-hosted JS (ffconf 2014)

JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
David Padbury
 
JavaScript Libraries: The Big Picture
JavaScript Libraries: The Big PictureJavaScript Libraries: The Big Picture
JavaScript Libraries: The Big Picture
Simon Willison
 
Why Node.js
Why Node.jsWhy Node.js
Why Node.js
guileen
 
Ajax tutorial
Ajax tutorialAjax tutorial
Ajax tutorial
Kat Roque
 
Modeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based GamesModeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based Games
Ray Toal
 
Microsoft PowerPoint - <b>jQuery</b>-1-Ajax.pptx
Microsoft PowerPoint - <b>jQuery</b>-1-Ajax.pptxMicrosoft PowerPoint - <b>jQuery</b>-1-Ajax.pptx
Microsoft PowerPoint - <b>jQuery</b>-1-Ajax.pptx
tutorialsruby
 

Similar to Self-hosted JS (ffconf 2014) (20)

JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
RxJS101 - What you need to know to get started with RxJS tomorrow
RxJS101 - What you need to know to get started with RxJS tomorrowRxJS101 - What you need to know to get started with RxJS tomorrow
RxJS101 - What you need to know to get started with RxJS tomorrow
 
The State of JavaScript
The State of JavaScriptThe State of JavaScript
The State of JavaScript
 
Java 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from OredevJava 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from Oredev
 
Json generation
Json generationJson generation
Json generation
 
JavaScript Libraries: The Big Picture
JavaScript Libraries: The Big PictureJavaScript Libraries: The Big Picture
JavaScript Libraries: The Big Picture
 
Node.JS briefly introduced
Node.JS briefly introducedNode.JS briefly introduced
Node.JS briefly introduced
 
Why Nodejs Guilin Shanghai
Why Nodejs Guilin ShanghaiWhy Nodejs Guilin Shanghai
Why Nodejs Guilin Shanghai
 
Why Node.js
Why Node.jsWhy Node.js
Why Node.js
 
Progscon 2017: Taming the wild fronteer - Adventures in Clojurescript
Progscon 2017: Taming the wild fronteer - Adventures in ClojurescriptProgscon 2017: Taming the wild fronteer - Adventures in Clojurescript
Progscon 2017: Taming the wild fronteer - Adventures in Clojurescript
 
Ajax tutorial
Ajax tutorialAjax tutorial
Ajax tutorial
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 Engine
 
JS everywhere 2011
JS everywhere 2011JS everywhere 2011
JS everywhere 2011
 
NodeJS
NodeJSNodeJS
NodeJS
 
Jet presentation
Jet presentationJet presentation
Jet presentation
 
Node.js - async for the rest of us.
Node.js - async for the rest of us.Node.js - async for the rest of us.
Node.js - async for the rest of us.
 
Scala.js for large and complex frontend apps
Scala.js for large and complex frontend appsScala.js for large and complex frontend apps
Scala.js for large and complex frontend apps
 
Modeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based GamesModeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based Games
 
Play framework
Play frameworkPlay framework
Play framework
 
Microsoft PowerPoint - <b>jQuery</b>-1-Ajax.pptx
Microsoft PowerPoint - <b>jQuery</b>-1-Ajax.pptxMicrosoft PowerPoint - <b>jQuery</b>-1-Ajax.pptx
Microsoft PowerPoint - <b>jQuery</b>-1-Ajax.pptx
 

More from Igalia

Building End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPEBuilding End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPE
Igalia
 
Automated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded DevicesAutomated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded Devices
Igalia
 
Running JS via WASM faster with JIT
Running JS via WASM      faster with JITRunning JS via WASM      faster with JIT
Running JS via WASM faster with JIT
Igalia
 
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por IgaliaIntroducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Igalia
 

More from Igalia (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Building End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPEBuilding End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPE
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Automated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded DevicesAutomated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded Devices
 
Embedding WPE WebKit - from Bring-up to Maintenance
Embedding WPE WebKit - from Bring-up to MaintenanceEmbedding WPE WebKit - from Bring-up to Maintenance
Embedding WPE WebKit - from Bring-up to Maintenance
 
Optimizing Scheduler for Linux Gaming.pdf
Optimizing Scheduler for Linux Gaming.pdfOptimizing Scheduler for Linux Gaming.pdf
Optimizing Scheduler for Linux Gaming.pdf
 
Running JS via WASM faster with JIT
Running JS via WASM      faster with JITRunning JS via WASM      faster with JIT
Running JS via WASM faster with JIT
 
To crash or not to crash: if you do, at least recover fast!
To crash or not to crash: if you do, at least recover fast!To crash or not to crash: if you do, at least recover fast!
To crash or not to crash: if you do, at least recover fast!
 
Implementing a Vulkan Video Encoder From Mesa to GStreamer
Implementing a Vulkan Video Encoder From Mesa to GStreamerImplementing a Vulkan Video Encoder From Mesa to GStreamer
Implementing a Vulkan Video Encoder From Mesa to GStreamer
 
8 Years of Open Drivers, including the State of Vulkan in Mesa
8 Years of Open Drivers, including the State of Vulkan in Mesa8 Years of Open Drivers, including the State of Vulkan in Mesa
8 Years of Open Drivers, including the State of Vulkan in Mesa
 
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por IgaliaIntroducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
 
2023 in Chimera Linux
2023 in Chimera                    Linux2023 in Chimera                    Linux
2023 in Chimera Linux
 
Building a Linux distro with LLVM
Building a Linux distro        with LLVMBuilding a Linux distro        with LLVM
Building a Linux distro with LLVM
 
turnip: Update on Open Source Vulkan Driver for Adreno GPUs
turnip: Update on Open Source Vulkan Driver for Adreno GPUsturnip: Update on Open Source Vulkan Driver for Adreno GPUs
turnip: Update on Open Source Vulkan Driver for Adreno GPUs
 
Graphics stack updates for Raspberry Pi devices
Graphics stack updates for Raspberry Pi devicesGraphics stack updates for Raspberry Pi devices
Graphics stack updates for Raspberry Pi devices
 
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOSDelegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
 
MessageFormat: The future of i18n on the web
MessageFormat: The future of i18n on the webMessageFormat: The future of i18n on the web
MessageFormat: The future of i18n on the web
 
Replacing the geometry pipeline with mesh shaders
Replacing the geometry pipeline with mesh shadersReplacing the geometry pipeline with mesh shaders
Replacing the geometry pipeline with mesh shaders
 
I'm not an AMD expert, but...
I'm not an AMD expert, but...I'm not an AMD expert, but...
I'm not an AMD expert, but...
 
Status of Vulkan on Raspberry
Status of Vulkan on RaspberryStatus of Vulkan on Raspberry
Status of Vulkan on Raspberry
 

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
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

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
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 

Self-hosted JS (ffconf 2014)

  • 1. javascripts in the javascripts ffconf 2014 andy wingo
  • 2.
  • 3. the es6 circus is coming to town es-discuss clownshoes C++ knife-jugglers JavaScript acrobats
  • 4. building es.next in es.now Hark, an agenda: ❧ Why? ❧ How: JavaScriptCore ❧ How: SpiderMonkey ❧ How: V8
  • 6. js is faster than c++
  • 7. js is faster than c++ JS can optimize in ways that C++ can’t ❧ dynamic inlining inline allocation (and possibly scalar replacement) ❧ inline hard-wiring of user object shapes (slot offsets, getters) ❧
  • 8. js is faster than c++ No JS/C++ transition cost Especially important for callbacks (e.g. forEach)
  • 9. js is faster than c++ JavaScriptCore’s Oliver Hunt, January 2014: “The initial proof of concept is Array.prototype.every, this shows a 65% performance improvement, and that improvement is significantly hurt by our poor optimisation of op_in.”
  • 10. js matches js semantics better Proxies, accessors, order of effects, has-property versus get-property, user-implemented iteration protocol, exceptions, catch Terse: for (var x of y) z(x);
  • 11. js more secure than c++ GC-related bugs approximately impossible ❧ SM, V8; JSC immune No C++ knife-throwing work-related accidents ❧ integer overflow, use-after-free, etc Cross-iframe leakage concerns lessened
  • 12. choosy hackers choose js Goal: As much in JS as possible For speed, for security, for maintainability How?
  • 13. simplest model: javascriptcore “Methods can be implemented in JS”
  • 14. example Source/JavaScriptCore/builtins/ Array.prototype.js function foo() { return 'ahoy ffconf'; } Source/JavaScriptCore/runtime/ ArrayPrototype.cpp foo arrayProtoFuncFoo DontEnum|Function 0
  • 15. weird js: jsc edition Function source compiled separately Access to globals forbidden in general Initial values of globals accessible via @ prefix, e.g. @Object Add @call and @apply http://svn.webkit.org/repository/ webkit/trunk@163195
  • 16. more complicated: spider monkey “Self-hosted JS” files concatenated and evaluated – more normal model C++ binds functions by name to prototype properties
  • 17. feature: es.next ‘pipelines’ Old SpiderMonkey: (x*2 for (x in [0,1,2].keys())) Erstwhile ES6: (for (x of [0,1,2].keys()) x*2) Maybe ES7: [0,1,2].keys().map(x=>x*2) Ideally on IteratorPrototype, but let’s hack it
  • 18. example js/src/builtin/Iterator.js function* IteratorMap(f) { for (var x of this) yield f(x); }
  • 19. example No function* at boot-time :( But, ES6 object literals function IteratorMap(f) { var iter = this[std_iterator](); return { next(val) { var result = iter.next(val) return result.done ? result : { value: callFunction(f, iter, result.value), done: false }; }, [std_iterator]: IteratorIdentity, } }
  • 20. example Link to C++ files; grep for surrounding identifiers, make similar modifications (e.g. in jsiter.cpp) js> for (var x of [1,2,3].keys().map(x=>x*2)) print(x) 024
  • 21. nerf the web forward
  • 22. nerf the web forward Your search - "nerf the web forward" - did not match any documents.
  • 23. nerf the web forward (like, nerf is like a more resilient polystyrene foam)
  • 24. nerf the web forward (the more joke explanation slides, the more amusing the joke, right?)
  • 25. nerf the web forward (right?)
  • 26. caveats @@iterator called before or after first next()? Prototype chain of the result of map()? Should final result.value be mapped? %IteratorPrototype% No spec; spec wonkiness throw()? next() applied to different object?
  • 28. languages are like operating systems Visit a page : Install an app Visit about:blank : Boot OS Weird self-hosted JS part of OS, not app
  • 29. genesis In the beginning, there was the empty function and the Object function and its prototype property
  • 30. genesis And Goog looked upon it and saw that it was good
  • 31. genesis Then the strict mode function “maps” (hidden classes) Then the first global object Then Array, Number, Boolean, String, Symbol, Date, RegExp, JSON, ArrayBuffer, the TypedArrays, Map, Set, iterator result shapes, WeakMap, WeakSet, arguments object shapes, ...
  • 32. genesis And Goog looked upon them and saw that they were good
  • 33. genesis And Goog looked upon them and saw that they were good But FFS it’s a lot of C++, innit?
  • 34. how 2 js Problem: Need to define helpers in JS, but they shouldn’t be in the user’s scope Solution: Second global object for self-hosted JS to play in; natives mutate to produce a more beautiful global
  • 35. builtins, globals Global: A global object, corresponding to a user-facing script-level scope builtins: The global object current when self-hosted JS is being defined In builtins, user-facing global bound to global Somewhat confusingly, in V8, “self-hosted JS facilities” are called “natives”
  • 36. on the seventh day So, “natives”. That’s JavaScript y’all!
  • 37. example src/generator.js function* GeneratorObjectMap(f) { for (var x of this) yield f(x); }
  • 38. weird js, v8 edition Verbs % prefix for low-level C++ runtime functions (--allow-natives-syntax) ❧ %_ prefix for magical “inline” runtime functions (%_CallFunction, %_IsSmi) ❧ ❧ macros (TO_UINT32, IS_NUMBER)
  • 39. weird js, v8 edition Nouns too ❧ global InternalArray (to allow builtins to use .push() without worrying about user pollution) ❧ Suggested reading order ❧ runtime.js ❧ v8natives.js ❧ array.js
  • 40. snapshots Lots of work amirite? Optimization: Serialize heap of new-born world Load fresh heap from disk to “boot” Necessary in context of Chrome’s multi-process model
  • 41. note: the dom is something else “Blink-in-JS” Kentaro Haro: DOM binding overhead is 5-15% in real web DOM objects live in a 1-to-N relationship to V8 globals Search for “Hardening security of content scripts”
  • 42. but seriously Strict spec reading Strict spec translation (optimize later if ever) Tests (especially proxies, getters, order of operations) Patch submission Feature flags (in v8)
  • 43. tx nerf the web forward! wingo@igalia.com http://wingolog.org/ . big kid circus, by ray forster: https:// www.flickr.com/photos/ 94418464@N08/8686092191