Skip to content

Instantly share code, notes, and snippets.

View JensAyton's full-sized avatar

Jens Ayton JensAyton

View GitHub Profile
@JensAyton
JensAyton / JADictionaryOfVariableBindings.h
Created April 28, 2014 22:28
NSDictionaryOfVariableBindings at compile time, just because.
#import <Foundation/Foundation.h>
#define JADictionaryOfVariableBindings(...) \
[NSDictionary dictionaryWithObjects:(const id []){ __VA_ARGS__ } \
forKeys:(const NSString *[]){ JATEMPLATE_MAP(JATEMPLATE_NAME_FROM_ARG, __VA_ARGS__) } \
count:JATEMPLATE_ARGUMENT_COUNT(__VA_ARGS__)]
#define JATEMPLATE_NAME_FROM_ARG(ITEM) @#ITEM
@JensAyton
JensAyton / oops.c
Last active August 29, 2015 13:56 — forked from davepeck/oops.c
One example of cleanup without using goto. (This isn't an opinion on what Apple “should have” done, it’s just an answer to a specific question on Twitter. It would have done nothing to avoid the actual bug.) I also removed the unused variable “signedHashes”.
static OSStatus
SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, SSLBuffer signedParams,
uint8_t *signature, UInt16 signatureLen)
{
SSLBuffer hashCtx;
hashCtx.date = 0;
OSStatus err = SSLVerifySignedServerKeyExchangeInner(ctx, isRsa, signedParams, signature, signatureLen, &hashCtx);
SSLFreeBuffer(hashCtx);
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface Test: NSObject
@end
@interface Test (Dynamic)
- (void)foo:(float)x :(float)y;
// Expansion of JATExpandLiteral(@"foo: {foo}; bar: {bar}; baz: {baz}", foo, bar, baz)
JAT_DoExpandTemplateUsingMacroKeysAndValues(@"foo: {foo}; bar: {bar}; baz: {baz}", (JATNameArray){ @"foo", @"bar", @"baz", ((void*)0) }, (JATParameterArray){ JATCastParameter(foo), JATCastParameter(bar), JATCastParameter(baz), ((void*)0) }, 3)
@JensAyton
JensAyton / gist:3855499
Created October 8, 2012 23:00
Some of the macros in NSObjCRuntime.h just aren’t ugly enough.
#undef MIN
#define MIN__(A,B,C) ({ __typeof__(A) __a##C = (A); __typeof__(B) __b##C = (B); __a##C < __b##C ? __a##C : __b##C; })
#define MIN_(A,B,C) MIN__(A,B,C)
#define MIN(A,B) MIN_(A,B,__COUNTER__)
#undef MAX
#define MAX__(A,B,C) ({ __typeof__(A) __a##C = (A); __typeof__(B) __b##C = (B); __a##C < __b##C ? __b##C : __a##C; })
#define MAX_(A,B,C) MAX__(A,B,C)
#define MAX(A,B) MAX_(A,B,__COUNTER__)
@implementation Foo
- (id) foo
{
return [self bar];
}
- (id) bar
{
@JensAyton
JensAyton / abjc_arc.m
Last active September 28, 2015 04:37
A trivial implementation of Objective-C ARC runtime support for the Cocotron, excluding weak references.
/* Copyright (c) 2011 Jens Ayton
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
@JensAyton
JensAyton / arc-exception-funhouse.m
Created October 22, 2011 17:08
Fun with ARC and exceptions
/*
The desired output of each test case is:
<MyException 0xPTR>{ TestName, retain count 1 } created.
<MyException 0xPTR>{ TestName, retain count 1 } caught.
<MyException 0xPTR>{ TestName, retain count 1 } dealloced.
For a fun time, try to predict the actual output for all six test cases!
// There are exact equivalents for CoreFoundation, if you like typing CFRelease a lot.
NSString *path = [[NSBundle mainBundle] builtInPlugInsPath]; // Or privateFrameworksPath, or some other thing.
path = [path stringByAppendingPathComponent:@"foo.dylib"];
void *handle = dlopen([path fileSystemRepresentation], flags);
@JensAyton
JensAyton / objc_dep.py
Created January 6, 2011 22:03
Header dependency mapper for Objective-C. This fork randomly colours arcs, for better tracing of spaghetti.
#!/usr/bin/python
# Nicolas Seriot
# 2011-01-06
# http://github.com/nst/objc_dep
"""
Input: path of an Objective-C project
Output: import dependancies Graphviz format