Skip to content

Instantly share code, notes, and snippets.

@nickav
nickav / hello.c
Last active August 25, 2023 01:48
WASM from Scratch
View hello.c
// Types
#define NULL 0
typedef unsigned char u8;
typedef unsigned int u32;
typedef signed int i32;
typedef signed long long i64;
typedef double f64;
// Imports
@nickav
nickav / defer.cpp
Created July 14, 2023 15:18
defer macro thing
View defer.cpp
#ifndef defer
// Defer macro/thing.
template<typename T>
struct ExitScope {
T lambda;
ExitScope(T lambda) : lambda(lambda) {}
~ExitScope() { lambda(); }
ExitScope(const ExitScope&);
private:
ExitScope& operator =(const ExitScope&);
@nickav
nickav / LightningLinks.js
Last active May 24, 2023 23:25
Definitely not a cheap knock-off of Turbolinks
View LightningLinks.js
@nickav
nickav / better-fetch.js
Created February 17, 2023 23:11
A nice wrapper around the browser's kind of broken fetch API
View better-fetch.js
const handleFetchResponse = (resp) => {
return resp.text().then((text) => {
let data = text;
const contentType = resp.headers.get("content-type");
const isJSON = contentType && contentType.indexOf("application/json") >= 0;
if (isJSON)
{
try {
data = JSON.parse(text);
View index.html
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<meta name="viewport" content="minimal-ui, width=device-width, height=device-height, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<title></title>
<meta name="description" content="">
@nickav
nickav / ReadDirectoryChangesW_craziness.cpp
Last active September 18, 2023 21:50
Example of how to poll ReadDirectoryChangesW on Windows
View ReadDirectoryChangesW_craziness.cpp
int main() {
char *path = "/path/to/my/directory";
print("watching %s for changes...\n", path);
HANDLE file = CreateFile(path,
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
View fidget_spinner.html
<a><b><c><d><style>*{background:#09042a}body *{position:fixed;padding:30;border-radius:50%;border:10px solid #09042a;margin:14 19}a{margin:104 90;box-shadow:4em 0 #e78481}c{margin:-146 -38}b,c{background:#f5bb9c}a,d{border-color:#e78481
View sqlParams.ts
// Converts SQL query from named parameters :param to [param]
export const sqlParams = (
sql: string,
params: Record<string, any>
): [string, any[]] => {
const matches = sql.split(/([^:]:[A-z]+[A-z0-9])/g);
let i = 0;
const nextParams: any[] = [];
const nextSql = matches.map((str, index) => {
View hello-opengl-glfw.cpp
#include "deps/glfw/include/GLFW/glfw3.h"
#include <stdio.h>
void shader_print_errors(GLuint id) {
GLint success;
glGetShaderiv(id, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar infoLog[1024];
glGetShaderInfoLog(id, 1024, NULL, infoLog);
@nickav
nickav / lisp.js
Last active November 15, 2019 19:25
View lisp.js
const isWhitespace = (char) =>
char === ' ' || char === '\n' || char === '\t' || char === '\r';
const isParen = (char) => char === '(' || char === ')';
const getLiteralType = (literal) => {
if (literal[0] === '"' && literal[literal.length - 1] === '"') {
return 'string';
}