-
Notifications
You must be signed in to change notification settings - Fork 192
/
environment.ts
311 lines (246 loc) · 9.09 KB
/
environment.ts
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
import { Reference, PathReference, OpaqueIterable } from '@glimmer/reference';
import { Macros, OpcodeBuilderConstructor } from '@glimmer/opcode-compiler';
import { Simple, RuntimeResolver, CompilableBlock, BlockSymbolTable } from '@glimmer/interfaces';
import { Program } from '@glimmer/program';
import { Dict, Option, Destroyable, Opaque, assert, expect } from '@glimmer/util';
import { DOMChanges, DOMTreeConstruction } from './dom/helper';
import { PublicVM } from './vm/append';
import { IArguments } from './vm/arguments';
import { UNDEFINED_REFERENCE, ConditionalReference } from './references';
import { DynamicAttribute, dynamicAttribute } from './vm/attributes/dynamic';
import { Component, ComponentManager, ModifierManager, Modifier } from './internal-interfaces';
export type ScopeBlock = [number | CompilableBlock, Scope, BlockSymbolTable];
export type BlockValue = ScopeBlock[0 | 1 | 2];
export type ScopeSlot = Option<PathReference<Opaque>> | Option<ScopeBlock>;
export interface DynamicScope {
get(key: string): PathReference<Opaque>;
set(key: string, reference: PathReference<Opaque>): PathReference<Opaque>;
child(): DynamicScope;
}
export class Scope {
static root(self: PathReference<Opaque>, size = 0) {
let refs: PathReference<Opaque>[] = new Array(size + 1);
for (let i = 0; i <= size; i++) {
refs[i] = UNDEFINED_REFERENCE;
}
return new Scope(refs, null, null, null).init({ self });
}
static sized(size = 0) {
let refs: PathReference<Opaque>[] = new Array(size + 1);
for (let i = 0; i <= size; i++) {
refs[i] = UNDEFINED_REFERENCE;
}
return new Scope(refs, null, null, null);
}
constructor(
// the 0th slot is `self`
private slots: ScopeSlot[],
private callerScope: Option<Scope>,
// named arguments and blocks passed to a layout that uses eval
private evalScope: Option<Dict<ScopeSlot>>,
// locals in scope when the partial was invoked
private partialMap: Option<Dict<PathReference<Opaque>>>
) {}
init({ self }: { self: PathReference<Opaque> }): this {
this.slots[0] = self;
return this;
}
getSelf(): PathReference<Opaque> {
return this.get<PathReference<Opaque>>(0);
}
getSymbol(symbol: number): PathReference<Opaque> {
return this.get<PathReference<Opaque>>(symbol);
}
getBlock(symbol: number): Option<ScopeBlock> {
let block = this.get(symbol);
return block === UNDEFINED_REFERENCE ? null : (block as ScopeBlock);
}
getEvalScope(): Option<Dict<ScopeSlot>> {
return this.evalScope;
}
getPartialMap(): Option<Dict<PathReference<Opaque>>> {
return this.partialMap;
}
bind(symbol: number, value: ScopeSlot) {
this.set(symbol, value);
}
bindSelf(self: PathReference<Opaque>) {
this.set<PathReference<Opaque>>(0, self);
}
bindSymbol(symbol: number, value: PathReference<Opaque>) {
this.set(symbol, value);
}
bindBlock(symbol: number, value: Option<ScopeBlock>) {
this.set<Option<ScopeBlock>>(symbol, value);
}
bindEvalScope(map: Option<Dict<ScopeSlot>>) {
this.evalScope = map;
}
bindPartialMap(map: Dict<PathReference<Opaque>>) {
this.partialMap = map;
}
bindCallerScope(scope: Option<Scope>) {
this.callerScope = scope;
}
getCallerScope(): Option<Scope> {
return this.callerScope;
}
child(): Scope {
return new Scope(this.slots.slice(), this.callerScope, this.evalScope, this.partialMap);
}
private get<T extends ScopeSlot>(index: number): T {
if (index >= this.slots.length) {
throw new RangeError(`BUG: cannot get $${index} from scope; length=${this.slots.length}`);
}
return this.slots[index] as T;
}
private set<T extends ScopeSlot>(index: number, value: T): void {
if (index >= this.slots.length) {
throw new RangeError(`BUG: cannot get $${index} from scope; length=${this.slots.length}`);
}
this.slots[index] = value;
}
}
class Transaction {
public scheduledInstallManagers: ModifierManager[] = [];
public scheduledInstallModifiers: Modifier[] = [];
public scheduledUpdateModifierManagers: ModifierManager[] = [];
public scheduledUpdateModifiers: Modifier[] = [];
public createdComponents: Component[] = [];
public createdManagers: ComponentManager[] = [];
public updatedComponents: Component[] = [];
public updatedManagers: ComponentManager[] = [];
public destructors: Destroyable[] = [];
didCreate(component: Component, manager: ComponentManager) {
this.createdComponents.push(component);
this.createdManagers.push(manager);
}
didUpdate(component: Component, manager: ComponentManager) {
this.updatedComponents.push(component);
this.updatedManagers.push(manager);
}
scheduleInstallModifier(modifier: Modifier, manager: ModifierManager) {
this.scheduledInstallManagers.push(manager);
this.scheduledInstallModifiers.push(modifier);
}
scheduleUpdateModifier(modifier: Modifier, manager: ModifierManager) {
this.scheduledUpdateModifierManagers.push(manager);
this.scheduledUpdateModifiers.push(modifier);
}
didDestroy(d: Destroyable) {
this.destructors.push(d);
}
commit() {
let { createdComponents, createdManagers } = this;
for (let i = 0; i < createdComponents.length; i++) {
let component = createdComponents[i];
let manager = createdManagers[i];
manager.didCreate(component);
}
let { updatedComponents, updatedManagers } = this;
for (let i = 0; i < updatedComponents.length; i++) {
let component = updatedComponents[i];
let manager = updatedManagers[i];
manager.didUpdate(component);
}
let { destructors } = this;
for (let i = 0; i < destructors.length; i++) {
destructors[i].destroy();
}
let { scheduledInstallManagers, scheduledInstallModifiers } = this;
for (let i = 0; i < scheduledInstallManagers.length; i++) {
let manager = scheduledInstallManagers[i];
let modifier = scheduledInstallModifiers[i];
manager.install(modifier);
}
let { scheduledUpdateModifierManagers, scheduledUpdateModifiers } = this;
for (let i = 0; i < scheduledUpdateModifierManagers.length; i++) {
let manager = scheduledUpdateModifierManagers[i];
let modifier = scheduledUpdateModifiers[i];
manager.update(modifier);
}
}
}
export interface CompilationOptions<Locator, R extends RuntimeResolver<Locator>> {
resolver: R;
program: Program<Locator>;
macros: Macros;
Builder: OpcodeBuilderConstructor;
}
export interface EnvironmentOptions {
appendOperations: DOMTreeConstruction;
updateOperations: DOMChanges;
}
export abstract class Environment {
protected updateOperations: DOMChanges;
protected appendOperations: DOMTreeConstruction;
private _transaction: Option<Transaction> = null;
constructor({ appendOperations, updateOperations }: EnvironmentOptions) {
this.appendOperations = appendOperations;
this.updateOperations = updateOperations;
}
toConditionalReference(reference: Reference): Reference<boolean> {
return new ConditionalReference(reference);
}
abstract iterableFor(reference: Reference, key: Opaque): OpaqueIterable;
abstract protocolForURL(s: string): string;
getAppendOperations(): DOMTreeConstruction {
return this.appendOperations;
}
getDOM(): DOMChanges {
return this.updateOperations;
}
begin() {
assert(
!this._transaction,
'A glimmer transaction was begun, but one already exists. You may have a nested transaction, possibly caused by an earlier runtime exception while rendering. Please check your console for the stack trace of any prior exceptions.'
);
this._transaction = new Transaction();
}
private get transaction(): Transaction {
return expect(this._transaction!, 'must be in a transaction');
}
didCreate(component: Component, manager: ComponentManager) {
this.transaction.didCreate(component, manager);
}
didUpdate(component: Component, manager: ComponentManager) {
this.transaction.didUpdate(component, manager);
}
scheduleInstallModifier(modifier: Modifier, manager: ModifierManager) {
this.transaction.scheduleInstallModifier(modifier, manager);
}
scheduleUpdateModifier(modifier: Modifier, manager: ModifierManager) {
this.transaction.scheduleUpdateModifier(modifier, manager);
}
didDestroy(d: Destroyable) {
this.transaction.didDestroy(d);
}
commit() {
let transaction = this.transaction;
this._transaction = null;
transaction.commit();
}
attributeFor(
element: Simple.Element,
attr: string,
_isTrusting: boolean,
namespace: Option<Simple.AttrNamespace> = null
): DynamicAttribute {
return dynamicAttribute(element, attr, namespace);
}
}
export abstract class DefaultEnvironment extends Environment {
constructor(options?: EnvironmentOptions) {
if (!options) {
let document = window.document as Simple.Document;
let appendOperations = new DOMTreeConstruction(document);
let updateOperations = new DOMChanges(document);
options = { appendOperations, updateOperations };
}
super(options);
}
}
export default Environment;
export interface Helper {
(vm: PublicVM, args: IArguments): PathReference<Opaque>;
}