-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathenvironment.ts
342 lines (279 loc) · 9.86 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
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
import { Statement as StatementSyntax } from './syntax';
import SymbolTable from './symbol-table';
import * as Simple from './dom/interfaces';
import { DOMChanges, DOMTreeConstruction } from './dom/helper';
import { Reference, PathReference, OpaqueIterable } from 'glimmer-reference';
import { UNDEFINED_REFERENCE, ConditionalReference } from './references';
import {
defaultChangeLists,
IChangeList
} from './dom/change-lists';
import {
PartialDefinition
} from './partial';
import {
Component,
ComponentManager,
ComponentDefinition
} from './component/interfaces';
import {
ModifierManager
} from './modifier/interfaces';
import {
Destroyable,
Opaque,
HasGuid,
ensureGuid
} from 'glimmer-util';
import {
BlockMeta
} from 'glimmer-wire-format';
import { EvaluatedArgs } from './compiled/expressions/args';
import { InlineBlock } from './compiled/blocks';
import * as Syntax from './syntax/core';
import IfSyntax from './syntax/builtins/if';
import UnlessSyntax from './syntax/builtins/unless';
import WithSyntax from './syntax/builtins/with';
import EachSyntax from './syntax/builtins/each';
import PartialSyntax from './syntax/builtins/partial';
import { PublicVM } from './vm/append';
type ScopeSlot = PathReference<Opaque> | InlineBlock;
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).init({ self });
}
// the 0th slot is `self`
private slots: ScopeSlot[];
private callerScope: Scope = null;
constructor(references: ScopeSlot[], callerScope: Scope = null) {
this.slots = references;
this.callerScope = callerScope;
}
init({ self }: { self: PathReference<Opaque> }): this {
this.slots[0] = self;
return this;
}
getSelf(): PathReference<Opaque> {
return this.slots[0] as PathReference<Opaque>;
}
getSymbol(symbol: number): PathReference<Opaque> {
return this.slots[symbol] as PathReference<Opaque>;
}
getBlock(symbol: number): InlineBlock {
return this.slots[symbol] as InlineBlock;
}
bindSymbol(symbol: number, value: PathReference<Opaque>) {
this.slots[symbol] = value;
}
bindBlock(symbol: number, value: InlineBlock) {
this.slots[symbol] = value;
}
bindCallerScope(scope: Scope) {
this.callerScope = scope;
}
getCallerScope(): Scope {
return this.callerScope;
}
child(): Scope {
return new Scope(this.slots.slice(), this.callerScope);
}
}
export abstract class Environment {
protected updateOperations: DOMChanges;
protected appendOperations: DOMTreeConstruction;
private scheduledInstallManagers: ModifierManager<Opaque>[] = null;
private scheduledInstallModifiers: Object[] = null;
private scheduledUpdateModifierManagers: ModifierManager<Opaque>[] = null;
private scheduledUpdateModifiers: Object[] = null;
private createdComponents: Component[] = null;
private createdManagers: ComponentManager<Component>[] = null;
private updatedComponents: Component[] = null;
private updatedManagers: ComponentManager<Component>[] = null;
private destructors: Destroyable[] = null;
constructor({ appendOperations, updateOperations }: { appendOperations: DOMTreeConstruction, updateOperations: DOMChanges }) {
this.appendOperations = appendOperations;
this.updateOperations = updateOperations;
}
toConditionalReference(reference: Reference<Opaque>): Reference<boolean> {
return new ConditionalReference(reference);
}
abstract iterableFor(reference: Reference<Opaque>, args: EvaluatedArgs): OpaqueIterable;
abstract protocolForURL(s: string): string;
getAppendOperations(): DOMTreeConstruction { return this.appendOperations; }
getDOM(): DOMChanges { return this.updateOperations; }
getIdentity(object: HasGuid): string {
return ensureGuid(object) + '';
}
statement(statement: StatementSyntax, symbolTable: SymbolTable): StatementSyntax {
return this.refineStatement(parseStatement(statement), symbolTable) || statement;
}
protected refineStatement(statement: ParsedStatement, symbolTable: SymbolTable): StatementSyntax {
let {
isSimple,
isBlock,
isInline,
key,
args,
templates
} = statement;
if (isSimple && isInline) {
if (key === 'partial') {
return new PartialSyntax({ args, symbolTable });
}
}
if (isSimple && isBlock) {
switch (key) {
case 'each':
return new EachSyntax({ args, templates });
case 'if':
return new IfSyntax({ args, templates });
case 'with':
return new WithSyntax({ args, templates });
case 'unless':
return new UnlessSyntax({ args, templates });
}
}
}
begin() {
this.createdComponents = [];
this.createdManagers = [];
this.updatedComponents = [];
this.updatedManagers = [];
this.destructors = [];
this.scheduledInstallManagers = [];
this.scheduledInstallModifiers = [];
this.scheduledUpdateModifierManagers = [];
this.scheduledUpdateModifiers = [];
}
didCreate<T>(component: T, manager: ComponentManager<T>) {
this.createdComponents.push(component as any);
this.createdManagers.push(manager as any);
}
didUpdate<T>(component: T, manager: ComponentManager<T>) {
this.updatedComponents.push(component as any);
this.updatedManagers.push(manager as any);
}
scheduleInstallModifier<T>(modifier: T, manager: ModifierManager<T>) {
this.scheduledInstallManagers.push(manager);
this.scheduledInstallModifiers.push(modifier);
}
scheduleUpdateModifier<T>(modifier: T, manager: ModifierManager<T>) {
this.scheduledUpdateModifierManagers.push(manager);
this.scheduledUpdateModifiers.push(modifier);
}
didDestroy(d: Destroyable) {
this.destructors.push(d);
}
commit() {
for (let i=0; i<this.createdComponents.length; i++) {
let component = this.createdComponents[i];
let manager = this.createdManagers[i];
manager.didCreate(component);
}
for (let i=0; i<this.updatedComponents.length; i++) {
let component = this.updatedComponents[i];
let manager = this.updatedManagers[i];
manager.didUpdate(component);
}
for (let i=0; i<this.destructors.length; i++) {
this.destructors[i].destroy();
}
for (let i = 0; i < this.scheduledInstallManagers.length; i++) {
let manager = this.scheduledInstallManagers[i];
let modifier = this.scheduledInstallModifiers[i];
manager.install(modifier);
}
for (let i = 0; i < this.scheduledUpdateModifierManagers.length; i++) {
let manager = this.scheduledUpdateModifierManagers[i];
let modifier = this.scheduledUpdateModifiers[i];
manager.update(modifier);
}
this.createdComponents = null;
this.createdManagers = null;
this.updatedComponents = null;
this.updatedManagers = null;
this.destructors = null;
this.scheduledInstallManagers = null;
this.scheduledInstallModifiers = null;
this.scheduledUpdateModifierManagers = null;
this.scheduledUpdateModifiers = null;
}
abstract hasHelper(helperName: string[], blockMeta: BlockMeta): boolean;
abstract lookupHelper(helperName: string[], blockMeta: BlockMeta): Helper;
attributeFor(element: Simple.Element, attr: string, isTrusting: boolean, namespace?: string): IChangeList {
return defaultChangeLists(element, attr, isTrusting, namespace);
}
abstract hasPartial(partialName: string[], symbolTable: SymbolTable): boolean;
abstract lookupPartial(PartialName: string[], symbolTable: SymbolTable): PartialDefinition;
abstract hasComponentDefinition(tagName: string[], symbolTable: SymbolTable): boolean;
abstract getComponentDefinition(tagName: string[], symbolTable: SymbolTable): ComponentDefinition<Opaque>;
abstract hasModifier(modifierName: string[], blockMeta: BlockMeta): boolean;
abstract lookupModifier(modifierName: string[], blockMeta: BlockMeta): ModifierManager<Opaque>;
}
export default Environment;
export interface Helper {
(vm: PublicVM, args: EvaluatedArgs, symbolTable: SymbolTable): PathReference<Opaque>;
}
export interface ParsedStatement {
isSimple: boolean;
path: string[];
key: string;
appendType: string;
args: Syntax.Args;
isInline: boolean;
isBlock: boolean;
isModifier: boolean;
templates: Syntax.Templates;
original: StatementSyntax;
}
function parseStatement(statement: StatementSyntax): ParsedStatement {
let type = statement.type;
let block = type === 'block' ? <Syntax.Block>statement : null;
let append = type === 'optimized-append' ? <Syntax.OptimizedAppend>statement : null;
let modifier = type === 'modifier' ? <Syntax.Modifier>statement : null;
let appendType = append && append.value.type;
type AppendValue = Syntax.Unknown | Syntax.Get;
let args: Syntax.Args;
let path: string[];
if (block) {
args = block.args;
path = block.path;
} else if (append && (appendType === 'unknown' || appendType === 'get')) {
let appendValue = <AppendValue>append.value;
args = Syntax.Args.empty();
path = appendValue.ref.path();
} else if (append && append.value.type === 'helper') {
let helper = <Syntax.Helper>append.value;
args = helper.args;
path = helper.ref.path();
} else if (modifier) {
path = modifier.path;
args = modifier.args;
}
let key: string, isSimple: boolean;
if (path) {
isSimple = path.length === 1;
key = path[0];
}
return {
isSimple,
path,
key,
args,
appendType,
original: statement,
isInline: !!append,
isBlock: !!block,
isModifier: !!modifier,
templates: block && block.templates
};
}