Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

Add Wasm implementation #492

Merged
merged 1 commit into from
Oct 3, 2020
Merged

Add Wasm implementation #492

merged 1 commit into from
Oct 3, 2020

Conversation

nlordell
Copy link
Contributor

@nlordell nlordell commented Oct 3, 2020

This PR adds a hand-written Wasm FizzBuzz implementation. It uses WASI fd_write for writing to standard out, so it should work for any WASI-compliant runtimes (such as Wasmtime or Wasmer). I tried hard to add descriptive comments explaining what is going on.

Alternatively, you can run this with JavaScript. Here is a sample host implementation that runs with Node.js. Note that using with the JavaScript WebAssembly interface requires byte-encoded Wasm, so something like wat2wasm fizzbuzz.wat needs to be done first in order to generate a Wasm binary module.

host.js
const assert = require("assert");
const fs = require("fs").promises;
const path = require("path");

const le = true;
const decoder = new TextDecoder("utf-8");

async function main() {
  const filename = path.join(__dirname, "fizzbuzz.wasm");
  const wasm = await fs.readFile(filename);
  const { instance } = await WebAssembly.instantiate(wasm, {
    wasi_snapshot_preview1: {
      fd_write: (fd, iovs, iovs_len, nwritten) => {
        assert(fd === 1, "only stdout supported");

        const buffer = instance.exports.memory.buffer;
        const view = new DataView(buffer);

        let out = "";
        let n = 0;
        for (let i = 0; i < iovs_len; i++) {
          const iov = iovs + 8*i;
          const base = view.getUint32(iov, le);
          const len = view.getUint32(iov + 4, le);

          out += decoder.decode(new Uint8Array(buffer, base, len));
          n += len;
        }

        console.log(out.trim());
        view.setUint32(nwritten, n, le);

        return 0;
      },
    },
  });

  instance.exports._start();
}

main().catch((err) => console.error(err));

@NullDev
Copy link
Owner

NullDev commented Oct 3, 2020

Awesome! Thank you :D
I'm reevaluating the Top 5 solutions in the readme.
May I feature your solution?

@NullDev NullDev merged commit f9487dd into NullDev:master Oct 3, 2020
@nlordell
Copy link
Contributor Author

nlordell commented Oct 4, 2020

May I feature your solution?

That would be awesome! 😄

Also, on a side note, I noticed how many PR submissions you get and how you take the time to answer them all. I tip my hat to you, sir.

@NullDev
Copy link
Owner

NullDev commented Oct 4, 2020

That would be awesome! smile

Perfect! Added it :)

Also, on a side note, I noticed how many PR submissions you get and how you take the time to answer them all. I tip my hat to you, sir.

Well, I didn't expect so many PR's to be honest :')
Thank you :'D

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants