Skip to content

Instantly share code, notes, and snippets.

local sem_t = {}
sem_t.__index = sem_t
function sem_t:wait()
if self.value == 0 then -- cannot decrement, wait for increment
local thread = coroutine.running()
table.insert(self.threads, thread)
coroutine.yield()
end

Lua Compatibility Matrix

No: This function or field will be missing in this version.
Yes: This function or field is supported unconditionally in this version.
Maybe: This function or field is behind a LUA_COMPAT_* define and may or may not be available.

Feature Lua 5.1 Lua 5.2 Lua 5.3 Lua 5.4
Base Library
assert(v, [msg]) Yes Yes Yes Yes
local pack, unpack, char, byte = string.pack, string.unpack, string.char, string.byte
local random = math.random
local leftover_fmt = {'>I1', '>I2', '>I3'}
local function mask_message(data, mask)
local res = {}
local ffi = require 'ffi'
local function permute_inner(arr, l, r)
if l == r then
coroutine.yield(ffi.string(arr))
else
for i = l, r do
arr[l], arr[i] = arr[i], arr[l]
permute_inner(arr, l + 1, r)
arr[l], arr[i] = arr[i], arr[l]
local uv = require 'uv'
local buffer = ""
local waiting = {}
process.stdin:on("data", function(data)
if not data then return end
buffer = buffer .. data
local ffi = require 'ffi'
local fs = require 'fs'
local Process = {}
Process.__index = Process
if ffi.abi('win') then
local psapi = ffi.load('psapi')
ffi.cdef [[
local ffi = require 'ffi'
local Mutex = {}
Mutex.__index = Mutex
if ffi.abi('win') then
-- trust me on these, we treat CRITICAL_SECTION like an opaque pointer.
local sizeof_critical_section = ffi.abi('32bit') and 24 or 40;
local libc = ffi.load('msvcrt')
@truemedian
truemedian / generate.sh
Last active November 21, 2021 01:47
Luvi and Lit versions over time
#!/bin/bash
base="$PWD"
file="$PWD/version.md"
git clone https://github.com/luvit/lit
git clone https://github.com/luvit/luvi
echo "" >$file
local fs = require 'fs'
local json = require 'json'
local Database = {}
Database.__index = Database
function Database.load(path)
local self = setmetatable({}, Database)
self.path = path
@truemedian
truemedian / README.md
Last active July 12, 2021 19:52
Systemd Service: Discordia

Systemd Service: Discordia

This is an example, expect no assistance with operation.

This file should go into /lib/systemd/system

Assumptions

  1. You will need to replace init.lua with the name of your bot's entrypoint file (maybe main.lua or bot.lua).