-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathexpressions.ts
338 lines (281 loc) · 9.45 KB
/
expressions.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
import type {
CapturedPositionalArguments,
CurriedType,
Helper,
HelperDefinitionState,
Initializable,
ScopeBlock,
} from '@glimmer/interfaces';
import type { Reference } from '@glimmer/reference';
import {
CURRIED_HELPER,
decodeHandle,
VM_CONCAT_OP,
VM_CURRY_OP,
VM_DYNAMIC_HELPER_OP,
VM_GET_BLOCK_OP,
VM_GET_DYNAMIC_VAR_OP,
VM_GET_PROPERTY_OP,
VM_GET_VARIABLE_OP,
VM_HAS_BLOCK_OP,
VM_HAS_BLOCK_PARAMS_OP,
VM_HELPER_OP,
VM_IF_INLINE_OP,
VM_LOG_OP,
VM_NOT_OP,
VM_ROOT_SCOPE_OP,
VM_SET_BLOCK_OP,
VM_SET_VARIABLE_OP,
VM_SPREAD_BLOCK_OP,
} from '@glimmer/constants';
import {
check,
CheckBlockSymbolTable,
CheckHandle,
CheckMaybe,
CheckNullable,
CheckOr,
} from '@glimmer/debug';
import { debugToString, localAssert } from '@glimmer/debug-util';
import { _hasDestroyableChildren, associateDestroyableChild, destroy } from '@glimmer/destroyable';
import { debugAssert, toBool } from '@glimmer/global-context';
import { getInternalHelperManager } from '@glimmer/manager';
import {
childRefFor,
createComputeRef,
FALSE_REFERENCE,
TRUE_REFERENCE,
UNDEFINED_REFERENCE,
valueForRef,
} from '@glimmer/reference';
import { assign, isIndexable } from '@glimmer/util';
import { $v0 } from '@glimmer/vm';
import { isCurriedType, resolveCurriedValue } from '../../curried-value';
import { APPEND_OPCODES } from '../../opcodes';
import createCurryRef from '../../references/curry-value';
import { reifyPositional } from '../../vm/arguments';
import { createConcatRef } from '../expressions/concat';
import {
CheckArguments,
CheckCapturedArguments,
CheckCompilableBlock,
CheckHelper,
CheckReference,
CheckScope,
CheckScopeBlock,
CheckUndefinedReference,
} from './-debug-strip';
APPEND_OPCODES.add(VM_CURRY_OP, (vm, { op1: type, op2: _isStrict }) => {
let stack = vm.stack;
let definition = check(stack.pop(), CheckReference);
let capturedArgs = check(stack.pop(), CheckCapturedArguments);
let owner = vm.getOwner();
let resolver = vm.context.resolver;
let isStrict = false;
if (import.meta.env.DEV) {
// strict check only happens in import.meta.env.DEV builds, no reason to load it otherwise
isStrict = vm.constants.getValue<boolean>(decodeHandle(_isStrict));
}
vm.loadValue(
$v0,
createCurryRef(type as CurriedType, definition, owner, capturedArgs, resolver, isStrict)
);
});
APPEND_OPCODES.add(VM_DYNAMIC_HELPER_OP, (vm) => {
let stack = vm.stack;
let ref = check(stack.pop(), CheckReference);
let args = check(stack.pop(), CheckArguments).capture();
let helperRef: Initializable<Reference>;
let initialOwner = vm.getOwner();
let helperInstanceRef = createComputeRef(() => {
if (helperRef !== undefined) {
destroy(helperRef);
}
let definition = valueForRef(ref);
if (isCurriedType(definition, CURRIED_HELPER)) {
let { definition: resolvedDef, owner, positional, named } = resolveCurriedValue(definition);
let helper = resolveHelper(resolvedDef, ref);
if (named !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
args.named = assign({}, ...named, args.named);
}
if (positional !== undefined) {
args.positional = positional.concat(args.positional) as CapturedPositionalArguments;
}
helperRef = helper(args, owner);
associateDestroyableChild(helperInstanceRef, helperRef);
} else if (isIndexable(definition)) {
let helper = resolveHelper(definition, ref);
helperRef = helper(args, initialOwner);
if (_hasDestroyableChildren(helperRef)) {
associateDestroyableChild(helperInstanceRef, helperRef);
}
} else {
helperRef = UNDEFINED_REFERENCE;
}
});
let helperValueRef = createComputeRef(() => {
valueForRef(helperInstanceRef);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- @fixme
return valueForRef(helperRef!);
});
vm.associateDestroyable(helperInstanceRef);
vm.loadValue($v0, helperValueRef);
});
function resolveHelper(definition: HelperDefinitionState, ref: Reference): Helper {
let managerOrHelper = getInternalHelperManager(definition, true);
let helper;
if (managerOrHelper === null) {
helper = null;
} else {
helper =
typeof managerOrHelper === 'function'
? managerOrHelper
: managerOrHelper.getHelper(definition);
localAssert(managerOrHelper, 'BUG: expected manager or helper');
}
debugAssert(
helper !== null,
() =>
`Expected a dynamic helper definition, but received an object or function that did not have a helper manager associated with it. The dynamic invocation was \`{{${
ref.debugLabel
}}}\` or \`(${ref.debugLabel})\`, and the incorrect definition is the value at the path \`${
ref.debugLabel
}\`, which was: ${debugToString?.(definition)}`
);
return helper;
}
APPEND_OPCODES.add(VM_HELPER_OP, (vm, { op1: handle }) => {
let stack = vm.stack;
let helper = check(vm.constants.getValue(handle), CheckHelper);
let args = check(stack.pop(), CheckArguments);
let value = helper(args.capture(), vm.getOwner(), vm.dynamicScope());
if (_hasDestroyableChildren(value)) {
vm.associateDestroyable(value);
}
vm.loadValue($v0, value);
});
APPEND_OPCODES.add(VM_GET_VARIABLE_OP, (vm, { op1: symbol }) => {
let expr = vm.referenceForSymbol(symbol);
vm.stack.push(expr);
});
APPEND_OPCODES.add(VM_SET_VARIABLE_OP, (vm, { op1: symbol }) => {
let expr = check(vm.stack.pop(), CheckReference);
vm.scope().bindSymbol(symbol, expr);
});
APPEND_OPCODES.add(VM_SET_BLOCK_OP, (vm, { op1: symbol }) => {
let handle = check(vm.stack.pop(), CheckCompilableBlock);
let scope = check(vm.stack.pop(), CheckScope);
let table = check(vm.stack.pop(), CheckBlockSymbolTable);
vm.scope().bindBlock(symbol, [handle, scope, table]);
});
APPEND_OPCODES.add(VM_ROOT_SCOPE_OP, (vm, { op1: size }) => {
vm.pushRootScope(size, vm.getOwner());
});
APPEND_OPCODES.add(VM_GET_PROPERTY_OP, (vm, { op1: _key }) => {
let key = vm.constants.getValue<string>(_key);
let expr = check(vm.stack.pop(), CheckReference);
vm.stack.push(childRefFor(expr, key));
});
APPEND_OPCODES.add(VM_GET_BLOCK_OP, (vm, { op1: _block }) => {
let { stack } = vm;
let block = vm.scope().getBlock(_block);
stack.push(block);
});
APPEND_OPCODES.add(VM_SPREAD_BLOCK_OP, (vm) => {
let { stack } = vm;
let block = check(stack.pop(), CheckNullable(CheckOr(CheckScopeBlock, CheckUndefinedReference)));
if (block && !isUndefinedReference(block)) {
let [handleOrCompilable, scope, table] = block;
stack.push(table);
stack.push(scope);
stack.push(handleOrCompilable);
} else {
stack.push(null);
stack.push(null);
stack.push(null);
}
});
function isUndefinedReference(input: ScopeBlock | Reference): input is Reference {
localAssert(
Array.isArray(input) || input === UNDEFINED_REFERENCE,
'a reference other than UNDEFINED_REFERENCE is illegal here'
);
return input === UNDEFINED_REFERENCE;
}
APPEND_OPCODES.add(VM_HAS_BLOCK_OP, (vm) => {
let { stack } = vm;
let block = check(stack.pop(), CheckNullable(CheckOr(CheckScopeBlock, CheckUndefinedReference)));
if (block && !isUndefinedReference(block)) {
stack.push(TRUE_REFERENCE);
} else {
stack.push(FALSE_REFERENCE);
}
});
APPEND_OPCODES.add(VM_HAS_BLOCK_PARAMS_OP, (vm) => {
// FIXME(mmun): should only need to push the symbol table
let block = vm.stack.pop();
let scope = vm.stack.pop();
check(block, CheckMaybe(CheckOr(CheckHandle, CheckCompilableBlock)));
check(scope, CheckMaybe(CheckScope));
let table = check(vm.stack.pop(), CheckMaybe(CheckBlockSymbolTable));
let hasBlockParams = table && table.parameters.length;
vm.stack.push(hasBlockParams ? TRUE_REFERENCE : FALSE_REFERENCE);
});
APPEND_OPCODES.add(VM_CONCAT_OP, (vm, { op1: count }) => {
let out = new Array<Reference>(count);
for (let i = count; i > 0; i--) {
let offset = i - 1;
out[offset] = check(vm.stack.pop(), CheckReference);
}
vm.stack.push(createConcatRef(out));
});
APPEND_OPCODES.add(VM_IF_INLINE_OP, (vm) => {
let condition = check(vm.stack.pop(), CheckReference);
let truthy = check(vm.stack.pop(), CheckReference);
let falsy = check(vm.stack.pop(), CheckReference);
vm.stack.push(
createComputeRef(() => {
// Explicitly consume the condition reference to ensure tracking
let conditionValue = valueForRef(condition);
// Also explicitly consume truthy and falsy references to ensure proper tracking
// when they are component references
let truthyValue = valueForRef(truthy);
let falsyValue = valueForRef(falsy);
if (toBool(conditionValue)) {
return truthyValue;
} else {
return falsyValue;
}
})
);
});
APPEND_OPCODES.add(VM_NOT_OP, (vm) => {
let ref = check(vm.stack.pop(), CheckReference);
vm.stack.push(
createComputeRef(() => {
return !toBool(valueForRef(ref));
})
);
});
APPEND_OPCODES.add(VM_GET_DYNAMIC_VAR_OP, (vm) => {
let scope = vm.dynamicScope();
let stack = vm.stack;
let nameRef = check(stack.pop(), CheckReference);
stack.push(
createComputeRef(() => {
let name = String(valueForRef(nameRef));
return valueForRef(scope.get(name));
})
);
});
APPEND_OPCODES.add(VM_LOG_OP, (vm) => {
let { positional } = check(vm.stack.pop(), CheckArguments).capture();
vm.loadValue(
$v0,
createComputeRef(() => {
// eslint-disable-next-line no-console
console.log(...reifyPositional(positional));
})
);
});