/
function.cpp
357 lines (302 loc) · 9.13 KB
/
function.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
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2004-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "de/Function"
#include "de/TextValue"
#include "de/ArrayValue"
#include "de/DictionaryValue"
#include "de/NoneValue"
#include "de/Writer"
#include "de/Reader"
#include "de/Log"
#include <QTextStream>
#include <QMap>
namespace de {
typedef QMap<String, Function::NativeEntryPoint> RegisteredEntryPoints;
static RegisteredEntryPoints entryPoints;
DENG2_PIMPL_NOREF(Function)
{
/// Argument names.
Function::Arguments arguments;
/// The function owns the default values stored in the arguments list.
Function::Defaults defaults;
/// The statements of this function.
Compound compound;
/// Namespace where the function was created. This global namespace is
/// used always when executing the function, regardless of where the
/// function is called.
Record *globals;
/// Name of the native function (empty, if this is not a native function).
String nativeName;
/// The native entry point.
Function::NativeEntryPoint nativeEntryPoint;
Instance() : globals(0), nativeEntryPoint(0)
{}
Instance(Function::Arguments const &args, Function::Defaults const &defaults)
: arguments(args), defaults(defaults), globals(0), nativeEntryPoint(0)
{}
};
Function::Function() : d(new Instance)
{}
Function::Function(Arguments const &args, Defaults const &defaults)
: d(new Instance(args, defaults))
{}
Function::Function(String const &nativeName, Arguments const &args, Defaults const &defaults)
: d(new Instance(args, defaults))
{
try
{
d->nativeName = nativeName;
d->nativeEntryPoint = nativeEntryPoint(nativeName);
}
catch(Error const &)
{
addRef(-1); // Cancelled construction of the instance; no one has a reference.
throw;
}
}
Function::~Function()
{
// Delete the default argument values.
DENG2_FOR_EACH(Defaults, i, d->defaults)
{
delete i.value();
}
if(d->globals)
{
// Stop observing the namespace.
d->globals->audienceForDeletion.remove(this);
}
}
String Function::asText() const
{
String result;
QTextStream os(&result);
os << "(Function " << this << " (";
DENG2_FOR_EACH_CONST(Arguments, i, d->arguments)
{
if(i != d->arguments.begin())
{
os << ", ";
}
os << *i;
Defaults::const_iterator def = d->defaults.find(*i);
if(def != d->defaults.end())
{
os << "=" << def.value()->asText();
}
}
os << "))";
return result;
}
Compound &Function::compound()
{
return d->compound;
}
Compound const &Function::compound() const
{
return d->compound;
}
Function::Arguments &Function::arguments()
{
return d->arguments;
}
Function::Arguments const &Function::arguments() const
{
return d->arguments;
}
Function::Defaults &Function::defaults()
{
return d->defaults;
}
Function::Defaults const &Function::defaults() const
{
return d->defaults;
}
void Function::mapArgumentValues(ArrayValue const &args, ArgumentValues &values) const
{
DENG2_ASSERT(args.size() > 0);
DictionaryValue const *labeledArgs = dynamic_cast<DictionaryValue const *>(
args.elements().front());
DENG2_ASSERT(labeledArgs != NULL);
// First use all the unlabeled arguments.
Arguments::const_iterator k = d->arguments.begin();
for(ArrayValue::Elements::const_iterator i = args.elements().begin() + 1;
i != args.elements().end(); ++i)
{
values.push_back(*i);
if(k != d->arguments.end())
{
if(labeledArgs->contains(TextValue(*k)))
{
/// @throw WrongArgumentsError An argument has been given more than one value.
throw WrongArgumentsError("Function::mapArgumentValues",
"More than one value has been given for '" +
*k + "' in function call");
}
++k;
}
}
if(values.size() < d->arguments.size())
{
// Then apply the labeled arguments, falling back to default values.
Arguments::const_iterator i = d->arguments.begin();
// Skip past arguments we already have a value for.
for(duint count = values.size(); count > 0; --count, ++i) {}
for(; i != d->arguments.end(); ++i)
{
try
{
values.push_back(&labeledArgs->element(TextValue(*i)));
}
catch(DictionaryValue::KeyError const &)
{
// Check the defaults.
Defaults::const_iterator k = d->defaults.find(*i);
if(k != d->defaults.end())
{
values.append(k.value());
}
else
{
/// @throw WrongArgumentsError Argument is missing a value.
throw WrongArgumentsError("Function::mapArgumentValues",
"The value of argument '" + *i + "' has not been defined in function call");
}
}
}
}
// Check that the number of arguments matches what we expect.
if(values.size() != d->arguments.size())
{
/// @throw WrongArgumentsError Wrong number of argument specified.
throw WrongArgumentsError("Function::mapArgumentValues",
"Expected " + QString::number(d->arguments.size()) +
" arguments, but got " + QString::number(values.size()) +
" arguments in function call");
}
}
void Function::setGlobals(Record *globals)
{
LOG_AS("Function::setGlobals");
if(!d->globals)
{
d->globals = globals;
d->globals->audienceForDeletion.add(this);
}
else if(d->globals != globals)
{
LOG_SCR_WARNING("Function was offered a different namespace");
}
}
Record *Function::globals() const
{
return d->globals;
}
bool Function::isNative() const
{
return d->nativeEntryPoint != NULL;
}
Value *Function::callNative(Context &context, ArgumentValues const &args) const
{
DENG2_ASSERT(isNative());
DENG2_ASSERT(args.size() == d->arguments.size()); // all arguments provided
Value *result = (d->nativeEntryPoint)(context, args);
if(!result)
{
// Must always return something.
result = new NoneValue;
}
return result;
}
void Function::operator >> (Writer &to) const
{
// Number of arguments.
to << duint16(d->arguments.size());
// Argument names.
DENG2_FOR_EACH_CONST(Arguments, i, d->arguments)
{
to << *i;
}
// Number of default values.
to << duint16(d->defaults.size());
// Default values.
DENG2_FOR_EACH_CONST(Defaults, i, d->defaults)
{
to << i.key() << *i.value();
}
// The statements of the function.
to << d->compound;
// The possible native entry point.
to << d->nativeName;
}
void Function::operator << (Reader &from)
{
duint16 count = 0;
// Argument names.
from >> count;
d->arguments.clear();
while(count--)
{
String argName;
from >> argName;
d->arguments.push_back(argName);
}
// Default values.
from >> count;
d->defaults.clear();
while(count--)
{
String name;
from >> name;
d->defaults[name] = Value::constructFrom(from);
}
// The statements.
from >> d->compound;
from >> d->nativeName;
// Restore the entry point.
if(!d->nativeName.isEmpty())
{
d->nativeEntryPoint = nativeEntryPoint(d->nativeName);
}
}
void Function::recordBeingDeleted(Record &DENG2_DEBUG_ONLY(record))
{
// The namespace of the record is being deleted.
DENG2_ASSERT(d->globals == &record);
d->globals = 0;
}
void Function::registerNativeEntryPoint(String const &name, Function::NativeEntryPoint entryPoint)
{
entryPoints.insert(name, entryPoint);
}
void Function::unregisterNativeEntryPoint(String const &name)
{
entryPoints.remove(name);
}
Function::NativeEntryPoint Function::nativeEntryPoint(String const &name)
{
RegisteredEntryPoints::const_iterator found = entryPoints.constFind(name);
if(found == entryPoints.constEnd())
{
throw UnknownEntryPointError("Function::nativeEntryPoint",
QString("Native entry point '%1' is not available").arg(name));
}
return found.value();
}
} // namespace de