Skip to content

Instantly share code, notes, and snippets.

@myobie
Created April 4, 2020 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myobie/9d15c0f5edca7d127f03081955dfd57f to your computer and use it in GitHub Desktop.
Save myobie/9d15c0f5edca7d127f03081955dfd57f to your computer and use it in GitHub Desktop.
Outline for my blog post about getting started with wasm
  • Why?
    • Use existing code and libraries, especially for encryption or hashing
    • Sharing code for algorithms or compute intensive operations
    • Don’t intend expose much (anything?) to the wasm instance
    • There some gotchas like 64bit integers
  • Browser only, no toolchain
    • I have toolchain fatigue and it turns out the latest browsers are kinda great
    • import and export, async and await, wasm, etc all work
    • I like to use a tiny file server to serve a directory on a port on localhost
      • serve
  • clang is all you need
    • Don’t use anything else. Seriously don’t.
    • Basic C example
    • wasm2wat
    • Exports and imports
    • Optmizations
    • Module and Instance
    • Memory
    • Always async. Two way to expose the API:
      • load then get exports
      • export only async functions which await on a Promise or the instance as their first item of business
  • WASI for stdlib
    • We need malloc and free if we are going to do anything interesting
    • Setup a sysroot
    • Example using malloc to send a string from js into C, manipulate it, and read it back out in js
    • Imports show up as needed
    • The start function
  • Heap
    • I’ve made a utility class to wrap all my memory related stuffs so I can free all memory left at the end or in the event of an error
    • This isn’t a library yet because I’m not done figuring out exactly what I need and like
  • Something useful: argon2
    • There was a hashing competitor and argon2 won
    • This can be useful to derive a key from a password to use for encryption or to implement something like SRPX
    • Which files do we need?
    • How do we disable threads?
    • clang
    • Imports required by WASI
  • Other ideas: libsodium
    • Can work the same way with just clang
    • Give it a try or try using a c library or project that you can about in the browser
  • i64 and i32
    • Link to data types in wasm
    • Cannot call a function which expects a 64bit int
    • Numbers are not safe in JS
    • BigInt isn’t in every browser and cannot be polyfilled transparently
      • JSBI (I don’t like this, but it’s what we got)
    • Converting to and from little endien 8 bytes
    • Can write a proxy function which takes a void pointer and just cast and realize and forward
    • This is very useful if you are using an existing library which uses a lot of unsigned long longs cough libsodium cough
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment