skip to main content
research-article

Type inference for static compilation of JavaScript

Published:19 October 2016Publication History
Skip Abstract Section

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.

Skip Supplemental Material Section

Supplemental Material

References

  1. Octane Benchmarks. https://developers.google.com/ octane/.Google ScholarGoogle Scholar
  2. SunSpider Benchmarks. https://www.webkit.org/perf/ sunspider/sunspider.html.Google ScholarGoogle Scholar
  3. Flow. http://www.flowtype.org.Google ScholarGoogle Scholar
  4. JetStream Benchmarks. http://browserbench.org/ JetStream/.Google ScholarGoogle Scholar
  5. JavaScriptCore JavaScript engine. http://trac.webkit. org/wiki/JavaScriptCore.Google ScholarGoogle Scholar
  6. The Redmonk Programming Language Rankings: June 2015.Google ScholarGoogle Scholar
  7. https://redmonk.com/sogrady/2015/07/01/ language-rankings-6-15/.Google ScholarGoogle Scholar
  8. Tizen Platform. https://www.tizen.org/.Google ScholarGoogle Scholar
  9. TypeScript. http://www.typescriptlang.org.Google ScholarGoogle Scholar
  10. V8 JavaScript Engine. https://developers.google.com/ v8/.Google ScholarGoogle Scholar
  11. T.J. Watson Libraries for Analysis (WALA). http://wala. sf.net, 2015.Google ScholarGoogle Scholar
  12. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  13. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  14. ISBN 3-540-57120-5. doi:10.1007/3-540-47910-4_14.Google ScholarGoogle Scholar
  15. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  16. Christopher Anderson, Paola Giannini, and Sophia Drossopoulou. Towards Type Inference for JavaScript. In ECOOP 2005. doi:10.1007/11531142_19. Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  18. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  19. doi:10.1007/3-540-61550-4_150.Google ScholarGoogle Scholar
  20. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  21. doi:10.1007/BFb0030619.Google ScholarGoogle Scholar
  22. 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 ScholarGoogle Scholar
  23. Avik Chaudhuri. Personal communication, 2016.Google ScholarGoogle Scholar
  24. 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 ScholarGoogle Scholar
  25. 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 ScholarGoogle Scholar
  26. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  27. François Pottier. Simplifying Subtyping Constraints: A Theory. Information & Computation, 170(2):153–183, November 2001. Google ScholarGoogle ScholarDigital LibraryDigital Library
  28. doi:10.1006/inco.2001.2963.Google ScholarGoogle Scholar
  29. 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 ScholarGoogle Scholar
  30. Douglas Crockford. JavaScript: The Good Parts. O’Reilly Media, 2008. Google ScholarGoogle ScholarDigital LibraryDigital Library
  31. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  32. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  33. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  34. Simon Holm Jensen, Anders Møller, and Peter Thiemann. Type Analysis for JavaScript. In SAS, pages 238–255, 2009.Google ScholarGoogle ScholarDigital LibraryDigital Library
  35. doi:10.1007/978-3-642-03237-0_17.Google ScholarGoogle Scholar
  36. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  37. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  38. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  39. ISBN 0-201-55417-8. doi:10.1145/117954.117965.Google ScholarGoogle Scholar
  40. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  41. 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 ScholarGoogle Scholar
  42. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  43. doi:10.1145/2103621.2103714.Google ScholarGoogle Scholar
  44. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  45. Didier Rémy. From classes to objects via subtyping. In European Symposium on Programming, pages 200–220. Springer, 1998. doi:10.1007/BFb0053572. Google ScholarGoogle ScholarDigital LibraryDigital Library
  46. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  47. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  48. 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 ScholarGoogle Scholar
  49. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  50. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  51. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  52. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  53. doi:10.1145/2775052.2661101.Google ScholarGoogle Scholar
  54. Mitchell Wand. Complete Type Inference for Simple Objects. In LICS, volume 87, pages 37–44, 1987.Google ScholarGoogle Scholar
  55. 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 ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Type inference for static compilation of JavaScript

      Recommendations

      Comments

      Login options

      Check if you have access through your login credentials or your institution to get full access on this article.

      Sign in

      Full Access

      • Published in

        cover image ACM SIGPLAN Notices
        ACM SIGPLAN Notices  Volume 51, Issue 10
        OOPSLA '16
        October 2016
        915 pages
        ISSN:0362-1340
        EISSN:1558-1160
        DOI:10.1145/3022671
        Issue’s Table of Contents
        • cover image ACM Conferences
          OOPSLA 2016: Proceedings of the 2016 ACM SIGPLAN International Conference on Object-Oriented Programming, Systems, Languages, and Applications
          October 2016
          915 pages
          ISBN:9781450344449
          DOI:10.1145/2983990

        Copyright © 2016 ACM

        Publisher

        Association for Computing Machinery

        New York, NY, United States

        Publication History

        • Published: 19 October 2016

        Check for updates

        Qualifiers

        • research-article

      PDF Format

      View or Download as a PDF file.

      PDF

      eReader

      View online with eReader.

      eReader
      About Cookies On This Site

      We use cookies to ensure that we give you the best experience on our website.

      Learn more

      Got it!