Skip to content

Instantly share code, notes, and snippets.

@tlrobinson
Forked from 140bytes/LICENSE.txt
Created October 4, 2011 14:04
Show Gist options
  • Save tlrobinson/1261720 to your computer and use it in GitHub Desktop.
Save tlrobinson/1261720 to your computer and use it in GitHub Desktop.
Simple JavaScript REPL for the browser.

Simple JavaScript REPL for the browser.

By default it evals JS:

repl() // JavaScript eval mode

but you can pass it a callback instead:

repl(function(s) { return eval(s); });

The second argument is the default message first displayed:

repl(null, "eval some JS!");
function(c,t,h,r){
// history
h=[t||""];
// loop until blank input or cancel
// print last 10 reults
while(s=prompt(h.slice(-10).join("\n"))){
// eval the input, coerce to string, push onto history
// TODO: JSON.stringify() or other pretty printing?
try{
r=(c||eval)(s)
}catch(e){
r=e
}
h.push(""+r)
}
}
function(c,t,h,r){h=[t||""];while(s=prompt(h.slice(-10).join("\n"))){try{r=(c||eval)(s)}catch(e){r=e}h.push(""+r)}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Tom Robinson <http://tlrobinson.net/>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "simpleREPL",
"description": "Simple JavaScript REPL for the browser.",
"keywords": [
"repl",
"browser",
"javascript"
]
}
<!DOCTYPE html>
<title>Simple REPL</title>
<div>Enter some JavaScript. Cancel or empty input to exit.</div>
<script>
repl = function(c,h,r){h=[];while(s=prompt(h.slice(-10).join("\n"))){try{r=(c||eval)(s)}catch(e){r=e}h.push(""+r)}}
repl();
</script>
@tsaniel
Copy link

tsaniel commented Oct 5, 2011

-10 could simply be replaced by ~9, while could be replaced by for.
Also, it seems there is no need to coerce the r, as prompt does it for you.

function(c,t,h,r){for(h=[t||""];s=prompt(h.slice(~9).join("\n"));h.push(r))try{r=(c||eval)(s)}catch(e){r=e};}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment