Skip to content

Instantly share code, notes, and snippets.

Avatar
©️
 ​[object Object] :trollface:

Aaron Patterson tenderlove

©️
 ​[object Object] :trollface:
View GitHub Profile
View syntax-check.rb
#!/Users/aaron/.rubies/arm64/ruby-trunk/bin/ruby
# This is a demo language server for Ruby, written in Ruby. It just checks
# the syntax of Ruby programs when you save them.
#
# Configure this in Vim by adding the vim-lsp plugin, then doing this
# in your vimrc:
#
# au User lsp_setup
# \ lsp#register_server({
View lexer_bench.rb
require "benchmark/ips"
require "graphql/language/lexer"
require "graphql/language/token"
require "graphql/language/block_string"
schema = DATA.read
Benchmark.ips { |x|
x.report("graphql") { GraphQL::Language::Lexer.tokenize(schema) }
}
View rb_tree.rb
# Okasaki style Functional Red Black Tree
# https://www.cs.tufts.edu/comp/150FP/archive/chris-okasaki/redblack99.pdf
#
# leaves and root are black
# BST
# No red node has a red child
# Every path from the root to a leaf has the same number of black nodes
module RBTree
class Leaf
View cross_platform.rb
require "fisk"
require "aarch64"
require "jit_buffer"
require "fiddle"
x86 = Fisk.new
x86.put_label(:foo)
x86.mov(x86.rax, x86.imm(42))
x86.ret
x86.jmp(x86.label(:foo))
View shc-decode.rb
# Smart Health Card decoder
# This decodes the SHC url thing that is stored in smart health card QR codes
str = DATA.readline
require "base64"
require "zlib"
require "json"
require "pp"
View fiddle_hacks.rb
# Fiddle, passing a pointer to an int. This gets the current process command
module Hacks
include Fiddle
func = "_NSGetExecutablePath"
path_ptr = Fiddle::Handle::DEFAULT[func]
path = Function.new path_ptr, [TYPE_VOIDP, TYPE_INTPTR_T], TYPE_INT, name: func
define_singleton_method(func, &path.to_proc)
@tenderlove
tenderlove / ruby_jit.rb
Last active January 12, 2022 00:17
Use fisk and fiddle to patch a Ruby method at runtime
View ruby_jit.rb
require "fisk"
require "fisk/helpers"
require "fiddle/import"
module Ruby
extend Fiddle::Importer
dlload
typealias "VALUE", "uintptr_t"
View break_dance.rb
# Have you ever wanted lldb to break in a certain place, but you weren't
# sure where to set the breakpoint? Look no further than this script!
#
# This script creates a chunk of executable code that uses the int3 x86
# instruction. This instruction is defined for use by debuggers, and you can
# read more about it here: https://en.wikipedia.org/wiki/INT_%28x86_instruction%29#INT3
#
# When that instruction is executed, the debugger will halt and you can do
# what you need!
#
View thing.rb
require "objspace"
require "json"
def go x
count = GC.stat(:major_gc_count)
loop do
info = JSON.parse(ObjectSpace.dump(x))
break if info["flags"]["old"]
GC.start
end
View profileyarv.rb
#!/usr/bin/env ruby
require 'pp'
files = {'vm.inc' => [], 'insns.def' => []}
current_insn = nil
current_filename = 'vm.inc'
lineno_offset = 0
IO.foreach('vm.inc') do |line|
case line
when /^INSN_ENTRY\((\w+)\)/