-
Notifications
You must be signed in to change notification settings - Fork 12
Quick Start
- Install the stopify executable using NPM (or Yarn):
npm install -g stopify
- Create a JavaScript program that needs to be stopified. For example, the following program has an infinite loop that make browsers lock up. Save the following program as
input.js
:
const elt = document.createElement("div");
document.body.appendChild(elt);
var i = 0;
var j = 0;
while(true) {
if (i++ == 10000000) {
j++;
elt.innerText = "Still runninng ... " + (new Date());
i = 0;
}
}
- Apply Stopify to the program:
stopify input.js output.js
- To run the program in a browser, we need to (1) create an HTML page that first loads the Stopify runtime system and then loads output.js and (2) specify how frequently the program should yield control to the browser. For this Quick Start, we can use Stopify's testing framework. The following command prints a (long!) URL:
stopify-url output.js
- Visit the URL that it generates in your Web browser. You'll see that the program "just works" and periodically prints the current time. In contrast, if you load the original program in a browser, it will not print anything and will eventually crash the browser tab.
Stopify has several command-line flags that affect program performance. Some flags produce better results on particular browsers. Other flags let you specify the sub-language of JavaScript that the input program uses. JavaScript has several peculiar features that make stopification difficult. Fortunately, when the input to Stopify is the output of some other compiler C, you can typically identify JavaScript features that C does not use. Stopify can exploit this information to further improve performance. Intuitively, the image of a simple language (e.g., OCaml, Scheme, etc.) in JavaScript is a simple sub-language of JavaScript.
Finally, instead of stopifying all the JavaScript that C produces (which may include an enormous runtime system for the source language), you can use Stopify as a library or as a Babel plugin and selectively apply it to smaller units of code.