Skip to content

Instantly share code, notes, and snippets.

View richardhundt's full-sized avatar

Richard Hundt richardhundt

View GitHub Profile
@richardhundt
richardhundt / httpd-k.lua
Created October 9, 2012 07:37
http server with keep-alive
ocal luv = require('luv')
local response_html = "<p>Hello, world!</p>"
local response_size = #response_html
local http_response = "HTTP/1.0 200 OK\r\
Content-Type: text/html\r\
Content-Length: "..tostring(response_size).."\r\
Connection: Keep-Alive\r\
\r\
"..response_html
@richardhundt
richardhundt / codegen.lua
Created December 1, 2012 07:29
LuaJIT 2 bytecode generator
--[=[
dump = header proto+ 0U
header = ESC 'L' 'J' versionB flagsU [namelenU nameB*]
proto = lengthU pdata
pdata = phead bcinsW* kgc* knum* uvdataH* [debugB*]
phead = flagsB numparamsB framesizeB numuvB numkgcU numknU numbcU
[debuglenU [firstlineU numlineU]]
kgc = kgctypeU { ktab | (loU hiU) | (rloU rhiU iloU ihiU) | strB* }
knum = intU0 | (loU1 hiU)
ktab = narrayU nhashU karray* khash*
@richardhundt
richardhundt / ray.c
Created February 24, 2013 11:01
libray
#include "ray.h"
int ray_last_error(ray_ctx_t* self) {
uv_err_t err = uv_last_error(self->loop);
return err.code;
}
const char* ray_strerror(int code) {
uv_err_t err = { .code = code };
return uv_strerror(err);
}
#include "ray.h"
static ray_fiber_t* RAY_MAIN;
static uv_async_t RAY_PUMP;
void ray_pump(void) {
uv_async_send(&RAY_PUMP);
}
ray_fiber_t* ray_current(lua_State* L) {
@richardhundt
richardhundt / quicksort.nga
Created September 23, 2013 06:27
quicksort.nga
insertion_thresold = 16
function less_than(a, b)
return a < b
end
function insertion_sort(array, compare, istart, iend)
for i = istart + 1, iend do
current_value = array[i]
hole_index = i
@richardhundt
richardhundt / fields.shn
Last active August 29, 2015 13:57
Shine final table fields
function fields(schema is Table)
module fields
local slots = { }
-- create slot objects to use as keys so that we can trip __set__
-- every time for a type check (just one way to do it, there are more)
for k, v in schema do
slot = { }
function slot.set(o, v)
if v is schema[k] then
@richardhundt
richardhundt / test.shn
Last active August 29, 2015 13:57
Guard composition
function conjoin(a, b)
meta = { }
repr = nil
function meta::__tostring()
if not repr then
repr = "%{a} * %{b}"
end
return repr
end
@richardhundt
richardhundt / swarm-http-router.shn
Last active August 29, 2015 13:58
Swarm framework HTTPRouter using patterns
class HTTPRouter
self()
self.routes = { }
end
add(verb is String, path is String, func is Function)
self.routes[#self.routes + 1] = Route(verb, path, func)
end
match(verb, path)
for i=1, #self.routes do
m, q = self.routes[i].match(verb, path)
<html>
<style>
.loader {
font-size: 10px;
margin: 50px auto;
text-indent: -9999em;
width: 11em;
height: 11em;
@richardhundt
richardhundt / keybase.md
Created October 17, 2017 07:40
keybase.md

Keybase proof

I hereby claim:

  • I am richardhundt on github.
  • I am richardhundt (https://keybase.io/richardhundt) on keybase.
  • I have a public key ASD51mYRq2hqnjM8AuqqIoocOAJYgQ44VLN6Qq5CLAhCPwo

To claim this, I am signing this object: