Skip to content

Instantly share code, notes, and snippets.

@ohAitch
Last active June 5, 2018 18:27
Show Gist options
  • Save ohAitch/8edb692a0d9852b0ce31ee887539f6fd to your computer and use it in GitHub Desktop.
Save ohAitch/8edb692a0d9852b0ce31ee887539f6fd to your computer and use it in GitHub Desktop.
Wast bootstrap under data:text/html,<textarea%20onkeyup="out.innerText=eval(this.value)"></textarea><pre%20id=out>
/*
- panes: input | compiler | wast | runtime | output
TODO
- snapshot button that saves json of [date input compiler runtime] to localStorage
- timestamp buttons to cycle through old versions - also sth with diffs?
- <details> on "foo =\n", w/ collapse back if backspace on beginning
*/
document.body.querySelector('script') || document.body.appendChild(
document.createRange().createContextualFragment(
'<script src=https://cdn.rawgit.com/WebAssembly/wabt/aae5a4b7/demo/libwabt.js>'
) )
document.body.querySelector('script').onload = ()=>
{out.innerText = eval(document.querySelector('textarea').value)}
mnt = document.body.querySelector('div')
mnt && mnt.remove()
document.body.appendChild(mnt = document.createElement('div'))
//out.innerText=''; (new Date)+"\n"+document.body.outerHTML
mnt.innerHTML = `
<pre id=e_inp contenteditable style=background:#efe></pre>
<pre id=e_comp contenteditable style=display:inline-block;background:#eff></pre>
<pre id=e_wast style=display:inline-block;vertical-align:top;background:#eee></pre>
<pre id=e_werr style=display:inline-block;vertical-align:top;background:#fee></pre>
<pre id=e_rt contenteditable style=background:#eff></pre>
<pre id=e_out></pre>
`
e_inp.innerText = "42"
e_comp.innerText = `(a)=>\`(module
(func $main (result i32)
i32.const \${a}
)
(export "main" (func $main))
)\``
run = () => {}
compile = ()=>{
var astModule
e_wast.innerText = ""
e_werr.innerText = ""
try {
this.wasmModule = null
e_wast.innerText = eval(e_comp.innerText)(e_inp.innerText)
astModule = wabt.parseWat('compiled.wat',e_wast.innerText)
astModule.resolveNames();
astModule.validate();
var binaryOutput = astModule.toBinary({write_debug_names:true})
this.wasmModule = new WebAssembly.Module(binaryOutput.buffer)
run()
} catch(err) {
e_werr.innerText = err
} finally {
astModule && astModule.destroy()
}
}
compile()
e_inp.onkeyup = compile
e_comp.onkeyup = compile
e_rt.innerText = `(module)=> {
const instance = new WebAssembly.Instance(module, {})
return instance.exports.main()
}`
run = ()=> {
try {
e_out.innerText = eval(e_rt.innerText)(this.wasmModule)
e_out.style.background = "#eee"
} catch(_res) {
e_out.innerText = _res
e_out.style.background = "#fee"
}
}
e_rt.onkeyup = run
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment