Slide 1

Slide 1 text

Servo inside (introduction to Servo’s DOM binding) Tetsuharu OHZEKI, Gecko Inside #6 (Feb 24, 2016)

Slide 2

Slide 2 text

About me • Tetsuharu OHZEKI (a.k.a @saneyuki_s) • Mozilla Committer (Level 3) as a volunteer contributor • Firefox developer, and Servo reviewer • My main work is Web client-side engineer at Shibuya • JavaScript, GUI Application architecture

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. http://rust-lang.org/

Slide 5

Slide 5 text

Rust Language • Develop by Mozilla & community • It’s just like “Modern Redesigned C++” • Today’s Gecko try to use Rust in some area: • https://wiki.mozilla.org/Oxidation • e.g. MP4 metadata parser, URL parser, style system

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Servo • Written with Rust • By Mozilla Research • Parallelism, Power, Correctness, Concurrent programming • more details: https://github.com/servo/servo/wiki/Research

Slide 8

Slide 8 text

Servo’s key “tasks” • Constellation • Script • Layout • Paint • Compositor • Resource

Slide 9

Slide 9 text

script crate • The implementation of “DOM” and scripting • Binding with SpiderMonkey • Handle a web driver’s script execution (from Constellation)

Slide 10

Slide 10 text

Rust Object JS Object browser JS Engine

Slide 11

Slide 11 text

DOM lifecycle & JS binding • DOM needs a complex lifecycle management • good article about this area: http://steps.dodgson.org/b/ 2012/12/19/dom-and-gc-or-what-happend-at-eden/ • Other browser engines have an own garbage collector for this complexity • Cycle Collector (Gecko) • Oilpan GC (Blink) • Servo need not own GC in the browser side like other engines. • All lifecycle of Servo’s DOM object are managed by SpiderMonkey GC

Slide 12

Slide 12 text

How to connect with JSObject & Rust Object? • DOM Object has “Reflector” field as the first field. • Reflector stores a pointer to “wrapper” JS Object which corresponds to it. • JS Object has a pointer to Rust object which is corresponds to it. • JS Object stores the pointer in its “slot”.

Slide 13

Slide 13 text

Node Reflector handlers parent_node …… JSObject

Slide 14

Slide 14 text

Generate a glue code from WebIDL • By /components/script/dom/bindings/codegen/ BindingGen.py • Auto generate from WebIDL with special annotation • Methods, Property, JIT Information, Constants, etc • “Glue” code to connect a Rust world & JS world

Slide 15

Slide 15 text

[Abstract] interface Node : EventTarget { [Constant] readonly attribute unsigned short nodeType; [Pure] readonly attribute DOMString nodeName; … }

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

How to trace object graph from SpiderMonkey GC • From GC roots, SpiderMonkey GC trace an object graph. • Remember these simple rules: • From a wrapper object (JSObject), GC can traces a Rust Object. • From a Rust Object, GC can trace a wrapper JS Object. • Some compiler plugins helps these tricks ;)

Slide 18

Slide 18 text

Reflector JSObject Rust Object JS JS Reflector Rust Object JSObject JS Reflector Rust Object JSObject

Slide 19

Slide 19 text

GC Managed pointer • We use a special smart pointer for GC managed object in Rust world • JS can be holden as a member of a Rust’s DOM struct • Root can be on stack with rooting explicitly. • To avoid SpiderMonkey exact (precise) GC’s mistake. • In practice, we use this for a return value.

Slide 20

Slide 20 text

Inheritance • DOM interface requires an inheritance • But Rust does not have a inheritance system as a language feature! • So we generate annotation traits for safe up/down casting from WebIDL information! • …..And write vtable by hand!

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

vtable by hand…

Slide 24

Slide 24 text

Special macros and plugins • It’s hard to implement a GC hook for an object correctly. • We use a special rust compiler plugins to check & avoid typical pitfalls. • auto generate a tricky implementation for GC interaction. • check unrooted object • valid order of struct (for inheritance) • etc…

Slide 25

Slide 25 text

How to start hacking Servo? • http://servo.github.io/servo-starters/ • Servo team picks up “Good First Pull Request” • CONTRIBUTING.md • HACKING_QUICKSTART.md