Skip to content

Latest commit

 

History

History
77 lines (55 loc) · 1.51 KB

README.md

File metadata and controls

77 lines (55 loc) · 1.51 KB

Lego

Lego is a serverless service with WebAssembly and wasmer.

Quick Guide

Prepare wasm

  1. Write your wasm in any language you like, in AssemblyScript for example:
export function helloWorld() {
    return "Hello, Lego";
}
  1. Tell Lego where to get/set bytes into memory by defining and exporting memory_ptr function:
let memory = new ArrayBuffer(1024);

export function memory_ptr(): usize {
    return changetype<usize>(memory);
}

// Help for set string into memory
function saveStringIntoMemory(data: String): usize {
    const len = raw.length;
    let view = new DataView(memory);
    for (let i=0;i<(len as i32);i++) {
        view.setUint8(i, raw.charCodeAt(i) as i8);
    }
    return len;
}
  1. Export http_get hook to Lego:
export function http_get(inputLen: usize): usize {
    return saveStringIntoMemory(helloWorld());
}
  1. Compiles into wasm and store in any path you like:
$ npm run build
$ cp ./build/helloworld.wasm /path/to/wasm/helloworld.wasm

Run Lego

  1. Setup environment varialble LEGO_WASM_ROOT as the root locatoin of your wasms.
export LEGO_WASM_ROOT="/path/to/wasm"
  1. Here we go ~
$ /path/to/lego

Request Lego

$ curl http://localhost:8000/helloworld | jq    # `helloworld` is the name of your wasm file without extension
{
  "result": "hello world"
}

You can find more AssemblyScript examples written in assemblyscripts.