This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Super work-in-progress on an exception handling system similar to Com…
…mon Lisp conditions and restarts
- Loading branch information
1 parent
9d13782
commit bb3c24e
Showing
5 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// | ||
// EXTCondition.h | ||
// extobjc | ||
// | ||
// Created by Justin Spahr-Summers on 08.01.12. | ||
// Released into the public domain. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// | ||
// EXTCondition.m | ||
// extobjc | ||
// | ||
// Created by Justin Spahr-Summers on 08.01.12. | ||
// Released into the public domain. | ||
// | ||
|
||
#import "EXTCondition.h" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
// | ||
// EXTConditionTest.h | ||
// extobjc | ||
// | ||
// Created by Justin Spahr-Summers on 08.01.12. | ||
// Released into the public domain. | ||
// | ||
|
||
// Logic unit tests contain unit test code that is designed to be linked into an independent test executable. | ||
|
||
#import <SenTestingKit/SenTestingKit.h> | ||
|
||
@interface EXTConditionTest : SenTestCase | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// | ||
// EXTConditionTest.m | ||
// extobjc | ||
// | ||
// Created by Justin Spahr-Summers on 08.01.12. | ||
// Released into the public domain. | ||
// | ||
|
||
#import "EXTConditionTest.h" | ||
#import "EXTCondition.h" | ||
#import "EXTSafeCategory.h" | ||
|
||
NSMutableArray *handlers; | ||
NSMutableDictionary *restarts; | ||
jmp_buf context; | ||
|
||
typedef enum { | ||
EXTHandlerStatusInstallingHandlers = 0, | ||
EXTHandlerStatusRunning, | ||
EXTHandlerStatusDone | ||
} EXTHandlerStatus; | ||
|
||
typedef enum { | ||
EXTConditionStatusNoException = 0, | ||
EXTConditionStatusExceptionThrown | ||
} EXTConditionStatus; | ||
|
||
#define handle \ | ||
for (EXTHandlerStatus handlerStatus_ = EXTHandlerStatusInstallingHandlers; handlerStatus_ != EXTHandlerStatusDone; ++handlerStatus_) \ | ||
switch (handlerStatus_) \ | ||
for (BOOL loop2Done_ = NO; !loop2Done_; loop2Done_ = YES) \ | ||
default: \ | ||
if (handlerStatus_ == EXTHandlerStatusRunning) | ||
/* @handle block begins with user code */ | ||
|
||
#define rescue(TYPE, VAR) \ | ||
else if (handlerStatus_ == EXTHandlerStatusInstallingHandlers) \ | ||
for (id handler_ = nil; !handler_; (handler_ && ([handlers addObject:handler_], YES))) \ | ||
handler_ = ^(TYPE *VAR) | ||
|
||
#define restart(...) \ | ||
if (!setjmp(context) && ((restarts = [NSDictionary dictionaryWithKeysAndCopiedObjects:__VA_ARGS__, nil]) || YES)) | ||
|
||
#define invoke(RESTART, ...) \ | ||
NSLog(@"%@", RESTART) \ | ||
|
||
@interface NSDictionary (ConditionExtensions) | ||
+ (id)dictionaryWithKeysAndCopiedObjects:(id)firstKey, ... NS_REQUIRES_NIL_TERMINATION; | ||
@end | ||
|
||
@interface TestCondition : NSObject | ||
+ (void)raise; | ||
@end | ||
|
||
@implementation TestCondition | ||
+ (void)raise; { | ||
NSLog(@"jumping"); | ||
longjmp(context, 1); | ||
} | ||
|
||
@end | ||
|
||
@implementation EXTConditionTest | ||
|
||
- (void)testRestart { | ||
handlers = [[NSMutableArray alloc] init]; | ||
|
||
handle { | ||
__block NSString *str = @"foobar"; | ||
|
||
restart( | ||
@"invalid-string", ^(NSString *newString){ str = newString; } | ||
) { | ||
if ([str isEqualToString:@"foobar"]) | ||
[TestCondition raise]; | ||
} | ||
|
||
NSLog(@"restarts: %@", restarts); | ||
|
||
STAssertEqualObjects(str, @"fizzbuzz", @""); | ||
} rescue (TestCondition, condition) { | ||
invoke(@"invalid-string", @"fizzbuzz"); | ||
}; | ||
|
||
void (^testHandler)(TestCondition *) = [handlers objectAtIndex:0]; | ||
testHandler(nil); | ||
} | ||
|
||
@end | ||
|
||
@safecategory (NSDictionary, ConditionExtensions) | ||
+ (id)dictionaryWithKeysAndCopiedObjects:(id)firstKey, ... { | ||
if (!firstKey) | ||
return [NSDictionary dictionary]; | ||
|
||
va_list args; | ||
va_start(args, firstKey); | ||
|
||
id firstObject = va_arg(args, id); | ||
|
||
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; | ||
[dictionary setObject:[firstObject copy] forKey:firstKey]; | ||
|
||
for (;;) { | ||
id nextKey = va_arg(args, id); | ||
if (!nextKey) | ||
break; | ||
|
||
id nextObject = va_arg(args, id); | ||
[dictionary setObject:[nextObject copy] forKey:nextKey]; | ||
} | ||
|
||
va_end(args); | ||
|
||
return [dictionary copy]; | ||
} | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters