-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomputable.js
434 lines (373 loc) · 13.6 KB
/
computable.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
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
"use strict";
/**
* An independent component that exists within a Scope, that can reference or be referenced by other components in that Scope.
*
* Rules:
* - All references must be bound, having unbound references is illegal.
* - All Computables must have a containing Scope.
* - Implementations are responsible for defining their traits by overriding the getters.
*/
var inherits = require('util').inherits;
var EventEmitter = require('events').EventEmitter;
var IRType = require('./type');
var IRVoidType = require('./types/void');
var current_unique_id = 0;
/**
* Abstract base for all Computable implementations.
* @constructor
* @extends EventEmitter
* @emits Scope#computable_added
* @param {Scope} scope The scope this computable is associated with.
* @param {Array.<Computable>} input_computables The Computables to bind the inputs to.
* @param {IRType} output_type The output type specification.
*/
function Computable (scope, input_computables, output_type) {
this.setMaxListeners(0);
if (!scope || !scope.emit) {
throw new Error("It is illegal to have an unscoped Computable");
}
if (!(output_type instanceof IRType)) {
throw new Error("Must pass in an IRType output type");
}
this._identity = ''+current_unique_id;
current_unique_id++;
this._output_type = output_type;
this._inputs = [];
this._dependees = [];
this._scope = scope;
var _this = this;
// TODO: this needs to be updated when dependee's inputs are removed
function find_dependee (computable) {
var dependee_entry;
for (var i = 0; i !== _this._dependees.length; ++i) {
var current_entry = _this._dependees[i];
if (current_entry.computable === computable) {
dependee_entry = current_entry;
break;
}
}
return { entry: dependee_entry, index: i };
}
// TODO: come up with a better way to handle all these cases than to use the event emitter pattern, just feels awkward.
this.on('dependee_added', function (computable, input_index) {
var dependee_result = find_dependee(computable);
var dependee_entry = dependee_result.entry;
if (dependee_entry) {
dependee_entry.indexes.push(input_index);
} else {
dependee_entry = {
computable: computable,
indexes: [input_index],
destroy_listener: function () {
var dependee_result = find_dependee(computable);
_this._dependees.splice(dependee_result.index, 1);
},
cleanup: function () {
computable.removeListener('destroyed', dependee_entry.destroy_listener);
}
};
_this._dependees.push(dependee_entry);
computable.once('destroyed', dependee_entry.destroy_listener);
}
});
this.on('dependee_removed', function (computable, input_index) {
var dependee_result = find_dependee(computable);
var dependee_entry = dependee_result.entry;
var indexes = dependee_entry.indexes;
var input_indexes_entry_index = indexes.indexOf(input_index);
if (input_indexes_entry_index !== -1) {
indexes.splice(input_indexes_entry_index, 1);
}
if (!indexes.length) {
_this._dependees.splice(dependee_result.index, 1);
dependee_entry.cleanup();
}
});
for (var i = 0; i < input_computables.length; i++) {
var input_computable = input_computables[i];
this.set_input(i, input_computable);
}
scope.emit('computable_added', this);
}
inherits(Computable, EventEmitter);
/**
* @returns {string}
*/
Computable.prototype.toString = function () {
return this.constructor.name + '-' + this.get_identity();
};
/**
* @returns {string} A string that uniquely identifies this Computable.
*/
Computable.prototype.get_identity = function () {
return this._identity;
};
// TODO: change to validate all at once? Would certainly make some spots less awkward.
/**
* Validate the usability of a Computable for the given index.
*
* Subclasses of Computable will want to override this to add the validations they want and call the base class' _validate_input method.
*
* @private
* @param {number} index The index the computable is requesting to fill.
* @param {Computable} computable The computable to check whether it can be used as an input.
* @throws {Error} When the input is invalid.
*/
Computable.prototype._validate_input = function (index, computable) {
if (!computable || !(computable instanceof Computable)) {
throw new Error("Input "+index+" is not a computable; got "+computable);
}
var void_type = new IRVoidType();
if (computable.get_output_type().equals(void_type)) {
throw new Error("Can not use void as an input "+computable);
}
var scope = this.get_containing_scope();
var input_containing_scope = computable.get_containing_scope();
if (input_containing_scope !== scope) {
throw new Error("All input computables must reside in the same scope as the computable that is being created");
}
if (computable.is_destroyed()) {
throw new Error("Cannot use a destroyed computable as an input");
}
};
/**
* @param {number} index
*/
Computable.prototype._reevaluate_input = function (index) {
var index_input_computable = this.get_input(index);
this._validate_input(index, index_input_computable);
};
/**
* @virtual
* @param {string} field_name The field name to get a computable at.
* @returns {Computable} The Computable at the given field name.
*/
Computable.prototype.get_property = function (field_name) {
throw new Error("Called get_property on a computable that didn't implement it");
};
/**
* @returns {Scope} The Scope that contains this Computable.
*/
Computable.prototype.get_containing_scope = function () {
return this._scope;
};
/**
* Removes the Computable and anything that relies on it from the containing Scope.
* @emits Computable#destroyed
*/
Computable.prototype.destroy = function () {
while (this.get_dependee_count()) {
this.get_dependee(0).destroy();
}
this._scope = null;
this.emit("destroyed");
};
/**
* @returns {boolean} Whether the Computable has been destroyed.
*/
Computable.prototype.is_destroyed = function () {
return !this._scope;
};
/**
* Performs a shallow clone of the Computable.
*
* @param {Array.<Computable>} [input_computables] The Computables to use as inputs in place of the current input Computables. Must reside in the given Scope.
* @param {Scope} [scope] The Scope the cloned Computable will reside in.
* @returns {Computable} A shallow clone of the Computable in the same scope (unless given a particular Scope), with the same input Computables (unless given alternative input Computables), but no dependees.
*/
Computable.prototype.clone = function (input_computables, scope) {
if (this.is_destroyed()) {
throw new Error("Cannot clone a destroyed Computable");
}
if (!input_computables) {
input_computables = this._inputs.slice();
}
if (!scope) {
scope = this.get_containing_scope();
}
return this._clone(scope, input_computables);
};
/**
* A private virtual method where each Computable subclass will define how they are cloned using the given input Computables and Scope.
*
* @virtual
* @private
* @param {Scope} scope The Scope that the Computable will reside in.
* @param {Array.<Computable>} [input_computables] The Computables to use as inputs. Defaults to the Computables current inputs if not provided.
* @returns {Computable} A shallow cloned version of the current Computable with no dependees.
*/
Computable.prototype._clone = function (scope, input_computables) {
throw new Error("This subclass of Computable has not implemented clone");
};
/**
* @returns {boolean} Returns whether the Computable is a scope parameter or not.
*/
Computable.prototype.is_scope_parameter = function () {
return false;
};
/**
* Sets the input parameter at the given index to a different Computable.
*
* This same thing can be accomplished using Computable.prototype.clone with new inputs and destroying the original Computable, however that destroy operation will mean recreating all of the dependees it may have. With this you can avoid that destroy and recreate process.
*
* @emits Computable#dependee_removed
* @emits Computable#dependee_added
* @param {number} index The index of the input parameter to bind.
* @param {Computable} computable The computable to use for the reference.
*/
Computable.prototype.set_input = function (index, computable) {
if (this.is_destroyed()) {
throw new Error("Cannot update inputs for a destroyed Computable");
}
if (!computable) {
throw new Error("It is illegal to unbind a reference");
}
var old_input_computable = this._inputs[index];
var is_same_computable = old_input_computable === computable;
if (is_same_computable) {
return;
}
this._validate_input(index, computable);
if (old_input_computable) {
old_input_computable.emit('dependee_removed', this, index);
}
this._inputs[index] = computable;
computable.emit('dependee_added', this, index);
};
/**
* @param {number} index The index to get the bound input Computable at.
* @returns {?Computable} The input Computable bound at the given index.
*/
Computable.prototype.get_input = function (index) {
return this._inputs[index];
};
/**
* @returns {number} The number of inputs the Computable has.
*/
Computable.prototype.get_input_count = function () {
return this._inputs.length;
};
/**
* @param {Computable} input The computable to get the input indexes of
* @returns {Array.<Number>} The indices of the given input computable
*/
Computable.prototype.get_input_indices = function(input) {
var indices = [];
var index = 0;
while ((index = this._inputs.indexOf(input, index)) !== -1) {
indices.push(index);
index++;
}
return indices;
};
/**
* @param {number} index The index to get the dependee at.
* @returns {Computable} The dependee at the given index.
*/
Computable.prototype.get_dependee = function (index) {
var dependee_entry = this._dependees[index];
return dependee_entry && dependee_entry.computable;
};
/**
* @param {number} index The index to get the dependee input indexes at.
* @returns {Array.<number>} The dependee input indexes for the dependee at the given index.
* * For example, a constant used twice by a certain Computable, would have one entry for that Computable using the constant twice, but would have two input indexes in the result from this function.
*/
Computable.prototype.get_dependee_input_indexes = function (index) {
var dependee_entry = this._dependees[index];
if (!dependee_entry) {
throw new Error("Unable to find dependee input indexes for dependee at index, " +index);
}
return dependee_entry.indexes;
};
/**
* @returns {number} The number of dependees this Computable has.
*/
Computable.prototype.get_dependee_count = function () {
return this._dependees.length;
};
/**
* @returns {IRType} The output type specified for the Computable.
*/
Computable.prototype.get_output_type = function () {
return this._output_type;
};
/**
* @param {IRType} type The output type specified for the Computable.
*/
Computable.prototype.set_output_type = function (type) {
var original_output_type = this._output_type;
this._output_type = type;
if (original_output_type.equals(this._output_type)) {
return;
}
var dependee_count = this.get_dependee_count();
for (var i = 0; i < dependee_count; i++) {
var dependee = this.get_dependee(i);
var input_indices = dependee.get_input_indices(this);
for (var j = 0; j < input_indices.length; ++j) {
dependee._reevaluate_input(input_indices[j]);
}
}
};
/**
* @param {number} index The index of the input to check whether it can cause the output to change.
* @returns {boolean} Whether the Computable will change if the input at the given index changes.
*/
Computable.prototype.is_output_updated_on_input_change = function (index) {
return false;
};
/**
* @returns {boolean} Whether the Computable may change by means outside of our control without any warning.
*/
Computable.prototype.is_output_updated_externally = function () {
return false;
};
/**
* @returns {boolean} Whether the Computable can change as the result of a mutation within the context of the framework.
*/
Computable.prototype.is_output_updated_on_mutate = function () {
return false;
};
/**
* @returns {boolean} Whether the Computable is allowed to be mutated by another Computable, it is possible that the Computable could be mutated without changing.
*/
Computable.prototype.is_mutable = function () {
return false;
};
/**
* @returns {boolean} Whether the Computable will ever change by any means after its initial value.
*/
Computable.prototype.is_invariant = function () {
if (this.is_output_updated_externally() || this.is_output_updated_on_mutate()) return false;
var input_count = this.get_input_count();
for (var i = 0; i < input_count; i++) {
if (this.is_output_updated_on_input_change(i)) return false;
}
return true;
};
/**
* @returns {boolean} Whether the Computable's initial value can be determined at compile time.
*/
Computable.prototype.is_compile_time_constant = function() {
return false;
};
/**
* @returns {boolean} Whether the Computable being present has some sort of side effect on the environment, such as displaying something to a user.
*/
Computable.prototype.is_side_effect_causing = function () {
return false;
};
/**
* @returns {boolean} Whether the Computable output must be initially resolved asynchronously.
*/
Computable.prototype.is_initially_async = function () {
return false;
};
/**
* @returns {boolean} Whether the computable is allowed to be moved from one scope to another, such as with inlining.
*/
Computable.prototype.is_immovable = function () {
return false;
};
module.exports = Computable;