Type inference for JavaScript

Object-oriented scripting languages like JavaScript are popular, in part because of their dynamic features. These include the runtime modification of objects and classes, through addition of fields or updating of methods. These features make static typing difficult and usually dynamic typing is used. Consequently, errors such as access to non-existent members, are not detected until runtime. We provide a static type system that can cope with dynamic features such as member addition, while providing the usual safety guarantees. Since the structure of objects may change over time, we employ a structural type system to track the changes. We show how type inference can be used to infer the structure of objects and give corresponding structural types. Therefore, the programmer can enjoy the safety offered by static typing, without having to give explicit types in their programs. We develop JS0, a formalisation of JavaScript with features including dynamic addition of fields and updating of methods. We give an operational semantics and static type system for JS0 using structural types. Our types allow objects to evolve in a controlled manner by classifying members as definite or potential. A member is potential until it has been assigned to and then it becomes definite. We prove that our type system is sound. We develop a type inference algorithm for JS0 based on a system of constraints between type variables. We define a translation between constraints and types that allows us to generate a type annotated JS0 program from an untype JS0 program. We prove that the constraints are sound with respect to the type system and that our translation is deterministic. We define a well-formedness criterion on constraints and conjecture that well-formed constraints are satisfiable. Therefore, combined with the soundness of constraints and the soundness of the type system, we conjecture that programs that generate well-formed constraints will not get stuck.

[1]  M. Tofte Operational Semantics and Polymorphic Type Inference , 1988 .

[2]  Tim Sheard,et al.  Accomplishments and Research Challenges in Meta-programming , 2001, SAIG.

[3]  Bjarne Stroustrup,et al.  The C++ Programming Language, Second Edition , 1991 .

[4]  Paola Giannini,et al.  Type Checking for JavaScript , 2005, Electron. Notes Theor. Comput. Sci..

[5]  Jens Palsberg Efficient Inference of Object Types , 1995, Inf. Comput..

[6]  Ole Agesen The Cartesian Product Algorithm: Simple and Precise Type Inference Of Parametric Polymorphism , 1995, ECOOP.

[7]  Steve McConnell,et al.  Code complete - a practical handbook of software construction, 2nd Edition , 1993 .

[8]  Andrew A. Chien,et al.  Concurrent aggregates (CA) , 1990, PPOPP '90.

[9]  Guy L. Steele,et al.  Java Language Specification, Second Edition: The Java Series , 2000 .

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

[11]  Viviana Bono,et al.  An Imperative, First-Order Calculus with Object Extension , 1998, ECOOP.

[12]  Bertrand Meyer,et al.  Eiffel: A language and environment for software engineering , 1988, J. Syst. Softw..

[13]  Sarfraz Khurshid,et al.  Is the Java Type System Sound? , 1999, Theory Pract. Object Syst..

[14]  Robin Milner,et al.  Definition of standard ML , 1990 .

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

[16]  Jens Palsberg,et al.  Object-oriented type systems , 1994, Wiley professional computing.

[17]  Mads Tofte,et al.  Type Inference for Polymorphic References , 1990, Inf. Comput..

[18]  Scott F. Smith,et al.  Precise Constraint-Based Type Inference for Java , 2001, ECOOP.

[19]  Kim B. Bruce A paradigmatic object-oriented programming language: Design, static typing and semantics , 1994, Journal of Functional Programming.

[20]  Scott F. Smith,et al.  Sound polymorphic type inference for objects , 1995, OOPSLA.

[21]  Kim B. Bruce,et al.  PolyTOIL: A Type-Safe Polymorphic Object-Oriented Language , 1995, ECOOP.

[22]  Sophia Drossopoulou,et al.  Java is Type Safe - Probably , 1997, ECOOP.

[23]  Andrew A. Chien,et al.  Precise concrete type inference for object-oriented languages , 1994, OOPSLA 1994.

[24]  Luigi Liquori On Object Extension , 1998, ECOOP.

[25]  David Robson,et al.  Smalltalk-80: The Language and Its Implementation , 1983 .

[26]  Sophia Drossopoulou,et al.  Can addresses be types? (A case study: objects with delegation) , 2003, Electron. Notes Theor. Comput. Sci..

[27]  Jens Palsberg,et al.  Making Type Inference Practical , 1992, ECOOP.

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

[29]  Sophia Drossopoulou,et al.  Type Inference for Scripting Languages , 2004 .

[30]  Jens Palsberg,et al.  Object-oriented type inference , 1991, OOPSLA 1991.

[31]  Luca Cardelli,et al.  Subtyping recursive types , 1991, POPL '91.

[32]  Jonathan Robie,et al.  Editors , 2003 .

[33]  Paola Giannini,et al.  A Calculus for "Environment-Aware" Computation , 2002, Electron. Notes Theor. Comput. Sci..

[34]  Bjarne Stroustrup,et al.  The Design and Evolution of C , 1994 .

[35]  David Walker,et al.  Alias Types for Recursive Data Structures , 2000, Types in Compilation.

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

[37]  David Flanagan,et al.  JavaScript: The Definitive Guide , 1996 .

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

[39]  Sophia Drossopoulou,et al.  BabyJ: from object based to class based programming via types , 2003, WOOD.

[40]  Ralph E. Johnson,et al.  Design Patterns: Abstraction and Reuse of Object-Oriented Design , 1993, ECOOP.

[41]  Paola Giannini,et al.  Alias types for "environment-aware" computations , 2003, Electron. Notes Theor. Comput. Sci..

[42]  Martín Abadi,et al.  An Imperative Object Calculus , 1995, TAPSOFT.

[43]  Luigi Liquori Bounded Polymorphism for Extensible Objects , 1998, TYPES.

[44]  Ole Agesen,et al.  Concrete type inference: delivering object-oriented applications , 1995 .

[45]  Luca Cardelli,et al.  The Modula–3 type system , 1989, POPL '89.

[46]  John C. Mitchell,et al.  Type systems for object-oriented programming languages , 1996 .

[47]  Craig Chambers,et al.  The cecil language: specification and rationale , 1993 .