Skip to content

Instantly share code, notes, and snippets.

View iby's full-sized avatar
👻

Ian Bytchek iby

👻
View GitHub Profile
@iby
iby / main.swift
Created June 17, 2019 14:27
Slang Example
import Slang
let source: String = "import Foundation; class Foo { let bar = 1 }"
let file: File = File(source)
let disassembly: Disassembly = try! Disassembly(file)
var edits: [Edit] = []
// Change "Foundation" identifier to "AppKit".
edits.append(Edit(disassembly.query.syntax.first(of: .identifier).select(where: { $0.contents == "Foundation" }).one!, "AppKit"))
// Change "class" keyword to "struct".
@iby
iby / Exclude.swift
Created September 5, 2021 09:22
Create a list of excluded paths at the base location keeping the specified paths.
private func exclude(paths: [String], at basePath: String) -> [String] {
let slash = CharacterSet(charactersIn: "/")
let baseURL = URL(fileURLWithPath: basePath, isDirectory: true)
var exclusions = try! FileManager.default.contentsOfDirectory(atPath: basePath).sorted()
var inclusions = paths
// Cleanup paths removing and expanding `/.`, `/..`, etc.
inclusions = inclusions.reduce(into: [], { inclusions, inclusion in
let path = URL(fileURLWithPath: inclusion, relativeTo: baseURL).standardized.relativePath.trimmingCharacters(in: slash)
if !path.isEmpty { inclusions.append(path) }
// This is a hideous hack for 10.11 where constraints are applied for detached view and the entire
// toolset panel looks like a huge cock…
if #available(macOS 10.13, *) {
} else if self.instructionView.isHidden && self.instructionView.superview != nil {
self.stackView.removeView(self.instructionView)
} else if !self.instructionView.isHidden && self.instructionView.superview == nil {
self.stackView.insertView(self.instructionView, at: 0, in: .center)
}
@iby
iby / FileDescriptor.swift
Last active July 10, 2024 07:30
At attempt to better understand Darwin's standard streams (stdout, stderr) and file descriptors.
import Foundation
fileprivate enum StdStream {
case out
case err
fileprivate var fileDescriptor: FileDescriptor {
switch self {
case .out: return FileDescriptor(STDOUT_FILENO, "stdout")
case .err: return FileDescriptor(STDERR_FILENO, "stderr")
@iby
iby / ISA-swizzling and breaking KVO.md
Last active August 8, 2024 11:55
Research notes and playground for ISA-swizzling breaking KVO and causing exceptions during observer removal / observee deinitialization when using ReactiveCocoa on macOS 10.15 and above, see full discussion: https://github.com/ReactiveCocoa/ReactiveCocoa/issues/3690

This experiment was born out of ReactiveCocoa#3690 – a homeopathy to fix swizzling with more swizzling.

Ideally, there would be a way to simply intercept the call with a custom handler to add some logic. However, that's far from simple:

  • Need to handle existing class methods (add vs. replace).
  • Need to call original implementation and pass it parameters – no way of doing it in Swift in some cases (via message forwarding), no way of doing it reliably in Objective-C in others (via va_list)…

In theory, where my research stops, it's possible to achieve this via scenario:

  1. Add a block implementation under a random non-clashing selector.