Toward automatic update from callbacks to promises

Javascript is the prevalent scripting language for the web. It lets web pages register callbacks to react to user events. A callback is a function to be invoked later with a result currently unavailable. This pattern also proved to respond efficiently to remote requests. Javascript is currently used to implement complete web applications. However, callbacks are ill-suited to arrange a large asynchronous execution flow. Promises are a more adapted alternative. They provide a unified control over both the synchronous and asynchronous execution flows. The next version of Javascript proposes to replace callbacks with Promises. This paper brings the first step toward a compiler to help developers prepare this shift. We present an equivalence between callbacks and Dues. The latter are a simpler specification of Promises developed for the purpose of this demonstration. From this equivalence, we implement a compiler to transform an imbrication of callbacks into a chain of Dues. This equivalence is limited to Node.js-style asynchronous callbacks declared in situ. We evaluate our compiler over 64 npm packages, 9 of them present compatible callbacks and compile successfully. We consider this shift to be a first step toward the merge of concepts from the data-flow programming model into the imperative programming model.

[1]  Simon Holm Jensen,et al.  Remedying the eval that men do , 2012, ISSTA 2012.

[2]  Philippa Gardner,et al.  Towards a program logic for JavaScript , 2012, POPL '12.

[3]  J. Morrison,et al.  Flow-based programming , 2012 .

[4]  Gareth Smith,et al.  Local reasoning about Web programs , 2011 .

[5]  Liuba Shrira,et al.  Promises: linguistic support for efficient asynchronous procedure calls in distributed systems , 1988, PLDI '88.

[6]  Arthur Charguéraud,et al.  A trusted mechanised JavaScript specification , 2014, POPL.

[7]  Barbara G. Ryder,et al.  State-Sensitive Points-to Analysis for the Dynamic Behavior of JavaScript Objects , 2014, ECOOP.

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

[9]  Paul Hudak,et al.  Wormholes: introducing effects to FRP , 2012, Haskell.

[10]  Ankur Taly,et al.  An Operational Semantics for JavaScript , 2008, APLAS.

[11]  Kwang-Moo Choe,et al.  Points-to analysis for JavaScript , 2009, SAC '09.

[12]  Lars Ole Andersen,et al.  Program Analysis and Specialization for the C Programming Language , 2005 .

[13]  P. Gardner,et al.  JuS: Squeezing the Sense out of JavaScript Programs , 2013 .

[14]  Gilles Kahn,et al.  The Semantics of a Simple Language for Parallel Programming , 1974, IFIP Congress.

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

[16]  Ankur Taly,et al.  Language-Based Isolation of Untrusted JavaScript , 2009, 2009 22nd IEEE Computer Security Foundations Symposium.

[17]  Ankur Taly,et al.  Isolating JavaScript with Filters, Rewriting, and Wrappers , 2009, ESORICS.

[18]  Marc Pouzet,et al.  Programming parallelism with futures in lustre , 2012, EMSOFT '12.