Experimental New Directions for JavaScript

Similar documents
Principles of Programming Languages

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

JavaScript: Sort of a Big Deal,

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

Principles of Programming Languages

Programming in Scala Second Edition

Introduction to Programming Using Java (98-388)

B l o c k B i n d i n g s

Principles of Programming Languages

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

An Introduction to TypeScript. Personal Info

Grafting Functional Support on Top of an Imperative Language

Introduction to Programming Microsoft.NET Applications with Visual Studio 2008 (C#)

TypeScript coding JavaScript without the pain

INF5750. Introduction to JavaScript and Node.js

Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming)

The role of semantic analysis in a compiler

WebAssembly what? why? whither? Ben L. Titzer Andreas Rossberg Google Germany

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Recursive Types and Subtyping

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Compiler Errors. Flash CS4 Professional ActionScript 3.0 Language Reference. 1 of 18 9/6/2010 9:40 PM

D Programming Language

KOTLIN/NATIVE + CLANG, TRAVEL NOTES NIKOLAY IGOTTI, JETBRAINS

Recap: Functions as first-class values

Principles of Programming Languages

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CSC 533: Organization of Programming Languages. Spring 2005

2.4 Structuring programs

COS 320. Compiling Techniques

Titanium. Titanium and Java Parallelism. Java: A Cleaner C++ Java Objects. Java Object Example. Immutable Classes in Titanium

Microsoft Visual C# Step by Step. John Sharp

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

Runtime Checking for Program Verification Systems

Contract Programming For C++0x

This document defines the ActionScript 3.0 language, which is designed to be forward- compatible with the next edition of ECMAScript (ECMA-262).

Concepts of Object-Oriented Programming Peter Müller

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

COMP284 Scripting Languages Lecture 14: JavaScript (Part 1) Handouts

Concepts of Programming Languages

CSCI-GA Scripting Languages

Soot A Java Bytecode Optimization Framework. Sable Research Group School of Computer Science McGill University

2 Blending static and dynamic typing

4 CoffeeStrainer Virtues and Limitations

TRANSLATING DART TO EFFICIENT JAVASCRIPT. Kasper Lund Google

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Spot: A Programming Language for Verified Flight Software

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

CSC Web Programming. Introduction to JavaScript

The New Java Technology Memory Model

CMSC 330: Organization of Programming Languages

JavaScript and V8 A functional-ish language and implementation in the mainstream

Consistent Subtyping for All

6.005 Elements of Software Construction Fall 2008

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness?

(Not Quite) Minijava

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Programming Languages Lecture 15: Recursive Types & Subtyping

A classic tool: slicing. CSE503: Software Engineering. Slicing, dicing, chopping. Basic ideas. Weiser s approach. Example

Contemporary Design. Traditional Hardware Design. Traditional Hardware Design. HDL Based Hardware Design User Inputs. Requirements.

Program Verification (6EC version only)

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Type Bindings. Static Type Binding

Introduction to Swift. Dr. Sarah Abraham

Once Upon a Polymorphic Type

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Recursive Types and Subtyping

CS312: Programming Languages. Lecture 21: JavaScript

Programming Languages. Streams Wrapup, Memoization, Type Systems, and Some Monty Python

Minsoo Ryu. College of Information and Communications Hanyang University.

COMP-520 GoLite Tutorial

TypeScript. Types. CS144: Web Applications

Classes, interfaces, & documentation. Review of basic building blocks

Pierce Ch. 3, 8, 11, 15. Type Systems

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

Why Discuss JavaScript? CS312: Programming Languages. Lecture 21: JavaScript. JavaScript Target. What s a Scripting Language?

Semantic Subtyping. Alain Frisch (ENS Paris) Giuseppe Castagna (ENS Paris) Véronique Benzaken (LRI U Paris Sud)

Structure of Programming Languages Lecture 10

Concepts of Object-Oriented Programming Peter Müller

Introduction to the SharePoint Framework Bob German Principal Architect - BlueMetal. An Insight company

The Typed Racket Guide

CSE 431S Type Checking. Washington University Spring 2013

Synchronization SPL/2010 SPL/20 1

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

C# in Depth SECOND EDITION JON SKEET. MANNING Greenwich (74 w. long.)

Software Exorcism: A Handbook for Debugging and Optimizing Legacy Code

CS 321 Homework 4 due 1:30pm, Thursday, March 15, 2012 This homework specification is copyright by Andrew Tolmach. All rights reserved.

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

The Trouble with Types

Introduce C# as Object Oriented programming language. Explain, tokens,

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Lecture 13: Subtyping

Microsoft. Microsoft Visual C# Step by Step. John Sharp

The Typed Racket Guide

JavaScript CS 4640 Programming Languages for Web Applications

Transcription:

Experimental New Directions for JavaScript Andreas Rossberg, V8/Google

Motivation Broad need for (more) scalable JavaScript Usability, esp. maintainability Performance, esp. predictability ES6 opens up new opportunities Types desperately needed (but tricky)

An Experiment Embrace Harmony Shun bad legacy Grow types In a VM!

Two Parts (Working titles) SaneScript a cleaner subset of JavaScript Focus on removing features Transition path to... SoundScript a gradual type system for JavaScript Based on TypeScript, but sound & effective Does not depend on, but benefits from, SaneScript Both fully interoperate with good old JavaScript

Plan Implement in V8, prototype in Traceur Test in the field, iterate Need feedback! Collaboration welcome Ideally, develop into ES proposals eventually

SaneScript In an insane world, it was the sanest choice. Sarah Connor

Motivation Guide programmers on well-lit path Safer semantics Predictable performance Aim for the 95% use cases

Sane Mode Opt-in: use sanity (TBD) Implies strict mode Freely interoperable with insane code Can still be run as insane code (with caveats)

Subsetting the Language Static restrictions (early errors) Dynamic restrictions (exceptions) Per-object restrictions ( sane objects )

Subsetting Compatibility Sane code not hitting any of the restrictions would have same meaning outside the mode That is, correct sane code can run unchanged on VMs not recognising the opt-in

Sane Scoping No more var No undeclared variables No use before declaration (static dead zone), except mutually recursive function declarations let is the new var. Proper scoping FTW.

Sane Objects Accessing missing properties throws (on both reads & writes) Objects created in sane mode are non-extensible No freezing of non-configurable properties If you want maps, you take maps.

Sane Classes Class objects and their prototypes are frozen Instances are created sealed Methods require proper instances Fast and safe method & field access FTW.

Sane Arrays No holes, no accessors, no reconfiguration Length always in sync No out-of-bounds access, except extension at the end Fast arrays FTW. Maps are the new sparse arrays.

Sane Functions No arguments object Calling with too few arguments throws Optional and rest arguments FTW.

Sane Coercions Nothing implicit besides ToBoolean (almost?) == and!= require compatible typeof No more WAT, no more WTF.

Plan Implement in Q1/2

SoundScript That s sound advice at any time. Jean-Luc Picard

Motivation Everybody keeps inventing type systems for JS Both user-facing and internal We strongly support standardisation! But inside a VM new requirements arise...and new opportunities! Needs investigation

Design Goals Based on TypeScript (familiarity, reuse) Gradual (interop with untyped code) Sound (reliability, non-local optimisations) Precise (aggressive optimisations) Effective (feasible inside VM) Modular (lazy compilation, caching)

Sound Gradual Typing When it claims E:T, then, in fact, E:T But type any means dynamically typed Type any induces runtime type checks if necessary Protects invariants of statically typed code Disallow higher-order casts that d require wrapping (expensive; observable in JavaScript!)

Runtime Type Checking Objects and functions carry (more) runtime type information Operations at type any may need to check get, set, call,... Should not be a common case Much cheaper when done by VM!

Structural à la TypeScript interface T extends U { a : number, m(x : string) : number, (x : boolean) : T, new(x : string) : U } (x : T) => U := {(x : T) : U}

Functions & Methods Can annotate type of this: function(this : T, x : U) {} Function types are contravariant (soundness!) Method types are different, covariant in this (tied to concise method syntax) Method extraction only allowed when this : any

Nominal Classes Class C introduces nominal instance type C...and nominal class type typeof C Both are subtypes of respective structural types Interfaces remain structural

Why Nominal? Sound private state Sound binary methods Sound first-class classes More efficient code More efficient compilation (it s runtime, too!)

Nominal Typing, Example class D extends C { public a : T constructor(x : T) {} m(x : T) : U {} static s(x : T) : U } D < C D < {a:t, m(x:t):u, constructor: typeof D,...C s...} typeof D < {new(x:t):d, s(x:t):u,...c s...}

Subtyping Nominal type are subtypes of structural Vice versa also allowed (semi-structural types) No (depth) subtyping on mutable properties But on immutable properties various requests for immutable data Invariant generics (at least for now)

Generics Sound (for realz) Runtime type passing (i.e., unerased) But no first-class instantiation (that is, f<t> is not a value) Rationale: would change operational semantics

Going More Gradual Choice between T or any not gradual enough Enter any<t> restricts uses as if T, but provides no more guarantees than any Essentially, TypeScript s interpretation of T Mainly for typing intrinsics, programmers shouldn t need it often

Type Inference Bidirectional type checking No inference across function boundaries Don t break lazy compilation!

Lazy Compilation Keep supporting function granularity jit Mayhaps require deferred early errors Consider eager type-checking later (cost?)

Numerous Challenges Would like non-nullable as default, feasible? Would like a proper integer type, how? How much immutability can we require in typed code? Full ES6: symbols, how avoid dependent types? Full ES6: first-class classes, how deal with generativity? Control-flow dependent typing, how much? Reflection, what API? Syntax, what to do about incompatibilities? Performance, how keep cost of type checking low? Blame tracking, do we need any in the absence of wrapping? Object.observe breaks all optimisation ideas...

Plan Implement in Q2-4 (?)

Types in VM: Challenges Type system must respect open world assumption additional definitions can be added at any time Type checking must be efficient enough preference for nominal typing Must not break lazy compilation of functions precludes non-local type inference necessitates deferred early error semantics

Types in VM: Opportunities More optimisations! aggressive ones require soundness Affordable runtime type checks easier debugging enabler for soundness Predictable performance Reduced warm-up time No opt/deopt cycles of death Ahead-of-time compilation/optimisation

Summary Both new challenges and new opportunities putting types into a VM Standardising an unsound type system would be a big lost opportunity This is an experiment All public, we would like your feedback!

Encore

Optional Types All types are non-nullable by default preferably exclude both null and undefined, but the latter might be very hard to reconcile with existing APIs Type?T as short-hand for T undefined null

First-Class Classes Requires proper class types: class C extends T {...} Essentially, F-bounded existential type Generative: functions returning a class create a new class (i.e., existential type) with each call Implicitly opened when bound to a variable Classes as parameters behave dually (universal type) do-expressions will introduce avoidance problem