-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
321 lines (260 loc) · 8.82 KB
/
index.js
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
var murmur3 = require('./lib/murmur3');
var random = require('./lib/random');
// Poor man's enum.
var PARAMETER = {}, COMPUTED = {};
// Allows fetching based on a relative key path, e.g., "someParent.xyz". For
// traversing up the tree, "..parentValue" and "...grandParentValue" also
// works, as well as "/rootValue".
function instanceGet(keyPath) {
var value = this;
// Make key path relative to root node if first character is "/".
if (keyPath[0] == '/') {
var parent;
while (parent = value.getParent()) value = parent;
keyPath = keyPath.substr(1);
}
var keys = keyPath.split('.');
// Remove first empty argument to make ".bla" refer to current instance.
if (!keys[0]) {
keys.shift();
}
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
value = key ? value[key] : value.getParent();
}
return value;
}
module.exports = function procedural(name, parent) {
var values = [], valuesMap = {hash: null};
var doNotHash = [];
var parameterCount = 0;
// Prevent defining values with the same name as the parent.
if (parent) {
valuesMap[parent.getName()] = null;
}
// Utility functions that can be passed for use outside this function.
function getName() {
return name;
}
function getValues(opt_type) {
var names = [];
for (var i = 0; i < values.length; i++) {
if (opt_type && values[i].type != opt_type) continue;
names.push(values[i].name);
}
return names;
}
function getParameters() {
return getValues(PARAMETER);
}
function getProvides() {
return getValues(COMPUTED);
}
// The constructor for a single instance of this procedural function.
function ProceduralInstance(parentInstance) {
if (parentInstance) {
this[parentInstance.getName()] = parentInstance;
} else if (parent) {
console.warn('Creating detached ' + name + ' instance (expected parent ' + parent + ')');
}
}
ProceduralInstance.prototype = {
get: instanceGet,
getName: getName,
getParameters: getParameters,
getProvides: getProvides,
getValues: getValues,
getParent: function () {
if (!parent) return null;
return this[parent.getName()];
},
getRandGen: function (opt_id) {
var p = this.getParent(),
s0 = murmur3.hash32(opt_id || 'default', this.hash) * 2.3283064365386963e-10,
s1 = this.hash * 2.3283064365386963e-10;
return random(s0, s1);
},
toString: function () {
var args, proc = this, pieces = [];
while (proc) {
args = proc.getParameters().map(function (paramName) {
return paramName + ': ' + JSON.stringify(proc[paramName]);
});
pieces.unshift(proc.getName() + '(' + args.join(', ') + ')');
proc = proc.getParent();
}
return pieces.join('.');
}
};
function create() {
if (arguments.length != parameterCount) {
throw new Error('Wrong number of parameters for ' + create + ': ' + arguments.length);
}
/*
// Ensure that the definition doesn't change after an instance has been created.
if (!Object.isFrozen(create)) {
Object.freeze(create);
Object.freeze(ProceduralInstance.prototype);
}
*/
// We assume that this function is bound to the parent instance. Example:
// jupiter.moon(13) -- "moon" is this function, bound to "jupiter".
var parentInstance = parent && parent.isInstance(this) ? this : null;
// Create the instance which will hold all the values.
var instance = new ProceduralInstance(parentInstance);
// Start setting up an array of all values that make up the hash.
var hashParts = [name], hashSeed = parentInstance ? parentInstance.hash : 0;
function createHashOnce() {
if ('hash' in instance) return;
// Calculate the hash for the instance based on parents and parameters.
instance.hash = murmur3.hash32(hashParts.join('\x00'), hashSeed);
}
// Fill in all the values specified on this procedural function.
var argumentIndex = 0;
for (var i = 0; i < values.length; i++) {
var value = values[i], shouldHash = doNotHash.indexOf(value.name) == -1;
if (value.type == PARAMETER) {
if ('hash' in instance && shouldHash) {
throw new Error('Cannot define hashed parameters after hash is generated');
}
var argument = arguments[argumentIndex++];
// Validate the value.
if (value.validator && !value.validator(instance, argument)) {
throw new Error('Invalid value for ' + name + '.' + value.name + ': ' + argument);
}
// Assign the argument value to the instance.
instance[value.name] = argument;
if (shouldHash) {
// TODO: Performance check for JSON.stringify, maybe toString is enough.
hashParts.push(JSON.stringify(instance[value.name]));
}
} else if (value.type == COMPUTED) {
// Compute and assign the value to the instance.
if (value.fn) {
// Always create the hash before computing values which may need it.
createHashOnce();
instance[value.name] = value.fn(instance);
} else {
instance[value.name] = value.constant;
}
}
}
// Create the hash now if it wasn't created above.
createHashOnce();
// Prevent the instance from changing before exposing it.
Object.freeze(instance);
return instance;
}
create.getName = getName;
create.getParameters = getParameters;
create.getProvides = getProvides;
create.getValues = getValues;
create.done = function () {
return parent;
};
create.doNotHash = function (var_args) {
Array.prototype.push.apply(doNotHash, arguments);
return this;
};
create.generates = function (nameValue) {
/*
if (Object.isFrozen(create)) {
throw new Error('Cannot define ' + this + '.' + nameValue + ': instances of ' + name + ' exist');
}
*/
var proc = procedural(nameValue, this);
ProceduralInstance.prototype[nameValue] = proc;
this[nameValue] = proc;
return proc;
};
create.getParent = function () {
return parent;
};
create.isInstance = function (obj) {
return obj instanceof ProceduralInstance;
};
create.provides = function (name, fnOrConstant) {
/*
if (Object.isFrozen(create)) {
throw new Error('Cannot define value after creation');
}
*/
if (name in valuesMap) {
throw new Error('A value named ' + name + ' is already defined');
}
if (name in ProceduralInstance.prototype) {
throw new Error('Invalid value name "' + name + '"');
}
var value = {name: name, type: COMPUTED};
if (typeof fnOrConstant == 'function') {
value.fn = fnOrConstant;
} else {
value.constant = fnOrConstant;
}
values.push(value);
valuesMap[name] = value;
return this;
};
create.providesMethod = function (name, fn) {
if (typeof fn != 'function') {
throw new Error('Method must be passed in as a function');
}
if (name in valuesMap) {
throw new Error('A value named ' + name + ' is already defined');
}
if (name in ProceduralInstance.prototype) {
throw new Error('Invalid method name "' + name + '"');
}
valuesMap[name] = function wrapper() {
return fn.apply(this, Array.prototype.concat.apply([this], arguments));
};
ProceduralInstance.prototype[name] = valuesMap[name];
return this;
};
create.takes = function (var_args) {
/*
if (Object.isFrozen(create)) {
throw new Error('Cannot define parameter after creation');
}
*/
// The last argument may be a validation function.
var numParams = arguments.length, validator;
if (typeof arguments[numParams - 1] == 'function') {
validator = arguments[numParams--];
}
if (!numParams) {
throw new Error('At least one parameter must be specified');
}
for (var i = 0; i < numParams; i++) {
var name = arguments[i];
if (typeof name != 'string') {
throw new Error('Invalid parameter name ' + name);
}
if (name in valuesMap) {
throw new Error('A value named ' + name + ' is already defined');
}
if (name in ProceduralInstance.prototype) {
throw new Error('Invalid parameter name "' + name + '"');
}
var param = {name: name, type: PARAMETER};
if (typeof validator == 'function') {
param.validator = validator;
}
values.push(param);
valuesMap[name] = param;
}
// Keep track of number of parameters for the constructor validation.
parameterCount += numParams;
return this;
};
create.toString = function () {
var pieces = [], proc = this;
while (proc) {
pieces.unshift(proc.getName());
proc = proc.getParent();
}
var names = this.getParameters().join(', ');
return pieces.join('.') + '(' + names + ')';
};
return create;
};