Skip to content

Commit

Permalink
Use async syntax and IIFE #47
Browse files Browse the repository at this point in the history
  • Loading branch information
mverleg committed May 13, 2018
1 parent 35c4d6c commit 19c84db
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions static/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<!-- TODO: NOTICE: -->
<!-- TODO: This is a simple wasm demo of the compiler. -->
<!-- TODO: The 'real' version should be in github.com/mangolang/website. -->
Expand Down Expand Up @@ -37,11 +36,16 @@
<section id="content">
<h1>Mango (wasm)</h1>
<div id="editor">// Type your Mango code here!

</div>
<button id="compile_wat" onclick="alert('Not ready yet, please wait or check console');" title="Compile the code to the ascii text representation of WebAssembly">Compile to wat</button>
<button onclick="alert('Not implemented');" title="Compile the code to binary, executable WebAssembly">Compile to wasm</button>
<button onclick="alert('Not implemented');" title="Compile the code to binary WebAssembly and run it, showing only the output">Compile and run</button>

</div>
<button id="compile_wat" onclick="alert('Not ready yet, please wait or check console');"
title="Compile the code to the ascii text representation of WebAssembly">Compile to wat
</button>
<button onclick="alert('Not implemented');" title="Compile the code to binary, executable WebAssembly">Compile to wasm
</button>
<button onclick="alert('Not implemented');"
title="Compile the code to binary WebAssembly and run it, showing only the output">Compile and run
</button>
<div id="output"></div>
</section>
</main>
Expand All @@ -60,36 +64,37 @@ <h1>Mango (wasm)</h1>
</script>

<!--Wasm Mango compiler-->
<script src="mango.js" type="module"></script>
<script src="./mango.js" type="module"></script>
<script type="module">
/**
* Show a string in the output area, replacing any previous results.
*/
function show_output(txt) {
let output = document.getElementById("output");
output.innerHTML = "";
let pre = document.createElement("pre");
pre.innerHTML = txt;
output.appendChild(pre);
}

/**
* Load the Mango compiler wasm code.
*/
window.wasm_bindgen("./mango_bg.wasm").then(() => {
(async function () {
/**
* Show a string in the output area, replacing any previous results.
*/
function show_output(txt) {
let output = document.getElementById("output");
output.innerHTML = "";
let pre = document.createElement("pre");
pre.innerHTML = txt;
output.appendChild(pre);
}

/**
* Load the Mango compiler wasm code (async).
*/
let _ = await window.wasm_bindgen("./mango_bg.wasm");
let mango = window.wasm_bindgen;

/**
* Bind the "compile to wat" button.
*/
document.getElementById("compile_wat").onclick = function(event) {
document.getElementById("compile_wat").onclick = function (event) {
let mangocode = window.editor.getValue();
console.log("Compiling Mango:\n", mangocode);
let watcode = mango.compile_string_to_wat(mangocode);
console.log("Resulting WebAssembly text:\n", watcode);
show_output(watcode);
let watcode = mango.compile_string_to_wat(mangocode);
console.log("Resulting WebAssembly text:\n", watcode);
show_output(watcode);
};
});
})();
</script>
</body>
</html>

0 comments on commit 19c84db

Please sign in to comment.