tamarin and ecmascript 4

44
Tamarin and ECMAScript 4 John Resig (ejohn.org) Mozilla Corporation

Upload: jeresig

Post on 06-May-2015

7.819 views

Category:

Technology


0 download

DESCRIPTION

The rough presentation that I gave at the Ajax Experience, Adobe Max Tokyo, and Mozilla Japan/Shibuya.JS.

TRANSCRIPT

Page 1: Tamarin and ECMAScript 4

Tamarin and ECMAScript 4John Resig (ejohn.org)Mozilla Corporation

Page 2: Tamarin and ECMAScript 4

The Big PictureECMAScript 3

JavaScript 1.5 JScriptActionScript 2 Etc.

SpiderMonkey

Rhino

AVM JScript Engine KJS (Apple)

Opera

Page 3: Tamarin and ECMAScript 4

The DirectionECMAScript 4

JavaScript 2 ActionScript 4

Tamarin

JScript Etc.

Screaming Monkey

KJS (Apple)

Opera

Page 4: Tamarin and ECMAScript 4

Tamarin✦ Tamarin

✦ New Virtual Machine from Adobe✦ Perfect for ActionScript

✦ (a mutant cousin of JavaScript 2)✦ The Three Monkeys:

✦ ActionMonkey✦ ScreamingMonkey✦ IronMonkey

Page 5: Tamarin and ECMAScript 4

Three Monkies✦ ActionMonkey

✦ Integrating Tamarin into SpiderMonkey✦ Powering Firefox 4 (?) + JavaScript 2

✦ ScreamingMonkey✦ Bringing Tamarin into Internet Explorer✦ (Kicking and screaming?)

✦ IronMonkey✦ Bringing Python + Ruby to Tamarin

Page 6: Tamarin and ECMAScript 4

Path to JavaScript 2

Java

Scrip

t 2

JavaS

cript

1.5

1999

JavaS

cript

1.6

2005

JavaS

cript

1.7

2006Jav

aScr

ipt 1

.8

2008 2008-2009

Page 7: Tamarin and ECMAScript 4

The JavaScript Language✦ Current:

JavaScript 1.5 (ECMAScript 3)✦ JavaScript 1.6 (Firefox 1.5)✦ JavaScript 1.7 (Firefox 2)✦ JavaScipt 1.8 (Firefox 3)✦ ...✦ JavaScript 2 (ECMAScript 4)

Page 8: Tamarin and ECMAScript 4

ECMAScript 4 Goals✦ Compatible with ECMAScript 3✦ Suitable to developing large systems✦ Allow for reusable libraries✦ Merge previous efforts (ActionScript)✦ Fix ECMAScript 3 bugs✦ Keep it usable for small programs

Page 9: Tamarin and ECMAScript 4

Features✦ Classes and Interfaces✦ Packages and Namespaces✦ Type Annotations✦ Strict Verification✦ Optimization✦ Syntax Shortcuts✦ Iterators and Generators✦ Self-hosting

Page 10: Tamarin and ECMAScript 4

Classes

Page 11: Tamarin and ECMAScript 4

Classes✦ class Programmer {

var name; var city = “Boston, MA”; const interest = “computers”; function work() {}}

✦ var p = new Programmer;p.name = “John”;p.work();p.work.apply( someotherp );p.interest = “science”; // Error

Page 12: Tamarin and ECMAScript 4

Dynamic Classes✦ dynamic class Programmer {

var name; var city = “Boston, MA”; const interest = “computers”; function work() {}}

✦ var p = new Programmer;p.lastName = “Resig”;for ( var i in p ) alert( i );// alert( “Resig” );

Page 13: Tamarin and ECMAScript 4

Getters and Setters✦ class Programmer {

var _name; function get name(){ return _name; } function set name(value){ _name = value + “ Resig”; }}

✦ var p = new Programmer;p.name = “John”;alert( p.name );// “John Resig”

Page 14: Tamarin and ECMAScript 4

