Firefox, JavaScript, and the Future of the Web

Brendan Eich

Mozilla Corporation

Firefox 2 Launched

What's new in Firefox 2?

DOM Session Storage

DOM Global Storage

JavaScript on the March

JavaScript Fun Facts

JavaScript 1.7

Angst over JavaScript

Why People Worry

Selling Out?!

Selling Out?!

Free Advice that We Hear

Free Advice that We Hear

Leave It Alone

What's Wrong with This?

Environments as Objects

What's the Cost?

Designing for the Next Ten Years

Designing for the Next Ten Years

Motivation

Why Now, After All These Years?

What's New

What's New

Too Much New?

Type Soundness

Types

Type Annotations

Type Expressions

Classes

Interfaces

Structural Types

Namespaces

Packages

Blocks

Block Statements

Block Expressions

Destructuring Assignment

Iterators

Generators

  • yield in a function makes a generator:
    function count(n : int) {
      for (let i = 0; i < n; i++)
        yield i
    }
    
  • A generator function returns an iterator:
    var gen = count(10)
    gen.next() returns 0; . . .; gen.next() returns 9
    gen.next() throws StopIteration
    
  • gen.send(value) passes value back to yield
  • gen.throw(exception) throws exception from yield

Array Comprehensions

  • Sugar for initializing an Array from iterators
  • let squares = [i * i for (i in count(10))]
  • print(squares) => "0,1,4,9,...,81"
  • let odd_squares = [i * i for (i in count(10)) if (i % 2)]
  • return [i * j for (i in it1) for (j in it2) if (i != j)]

Operators

  • Much-requested ability to customize operators by type
  • Does not require dispatching based on argument type
  • Example showing binary vs. unary operator handling:
    class Complex {
      var real:Number, imag:Number;
      public static function +(a:Object, b:Object):Complex {
        if (a is Complex && b is Complex)
          return new Complex(a.real + b.real, a.imag + b.imag)
        if (b == null)
          return (a is Complex) ? a : new Complex(a)
        return intrinsic::+(a, b)
      }
      . . .
    }
    

Decimal

  • print(79.49 - 35.99) => 43.49999999999999
  • "JavaScript math is broken" (it's the same as Java, C, etc.)
  • Most-duplicated bug at https://bugzilla.mozilla.org
  • Solution: new decimal type and pragma to use it
    {
      use decimal
      print(79.49 - 35.99) // => 43.50
    }
    
  • Enough precision to represent all normalized doubles
  • Tracking IEEE 754r

Miscellany

  • A built-in, extensible JSON encoder and decoder
  • Object getters, setters, and "catch-alls":
      return {
        get x() { return this.inner.x },
        set x(nx) { this.inner.x = nx },
        get *(id) { return this.intrinsic::get(id) },
        set *(id,v) { return this.intrinsic::set(id, v) },
      }
    
  • Standard Date.parse format based on ISO 8601
    let today = Date.parse("2006-10-25T");
    
  • Slice operators: s[i:j], a[-5:-2]
  • Slice step: reversed = array[::-1], evens = nums[i:j:2];

Bug Fixes

  • arguments delegates to Array.prototype
  • Inner function this binds to outer this by default
  • Standard global properties (Date, String, etc.) immutable
  • new RegExp("pattern") same as /pattern/
  • Allow a trailing comma in object initialisers
  • s[i] for fast/guaranteed s.charAt(i) alternative
  • map and other generic functions on Array-likes

The Near Term

  • Access to the ECMA TG1 wiki in June 2006
  • JS1.7 in Firefox 2 released yesterday
    • let blocks
    • Destructuring assignment
    • Iterators
    • Generators
    • Array comprehensions
  • JS1.8 in Firefox 3 pretty soon (2007)
  • ECMAScript Edition 4 spec deadline: end of Q1 2007
  • JS2 based on final ES4 spec in Mozilla, in 2007

The Future of JavaScript

  • Web browsers under increasing size constraints
  • JS likely to remain the default/only browser programming language
  • JS2 will displace JS1 eventually (~4 years?)
  • JS2 must be strong enough for the long run
    • Small dynamic prototypes
    • Programming in the large support
    • Optimized for space and speed
    • Extensible enough to forestall JS3000
    • Security through virtual machine (VM) techniques

The Future of the Web

  • Browsers are runtimes for JS + HTML/CSS + XML
  • JS2 + XBL2 support new XML languages
    • With the right low-level rendering APIs
    • And sufficient runtime and memory performance
    • Which is coming in the form of JITed JS VMs
    • VMs provide better security models/guarantees
  • JS is good enough for a lot, but not for the future
  • JS2 is vital to the future of the Web

Finis