Type Inference for Static Compilation of JavaScript (Extended Version)

We present a type system and inference algorithm for a rich subset of JavaScript equipped with objects, structural subtyping, prototype inheritance, and first-class methods. The type system supports abstract and recursive objects, and is expressive enough to accommodate several standard benchmarks with only minor workarounds. The invariants enforced by the types enable an ahead-of-time compiler to carry out optimizations typically beyond the reach of static compilers for dynamic languages. Unlike previous inference techniques for prototype inheritance, our algorithm uses a combination of lower and upper bound propagation to infer types and discover type errors in all code, including uninvoked functions. The inference is expressed in a simple constraint language, designed to leverage off-the-shelf fixed point solvers. We prove soundness for both the type system and inference algorithm. An experimental evaluation showed that the inference is powerful, handling the aforementioned benchmarks with no manual type annotation, and that the inferred types enable effective static compilation.

[1]  John C. Mitchell,et al.  A Delegation-based Object Calculus with Subtying , 1995, FCT.

[2]  François Pottier,et al.  A framework for type inference with subtyping , 1998, ICFP '98.

[3]  Didier Rémy,et al.  Objective ML: An Effective Object-Oriented Extension to ML , 1998, Theory Pract. Object Syst..

[4]  George C. Necula,et al.  SJS: a Typed Subset of JavaScript with Fixed Object Layout , 2015 .

[5]  Jens Palsberg,et al.  Object-oriented type inference , 1991, OOPSLA '91.

[6]  Jim Baker,et al.  Design and evaluation of gradual typing for python , 2015, DLS.

[7]  Manu Sridharan,et al.  Tech Report : A Practical Framework for Type Inference Error Explanation , 2016 .

[8]  George C. Necula,et al.  SJS: A Type System for JavaScript with Fixed Object Layout , 2015, SAS.

[9]  Jens Palsberg,et al.  Type inference for record concatenation and subtyping , 2004, Inf. Comput..

[10]  Mitchell Wand,et al.  Type inference for record concatenation and multiple inheritance , 1989, [1989] Proceedings. Fourth Annual Symposium on Logic in Computer Science.

[11]  Karthikeyan Bhargavan,et al.  Language-based Defenses Against Untrusted Browser Origins , 2013, USENIX Security Symposium.

[12]  Jim Baker,et al.  Design and evaluation of gradual typing for python , 2014, DLS.

[13]  Sam Tobin-Hochstadt,et al.  Monotonic References for Efficient Gradual Typing , 2015, ESOP.

[14]  François Pottier,et al.  Simplifying Subtyping Constraints: A Theory , 2001, Inf. Comput..

[15]  Shriram Krishnamurthi,et al.  The Essence of JavaScript , 2010, ECOOP.

[16]  Didier Rémy From Classes to Objects via Subtyping , 1998, ESOP.

[17]  Peter Thiemann Towards a Type System for Analyzing JavaScript Programs , 2005, ESOP.

[18]  Avik Chaudhuri,et al.  The ins and outs of gradual type inference , 2012, POPL '12.

[19]  Sophia Drossopoulou,et al.  Towards Type Inference for JavaScript , 2005, ECOOP.

[20]  Martín Abadi,et al.  A Theory of Primitive Objects - Untyped and First-Order Systems , 1994, TACS.

[21]  Joe Gibbs Politz,et al.  TeJaS: retrofitting type systems for JavaScript , 2013, DLS '13.

[22]  Alexander Aiken,et al.  Introduction to Set Constraint-Based Program Analysis , 1999, Sci. Comput. Program..

[23]  Michele Bugliesi,et al.  A Lambda Calculus of Incomplete Objects , 1996, MFCS.

[24]  Robin Milner,et al.  Principal type-schemes for functional programs , 1982, POPL '82.

[25]  Yvonne Koch,et al.  Javascript The Good Parts , 2016 .

[26]  Jens Palsberg,et al.  Type Inference of SELF , 1993, ECOOP.

[27]  Mitchell Wand,et al.  Complete Type Inference for Simple Objects , 1987, LICS.

[28]  Peter Thiemann,et al.  Type Analysis for JavaScript , 2009, SAS.

[29]  Scott F. Smith,et al.  Subtyping Constrained Types , 1996, SAS.

[30]  Jan Vitek,et al.  An analysis of the dynamic behavior of JavaScript programs , 2010, PLDI '10.

[31]  Joe Gibbs Politz,et al.  Semantics and Types for Objects with First-Class Member Names , 2012 .