Skip to content

Instantly share code, notes, and snippets.

View creationix's full-sized avatar
💚
<3

Tim Caswell creationix

💚
<3
View GitHub Profile
do
-- Polyfill for luajit without __pairs and __ipairs extensions
local triggered = false
pairs(setmetatable({}, { __pairs = function()
triggered = true
return function() end
end }))
if not triggered then
print "Polyfilling pairs"
function _G.pairs(t)
@creationix
creationix / jsonparse.js
Created February 13, 2012 23:20
event-only version of jsonparse
// Named constants with unique integer values
var C = {};
// Tokenizer States
var START = C.START = 0x11;
var TRUE1 = C.TRUE1 = 0x21;
var TRUE2 = C.TRUE2 = 0x22;
var TRUE3 = C.TRUE3 = 0x23;
var FALSE1 = C.FALSE1 = 0x31;
var FALSE2 = C.FALSE2 = 0x32;
var FALSE3 = C.FALSE3 = 0x33;
@creationix
creationix / mazemaker.js
Created November 5, 2009 22:32
Maze generator for node.js
Array.prototype.shuffle = function () {
var i, j, tempi, tempj;
i = this.length;
if ( i === 0 ) {
return false;
}
while ( --i ) {
j = Math.floor( Math.random() * ( i + 1 ) );
tempi = this[i];
@creationix
creationix / getMyCode.js
Created September 15, 2011 18:00
Script to download all repos for a user
// Run this with node and then run the output with sh
var Http = require('http')
var ChildProcess = require('child_process');
Http.cat("http://github.com/api/v2/json/repos/show/creationix", function (err, json) {
if (err) throw err;
var data = JSON.parse(json);
data.repositories.forEach(function (repo) {
console.log("echo " + JSON.stringify(repo.name + " - " + repo.description));
console.log("git clone --bare " + repo.url);
});
@creationix
creationix / ffi-sdl.lua
Created September 13, 2011 06:52
Using SDL from luajit's built-in ffi module
-- load the luajit ffi module
local ffi = require "ffi"
-- Parse the C API header
-- It's generated with:
--
-- echo '#include <SDL.h>' > stub.c
-- gcc -I /usr/include/SDL -E stub.c | grep -v '^#' > ffi_SDL.h
--
ffi.cdef(io.open('ffi_SDL.h', 'r'):read('*a'))
-- Load the shared object
/* types
Button - represents a GPIO pin hooked to a button
Neopixel - represents a GPIO pin hooked to neopixels
Strip - represents a memory buffer for 1d array of colors
Grid - represents a memory buffer for a 2d array of colors
Position1d - represents a bounded 2D coordinate
Position2d - represents a bounded 2D coordinate
Interval - represents a clock 33345rtfv ith a given frequency
Schedule - represents a schedule to alarm at given times
Color - represents a 24 bit RGB color

Generators vs Fibers

Both ES6 generators and node-fibers can be used to block a coroutine while waiting on some I/O without blocking the entire process. Both can do this for arbitrarily deep call stacks. The main difference between the capabilities of the two is how explicit the syntax is.

Generators - Safe, but Explicit

In code that uses ES6 generators:

var run = require('gen-run'); // https://github.com/creationix/gen-run
@creationix
creationix / Dockerfile
Last active November 14, 2022 22:16
Build luvit from source using alpine docker
FROM alpine AS build-luv
RUN apk add build-base cmake git
RUN git clone --recursive https://github.com/luvit/luvi.git
RUN make -C luvi/deps/luv
FROM alpine AS build-luvi
RUN apk add build-base cmake git
RUN git clone --recursive https://github.com/luvit/luvi.git
RUN apk add perl linux-headers
RUN make -C luvi regular-asm
--- xxHash
--- https://github.com/Cyan4973/xxHash/blob/dev/doc/xxhash_spec.md#xxh32-algorithm-description
local bit = require 'bit'
local rshift = bit.rshift
local rol = bit.rol
local bxor = bit.bxor
local tobit = bit.tobit
local ffi = require 'ffi'
local bit = require 'bit'
local bxor = bit.bxor
local rol = bit.rol
local lshift = bit.lshift
local band = bit.band
local bor = bit.bor
local function gimli(state)
for round = 24, 1, -1 do
for column = 0, 3, 1 do