javascripts in the javascripts ffconf 2014 andy wingo the - - PowerPoint PPT Presentation

javascripts
SMART_READER_LITE
LIVE PREVIEW

javascripts in the javascripts ffconf 2014 andy wingo the - - PowerPoint PPT Presentation

javascripts in the javascripts ffconf 2014 andy wingo the es-discuss clownshoes es6 C++ knife-jugglers circus JavaScript acrobats is coming to town building Hark, an agenda: es.next Why? in How: JavaScriptCore es.now


slide-1
SLIDE 1

javascripts

in the javascripts ffconf 2014 andy wingo

slide-2
SLIDE 2
slide-3
SLIDE 3

the es6 circus is coming to town

es-discuss clownshoes

C++ knife-jugglers JavaScript acrobats

slide-4
SLIDE 4

building es.next in es.now

Hark, an agenda: Why? ❧ How: JavaScriptCore ❧ How: SpiderMonkey ❧ How: V8 ❧

slide-5
SLIDE 5

why implement js in js?

slide-6
SLIDE 6

js is faster than c++

slide-7
SLIDE 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) ❧

slide-8
SLIDE 8

js is faster than c++

No JS/C++ transition cost Especially important for callbacks (e.g.

forEach)

slide-9
SLIDE 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.”

slide-10
SLIDE 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);

slide-11
SLIDE 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

slide-12
SLIDE 12

choosy hackers choose js

Goal: As much in JS as possible For speed, for security, for maintainability How?

slide-13
SLIDE 13

simplest model: javascriptcore

“Methods can be implemented in JS”

slide-14
SLIDE 14

example

Source/JavaScriptCore/builtins/ Array.prototype.js

function foo() { return 'ahoy ffconf'; }

Source/JavaScriptCore/runtime/ ArrayPrototype.cpp

foo arrayProtoFuncFoo DontEnum|Function 0

slide-15
SLIDE 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

slide-16
SLIDE 16

more complicated: spider monkey

“Self-hosted JS” files concatenated and evaluated – more normal model C++ binds functions by name to prototype properties

slide-17
SLIDE 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

slide-18
SLIDE 18

example

js/src/builtin/Iterator.js

function* IteratorMap(f) { for (var x of this) yield f(x); }

slide-19
SLIDE 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, } }

slide-20
SLIDE 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) 2 4

slide-21
SLIDE 21

nerf the web forward

slide-22
SLIDE 22

nerf the web forward

Your search - "nerf the web forward" - did not match any documents.

slide-23
SLIDE 23

nerf the web forward

(like, nerf is like a more resilient polystyrene foam)

slide-24
SLIDE 24

nerf the web forward

(the more joke explanation slides, the more amusing the joke, right?)

slide-25
SLIDE 25

nerf the web forward

(right?)

slide-26
SLIDE 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?

slide-27
SLIDE 27

v8

Story time!

slide-28
SLIDE 28

languages are like

  • perating

systems

Visit a page : Install an app Visit about:blank : Boot OS Weird self-hosted JS part of OS, not app

slide-29
SLIDE 29

genesis

In the beginning, there was the empty function and the Object function and its prototype property

slide-30
SLIDE 30

genesis

And Goog looked upon it and saw that it was good

slide-31
SLIDE 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, ...

slide-32
SLIDE 32

genesis

And Goog looked upon them and saw that they were good

slide-33
SLIDE 33

genesis

And Goog looked upon them and saw that they were good But FFS it’s a lot of C++, innit?

slide-34
SLIDE 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

slide-35
SLIDE 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”

slide-36
SLIDE 36
  • n the

seventh day

So, “natives”. That’s JavaScript y’all!

slide-37
SLIDE 37

example

src/generator.js

function* GeneratorObjectMap(f) { for (var x of this) yield f(x); }

slide-38
SLIDE 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) ❧

slide-39
SLIDE 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

slide-40
SLIDE 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

slide-41
SLIDE 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”

slide-42
SLIDE 42

but seriously

Strict spec reading Strict spec translation (optimize later if ever) Tests (especially proxies, getters, order

  • f operations)

Patch submission Feature flags (in v8)

slide-43
SLIDE 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