Catch-Alls✦ dynamic class Programmer {

meta function get(name) { ... } meta function set(name, value) { alert(“Setting “ + name + “ to “ + value); }}

✦ var p = new Programmerp.name = “John”;// alert(“Setting name to John”);

Page 15: Tamarin and ECMAScript 4

Inheritance✦ class Artist {

function draw() { alert(“Drawing!”); }}class Designer extends Artist { override function draw() { alert(“Designing!”); }}

✦ var d = new Designerd.draw();// alert(“Designing!”);

Page 16: Tamarin and ECMAScript 4

Inheritance (cont.)✦ ‘final’ methods can’t be overriden✦ class Artist {

final function draw() {alert(“Drawing!”);}}class Designer extends Artist { // ERROR: Can’t override draw! override function draw() { alert(“Designing!”); }}

Page 17: Tamarin and ECMAScript 4

Inheritance (cont.)✦ ‘final’ classes can’t be inherited from✦ final class Artist {

function draw() { alert(“Drawing!”); }}

// ERROR: Can’t inherit from Artistclass Designer extends Artist { override function draw() { alert(“Designing!”); }}

Page 18: Tamarin and ECMAScript 4

Metaclass✦ Provide global functions and properties on

a class object✦ class Users {

static function find( name ) { // ... }}

✦ Users.find( “John” );

Page 19: Tamarin and ECMAScript 4

Interfaces✦ Verify that a class implements another✦ interface Artist {

function draw();}class Designer implements Artist { function draw() { alert(“Designing!”); }}

✦ var d = new Designer();if ( d is Artist ) alert(“Designers are Artists!”);

Page 20: Tamarin and ECMAScript 4

Types

Page 21: Tamarin and ECMAScript 4

Numbers✦ Numbers are now broken down into:

✦ byte✦ int✦ uint✦ double (ECMAScript 3-style Number)✦ decimal

Page 22: Tamarin and ECMAScript 4

Type Annotations✦ var name : string = “John”;✦ let x : double = 5.3;✦ function stuff( x: int, obj: Object ) :

boolean {}

Page 23: Tamarin and ECMAScript 4

Function Types✦ Only return specific types:

function isValid() : boolean { }✦ Only be used on certain objects types:

function every( this: Array, value: int ) { for ( var i = 0; i < this.length; i++ ) alert( this[i] );}every.call( [0,1,2], 3 );// alert(0); alert(1); alert(2);every.call({a: “b”}, 4);// ERROR

Page 24: Tamarin and ECMAScript 4

Rest Arguments✦ function stuff( name, ...values ){

alert( values.length );}

✦ stuff( “John”, 1, 2, 3, 4 );// alert( 4 );

✦ function stuff( name : string, ...values : [int] ) : void { alert( values.length );}

Page 25: Tamarin and ECMAScript 4

Union and Any Types✦ var test : (string, int, double) = “test”;

test = 3;test = false; // ERROR

✦ type AnyNumber = (byte, int, double, decimal, uint);

✦ var test : AnyNumber = 3✦ These are equivalent:

✦ var test : * = “test”;✦ var test = “test”;

Page 26: Tamarin and ECMAScript 4

Type Definitions✦ type Point = { x: int, y: int };✦ var p : Point = { x: 3, y: 24 };

Page 27: Tamarin and ECMAScript 4

Nullability✦ Prevent variables from accepting null

values✦ var name : * = “John”;✦ var name : String! = “John”;

name = “Ted”;name = null; // ERROR

✦ function test( name: String? ) { alert( name );}

Page 28: Tamarin and ECMAScript 4

Initialization✦ class User {

var name : string!; // Must be initialized var last : string!; function User( n, l ) : name = n, last = l { // ... }}

Page 29: Tamarin and ECMAScript 4

“like”✦ type Point = { x: int, y: int };✦ if ( { x: 3, y: 5 } like Point )

alert( “That looks like a point to me!” );✦ if ( !({ x: 3 } like Point) )

alert( “Not a point!” );✦ // Loop over array-like things:

function every( a: like { length: uint } ) { for ( var i = 0; i < a.length; i++ ) alert( a[i] );}

Page 30: Tamarin and ECMAScript 4

“wrap”✦ Force a type if compatible one doesn’t

exist✦ type Point = { x: int, y: int };

var p: wrap Point = { x: 3, y: 8 };✦ var p: Point = { x: 3, y: 8 } wrap Point;✦ var p: Point = { x: 3, y: 8 } : Point;

Page 31: Tamarin and ECMAScript 4

Parameterized Types✦ var m: Map.<Object, string>;✦ class Point.<T> {

var x: T, y: T;}

✦ var p = new Point.<double>;p.x = 3.0;p.y = 5.0;

Page 32: Tamarin and ECMAScript 4

Structure

Page 33: Tamarin and ECMAScript 4

For .. Each✦ For each loops through values✦ let s = “”;

for each ( let n in [“a”,”b”,”c”] ) s += n;alert(s);// “abc”

Page 34: Tamarin and ECMAScript 4

let statements✦ for ( let i = 0; i < a.length; i++ )

alert( a[i] );// i is undefined

✦ Using block statements:{ let x = 5; { let x = 6; alert( x ); // 6 } alert( x ); // 5}

Page 35: Tamarin and ECMAScript 4

let (cont.)✦ let expressions:

var a = 5;var x = 10 + (let (a=3) a) + a*a;// x == 19

✦ let blocks:let ( a=3 ) { alert( a ); // 3}// a is undefined

✦ let a = function(){ };

Page 36: Tamarin and ECMAScript 4

Packages✦ package simple.tracker {

internal var count: int = 0; public function add(){ return ++count; }}

✦ import simple.tracker.*alert( add() ); // alert(“1”)count // ERROR, undefined

Page 37: Tamarin and ECMAScript 4

Namespaces✦ namespace extra = “extra”;✦ Pre-defined namespaces:

✦ __ES4__✦ intrinsic✦ iterator✦ meta

✦ import dojo.query;import jquery.query;dojo::query(“#foo”)jquery::query(“div > .foo”)

Page 38: Tamarin and ECMAScript 4

Namespaces (cont.)✦ import dojo.query;

import jquery.query;use namespace dojo;query(“#foo”) // using dojo

use namespace jquery;query(“div > .foo”) // using jquery

Page 39: Tamarin and ECMAScript 4

Multimethods✦ generic function intersect(s1, s2);

generic function intersect(s1: Shape, s2: Shape ) { // ...}generic function intersect(s1: Rect, s2: Rect ) { // ...}

Page 40: Tamarin and ECMAScript 4

Program Units✦ use unit jQuery “http://jquery.com/jQuery”

import com.jquery.*;new jQuery();

✦ unit jQuery { use unit Selectors “lib/Selectors”; package com.jquery { class jQuery { function find() : jQuery {} } }}

Page 41: Tamarin and ECMAScript 4

Operator Overloading✦ class Complex! { ... }

generic intrinsic function +(a: Complex, b: Complex) new Complex( a.real + b.real, a.imag + b.imag )

generic intrinsic function +(a: Complex, b: AnyNumber) a + Complex(b)

generic intrinsic function +(a: AnyNumber, b: Complex) Complex(a) + b

Page 42: Tamarin and ECMAScript 4

Self-Hosting

Page 43: Tamarin and ECMAScript 4

Map.es✦ The reference implementation’s classes are

written in ECMAScript✦ package{ use namespace intrinsic; use default namespace public; intrinsic class Map.<K,V> { static const length = 2; function Map(equals=intrinsic::===, hashcode=intrinsic::hashcode) : equals = equals , hashcode = hashcode , element_count = 0 { } // ...}

Page 44: Tamarin and ECMAScript 4

More Info✦ ECMAScript site:

http://ecmascript.org/✦ ECMAScript 4 White Paper Overview:

http://www.ecmascript.org/es4/spec/overview.pdf

✦ Blogging:http://ejohn.org/