Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Here is a minimal example for inline webassembly: A function a that adds two numbers. Can someone make the entire example shorter? (added linebreaks for readability)

    <!DOCTYPE html>
    <p id=r>
    <script>
    WebAssembly.instantiateStreaming(fetch(
    'data:application/wasm;base64,AGFzbQEAAAABBwFgAn9/AX8DAgEABwUBAWEAAAoJAQcAIAAgAWoL'))
    .then(x=>r.append(x.instance.exports.a(51,4)))
    </script>

And here is the wat code that we can turn into wasm with wat2wasm and then into base64 for a data URL:

    (module
      (func (export "a") (param i32 i32) (result i32)
        local.get 0
        local.get 1
        i32.add))


Found a shorter version, again with linebreaks for readability:

    <p id=r>
    <script>WebAssembly.instantiate(Uint8Array.fromBase64(
    'AGFzbQEAAAABBwFgAn9/AX8DAgEABwUBAWEAAAoJAQcAIAAgAWoL'))
    .then(x=>r.append(x.instance.exports.a(51,4)))
    </script>




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: