[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: StepTalk rocks
From: |
Matthew D Swank |
Subject: |
Re: StepTalk rocks |
Date: |
Sat, 19 Feb 2005 13:32:38 -0600 |
User-agent: |
Mozilla Thunderbird 1.0 (X11/20050209) |
StepTalkLocalLib is just a small framework that wraps some utility
functions and provides a few simple methods to support the existing
instance variable infrastructure in STScriptObject.
Can you provide the library so I can try the script you have posted?
I'm not sure what proper etiquette is regarding the posting of source,
but this shouldn't be too long.
Caveat: I am a novice ObjC programmer and I wanted this to work quickly,
so this won't be an epitome of good style or design:
//begin source
//---------------------------------------------------------
Library.h
#import <Foundation/Foundation.h>
#import <StepTalk/STScriptObject.h>
@interface STExtendedScriptObject:STScriptObject
// small hacks to fill ivars collection
-initWithInstanceVariableDictionary: (NSDictionary *)vars;
-initWithInstanceVariableArray: (NSArray *)vars;
// these are a necessary part of the compiler protocol
-setInstanceVariable: (id)var atIndex:(int)index;
-instanceVariableAtIndex: (int)index;
// just fooling around with protocol based inheritance
-newInstance;
-addInstanceVariable: (NSString *) var;
-removeInstanceVariable: (NSString *) var;
@end
@interface STLLUtil:NSObject
+makePointX:(float)x withY:(float)y;
+makeRangeX:(float)x withY:(float)y;
+makeRectX:(float)x withY:(float)y withW:(float)w withH:(float)h;
+log:(id)anObject;
@end
//-------------
Library.m
#import "Library.h"
@implementation STExtendedScriptObject
-initWithInstanceVariableDictionary: (NSDictionary *)vars
{
ivars = [[NSMutableDictionary alloc] initWithDictionary: vars];
return [self init];
}
-initWithInstanceVariableArray: (NSArray *)vars
{
int i;
int count = [vars count];
ivars = [[NSMutableDictionary alloc] init];
for(i = 0;i < count; ++i)
[ivars setObject:AUTORELEASE([[NSObject alloc] init])
forKey:[vars objectAtIndex: i]];
return [self init];
}
-setInstanceVariable: (id)var atIndex: (int)index
{
// [self notImplemented:_cmd];
[ivars setObject: var forKey: [[ivars allKeys] objectAtIndex: index]];
return self;
}
-instanceVariableAtIndex: (int)index
{
return [ivars objectForKey:[[ivars allKeys] objectAtIndex: index]];
}
//our little prototype inheritance method
-newInstance
{
STScriptObject *clone;
// fresh set of instance vars
clone = AUTORELEASE([[[self class] alloc ]
initWithInstanceVariableArray: [ivars allKeys]]);
// shallow copy of the methods
int i, methodCount = [methodDictionary count];
NSArray *methodKeys = [methodDictionary allKeys];
for (i = 0; i < methodCount; ++i)
[clone addMethod:
[methodDictionary objectForKey:
[methodKeys objectAtIndex:i]]];
[clone setEnvironment: [self environment]];
return clone;
}
// these are probably reasonably dangerous
-addInstanceVariable: (NSString *) var
{
if ([ivars objectForKey:var] != nil)
[ivars setObject:
AUTORELEASE([[NSObject alloc] init])
forKey: var];
return self;
}
-removeInstanceVariable: (NSString *) var
{
[ivars removeObjectForKey: var];
return self;
}
@end
@implementation STLLUtil
+makePointX:(float)x withY:(float)y
{
return [NSValue valueWithPoint:NSMakePoint(x,y)];
}
+makeRangeX:(float)x withY:(float)y
{
return [NSValue valueWithRange:NSMakeRange(x,y)];
}
+makeRectX:(float)x withY:(float)y withW:(float)w withH:(float)h
{
return [NSValue valueWithRect:NSMakeRect(x,y,w,h)];
}
+log:(id)anObject
{
NSLog(anObject);
return nil;
}
@end
//--------------
ScriptingInfo.h
#import<Foundation/Foundation.h>
@interface StepTalkLocalLibObjectInfo:NSObject
+ (NSDictionary *)namedObjectsForScripting;
@end
//--------------
ScriptingInfo.m
#import "ScriptingInfo.h"
@implementation StepTalkLocalLibObjectInfo
+ (NSDictionary *)namedObjectsForScripting
{
NSMutableDictionary *dictionary;
dictionary = [NSMutableDictionary new];
//immutable
return [NSDictionary dictionaryWithDictionary:dictionary];
}
@end
//------------
ScriptingInfo.plist
{
ScriptingInfoClass = StepTalkLocalLibObjectInfo;
Classes = ( STExtendedScriptObject, STLLUtil );
}
//end source
//---------------------------------------------------------------
This is the .plist I used for the script:
Info.plist
{
ApplicationDescription = "Hello World Tutorial";
ApplicationIcon = "";
ApplicationName = HelloWorld;
ApplicationRelease = 0.1;
Authors = "";
Copyright = "Copyright (C) 200x by ...";
CopyrightDescription = "Released under...";
FullVersionID = 0.1;
URL = "";
}
Matt