Rewriting JavaScript Module System

[Extended Abstract]

Junhee Cho KAIST [email protected]

ABSTRACT overrides the other for the symbol $. In substitution for Although JavaScript is one of the major languages used module system, module pattern is recommended. However, for web and other general applications, it does not have since it is not a system on language level, it is hard to guar- a language-level module system. Lack of module system antee properties on module system. causes name conflicts when programmer uses libraries. We Because of the needs for language-level module system, introduce a JavaScript module system with formal seman- ECMAScript Harmony [8], a proposal for the next genera- tics. As an implementation, we also introduce a source- tion of JavaScript, introduces a language-level module sys- to-source transformation from JavaScript with module to tem. Since ECMAScript Harmony is still a work in progress, JavaScript itself for current JavaScript engines to use them. it does not provide complete module , but part of module semantics in high-level description in prose. Kang et al. [9] introduced the formal specification and the imple- Categories and Subject Descriptors mentation of a module system based on the module system D.3.3 [Programming Languages]: Language Constructs in ECMAScript Harmony by the means of desugaring rule and Features from JavaScript with module to λJS [10], a core-calculus of JavaScript. Unfortunately, due to the innate limitations of Keywords λJS , the module system introduced is impractical. First of all, desugared λJS program is very large, and interpretation JavaScript, module system, source-to-source transformation of program involves a large amount of memory usage. Be- cause the main objective for λJS is to prove properties on 1. INTRODUCTION JavaScript easily, interpreting a general JavaScript program JavaScript [1] is the most prevalent client-side scripting with λJS is nearly impossible. λJS is a purely functional language for the web. It enriches web documents with dy- language. Thus, it consumes a lot of memory for immutable namic reaction to user’s action. The primary method for a objects while interpreting a program. Secondly, λJS does web document to react to a user’s action is to reload an- not support eval function which generates code dynamically other web document on the web browser triggered by a click in run-time while use of dynamic features is evident in web- on a link. For instance, in a small messanger widget nested sites [11, 12, 13]. In the web environment, most of data is in a web document, however, it is not necessary to reload encoded in either XML or JSON. While we need XML parser the whole web document frequently for each message. With to parse XML string, we do not need JSON parser to parse JavaScript, one can reload only the small widget by com- JSON string because JSON string is JavaScript expression municating with the server through HTTP and updating and evaluating JSON string with eval function gives JSON the web document with Brower API for Document Object object. Likewise, though we can avoid use of eval function, Model (DOM) [2]. The fact that 98 out of the 100 most it is widely used in practice due to its convenience. visited websites use JavaScript for client-side programming according to Alexa [3] presents its share on the market that cannot be ignored. Evenmore, JavaScript has been used 2. JAVASCRIPT MODULE SYSTEM outside client-side programming for the web; for example, We introduce the formal specification and the implemen- node.js [4] for a general scalable network application, and tation of a module system based on the aforementioned pre- Samsung SmartTV SDK [5] for a SmartTV app. vious works. The module system is based on Scalable Anal- Nevertheless most of JavaScript program use libraries such ysis Framework for ECMAScript (SAFE) [14]: the formal as jQuery [6] or Prototype [7], JavaScript does not provide specification is based on the formal specification of SAFE any language-level module system, which can cause a name Intermediate Representation (IR), and the implementation conflict if distinct libraries using a common symbol are in- is done by adding module rewriter between SAFE Parser serted in the same context. Since both jQuery and Proto- and SAFE Interpreter in the interpretation . The type use the symbol $, if both libraries are inserted in a formal specification provides formal module semantics by HTML document by