CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This...

32
CSE 3302 Lecture 9: Prototype-based languages 23 September 2010 Nate Nystrom UTA

Transcript of CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This...

Page 1: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

CSE 3302Lecture 9: Prototype-based languages

23 September 2010

Nate NystromUTA

Page 2: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Today

• Prototype-based languages

• Self• JavaScript

Page 3: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Self

• David Ungar (Stanford) and Randall Smith (Xerox PARC)• late 1980s

• Not a popular success• But, very influential• language design: JavaScript• implementation: Java, C#, JavaScript, most scripting

languages• Now at Version 4.4 (Linux and Mac)• http://selflanguage.org/

Page 4: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Smalltalk

• First “modern” OO language – Alan Kay, 1970s• Everything is an object (numbers, lists, procedures)• Garbage collection• Closures

• Exploratory programming• Dynamically typed• Add new code/evaluate expressions with no

compilation required• Try it: squeak.org

Page 5: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Smalltalk

• Class-based language• Every object has a class

• defines structure of the object

• But, everything is an object• so, classes are also objects

• What is the class of a class?

Page 6: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Meta-regress

• Object is the base class of all objects• 3 is an instance of Object

• Class is the base metaclass of all classes• Integer is an instance of Class

• Metaclass is the class of all metaclasses• Class is an instance of Metaclass

• Metaclass is an instance of ... ?

Page 7: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Self: goal is simplicity

• Reduce number of concepts in the language• no classes• everything is an object

• no builtin control structures (these are also objects)• procedures = prototypes of activation records

• no variables• no distinction between accessing a variable and

sending a message (= calling a method)

Page 8: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Prototypes

• To create an object, clone an existing object(the prototype)

• Can add new methods• Can override existing methods

• Can even remove methods

Page 9: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Inheritance

• Use delegation rather than inheritance

• Every object has one or more parent slots• When sending a message (= calling a method), if object

cannot handle the message, forward to the parent

Page 10: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Inheritance in Self

• Objects can have more than one parent• multiple inheritance!

• Parents can be changed at run time• dynamic inheritance!

• useful when developing/debugging code• can refactor running code

Page 11: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Smalltalk vs. Self

6 UNGAR AND SMITH

pointclone

(class)

(superclass)(inst vars)(methods)

(name) Point

class, x, y

how to

(class)

(inst vars)

(methods)

(superclass)name,

new make objects

xx:yy:

parent*0

0

parent*

how toclone objects

Creating a SELF object

. . .

. . .

Creating a Smalltalk object

. . .

superclass,inst vars,methods

(class)

(inst vars)

(methods)

(superclass)name,

. . .

superclass,inst vars,methods

To create a new point in SELF, the clonemessage is sent to the prototypical point.The clone method copies its receiver.Because the point slot resides in the root,any object can create a point.

. . .

. . .

Figure 2. Object creation in Smalltalk and in SELF.

To create a new point in Smalltalk, thenew message is sent to the class Point.The new method—found in Point’sclass’s superclass—uses information inits receiver (Point) to define the size andformat of the new object.

6 UNGAR AND SMITH

pointclone

(class)

(superclass)(inst vars)(methods)

(name) Point

class, x, y

how to

(class)

(inst vars)

(methods)

(superclass)name,

new make objects

xx:yy:

parent*0

0

parent*

how toclone objects

Creating a SELF object

. . .

. . .

Creating a Smalltalk object

. . .

superclass,inst vars,methods

(class)

(inst vars)

(methods)

(superclass)name,

. . .

superclass,inst vars,methods

To create a new point in SELF, the clonemessage is sent to the prototypical point.The clone method copies its receiver.Because the point slot resides in the root,any object can create a point.

. . .

. . .

Figure 2. Object creation in Smalltalk and in SELF.

To create a new point in Smalltalk, thenew message is sent to the class Point.The new method—found in Point’sclass’s superclass—uses information inits receiver (Point) to define the size andformat of the new object.

Smalltalk Self

Page 12: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Messages

• When message is sent, object is searched for slot with message name

• If none found, all parents are searched• Runtime error if more than one parent has a slot with

the same name• If slot is found, its contents evaluated and returned.

• Runtime error if no slot found

Page 13: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

FlexibilitySELF: THE POWER OF SIMPLICITY 9

Figure 4. Two examples of flexibility in SELF. On the left is a point whose x coordinateis computed by a random number generator. Since all the code for point sends messagesto get the x value, the random x point can reuse all existing point code. On the right aretwo points that share the same x variable.

xyx:y:

parent*35

printparent*

print objects. . .

+parent*

add points

x

y

parent*randomnumber

generator0

y:

printparent*

print objects. . .

+parent*

add points

xx:

parent*7

yy:

parent*4y

y:

parent*2

Computing a value instead of storing it Shared state

In most object-oriented languages, accessing a variable is a different operationthan sending a message. This dilutes the message passing model of computationwith assignment and access. As a result, message passing becomes less powerful.For instance, the inclusion of variables makes it harder for a specialization(subclass) to replace a variable with a computed result, because there may be codein a superclass that directly accesses the variable. Also, class-based languagestypically store the names and order of instance variables in an object’s class (asshown in Figure 1). This further limits the power of inheritance; the specificationwithin a class unnecessarily restricts an instance’s format. Finally, variable accessrequires scoping rules, yet a further complication. For instance, Smalltalk has fivekinds of variables: local variables (temporaries), instance variables, class variables,pool variables, and global variables, whose scopes roughly correspond to rungs ona ladder of instantiation.

SELF: THE POWER OF SIMPLICITY 9

Figure 4. Two examples of flexibility in SELF. On the left is a point whose x coordinateis computed by a random number generator. Since all the code for point sends messagesto get the x value, the random x point can reuse all existing point code. On the right aretwo points that share the same x variable.

xyx:y:

parent*35

printparent*

print objects. . .

+parent*

add points

x

y

parent*randomnumber

generator0

y:

printparent*

print objects. . .

+parent*

add points

xx:

parent*7

yy:

parent*4y

y:

parent*2

Computing a value instead of storing it Shared state

In most object-oriented languages, accessing a variable is a different operationthan sending a message. This dilutes the message passing model of computationwith assignment and access. As a result, message passing becomes less powerful.For instance, the inclusion of variables makes it harder for a specialization(subclass) to replace a variable with a computed result, because there may be codein a superclass that directly accesses the variable. Also, class-based languagestypically store the names and order of instance variables in an object’s class (asshown in Figure 1). This further limits the power of inheritance; the specificationwithin a class unnecessarily restricts an instance’s format. Finally, variable accessrequires scoping rules, yet a further complication. For instance, Smalltalk has fivekinds of variables: local variables (temporaries), instance variables, class variables,pool variables, and global variables, whose scopes roughly correspond to rungs ona ladder of instantiation.

Page 14: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Activation recordsSELF: THE POWER OF SIMPLICITY 11

xyx:y:

parent*35

how to print objects

Activation in SELF

cloneprint

xyx:y:

parent*79

how to clone objects

arg<code>

self*

+parent*

arg<code>

self*nil

(clone x: x + arg x) y: y + arg y

shared behavior for all objects

shared behavior for points

a point another point

cloned activationrecord

prototype activationrecord

Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” messagewith argument (7, 9). Lookup for plus starts at (3, 5) and finds a matching slot in the objectholding shared behavior for points. Since the contents of the slot is a method object, it iscloned, the clone’s argument slot is set to the argument of the message, and its parent isset to the receiver. When the code for “+” executes, the lookup for x will find the receiver’sx slot by following the inheritance chain from the current activation record. It will also findthe contents of the “arg” slot in the same way. It is this technique of having the lookup forthe implicit “self” receiver start at the current activation that allows local variables, in-stance variables, and method lookup to be unified in SELF.

(a point) + (another point)

Page 15: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Typing

• In a class-based language, every class defines a type

• Most prototype-based languages are dynamically typed• Why?

Page 16: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Typing

• In a class-based language, every class defines a type

• Most prototype-based languages are dynamically typed• Why?• Difficult to describe the type succinctly (no class

names)

Page 17: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Disadvantages of classes?

• Require programmers to understand a more complex model• To make a new kind of object, have to create a class

first• To change an object, have to change the class

• But:• Does Self require programmer to reinvent structure?• Common to structure Self programs with traits:

objects that simply collect behavior for sharing

Page 18: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Implementation• Very high-level abstractions in the language• Challenges:

• Many function calls, small methods• Everything is a message, even variable access• No static type system• No class structure to enforce sharing

• each object has its own copy of its methods

• Compiler must do a lot of work to optimize this code• adaptive run-time optimization• method inlining

• The students who worked on Self developed the technology in the HotSpot Java VM• Urs Hölzle: first VP of Engineering at Google, now VP of Operations• Lars Bak: developed the V8 JavaScript virtual machine in Chrome

Page 19: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Implementation

• Compiler must do a lot of work to optimize this code• adaptive run-time optimization• method inlining

• Side note:• The grad students working on Self formed a startup

to build a VM for Smalltalk• Bought by Sun, evolved into HotSpot JVM• Urs Hölzle: VP of Operations at Google• Lars Bak: developed the V8 JavaScript VM in Chrome

Page 20: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

JavaScript

• Brendan Eich• 1995 Netscape––remember them?

• OO language• allows websites to run client-side code

• Standardized as ECMAScript• variants: JavaScript, JScript (MS), ActionScript

(Adobe)

Page 21: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

JavaScript

• Java-like syntax• prototype-based object system

• object is a set of properties, a mutable map from strings to values

• a property that evaluates to a function is a method

Page 22: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Properties

• object is a mutable map from strings to values

• obj.x = 1• equivalent to:• obj[“x”] = 1

• assigning to a field that doesn’t exist creates the field

Page 23: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Functions

• Functions are just objects

• function (x) { return x + 1 }• returns an object whose call method increments its

argument

Page 24: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Prototypes

• Each object has a single __proto__ field that refers to another object

• property lookup:• search the object• if not found, search the prototype

• __proto__ field initially refers to an empty object

Page 25: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Constructors

• Constructors are just functions• function C(x) { this.x = x; }

• Constructors have a prototype field• this is not the prototype of the function object• it’s the prototype of objects created by the

constructor• the constructor assigns its prototype to the __proto__ field of the new object

Page 26: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

New

• new creates an object based on the prototype field

• new C(17)• creates an empty object z• set z.__proto__ = C.prototype• calls C with x=17, this=z• returns z

Page 27: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

List exampleOutline Introduction JavaScript Measurements Results Conclusions

JavaScript — Prototypes Example

function List(v, n) { this.v = v; this.n = n; }

List.prototype.map = function(f) {return new List(f(this.v),

this.n ? this.n.map(f) : null);}var l = new List(1, null);delete(List.prototype.map);

11 / 28

Page 28: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

List exampleOutline Introduction JavaScript Measurements Results Conclusions

JavaScript — Prototypes Example

function List(v, n) { this.v = v; this.n = n; }List.prototype.map = function(f) {

return new List(f(this.v),this.n ? this.n.map(f) : null);

}

var l = new List(1, null);delete(List.prototype.map);

11 / 28

Page 29: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

List exampleOutline Introduction JavaScript Measurements Results Conclusions

JavaScript — Prototypes Example

function List(v, n) { this.v = v; this.n = n; }List.prototype.map = function(f) {

return new List(f(this.v),this.n ? this.n.map(f) : null);

}var l = new List(1, null);

delete(List.prototype.map);

11 / 28

Page 30: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

List exampleOutline Introduction JavaScript Measurements Results Conclusions

JavaScript — Prototypes Example

function List(v, n) { this.v = v; this.n = n; }List.prototype.map = function(f) {

return new List(f(this.v),this.n ? this.n.map(f) : null);

}var l = new List(1, null);

delete(List.prototype.map);

11 / 28

Page 31: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

List exampleOutline Introduction JavaScript Measurements Results Conclusions

JavaScript — Prototypes Example

function List(v, n) { this.v = v; this.n = n; }List.prototype.map = function(f) {

return new List(f(this.v),this.n ? this.n.map(f) : null);

}var l = new List(1, null);delete(List.prototype.map);

11 / 28

Page 32: CSE 3302 - Rangerranger.uta.edu/~nystrom/courses/cse3302-fa10/lec/3302-09.pdf · Figure 5. This figure shows what happens when the point (3, 5) is sent the “+” message with argument

Resources

• Download Self and play with the demo• selflanguage.org

• Flanagan, JavaScript: The Definitive Guide• most other JS books are not very good