Skip to content

Commit

Permalink
use web workers to compile
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Oct 7, 2011
1 parent 75fcfba commit b2bbc55
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 65 deletions.
61 changes: 61 additions & 0 deletions client.js
@@ -0,0 +1,61 @@
window.onload = function() {
if (!window.Worker) {
alert("This page requires javascript workers");
return;
}
var worker = new Worker("worker.js");

worker.onmessage = function(event) {
var data = event.data;
var handlers = this.handlers[data.type];
if (handlers && handlers.length > 0) {
handlers.shift()(data);
} else {
console.log("unhandled message:");
console.log(data.type + ": " + data.msg);
}
}

worker.handlers = {}
worker.push_handler = function(type, func) {
if (!this.handlers[type]) {
this.handlers[type] = [];
}
this.handlers[type].push(func);
}

var compile_moon = function(code, result) {
worker.postMessage({type:"compile", code:code});
worker.push_handler("compile", result);
}

var execute_moon = function(code, result) {
worker.postMessage({type:"execute", code:code});
worker.push_handler("execute", result);
}

var escape_html = function(text) {
return text.replace(/</g, "&lt;").replace(/>/g, "&gt;");
}

$ = function() {
return document.getElementById.apply(document, arguments);
}

$("compile").onclick = function() {
var start = new Date;
compile_moon($("input").value, function(data) {
$("out").innerHTML = escape_html(data.code);
$("time").innerHTML = new Date - start;
});
};

$("run").onclick = function() {
var start = new Date;
execute_moon($("input").value, function(data) {
$("out").innerHTML = escape_html(data.code);
$("time").innerHTML = new Date - start;
});
};

};
67 changes: 6 additions & 61 deletions index.html
Expand Up @@ -3,68 +3,13 @@
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="worker.js"></script>
<script type="text/javascript">

window.onload = function() {
console.log(printer.get_str());
$ = function() {
return document.getElementById.apply(document, arguments);
}

$("compile").onclick = function() {
var start = new Date;
var out = compile_moon($("input").value);
$("out").innerHTML = out;
$("time").innerHTML = new Date - start;
}

$("run").onclick = function() {
var start = new Date;
var out = execute_moon($("input").value);
$("out").innerHTML = out;
$("time").innerHTML = new Date - start;
}

}

function run_lua(code) {
str = intArrayFromString(code);
pt = allocate(str, "i8");
_run_lua(pt);
}

function parse_moon(moon_code) {
printer.clear();
run_lua("tree = moonscript.parse.string([==[" + moon_code + "]==])");
run_lua("print(moonscript.util.dump(tree))");
return printer.get_str();
}

function compile_moon(moon_code) {
printer.clear();
run_lua("tree = moonscript.parse.string([==[" + moon_code + "]==])");
run_lua("code = moonscript.compile.tree(tree)");
run_lua("print(code)")
return printer.get_str()
}

function execute_moon(moon_code) {
var lua = compile_moon(moon_code);
printer.clear();
run_lua(lua);
return printer.get_str();
}

</script>
<script type="text/javascript" src="client.js"></script>
</head>
<body>

<textarea id="input" name="" rows="10" cols="30"></textarea>
<button id="compile">Compile</button>
<button id="run">Run</button>
<div id="time"></div>
<pre id="out"></pre>

<textarea id="input" name="" rows="10" cols="30"></textarea>
<button id="compile">Compile</button>
<button id="run">Run</button>
<div id="time"></div>
<pre id="out"></pre>
</body>
</html>
56 changes: 52 additions & 4 deletions worker_head.js
Expand Up @@ -27,10 +27,58 @@ function run_lua(code) {
_run_lua(pt);
}

var start = +new Date;
function parse_moon(moon_code) {
printer.clear();
run_lua([
"local tree = moonscript.parse.string([==[" + moon_code + "]==])",
"print(moonscript.util.dump(tree))"
].join("\n"))

return printer.get_str();
}

setTimeout(function() {
postMessage("we are done loading after: " + (new Date - start));
function compile_moon(moon_code) {
printer.clear();
}, 0);
run_lua([
"local tree = moonscript.parse.string([==[" + moon_code + "]==])",
"local code = moonscript.compile.tree(tree)",
"print(code)"
].join("\n"));
return printer.get_str()
}

function execute_moon(moon_code) {
var lua = compile_moon(moon_code);
printer.clear();
run_lua(lua);
return printer.get_str();
}

var start = new Date;

if (typeof document == "undefined") { // running inside of worker
setTimeout(function() {
var msg = "we are done loading after: " + (new Date - start);
printer.clear();
postMessage({type:"ready", msg: msg});

onmessage = function(event) {
var type = event.data.type;
switch (type) {
case "parse":
postMessage({type:"parse", code: parse_moon(event.data.code)});
break;
case "compile":
postMessage({type:"compile", code: compile_moon(event.data.code)});
break;
case "execute":
postMessage({type:"execute", code: execute_moon(event.data.code)});
break;
default:
postMessage({type: "error", msg: "Unknown message: " + type})
}
}

}, 0);
}

0 comments on commit b2bbc55

Please sign in to comment.