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
|
/* 2003 Aug 5 */
#import "STScriptObject.h"
#import "NSInvocation+additions.h"
#import "STEnvironment.h"
#import "STEngine.h"
#import "STExterns.h"
#import "STObjCRuntime.h"
#import <Foundation/NSArray.h>
#import <Foundation/NSCoder.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSException.h>
#import <Foundation/NSInvocation.h>
#import <Foundation/NSString.h>
@implementation STScriptObject
/** Return new instance of script object without any instance variables */
+ scriptObject
{
return AUTORELEASE([[self alloc] init]);
}
- init
{
if ((self = [super init]) != nil)
{
methodDictionary = [[NSMutableDictionary alloc] init];
}
return self;
}
- initWithInstanceVariableNames:(NSString *)names
{
return [self init];
}
- (void)dealloc
{
RELEASE(methodDictionary);
RELEASE(ivars);
[super dealloc];
}
- (void)setObject:(id)anObject forVariable:(NSString *)aName
{
[self notImplemented:_cmd];
}
- (id)objectForVariable:(NSString *)aName
{
[self notImplemented:_cmd];
return nil;
}
- (NSArray *)instanceVariableNames
{
return [ivars allKeys];
}
- (void)addMethod:(id <STMethod>)aMethod
{
[methodDictionary setObject:aMethod forKey:[aMethod methodName]];
}
- (id <STMethod>)methodWithName:(NSString *)aName
{
return [methodDictionary objectForKey:aName];
}
- (void)removeMethod:(id <STMethod>)aMethod
{
[self notImplemented:_cmd];
}
- (void)removeMethodWithName:(NSString *)aName
{
[methodDictionary removeObjectForKey:aName];
}
- (NSArray *)methodNames
{
return [methodDictionary allKeys];
}
- (NSDictionary *)methodDictionary
{
return [NSDictionary dictionaryWithDictionary:methodDictionary];
}
/** Set object's environment. Note: This method should be replaced by
some other, more clever mechanism. */
- (void)setEnvironment:(STEnvironment *)env
{
ASSIGN(environment, env);
}
- (STEnvironment *)environment
{
return environment;
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
if( [super respondsToSelector:(SEL)aSelector] )
{
return YES;
}
return ([methodDictionary objectForKey:NSStringFromSelector(aSelector)] != nil);
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
NSMethodSignature *signature = nil;
signature = [super methodSignatureForSelector:sel];
if(!signature)
{
signature = STConstructMethodSignatureForSelector(sel);
}
return signature;
}
- (void) forwardInvocation:(NSInvocation *)invocation
{
STEngine *engine;
id <STMethod> method;
NSString *methodName = NSStringFromSelector([invocation selector]);
NSMutableArray *args;
id arg;
NSUInteger index;
NSUInteger count;
id retval = nil;
method = [methodDictionary objectForKey:methodName];
if(!method)
{
[NSException raise:@"STScriptObjectException"
format:@"No script object method with name '%@'",
methodName];
return;
}
engine = [STEngine engineForLanguage:[method languageName]];
/* Get arguments as array */
count = [[invocation methodSignature] numberOfArguments];
args = [NSMutableArray array];
for(index = 2; index < count; index++)
{
arg = [invocation getArgumentAsObjectAtIndex:index];
if (arg == nil)
{
[args addObject:STNil];
}
else
{
[args addObject:arg];
}
}
retval = [engine executeMethod:method
forReceiver:self
withArguments:args
inContext:environment];
[invocation setReturnValue:&retval];
}
- (void)encodeWithCoder:(NSCoder *)coder
{
// [super encodeWithCoder: coder];
[coder encodeObject:methodDictionary];
[coder encodeObject:ivars];
}
- initWithCoder:(NSCoder *)decoder
{
if ((self = [super init] /*[super initWithCoder: decoder]*/) != nil)
{
[decoder decodeValueOfObjCType: @encode(id) at: &methodDictionary];
[decoder decodeValueOfObjCType: @encode(id) at: &ivars];
}
return self;
}
@end
|