Abstract
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.
Supplemental Material
Available for Download
This archive contains the benchmarks listed in Table 2 of the paper, ported to our JavaScript subset.
- Octane Benchmarks. https://developers.google.com/ octane/.Google Scholar
- SunSpider Benchmarks. https://www.webkit.org/perf/ sunspider/sunspider.html.Google Scholar
- Flow. http://www.flowtype.org.Google Scholar
- JetStream Benchmarks. http://browserbench.org/ JetStream/.Google Scholar
- JavaScriptCore JavaScript engine. http://trac.webkit. org/wiki/JavaScriptCore.Google Scholar
- The Redmonk Programming Language Rankings: June 2015.Google Scholar
- https://redmonk.com/sogrady/2015/07/01/ language-rankings-6-15/.Google Scholar
- Tizen Platform. https://www.tizen.org/.Google Scholar
- TypeScript. http://www.typescriptlang.org.Google Scholar
- V8 JavaScript Engine. https://developers.google.com/ v8/.Google Scholar
- T.J. Watson Libraries for Analysis (WALA). http://wala. sf.net, 2015.Google Scholar
- Martín Abadi and Luca Cardelli. A Theory of Primitive Objects: Untyped and First-order Systems. Information and Computation, 125(2):78–102, 1996. doi:10.1006/inco.1996.0024. Google Scholar
Digital Library
- Ole Agesen, Jens Palsberg, and Michael I. Schwartzbach. Type Inference of SELF. In Proceedings of the 7th European Conference on Object-Oriented Programming, ECOOP ’93, pages 247–267, London, UK, UK, 1993. Springer-Verlag. Google Scholar
Digital Library
- ISBN 3-540-57120-5. doi:10.1007/3-540-47910-4_14.Google Scholar
- Alexander Aiken. Introduction to Set Constraint-based Program Analysis. Sci. Comput. Program., November 1999. ISSN 0167-6423. doi:10.1016/S0167-6423(99)00007-6. Google Scholar
Digital Library
- Christopher Anderson, Paola Giannini, and Sophia Drossopoulou. Towards Type Inference for JavaScript. In ECOOP 2005. doi:10.1007/11531142_19. Google Scholar
Digital Library
- Karthikeyan Bhargavan, Antoine Delignat-Lavaud, and Sergio Maffeis. Language-based defenses against untrusted browser origins. In Presented as part of the 22nd USENIX Security Symposium (USENIX Security 13), pages 653–670, Washington, D.C., 2013. USENIX. ISBN 978-1-931971-03-4. Google Scholar
Digital Library
- Viviana Bono, Michele Bugliesi, and Luigi Liquori. A Lambda Calculus of Incomplete Objects. In Mathematical Foundations of Computer Science 1996, pages 218–229. Springer, 1996. Google Scholar
Digital Library
- doi:10.1007/3-540-61550-4_150.Google Scholar
- Viviana Bono, Michele Bugliesi, Mariangiola Dezani-Ciancaglini, and Luigi Liquori. Subtyping Constraints for Incomplete Objects. In TAPSOFT’97: Theory and Practice of Software Development, pages 465–477. Springer, 1997. Google Scholar
Digital Library
- doi:10.1007/BFb0030619.Google Scholar
- Satish Chandra, Colin S. Gordon, Jean-Baptiste Jeannin, Cole Schlesinger, Manu Sridharan, Frank Tip, and Young-il Choi. Type Inference for Static Compilation of JavaScript. Technical report, Samsung Research America, August 2016.Google Scholar
- Avik Chaudhuri. Personal communication, 2016.Google Scholar
- Philip Wontae Choi, Satish Chandra, George Necula, and Koushik Sen. SJS: A Typed Subset of JavaScript with Fixed Object Layout. Technical Report UCB/EECS-2015- 13, EECS Department, University of California, Berkeley, Apr 2015.Google Scholar
- Wontae Choi, Satish Chandra, George C. Necula, and Koushik Sen. SJS: A Type System for JavaScript with Fixed Object Layout. In Static Analysis - 22nd International Symposium, SAS 2015, Saint-Malo, France, September 9-11, 2015, Proceedings, pages 181–198, 2015. doi:10.1007/978-3-662-48288-9_11.Google Scholar
- François Pottier. A Framework for Type Inference with Subtyping. In Proceedings of the third ACM SIGPLAN International Conference on Functional Programming (ICFP’98), pages 228–238, September 1998. doi:10.1145/291251.289448. Google Scholar
Digital Library
- François Pottier. Simplifying Subtyping Constraints: A Theory. Information & Computation, 170(2):153–183, November 2001. Google Scholar
Digital Library
- doi:10.1006/inco.2001.2963.Google Scholar
- François Pottier and Didier Rémy. The Essence of ML Type Inference. In Benjamin C. Pierce, editor, Advanced Topics in Types and Programming Languages, chapter 10, pages 389– 489. MIT Press, 2005.Google Scholar
- Douglas Crockford. JavaScript: The Good Parts. O’Reilly Media, 2008. Google Scholar
Digital Library
- Luis Damas and Robin Milner. Principal type-schemes for functional programs. In Proceedings of the 9th ACM SIGPLANSIGACT symposium on Principles of programming languages, pages 207–212. ACM, 1982. doi:10.1145/582153.582176. Google Scholar
Digital Library
- Kathleen Fisher and John C Mitchell. A Delegation-based Object Calculus with Subtyping. In Fundamentals of Computation Theory, pages 42–61. Springer, 1995. doi:10.1007/3- 540-60249-6_40. Google Scholar
Digital Library
- Arjun Guha, Claudiu Saftoiu, and Shriram Krishnamurthi. The Essence of JavaScript. In European Conference on Object-Oriented Programming (ECOOP), pages 126–150. Springer, 2010. doi:10.1007/978-3-642-14107-2_7. Google Scholar
Digital Library
- Simon Holm Jensen, Anders Møller, and Peter Thiemann. Type Analysis for JavaScript. In SAS, pages 238–255, 2009.Google Scholar
Digital Library
- doi:10.1007/978-3-642-03237-0_17.Google Scholar
- Benjamin S. Lerner, Joe Gibbs Politz, Arjun Guha, and Shriram Krishnamurthi. TeJaS: Retrofitting Type Systems for JavaScript. In Proceedings of the 9th Symposium on Dynamic Languages, DLS ’13, pages 1–16, New York, NY, USA, 2013. ACM. ISBN 978-1-4503-2433-5. doi:10.1145/2508168.2508170. Google Scholar
Digital Library
- Calvin Loncaric, Satish Chandra, Cole Schlesinger, and Manu Sridharan. A practical framework for type inference error explanation. In Proceedings of the 2016 ACM International Conference on Object Oriented Programming Systems Languages & Applications, OOPSLA ’16, New York, NY, USA, 2016. ACM. Google Scholar
Digital Library
- Jens Palsberg and Michael I. Schwartzbach. Object-oriented Type Inference. In Conference Proceedings on Object-oriented Programming Systems, Languages, and Applications, OOPSLA ’91, pages 146–161, New York, NY, USA, 1991. ACM. Google Scholar
Digital Library
- ISBN 0-201-55417-8. doi:10.1145/117954.117965.Google Scholar
- Jens Palsberg and Tian Zhao. Type Inference for Record Concatenation and Subtyping. Inf. Comput., 189(1):54–86, 2004. doi:10.1016/j.ic.2003.10.001. Google Scholar
Digital Library
- Joe Gibbs Politz, Arjun Guha, and Shriram Krishnamurthi. Semantics and Types for Objects with First-class Member Names. In FOOL 2012: 19th International Workshop on Foundations of Object-Oriented Languages, page 37, 2012.Google Scholar
- Aseem Rastogi, Avik Chaudhuri, and Basil Hosmer. The Ins and Outs of Gradual Type Inference. In Proceedings of the 39th ACM Symposium on Principles of Programming Languages (POPL’12), pages 481–494. ACM, 2012. Google Scholar
Digital Library
- doi:10.1145/2103621.2103714.Google Scholar
- Aseem Rastogi, Nikhil Swamy, Cédric Fournet, Gavin Bierman, and Panagiotis Vekris. Safe and Efficient Gradual Typing for TypeScript. In Proceedings of the 39th ACM Symposium on Principles of Programming Languages (POPL’15). ACM, 2015. doi:10.1145/2775051.2676971. Google Scholar
Digital Library
- Didier Rémy. From classes to objects via subtyping. In European Symposium on Programming, pages 200–220. Springer, 1998. doi:10.1007/BFb0053572. Google Scholar
Digital Library
- Didier Rémy and Jérôme Vouillon. Objective ML: An effective object-oriented extension to ML. Theory And Practice of Object Systems, 4(1):27–50, 1998. doi:10.1002/(SICI)1096- 9942(1998)4:1<27::AID-TAPO3>3.0.CO;2-4. Google Scholar
Digital Library
- Gregor Richards, Sylvain Lebresne, Brian Burg, and Jan Vitek. An Analysis of the Dynamic Behavior of JavaScript Programs. PLDI ’10, pages 1–12, New York, NY, USA, 2010. ACM. ISBN 978-1-4503-0019-3. doi:10.1145/1806596.1806598. Google Scholar
Digital Library
- Gregor Richards, Francesco Zappa Nardelli, and Jan Vitek. Concrete Types for TypeScript. In John Tang Boyland, editor, 29th European Conference on Object-Oriented Programming (ECOOP 2015), volume 37 of Leibniz International Proceedings in Informatics (LIPIcs), pages 76– 100, Dagstuhl, Germany, 2015. Schloss Dagstuhl–Leibniz-Zentrum fuer Informatik. ISBN 978-3-939897-86-6. doi:10.4230/LIPIcs.ECOOP.2015.76.Google Scholar
- Jeremy G Siek, Michael M Vitousek, Matteo Cimini, Sam Tobin-Hochstadt, and Ronald Garcia. Monotonic References for Efficient Gradual Typing. In European Symposium on Programming Languages and Systems, ESOP, pages 432–456. Springer, 2015. doi:10.1007/978-3-662-46669-8_18. Google Scholar
Digital Library
- Peter Thiemann. Towards a Type System for Analyzing Javascript Programs. In ESOP 2005. ISBN 3-540-25435-8, 978-3-540-25435-5. doi:10.1007/978-3-540-31987-0_28. Google Scholar
Digital Library
- Valery Trifonov and Scott F. Smith. Subtyping Constrained Types. In Static Analysis, Third International Symposium, SAS’96, Aachen, Germany, September 24-26, 1996, Proceedings, pages 349–365, 1996. doi:10.1007/3-540-61739-6_52. Google Scholar
Digital Library
- Michael M Vitousek, Andrew M Kent, Jeremy G Siek, and Jim Baker. Design and Evaluation of Gradual Typing for Python. In Dynamic Language Symposium (DLS). ACM, 2014. Google Scholar
Digital Library
- doi:10.1145/2775052.2661101.Google Scholar
- Mitchell Wand. Complete Type Inference for Simple Objects. In LICS, volume 87, pages 37–44, 1987.Google Scholar
- Mitchell Wand. Type Inference for Record Concatenation and Multiple Inheritance. In Logic in Computer Science, 1989. LICS’89, Proceedings., Fourth Annual Symposium on, pages 92–97. IEEE, 1989. doi:10.1016/0890-5401(91)90050-C. Introduction Overview Type System Requirements The Type System Inference Challenges Terms, Types, and Constraint Generation Terms Types Subtyping and Type Equivalence Constraint Language Constraint Generation Constraint Solving Type Propagation Type Ascription Soundness of Type Inference Evaluation Practical Considerations Explanation of Workarounds More Problematic Constructs The Promise of Ahead-of-Time Compilation Related Work Google Scholar
Digital Library
Index Terms
Type inference for static compilation of JavaScript
Recommendations
Type inference for static compilation of JavaScript
OOPSLA 2016: Proceedings of the 2016 ACM SIGPLAN International Conference on Object-Oriented Programming, Systems, Languages, and ApplicationsWe 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 ...
Type elaboration and subtype completion for Java bytecode
Java source code is strongly typed, but the translation from Java source to bytecode omits much of the type information originally contained within methods. Type elaboration is a technique for reconstructing strongly typed programs from incompletely ...
Kinded type inference for parameteric overloading
AbstractParameteric overloading refers to the combination of parameteric polymorphism and overloading of polymorphic operators. The formal basis for parametric overloading, proposed by Kaes and extended by Wadler and Blott, is based on type predicates. In ...







Comments