3
Wrapper for STBlockContext to make it invisible from the user
5
Copyright (c) 2002 Free Software Foundation
7
This file is part of the StepTalk project.
9
This library is free software; you can redistribute it and/or
10
modify it under the terms of the GNU Lesser General Public
11
License as published by the Free Software Foundation; either
12
version 2 of the License, or (at your option) any later version.
14
This library is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
Lesser General Public License for more details.
19
You should have received a copy of the GNU Lesser General Public
20
License along with this library; if not, write to the Free Software
21
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
#import "STLiterals.h"
28
#import "STBlockContext.h"
29
#import "STBytecodeInterpreter.h"
32
#import <StepTalk/STExterns.h>
34
#import <Foundation/NSArray.h>
35
#import <Foundation/NSDebug.h>
36
#import <Foundation/NSException.h>
38
Class STBlockContextClass = nil;
40
@implementation STBlock
43
STBlockContextClass = [STBlockContext class];
45
- initWithInterpreter:(STBytecodeInterpreter *)anInterpreter
46
homeContext:(STMethodContext *)context
47
initialIP:(unsigned)ptr
48
argumentCount:(int)count
51
homeContext = context;
55
interpreter = anInterpreter;
60
- (unsigned)argumentCount
69
[NSException raise:STScriptingException
70
format:@"Block needs %i arguments", argCount];
73
return [self valueWithArgs:(id*)0 count:0];
81
retval = [self valueWithArgs:args count:1];
85
- valueWith:arg1 with:arg2
87
id args[2] = {arg1,arg2};
90
retval = [self valueWithArgs:args count:2];
94
- valueWith:arg1 with:arg2 with:arg3
96
id args[3] = {arg1,arg2,arg3};
99
retval = [self valueWithArgs:args count:3];
103
- valueWith:arg1 with:arg2 with:arg3 with:arg4
105
id args[4] = {arg1,arg2,arg3,arg4};
108
retval = [self valueWithArgs:args count:4];
112
- valueWithArgs:(id *)args count:(unsigned)count;
114
STExecutionContext *parentContext;
115
STBlockContext *context;
120
if(argCount != count)
122
[NSException raise:STScriptingException
123
format:@"Invalid block argument count %i, "
124
@"wants to be %i", count, argCount];
128
if(!usingCachedContext)
130
/* In case of recursive block nesting */
131
usingCachedContext = YES;
135
cachedContext = [[STBlockContextClass alloc]
136
initWithInterpreter:interpreter
138
stackSize:stackSize];
141
/* Avoid allocation */
142
context = cachedContext;
143
[[context stack] empty];
144
[context resetInstructionPointer];
148
/* Create new context */
149
context = [[STBlockContextClass alloc] initWithInterpreter:interpreter
151
stackSize:stackSize];
153
AUTORELEASE(context);
156
/* push block arguments to the stack */
158
stack = [context stack];
159
for(i = 0; i<count; i++)
161
[stack push:args[i]];
164
[context setHomeContext:homeContext];
166
parentContext = [interpreter context];
168
[interpreter setContext:context];
169
retval = [interpreter interpret];
170
[interpreter setContext:parentContext];
172
/* Release cached context */
173
if(usingCachedContext)
175
usingCachedContext = NO;
181
- handler:(STBlock *)handlerBlock
183
STExecutionContext *context;
186
/* save the execution context for the case of exception */
187
context = [interpreter context];
190
retval = [self value];
192
retval = [handlerBlock valueWith:localException];
193
/* restore the execution context */
194
[interpreter setContext:context];
200
- whileTrue:(STBlock *)doBlock
204
while([[self value] boolValue])
206
retval = [doBlock value];
211
- whileFalse:(STBlock *)doBlock
215
while(! [[self value] boolValue])
217
retval = [doBlock value];