Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a...
Transcript of Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a...
![Page 1: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/1.jpg)
Hacking the Rust object system at MozillaLindsey Kuper
Grinnell CollegeApril 5, 2012
1
![Page 2: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/2.jpg)
2
Me and how I got here
![Page 3: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/3.jpg)
2
Me and how I got here
■ Graduated from Grinnell (CS and music) in 2004
![Page 4: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/4.jpg)
2
Me and how I got here
■ Graduated from Grinnell (CS and music) in 2004
■ Web development at a (failed) startup, 2004–2006
![Page 5: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/5.jpg)
2
Me and how I got here
■ Graduated from Grinnell (CS and music) in 2004
■ Web development at a (failed) startup, 2004–2006
■ Perl plumbing at a publishing company, 2006–2008
![Page 6: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/6.jpg)
2
Me and how I got here
■ Graduated from Grinnell (CS and music) in 2004
■ Web development at a (failed) startup, 2004–2006
■ Perl plumbing at a publishing company, 2006–2008■ but in 2007, I moved in with a
couple of Haskell hackers...
![Page 7: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/7.jpg)
2
Me and how I got here
■ Graduated from Grinnell (CS and music) in 2004
■ Web development at a (failed) startup, 2004–2006
■ Perl plumbing at a publishing company, 2006–2008■ but in 2007, I moved in with a
couple of Haskell hackers...
■ Ph.D. student at Indiana studying PL since fall 2008
![Page 8: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/8.jpg)
My field: programming language semantics
■ Questions we might want to answer:
3
Source: Andrew Myers’ CS 611 course at Cornell
![Page 9: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/9.jpg)
My field: programming language semantics
■ Questions we might want to answer:■ Is this program correct?
3
Source: Andrew Myers’ CS 611 course at Cornell
![Page 10: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/10.jpg)
My field: programming language semantics
■ Questions we might want to answer:■ Is this program correct?
■ Will this program encounter a run-time type error?
3
Source: Andrew Myers’ CS 611 course at Cornell
![Page 11: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/11.jpg)
My field: programming language semantics
■ Questions we might want to answer:■ Is this program correct?
■ Will this program encounter a run-time type error?
■ Is one program indistinguishable from another?
3
Source: Andrew Myers’ CS 611 course at Cornell
![Page 12: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/12.jpg)
My field: programming language semantics
■ Questions we might want to answer:■ Is this program correct?
■ Will this program encounter a run-time type error?
■ Is one program indistinguishable from another?
■ Is this optimization a safe program transformation?
3
Source: Andrew Myers’ CS 611 course at Cornell
![Page 13: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/13.jpg)
My field: programming language semantics
■ Questions we might want to answer:■ Is this program correct?
■ Will this program encounter a run-time type error?
■ Is one program indistinguishable from another?
■ Is this optimization a safe program transformation?
■ Can programs written in this language crash?
3
Source: Andrew Myers’ CS 611 course at Cornell
![Page 14: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/14.jpg)
My field: programming language semantics
■ Questions we might want to answer:■ Is this program correct?
■ Will this program encounter a run-time type error?
■ Is one program indistinguishable from another?
■ Is this optimization a safe program transformation?
■ Can programs written in this language crash?
■ Is this compiler translation correct?
3
Source: Andrew Myers’ CS 611 course at Cornell
![Page 15: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/15.jpg)
My field: programming language semantics
■ Questions we might want to answer:■ Is this program correct?
■ Will this program encounter a run-time type error?
■ Is one program indistinguishable from another?
■ Is this optimization a safe program transformation?
■ Can programs written in this language crash?
■ Is this compiler translation correct?
■ Can source language A be translated into target language B?
3
Source: Andrew Myers’ CS 611 course at Cornell
![Page 16: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/16.jpg)
Why Rust?
■ What do we want in a programming language?
4
Source: Michael Sullivan, “Closures for Rust”
![Page 17: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/17.jpg)
Why Rust?
■ What do we want in a programming language?■ Fast: generates efficient machine code
4
Source: Michael Sullivan, “Closures for Rust”
![Page 18: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/18.jpg)
Why Rust?
■ What do we want in a programming language?■ Fast: generates efficient machine code
■ Safe: type system provides guarantees that prevent certain bugs
4
Source: Michael Sullivan, “Closures for Rust”
![Page 19: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/19.jpg)
Why Rust?
■ What do we want in a programming language?■ Fast: generates efficient machine code
■ Safe: type system provides guarantees that prevent certain bugs
■ Concurrent: easy to build concurrent programs and to take advantage of parallelism
4
Source: Michael Sullivan, “Closures for Rust”
![Page 20: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/20.jpg)
Why Rust?
■ What do we want in a programming language?■ Fast: generates efficient machine code
■ Safe: type system provides guarantees that prevent certain bugs
■ Concurrent: easy to build concurrent programs and to take advantage of parallelism
■ “Systemsy”: fine-grained control, predictable performance characteristics
4
Source: Michael Sullivan, “Closures for Rust”
![Page 21: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/21.jpg)
Why Rust?
■ What do we have now?
5
Source: Michael Sullivan, “Closures for Rust”
![Page 22: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/22.jpg)
Why Rust?
■ What do we have now?■ Firefox is in C++, which is Fast and
Systemsy
5
Source: Michael Sullivan, “Closures for Rust”
![Page 23: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/23.jpg)
Why Rust?
■ What do we have now?■ Firefox is in C++, which is Fast and
Systemsy
■ ML is (sometimes) Fast and (very) Safe
5
Source: Michael Sullivan, “Closures for Rust”
![Page 24: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/24.jpg)
Why Rust?
■ What do we have now?■ Firefox is in C++, which is Fast and
Systemsy
■ ML is (sometimes) Fast and (very) Safe
■ Erlang is Safe and Concurrent
5
Source: Michael Sullivan, “Closures for Rust”
![Page 25: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/25.jpg)
Why Rust?
■ What do we have now?■ Firefox is in C++, which is Fast and
Systemsy
■ ML is (sometimes) Fast and (very) Safe
■ Erlang is Safe and Concurrent
■ Haskell is (sometimes) Fast, (very) Safe, and Concurrent
5
Source: Michael Sullivan, “Closures for Rust”
![Page 26: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/26.jpg)
Why Rust?
■ What do we have now?■ Firefox is in C++, which is Fast and
Systemsy
■ ML is (sometimes) Fast and (very) Safe
■ Erlang is Safe and Concurrent
■ Haskell is (sometimes) Fast, (very) Safe, and Concurrent
■ Java and C# are Fast and Safe
5
Source: Michael Sullivan, “Closures for Rust”
![Page 27: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/27.jpg)
Why Rust?
6
A systems languagepursuing the trifecta:fast, concurrent, safe
![Page 28: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/28.jpg)
You worked on the what system?!
7
![Page 29: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/29.jpg)
You worked on the what system?!
■ I didn’t arrive with the intention of working on the object system, but...
7
![Page 30: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/30.jpg)
You worked on the what system?!
■ I didn’t arrive with the intention of working on the object system, but...
■ I was intrigued by the idea of a classless object model and flexible prototype-based objects
7
![Page 31: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/31.jpg)
You worked on the what system?!
■ I didn’t arrive with the intention of working on the object system, but...
■ I was intrigued by the idea of a classless object model and flexible prototype-based objects■ and was told, “None of that’s implemented yet; go for it!”
7
![Page 32: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/32.jpg)
You worked on the what system?!
■ I didn’t arrive with the intention of working on the object system, but...
■ I was intrigued by the idea of a classless object model and flexible prototype-based objects■ and was told, “None of that’s implemented yet; go for it!”
■ When I started: no object extension, method overriding, or self-dispatch
7
![Page 33: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/33.jpg)
You worked on the what system?!
■ I didn’t arrive with the intention of working on the object system, but...
■ I was intrigued by the idea of a classless object model and flexible prototype-based objects■ and was told, “None of that’s implemented yet; go for it!”
■ When I started: no object extension, method overriding, or self-dispatch
■ During my internship, I implemented those things
7
![Page 34: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/34.jpg)
You worked on the what system?!
■ I didn’t arrive with the intention of working on the object system, but...
■ I was intrigued by the idea of a classless object model and flexible prototype-based objects■ and was told, “None of that’s implemented yet; go for it!”
■ When I started: no object extension, method overriding, or self-dispatch
■ During my internship, I implemented those things■ and learned that they interact with each other in
interesting ways
7
![Page 35: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/35.jpg)
Self-dispatch
8
![Page 36: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/36.jpg)
Self-dispatch
8
![Page 37: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/37.jpg)
Self-dispatch + object extension
9
![Page 38: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/38.jpg)
Self-dispatch + object extension
9
![Page 39: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/39.jpg)
A brainteaser...
10
After my first implementation attempt,
this assertion failed.Why?
![Page 40: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/40.jpg)
A hint...
11
![Page 41: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/41.jpg)
A brainteaser...
12
After my first implementation attempt,
this assertion failed.Why?
![Page 42: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/42.jpg)
A brainteaser...
12
After my first implementation attempt,
this assertion failed.Why?
longcat’s vtablelongcat’s vtablelongcat’s vtable
0 ack forward to shortcat.ack()1 lol ret “lol”
2 meow forward to shortcat.meow()3 nyan ret “nyan”
4 zzz forward to shortcat.zzz()
![Page 43: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/43.jpg)
A brainteaser...
12
After my first implementation attempt,
this assertion failed.Why?
longcat’s vtablelongcat’s vtablelongcat’s vtable
0 ack forward to shortcat.ack()1 lol ret “lol”
2 meow forward to shortcat.meow()3 nyan ret “nyan”
4 zzz forward to shortcat.zzz()
shortcat’s vtableshortcat’s vtableshortcat’s vtable
0 ack ret “ack”
1 meow ret “meow”
2 zzz ret self.meow()
![Page 44: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/44.jpg)
How to fix it
13
longcat’s vtablelongcat’s vtablelongcat’s vtable
0 ack forward to shortcat.ack()1 lol ret “lol”
2 meow forward to shortcat.meow()3 nyan ret “nyan”
4 zzz forward to shortcat.zzz()
shortcat’s vtableshortcat’s vtableshortcat’s vtable
0 ack ret “ack”
1 meow ret “meow”
2 zzz ret self.meow()
![Page 45: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/45.jpg)
How to fix it
13
longcat’s vtablelongcat’s vtablelongcat’s vtable
0 ack forward to shortcat.ack()1 lol ret “lol”
2 meow forward to shortcat.meow()3 nyan ret “nyan”
4 zzz forward to shortcat.zzz()
shortcat’s vtableshortcat’s vtableshortcat’s vtable
0 ack ret “ack”
1 meow ret “meow”
2 zzz ret self.meow()
shortcat’s backwarding vtableshortcat’s backwarding vtableshortcat’s backwarding vtable
0 ack backward to longcat.ack()
1 meow backward to longcat.meow()
2 zzz backward to longcat.zzz()
![Page 46: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/46.jpg)
How to fix it
13
longcat’s vtablelongcat’s vtablelongcat’s vtable
0 ack forward to shortcat.ack()1 lol ret “lol”
2 meow forward to shortcat.meow()3 nyan ret “nyan”
4 zzz forward to shortcat.zzz()
shortcat’s vtableshortcat’s vtableshortcat’s vtable
0 ack ret “ack”
1 meow ret “meow”
2 zzz ret self.meow()
shortcat’s backwarding vtableshortcat’s backwarding vtableshortcat’s backwarding vtable
0 ack backward to longcat.ack()
1 meow backward to longcat.meow()
2 zzz backward to longcat.zzz()
self
![Page 47: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/47.jpg)
Self-dispatch + object extension + overriding
14
![Page 48: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/48.jpg)
Self-dispatch + object extension + overriding
14
![Page 49: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/49.jpg)
Self-dispatch + object extension + overriding
14
![Page 50: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/50.jpg)
Self-dispatch + object extension + overriding
14
Caveat: Some disagreement on whether it should work this way (see: Aldrich, “Selective Open Recursion”)
![Page 51: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/51.jpg)
Self-dispatch + object extension + overriding
14
![Page 52: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/52.jpg)
Self-dispatch + object extension + overriding
14
longercat’s vtablelongercat’s vtablelongercat’s vtable
0 ack forward to shortcat.ack()1 meow ret “zzz”
2 zzz forward to shortcat.zzz()
![Page 53: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/53.jpg)
Self-dispatch + object extension + overriding
14
longercat’s vtablelongercat’s vtablelongercat’s vtable
0 ack forward to shortcat.ack()1 meow ret “zzz”
2 zzz forward to shortcat.zzz()
shortcat’s vtableshortcat’s vtableshortcat’s vtable
0 ack ret “ack”
1 meow ret “meow”
2 zzz ret self.meow()
![Page 54: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/54.jpg)
Self-dispatch + object extension + overriding
14
longercat’s vtablelongercat’s vtablelongercat’s vtable
0 ack forward to shortcat.ack()1 meow ret “zzz”
2 zzz forward to shortcat.zzz()
shortcat’s vtableshortcat’s vtableshortcat’s vtable
0 ack ret “ack”
1 meow ret “meow”
2 zzz ret self.meow()
shortcat’s backwarding vtableshortcat’s backwarding vtableshortcat’s backwarding vtable
0 ack backward to longercat.ack()
1 meow backward to longercat.meow()
2 zzz backward to longercat.zzz()
![Page 55: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/55.jpg)
Self-dispatch + object extension + overriding
14
longercat’s vtablelongercat’s vtablelongercat’s vtable
0 ack forward to shortcat.ack()1 meow ret “zzz”
2 zzz forward to shortcat.zzz()
shortcat’s vtableshortcat’s vtableshortcat’s vtable
0 ack ret “ack”
1 meow ret “meow”
2 zzz ret self.meow()
shortcat’s backwarding vtableshortcat’s backwarding vtableshortcat’s backwarding vtable
0 ack backward to longercat.ack()
1 meow backward to longercat.meow()
2 zzz backward to longercat.zzz()
self
![Page 56: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/56.jpg)
Self-dispatch + object extension + overriding
1511
to arbitrary depth
![Page 57: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/57.jpg)
Self-dispatch + object extension + overriding
1511
to arbitrary depth
![Page 58: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/58.jpg)
Self-dispatch + object extension + overriding
1511
to arbitrary depth
![Page 59: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/59.jpg)
Self-dispatch + object extension + overriding
1511
to arbitrary depth
![Page 60: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/60.jpg)
Self-dispatch + object extension + overriding
1511
to arbitrary depth■ We need a way to temporarily pretend that self is the inner object, while still keeping track of what the extended self is
![Page 61: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/61.jpg)
Self-dispatch + object extension + overriding
1511
to arbitrary depth■ We need a way to temporarily pretend that self is the inner object, while still keeping track of what the extended self is
■ Solution: create a stack of “fake selves” threaded through the run-time stack
![Page 62: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/62.jpg)
Self-dispatch + object extension + overriding
1511
to arbitrary depth■ We need a way to temporarily pretend that self is the inner object, while still keeping track of what the extended self is
■ Solution: create a stack of “fake selves” threaded through the run-time stack
■ Every forwarding function allocates space in its frame for a “fake self” comprising a backwarding vtable and an inner object body
![Page 64: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides](https://reader036.fdocuments.in/reader036/viewer/2022070712/5ecccf86c221095fc21e2bc2/html5/thumbnails/64.jpg)
17Photo by jamesrbowe on Flickr. Thanks!
Questions?
Thanks to:Graydon Hoare and everyone on the Rust teamDave Herman and everyone at Mozilla Research