Skip to content

Instantly share code, notes, and snippets.

View game.js
// Avaliable globals:
// ctx (rendering context)
// width, height (the width and height of the window)
const TAU = Math.PI * 2
const circle = (cx, cy, r) => {
ctx.arc(cx, cy, r, 0, TAU)
const mouse = {x:0, y:0}
josephg / test.c
Created Mar 25, 2019
async crash
View test.c
#include <strings.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <stdbool.h>
josephg /
Created Feb 4, 2019
Foundationdb C API example

This is a simple example program using the foundationdb C API. Its written & tested on macos; but it should be easy to adapt to work on BSD / linux.

This also demonstrates using libuv to integrate foundationdb into a run loop. Promises are run in the main libuv loop.

Compile with

$ clang test.c -Wall -lfdb_c -I/usr/local/include -luv
josephg /
Created Jan 14, 2019
Bad comment script in JS

This is a port of this blog post to javascript. The one annoying issue with it is that you need to install node-fetch somehow (or put this script somewhere that has access to node-fetch). I could adapt this to use no deps, but it'd be longer and uglier.

josephg / transformJSPosition.js
Last active Jan 8, 2019
transform raw string positions with text ops
View transformJSPosition.js
const {strPosToUni, uniToStrPos} = require('unicount')
// The cursor position (input and output) are specified in JS string
// offsets here.
function transformJSPosition(cursor /*: number*/, doc /*: string*/, op /*: TextOp*/) {
let prePos = 0, postPos = 0 // string index in doc.
for (let i = 0; i < op.length && cursor > postPos; i++) {
const c = op[i]
View rust-ssb.patch
diff --git a/src/ b/src/
index 19c571c..3cf9380 100644
--- a/src/
+++ b/src/
@@ -3,10 +3,7 @@
extern crate serde_json;
-use sodiumoxide::crypto::{box_, sign, auth, scalarmult, secretbox};
-use sodiumoxide::crypto::hash::sha256;
josephg /
Last active Oct 9, 2018
Rust allocations
// This is an example of making a c-style struct ending in a dynamically sized array in stable rust.
use std::alloc::{GlobalAlloc, Layout, System};
struct Blah {
height: usize,
arr: [u8; 0],
impl Blah {
fn get_layout_at(height: usize) -> Layout {
josephg /
Last active Sep 17, 2020
Getting Zig compiling to WASM

In case anyone else wants to play with Zig on webassembly, here's what you need to do to make it work on a mac today.

1. Get LLVM 7 compiled with webassembly support.

You'll need LLVM to output to the WASM target. This has just been added by default in trunk, so if LLVM >7 is available, you might be able to just brew install llvm.

If you have wasm support already you should see:

$ llc --version
josephg / foundationdb.conf
Created Jun 7, 2018
Foundationdb freebsd installation first pass
View foundationdb.conf
## foundationdb.conf
## Configuration file for FoundationDB server processes
## Full documentation is available at
restart_delay = 60
## by default, restart_backoff = restart_delay_reset_interval = restart_delay
# initial_restart_delay = 0
josephg / bt.js
Last active Apr 17, 2018
Behaviour trees with generators example 2
View bt.js
// 2 entities, George and Phil will meet on this fateful night
const e1 = {
name: 'George',
health: 400,
target: null,
const e2 = {
name: 'Phil',
health: 400,
You can’t perform that action at this time.