forked from pmougin/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFSNSObject.m
193 lines (148 loc) · 5.14 KB
/
FSNSObject.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* FSNSObject.m Copyright (c) 1998-2009 Philippe Mougin. */
/* This software is open source. See the license. */
#import "build_config.h"
#import "FSNSObject.h"
#import "ArrayPrivate.h"
#import "FSNumber.h"
#import "NumberPrivate.h"
#import "FSBoolean.h"
#import "FSBooleanPrivate.h"
#import "FSNSStringPrivate.h"
#import "FSNSString.h"
#import "FSNSNumber.h"
#import <Foundation/Foundation.h>
#import "FSReplacementForCoderForClass.h"
#import "FScriptFunctions.h"
#import "FSKeyedArchiver.h"
#import "FSArchiver.h"
#import "FSBlock.h"
#import <objc/objc-runtime.h>
#import "FSMiscTools.h"
#import <objc/objc-api.h>
#import <AppKit/AppKit.h>
#import "FSAssociation.h"
@interface NSObject (DebugDescriptionDeclaration)
- (NSString *) debugDescription;
@end
@implementation NSObject (FSNSObject)
+ (id)replacementObjectForCoder:(NSCoder *)encoder
{
if (!encoder || [encoder isKindOfClass:[FSArchiver class]] || [encoder isKindOfClass:[FSKeyedArchiver class]]) // we provide a replacment only for archiving.
// Distributing seems to be automaticaly handled
// in Openstep 4.2
// Note that the "!encoder" test is a woraround for what seems
// to be a bug in Jag, where nil is sometimes passed a the encoder.
return [[[FSReplacementForCoderForClass alloc] initWithClass:self] autorelease];
else
return self;
}
//////////////////// USER METHODS /////////////////////
+ (NSString *)printString
{
NSString *description = [self description];
if (description == nil) description = @""; // Some Cocoa classes return nil when asked for their descriptions!
if ([self classOrMetaclass] == [NSObject classOrMetaclass] && self != (id)[NSObject class])
return [description stringByAppendingString:@" (meta)"];
else
return description;
}
- (id)applyBlock:(FSBlock *)block
{
return [block value:self];
}
- (id)classOrMetaclass
{
return object_getClass(self);
}
- (FSArray *) enlist
{
return [FSArray arrayWithObject:self];
}
- (FSArray *)enlist:(NSNumber *)operand // raise if not enough memory
{
double operandDouble;
FSArray *r;
VERIF_OP_NSNUMBER(@"enlist:")
operandDouble = [operand doubleValue];
if (operandDouble < 0)
FSExecError([NSString stringWithFormat:@"argument of method \"enlist:\" must be non-negative"]);
if (operandDouble > NSUIntegerMax)
FSExecError([NSString stringWithFormat:@"argument of method \"enlist:\" must be less or equal to %lu",(unsigned long)NSUIntegerMax]);
if (r = [[[FSArray alloc] initFilledWith:self count:operandDouble] autorelease])
return r;
else
FSExecError(@"not enough memory");
}
- (NSString *)printString
{
NSString *result;
if ([self respondsToSelector:@selector(debugDescription)]) result = [self debugDescription];
else result = [self description];
if (result == nil) result = @""; // Some Cocoa classes return nil when asked for their descriptions!
return result;
}
- (FSBoolean *)operator_equal_equal:(id)operand
{
return ((self == operand) ? fsTrue : fsFalse);
}
- (FSAssociation *)operator_hyphen_greater:(id)operand
{
return [FSAssociation associationWithKey:self value:operand];
}
- (FSBoolean *)operator_tilde_tilde:(id)operand
{
return ((self == operand) ? fsFalse : fsTrue);
}
- (void)save:(NSString *)operand
{
NSString *logFmt = @"failure while saving on file \"%@\" %@";
NSMutableData *data;
NSKeyedArchiver *archiver;
VERIF_OP_NSSTRING(@"save:")
data = [NSMutableData data];
archiver = [[[FSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease];
//[archiver setOutputFormat:NSPropertyListXMLFormat_v1_0];
@try
{
[archiver encodeObject:self forKey:@"root"];
[archiver finishEncoding];
}
@catch (id exception)
{
FSExecError([NSString stringWithFormat:logFmt, operand, FSErrorMessageFromException(exception)]);
}
if (![data writeToFile:operand atomically:YES])
FSExecError([NSString stringWithFormat:@"failure while saving on file \"%@\"",[NSString stringWithString:operand]]);
}
- (void)save
{
NSSavePanel *panel = [NSSavePanel savePanel];
//const char *dir;
//dir = NXHomeDirectory();
//[panel setRequiredFileType:@"fsobject"];
if ([panel runModal] == NSOKButton)
[self save:[panel filename]];
}
- (void)throw
{
@throw self;
}
- (NSConnection *)vend:(NSString *)operand
{
NSConnection *theConnection;
VERIF_OP_NSSTRING(@"vend:")
theConnection = [[NSConnection alloc] init];
[theConnection setRootObject:self];
if ([theConnection registerName:operand] == NO)
{
[theConnection release];
return nil;
}
else return theConnection;
}
///////////////////////////////// PRIVATE FOR USE BY FSExecEngine ///////////////
- (NSUInteger) _ul_count { return 1; }
- (id)_ul_objectAtIndex:(NSUInteger)index { return self;}
//////////////////////////////// PRIVATE for use by FSNSDistantObject ////////////////
- (NSString *) _printString_remote {return [self printString];}
@end