Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
Some pieces of the Rust object system:extension, overriding, and self
Lindsey KuperIndiana University Programming Languages Group
September 9, 20111
2
What’s Rust?
a systems languagepursuing the trifecta:safe, concurrent, fast
You’re working on the what system?!
3
You’re working on the what system?!
■ I didn’t arrive with the intention of working on the object system, but...
3
You’re working 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
3
You’re working 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!”
3
You’re working 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
3
You’re working 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
3
You’re working 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
3
Self-dispatch
4
Self-dispatch
4
Self-dispatch + object extension
5
Self-dispatch + object extension
5
A brainteaser...
6
After my first implementation attempt,
this assertion failed.Why?
A hint...
7
A brainteaser...
8
After my first implementation attempt,
this assertion failed.Why?
A brainteaser...
8
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()
A brainteaser...
8
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()
How to fix it
9
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()
How to fix it
9
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()
How to fix it
9
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
Self-dispatch + object extension + overriding
10
Self-dispatch + object extension + overriding
10
Self-dispatch + object extension + overriding
10
Self-dispatch + object extension + overriding
10
Caveat: Some disagreement on whether it should work this way (see: Aldrich, “Selective Open Recursion”)
Self-dispatch + object extension + overriding
10
Self-dispatch + object extension + overriding
10
longercat’s vtablelongercat’s vtablelongercat’s vtable
0 ack forward to shortcat.ack()1 meow ret “zzz”
2 zzz forward to shortcat.zzz()
Self-dispatch + object extension + overriding
10
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()
Self-dispatch + object extension + overriding
10
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-dispatch + object extension + overriding
10
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
Self-dispatch + object extension + overriding
1111
to arbitrary depth
Self-dispatch + object extension + overriding
1111
to arbitrary depth
Self-dispatch + object extension + overriding
1111
to arbitrary depth
Self-dispatch + object extension + overriding
1111
to arbitrary depth
Self-dispatch + object extension + overriding
1111
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
Self-dispatch + object extension + overriding
1111
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
Self-dispatch + object extension + overriding
1111
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
Possible formalisms for reasoning about this stuff
12
Possible formalisms for reasoning about this stuff
■ Abadi and Cardelli: Self-types
12
Possible formalisms for reasoning about this stuff
■ Abadi and Cardelli: Self-types■ A close cousin to recursive types
12
Possible formalisms for reasoning about this stuff
■ Abadi and Cardelli: Self-types■ A close cousin to recursive types
■ Can be encoded using a combination of recursive and existential types
12
Possible formalisms for reasoning about this stuff
■ Abadi and Cardelli: Self-types■ A close cousin to recursive types
■ Can be encoded using a combination of recursive and existential types
■ Fisher, Honsell, and Mitchell: “A lambda calculus of objects and method specialization”
12
Possible formalisms for reasoning about this stuff
■ Abadi and Cardelli: Self-types■ A close cousin to recursive types
■ Can be encoded using a combination of recursive and existential types
■ Fisher, Honsell, and Mitchell: “A lambda calculus of objects and method specialization”■ “a type system that allows methods to be specialized
appropriately as they are inherited”
12
Possible formalisms for reasoning about this stuff
■ Abadi and Cardelli: Self-types■ A close cousin to recursive types
■ Can be encoded using a combination of recursive and existential types
■ Fisher, Honsell, and Mitchell: “A lambda calculus of objects and method specialization”■ “a type system that allows methods to be specialized
appropriately as they are inherited”
■ ...
12
14Photo by jamesrbowe on Flickr. Thanks!
Questions?
Thanks to:Graydon Hoare and everyone on the Rust teamDave Herman and everyone at Mozilla Research