/
process.cpp
454 lines (388 loc) · 12.3 KB
/
process.cpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* The Doomsday Engine Project -- libcore
*
* Copyright © 2004-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/Process"
#include "de/Variable"
#include "de/ArrayValue"
#include "de/RecordValue"
#include "de/TextValue"
#include "de/NoneValue"
#include "de/TryStatement"
#include "de/CatchStatement"
#include <sstream>
using std::auto_ptr;
namespace de {
DENG2_PIMPL(Process)
{
State state;
// The execution environment.
typedef std::vector<Context *> ContextStack;
ContextStack stack;
/// This is the current working folder of the process. Relative paths
/// given to workingFile() are located in relation to this
/// folder. Initial value is the root folder.
String workingPath;
/// Time when execution was started at depth 1.
Time startedAt;
Instance(Public *i)
: Base(i)
, state(Stopped)
, workingPath("/")
{}
~Instance()
{
clearStack();
}
void clear()
{
state = Stopped;
clearStack();
self.pushContext(new Context(Context::BaseProcess, thisPublic));
workingPath = "/";
}
Context &context(duint downDepth = 0)
{
DENG2_ASSERT(downDepth < depth());
return **(stack.rbegin() + downDepth);
}
inline dsize depth() const
{
return stack.size();
}
/// Pops contexts off the stack until depth @a downToLevel is reached.
void clearStack(duint downToLevel = 0)
{
while(depth() > downToLevel)
{
delete stack.back();
stack.pop_back();
}
}
void run(Statement const *firstStatement)
{
if(state != Stopped)
{
throw NotStoppedError("Process::run", "Process must be stopped first");
}
state = Running;
// Make sure the stack is clear except for the process context.
clearStack(1);
context().start(firstStatement);
}
/// Fast forward to a suitable catch statement for @a err.
/// @return @c true, if suitable catch statement found.
bool jumpIntoCatch(Error const &err)
{
dint level = 0;
// Proceed along default flow.
for(context().proceed(); context().current(); context().proceed())
{
Statement const *statement = context().current();
TryStatement const *tryStatement = dynamic_cast<TryStatement const *>(statement);
if(tryStatement)
{
// Encountered a nested try statement.
++level;
continue;
}
CatchStatement const *catchStatement = dynamic_cast<CatchStatement const *>(statement);
if(catchStatement)
{
if(!level)
{
// This might be the catch for us.
if(catchStatement->matches(err))
{
catchStatement->executeCatch(context(), err);
return true;
}
}
if(catchStatement->isFinal() && level > 0)
{
// A sequence of catch statements has ended.
--level;
}
}
}
// Failed to find a catch statement that matches the given error.
return false;
}
};
/// If execution continues for longer than this, a HangError is thrown.
static TimeDelta const MAX_EXECUTION_TIME = 10;
Process::Process(Record *externalGlobalNamespace) : d(new Instance(this))
{
// Push the first context on the stack. This bottommost context
// is never popped from the stack. Its namespace is the global namespace
// of the process.
pushContext(new Context(Context::BaseProcess, this, externalGlobalNamespace));
}
Process::Process(Script const &script) : d(new Instance(this))
{
clear();
// If a script is provided, start running it automatically.
run(script);
}
Process::State Process::state() const
{
return d->state;
}
void Process::clear()
{
d->clear();
}
dsize Process::depth() const
{
return d->depth();
}
void Process::run(Script const &script)
{
d->run(script.firstStatement());
// Set up the automatic variables.
Record &ns = globals();
if(ns.has("__file__"))
{
ns["__file__"].set(TextValue(script.path()));
}
else
{
ns.add(new Variable("__file__", new TextValue(script.path()), Variable::AllowText));
}
}
void Process::suspend(bool suspended)
{
if(d->state == Stopped)
{
throw SuspendError("Process:suspend",
"Stopped processes cannot be suspended or resumed");
}
d->state = (suspended? Suspended : Running);
}
void Process::stop()
{
d->state = Stopped;
// Clear the context stack, apart from the bottommost context, which
// represents the process itself.
DENG2_FOR_EACH_REVERSE(Instance::ContextStack, i, d->stack)
{
if(*i != d->stack[0])
{
delete *i;
}
}
DENG2_ASSERT(!d->stack.empty());
// Erase all but the first context.
d->stack.erase(d->stack.begin() + 1, d->stack.end());
// This will reset any half-done evaluations, but it won't clear the namespace.
context().reset();
}
void Process::execute()
{
if(d->state == Suspended || d->state == Stopped)
{
// The process is not active.
return;
}
// We will execute until this depth is complete.
duint startDepth = d->depth();
if(startDepth == 1)
{
// Mark the start time.
d->startedAt = Time();
}
// Execute the next command(s).
while(d->state == Running && d->depth() >= startDepth)
{
try
{
dsize execDepth = d->depth();
if(!context().execute() && d->depth() == execDepth)
{
// There was no statement left to execute, and no new contexts were
// added to the stack.
finish();
}
else if(d->startedAt.since() > MAX_EXECUTION_TIME)
{
/// @throw HangError Execution takes too long.
throw HangError("Process::execute",
"Script execution takes too long, or is stuck in an infinite loop");
}
}
catch(Error const &err)
{
//std::cerr << "Caught " << err.asText() << " at depth " << depth() << "\n";
// Fast-forward to find a suitable catch statement.
if(d->jumpIntoCatch(err))
{
// Suitable catch statement was found. The current statement is now
// pointing at the catch compound's first statement.
continue;
}
if(startDepth > 1)
{
// Pop this context off, it has not caught the exception.
delete popContext();
throw;
}
else
{
// Exception uncaught by all contexts, script execution stops.
LOG_SCR_NOTE("Stopping process: ") << err.asText();
stop();
}
}
}
}
Context &Process::context(duint downDepth)
{
return d->context(downDepth);
}
void Process::pushContext(Context *context)
{
d->stack.push_back(context);
}
Context *Process::popContext()
{
Context *topmost = d->stack.back();
d->stack.pop_back();
// Pop a global namespace as well, if present.
if(context().type() == Context::GlobalNamespace)
{
delete d->stack.back();
d->stack.pop_back();
}
return topmost;
}
void Process::finish(Value *returnValue)
{
DENG2_ASSERT(depth() >= 1);
// Move one level downwards in the context stack.
if(depth() > 1)
{
// Finish the topmost context.
std::auto_ptr<Context> topmost(popContext());
if(topmost->type() == Context::FunctionCall)
{
// Return value to the new topmost level.
context().evaluator().pushResult(returnValue? returnValue : new NoneValue);
}
delete topmost.release();
}
else
{
DENG2_ASSERT(d->stack.back()->type() == Context::BaseProcess);
// This was the last level.
d->state = Stopped;
}
}
String const &Process::workingPath() const
{
return d->workingPath;
}
void Process::setWorkingPath(String const &newWorkingPath)
{
d->workingPath = newWorkingPath;
}
void Process::call(Function const &function, ArrayValue const &arguments, Value *instanceScope)
{
// First map the argument values.
Function::ArgumentValues argValues;
function.mapArgumentValues(arguments, argValues);
if(function.isNative())
{
// Do a native function call.
context().setInstanceScope(instanceScope);
context().evaluator().pushResult(function.callNative(context(), argValues));
context().setInstanceScope(0);
}
else
{
// If the function resides in another process's namespace, push
// that namespace on the stack first.
if(function.globals() && function.globals() != &globals())
{
pushContext(new Context(Context::GlobalNamespace, this, function.globals()));
}
// If the function is not in the global namespace, add its own parent
// namespace to the stack, too.
// Create a new context.
pushContext(new Context(Context::FunctionCall, this));
// If the scope is defined, create the "self" variable for it.
if(instanceScope)
{
context().names().add(new Variable("self", instanceScope /*taken*/));
}
// Create local variables for the arguments in the new context.
Function::ArgumentValues::const_iterator b = argValues.begin();
Function::Arguments::const_iterator a = function.arguments().begin();
for(; b != argValues.end() && a != function.arguments().end(); ++b, ++a)
{
// Records must only be passed as unowned references.
DENG2_ASSERT(!(*b)->is<RecordValue>() || !(*b)->as<RecordValue>().hasOwnership());
context().names().add(new Variable(*a, (*b)->duplicate()));
}
// This should never be called if the process is suspended.
DENG2_ASSERT(d->state != Suspended);
if(d->state == Running)
{
// Execute the function as part of the currently running process.
context().start(function.compound().firstStatement());
execute();
}
else if(d->state == Stopped)
{
// We'll execute just this one function.
d->state = Running;
context().start(function.compound().firstStatement());
execute();
d->state = Stopped;
}
}
}
void Process::namespaces(Namespaces &spaces) const
{
spaces.clear();
bool gotFunction = false;
DENG2_FOR_EACH_CONST_REVERSE(Instance::ContextStack, i, d->stack)
{
Context &context = **i;
if(context.type() == Context::FunctionCall)
{
// Only the topmost function call namespace is available: one cannot
// access the local variables of the callers.
if(gotFunction) continue;
gotFunction = true;
}
spaces.push_back(&context.names());
if(context.type() == Context::GlobalNamespace)
{
// This shadows everything below.
break;
}
}
}
Record &Process::globals()
{
return d->stack[0]->names();
}
Record &Process::locals()
{
return d->stack.back()->names();
}
} // namespace de