forked from pmougin/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFSGlobalScope.m
65 lines (47 loc) · 1.46 KB
/
FSGlobalScope.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* FSGlobalScope.m Copyright (c) 2009 Philippe Mougin. */
/* This software is open source. See the license. */
#import "FSGlobalScope.h"
#import "FSNSFileHandle.h"
FSGlobalScope *FSSharedGlobalScope;
void __attribute__ ((constructor)) initializeFSGlobalScope(void)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
FSSharedGlobalScope = [[FSGlobalScope alloc] init];
[FSSharedGlobalScope setObject:[NSFileHandle fileHandleWithStandardOutput] forSymbol:@"stdout"];
[FSSharedGlobalScope setObject:[NSFileHandle fileHandleWithStandardError] forSymbol:@"stderr"];
[FSSharedGlobalScope setObject:[NSFileHandle fileHandleWithStandardInput] forSymbol:@"stdin"];
[pool release];
}
@implementation FSGlobalScope
+ (FSGlobalScope *) sharedGlobalScope
{
return FSSharedGlobalScope;
}
- (id) init
{
self = [super init];
if (self != nil)
{
globals = [[NSMutableDictionary alloc] init];
}
return self;
}
- (id) objectForSymbol:(NSString *)symbol found:(BOOL *)found // foud may be passed as NULL
{
id object;
@synchronized(self)
{
object = [globals objectForKey:symbol];
}
*found = object ? YES : NO;
return object;
}
- (void) setObject:(id)object forSymbol:(NSString *)symbol // object must ne non-nil (current implementation does not support storing nil in the global scope)
{
NSParameterAssert(object != nil);
@synchronized(self)
{
[globals setObject:object forKey:symbol];
}
}
@end