-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathvariable.js
219 lines (191 loc) · 8.45 KB
/
variable.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
import {map} from "./array.js";
import {constant} from "./constant.js";
import {RuntimeError} from "./errors.js";
import {identity} from "./identity.js";
import {noop} from "./noop.js";
export const TYPE_NORMAL = 1; // a normal variable
export const TYPE_IMPLICIT = 2; // created on reference
export const TYPE_DUPLICATE = 3; // created on duplicate definition
export const no_observer = Symbol("no-observer");
export const no_value = Promise.resolve();
export function Variable(type, module, observer, options) {
if (!observer) observer = no_observer;
Object.defineProperties(this, {
_observer: {value: observer, writable: true},
_definition: {value: variable_undefined, writable: true},
_duplicate: {value: undefined, writable: true},
_duplicates: {value: undefined, writable: true},
_indegree: {value: NaN, writable: true}, // The number of computing inputs.
_inputs: {value: [], writable: true},
_invalidate: {value: noop, writable: true},
_module: {value: module},
_name: {value: null, writable: true},
_outputs: {value: new Set, writable: true},
_promise: {value: no_value, writable: true},
_reachable: {value: observer !== no_observer, writable: true}, // Is this variable transitively visible?
_rejector: {value: variable_rejector(this)},
_shadow: {value: initShadow(module, options)},
_type: {value: type},
_value: {value: undefined, writable: true},
_version: {value: 0, writable: true}
});
}
Object.defineProperties(Variable.prototype, {
_pending: {value: variable_pending, writable: true, configurable: true},
_fulfilled: {value: variable_fulfilled, writable: true, configurable: true},
_rejected: {value: variable_rejected, writable: true, configurable: true},
_resolve: {value: variable_resolve, writable: true, configurable: true},
define: {value: variable_define, writable: true, configurable: true},
delete: {value: variable_delete, writable: true, configurable: true},
import: {value: variable_import, writable: true, configurable: true}
});
function initShadow(module, options) {
if (!options?.shadow) return null;
return new Map(
Object.entries(options.shadow)
.map(([name, definition]) => [name, (new Variable(TYPE_IMPLICIT, module)).define([], definition)])
);
}
function variable_attach(variable) {
variable._module._runtime._dirty.add(variable);
variable._outputs.add(this);
}
function variable_detach(variable) {
variable._module._runtime._dirty.add(variable);
variable._outputs.delete(this);
}
function variable_undefined() {
throw variable_undefined;
}
export function variable_stale() {
throw variable_stale;
}
function variable_rejector(variable) {
return (error) => {
if (error === variable_stale) throw error;
if (error === variable_undefined) throw new RuntimeError(`${variable._name} is not defined`, variable._name);
if (error instanceof Error && error.message) throw new RuntimeError(error.message, variable._name);
throw new RuntimeError(`${variable._name} could not be resolved`, variable._name);
};
}
function variable_duplicate(name) {
return () => {
throw new RuntimeError(`${name} is defined more than once`);
};
}
function variable_define(name, inputs, definition) {
switch (arguments.length) {
case 1: {
definition = name, name = inputs = null;
break;
}
case 2: {
definition = inputs;
if (typeof name === "string") inputs = null;
else inputs = name, name = null;
break;
}
}
return variable_defineImpl.call(this,
name == null ? null : String(name),
inputs == null ? [] : map.call(inputs, this._resolve, this),
typeof definition === "function" ? definition : constant(definition)
);
}
function variable_resolve(name) {
return this._shadow?.get(name) ?? this._module._resolve(name);
}
function variable_defineImpl(name, inputs, definition) {
const scope = this._module._scope, runtime = this._module._runtime;
this._inputs.forEach(variable_detach, this);
inputs.forEach(variable_attach, this);
this._inputs = inputs;
this._definition = definition;
this._value = undefined;
// Is this an active variable (that may require disposal)?
if (definition === noop) runtime._variables.delete(this);
else runtime._variables.add(this);
// Did the variable’s name change? Time to patch references!
if (name !== this._name || scope.get(name) !== this) {
let error, found;
if (this._name) { // Did this variable previously have a name?
if (this._outputs.size) { // And did other variables reference this variable?
scope.delete(this._name);
found = this._module._resolve(this._name);
found._outputs = this._outputs, this._outputs = new Set;
found._outputs.forEach(function(output) { output._inputs[output._inputs.indexOf(this)] = found; }, this);
found._outputs.forEach(runtime._updates.add, runtime._updates);
runtime._dirty.add(found).add(this);
scope.set(this._name, found);
} else if ((found = scope.get(this._name)) === this) { // Do no other variables reference this variable?
scope.delete(this._name); // It’s safe to delete!
} else if (found._type === TYPE_DUPLICATE) { // Do other variables assign this name?
found._duplicates.delete(this); // This variable no longer assigns this name.
this._duplicate = undefined;
if (found._duplicates.size === 1) { // Is there now only one variable assigning this name?
found = found._duplicates.keys().next().value; // Any references are now fixed!
error = scope.get(this._name);
found._outputs = error._outputs, error._outputs = new Set;
found._outputs.forEach(function(output) { output._inputs[output._inputs.indexOf(error)] = found; });
found._definition = found._duplicate, found._duplicate = undefined;
runtime._dirty.add(error).add(found);
runtime._updates.add(found);
scope.set(this._name, found);
}
} else {
throw new Error;
}
}
if (this._outputs.size) throw new Error;
if (name) { // Does this variable have a new name?
if (found = scope.get(name)) { // Do other variables reference or assign this name?
if (found._type === TYPE_DUPLICATE) { // Do multiple other variables already define this name?
this._definition = variable_duplicate(name), this._duplicate = definition;
found._duplicates.add(this);
} else if (found._type === TYPE_IMPLICIT) { // Are the variable references broken?
this._outputs = found._outputs, found._outputs = new Set; // Now they’re fixed!
this._outputs.forEach(function(output) { output._inputs[output._inputs.indexOf(found)] = this; }, this);
runtime._dirty.add(found).add(this);
scope.set(name, this);
} else { // Does another variable define this name?
found._duplicate = found._definition, this._duplicate = definition; // Now they’re duplicates.
error = new Variable(TYPE_DUPLICATE, this._module);
error._name = name;
error._definition = this._definition = found._definition = variable_duplicate(name);
error._outputs = found._outputs, found._outputs = new Set;
error._outputs.forEach(function(output) { output._inputs[output._inputs.indexOf(found)] = error; });
error._duplicates = new Set([this, found]);
runtime._dirty.add(found).add(error);
runtime._updates.add(found).add(error);
scope.set(name, error);
}
} else {
scope.set(name, this);
}
}
this._name = name;
}
// If this redefined variable was previously evaluated, invalidate it. (If the
// variable was never evaluated, then the invalidated value could never have
// been exposed and we can avoid this extra work.)
if (this._version > 0) ++this._version;
runtime._updates.add(this);
runtime._compute();
return this;
}
function variable_import(remote, name, module) {
if (arguments.length < 3) module = name, name = remote;
return variable_defineImpl.call(this, String(name), [module._resolve(String(remote))], identity);
}
function variable_delete() {
return variable_defineImpl.call(this, null, [], noop);
}
function variable_pending() {
if (this._observer.pending) this._observer.pending();
}
function variable_fulfilled(value) {
if (this._observer.fulfilled) this._observer.fulfilled(value, this._name);
}
function variable_rejected(error) {
if (this._observer.rejected) this._observer.rejected(error, this._name);
}