Skip to content

Commit

Permalink
reload the web page after abort()
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed Sep 19, 2013
1 parent fef1a27 commit bc65316
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 55 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ PERL_GIT:=https://github.com/mirrors/perl.git
PERL_VERSION:=v5.18.1

all: microperl.js gen.modules.js
echo '(function bootPerl() {' > perl.js
cat prelude.js >> perl.js
cat microperl.js >> perl.js
echo '}());' >> perl.js
echo '#!/usr/bin/env node' > perl-cli.js
cat perl.js >> perl-cli.js
chmod +x perl-cli.js
Expand Down
68 changes: 68 additions & 0 deletions prelude.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,71 @@ var Module = {
//preRun: [], // called before every run()
};

if (typeof jQuery !== "undefined") { // for the web
jQuery(function($) {
function output(s) {
var output = $('#output');
output.val(output.val() + s + "\n");
}
function onOutput(c) {
var o = $('#output');
o.val(o.val() + String.fromCharCode(s));
}
function onInput() {
return null;
}
Module.stdin = onInput;
Module.stdout = onOutput;
Module.stderr = onOutput;
Module.print = output;
Module.printErr = output;
Module.exit = function exit(status) {
EXITSTATUS = status;
STACKTOP = initialStackTop;
throw new ExitStatus(status);
};

$('#output').val(sessionStorage.getItem('output'));

$('#eval').click(function() {
sessionStorage.removeItem('output');
$('#output').val('');
preloadStartTime = null;
ABORT = false;
try {
try { FS.unlink('/input') } catch (e) { }
FS.createDataFile('/', 'input', $('#input').val(), true, true);
Module.callMain(['/input']);
} catch (err) {
output(err.message || err.toString());
}
finally {
var src = encodeURIComponent($('#input').val());
location.hash = src;
}
});

var src = location.hash;
if (src) {
$('#input').val(decodeURIComponent(src.slice(1)));
$('#eval').trigger('click');
}
else {
$('#input').val(
"use strict;\n" +
"use warnings;\n" +
"\n" +
"print \"Hello, perl.js!\\n\"\n");
Module.callMain(['--version'])
}


// monky patch
Module.abort = abort = function abort(text) {
alert('abort!');
sessionStorage.setItem('output', $('#output').val());
window.location.reload();
Module.exit(1);
};
});
}
55 changes: 0 additions & 55 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,61 +25,6 @@
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="perl.js"></script>
<script>
jQuery(function($) {
function output(s) {
var output = $('#output');
output.val(output.val() + s + "\n");
}
function onOutput(c) {
var o = $('#output');
o.val(o.val() + String.fromCharCode(s));
}
function onInput() {
return null;
}
Module.stdin = onInput;
Module.stdout = onOutput;
Module.stderr = onOutput;
Module.print = output;
Module.printErr = output;
Module.exit = function exit(status) {
EXITSTATUS = status;
STACKTOP = initialStackTop;
throw new ExitStatus(status);
};

var src = location.hash;
if (src) {
$('#input').val(decodeURIComponent(src.slice(1)));
}
else {
$('#input').val(
"use strict;\n" +
"use warnings;\n" +
"\n" +
"print \"Hello, perl.js!\\n\"\n");
Module.callMain(['--version'])
}

$('#eval').click(function() {
$('#output').val('');
preloadStartTime = null;
ABORT = false;
try {
try { FS.unlink('/input') } catch (e) { }
FS.createDataFile('/', 'input', $('#input').val(), true, true);
Module.callMain(['/input'])
} catch (err) {
output(err.message || err.toString());
}
finally {
var src = encodeURIComponent($('#input').val());
location.hash = src;
}
});
});
</script>
</head>
<body>

Expand Down

0 comments on commit bc65316

Please sign in to comment.