Skip to content

Commit a5f57d9

Browse files
committed
Checkpoint
1 parent 3dafadd commit a5f57d9

File tree

22 files changed

+819
-412
lines changed

22 files changed

+819
-412
lines changed

packages/@glimmer-workspace/integration-tests/test/compiler/compile-options-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ module('[glimmer-compiler] precompile', ({ test }) => {
167167

168168
let block: WireFormat.SerializedTemplateBlock = JSON.parse(wire.block);
169169

170-
let [[, componentNameExpr]] = block[0] as [WireFormat.Content.ResolvedBlock];
170+
let [[, componentNameExpr]] = block[0] as [WireFormat.Content.InvokeResolvedBlockComponent];
171171

172172
localAssert(
173173
Array.isArray(componentNameExpr) && componentNameExpr[0] === Op.ResolveAsComponentCallee,

packages/@glimmer/compiler/lib/builder/builder.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import type { RequireAtLeastOne, Simplify } from 'type-fest';
33
import { localAssert } from '@glimmer/debug-util';
44
import { LOCAL_DEBUG } from '@glimmer/local-debug-flags';
55
import { dict, values } from '@glimmer/util';
6-
import { SexpOpcodes as Op } from '@glimmer/wire-format';
6+
import {
7+
BLOCKS_OPCODE,
8+
EMPTY_ARGS_OPCODE,
9+
NAMED_ARGS_AND_BLOCKS_OPCODE,
10+
NAMED_ARGS_OPCODE,
11+
SexpOpcodes as Op,
12+
} from '@glimmer/wire-format';
713

814
export interface Symbols {
915
top: ProgramSymbols;
@@ -146,13 +152,35 @@ export interface BuilderGetFree {
146152
export function buildComponentArgs(
147153
splattributes: Optional<WireFormat.Core.Splattributes>,
148154
hash: Optional<WireFormat.Core.Hash>,
149-
blocks: Optional<WireFormat.Core.Blocks>
150-
): Optional<WireFormat.Core.ComponentArgs> {
151-
return compact({
152-
splattributes,
153-
hash,
154-
blocks,
155-
});
155+
componentBlocks: Optional<WireFormat.Core.Blocks>
156+
): WireFormat.Core.BlockArgs {
157+
const blocks = combineSplattributes(componentBlocks, splattributes);
158+
159+
if (hash && blocks) {
160+
return [NAMED_ARGS_AND_BLOCKS_OPCODE, hash, blocks];
161+
} else if (hash) {
162+
return [NAMED_ARGS_OPCODE, hash];
163+
} else if (blocks) {
164+
return [BLOCKS_OPCODE, blocks];
165+
} else {
166+
return [EMPTY_ARGS_OPCODE];
167+
}
168+
}
169+
170+
function combineSplattributes(
171+
blocks: Optional<WireFormat.Core.Blocks>,
172+
splattributes: Optional<WireFormat.Core.Splattributes>
173+
): Optional<WireFormat.Core.Blocks> {
174+
if (blocks && splattributes) {
175+
return [
176+
[...blocks[0], 'attrs'],
177+
[...blocks[1], [splattributes, []] satisfies WireFormat.SerializedInlineBlock],
178+
];
179+
} else if (splattributes) {
180+
return [['attrs'], [[splattributes, []] satisfies WireFormat.SerializedInlineBlock]];
181+
} else {
182+
return blocks;
183+
}
156184
}
157185

158186
type CompactObject<T> = Simplify<

packages/@glimmer/compiler/lib/builder/test-support/test-support.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ import {
4141
import { exhausted, expect, isPresentArray, localAssert, unreachable } from '@glimmer/debug-util';
4242
import { assertNever } from '@glimmer/util';
4343
import {
44+
BLOCKS_OPCODE,
4445
EMPTY_ARGS_OPCODE,
46+
NAMED_ARGS_AND_BLOCKS_OPCODE,
4547
NAMED_ARGS_OPCODE,
48+
POSITIONAL_AND_BLOCKS_OPCODE,
49+
POSITIONAL_AND_NAMED_ARGS_AND_BLOCKS_OPCODE,
4650
POSITIONAL_AND_NAMED_ARGS_OPCODE,
4751
POSITIONAL_ARGS_OPCODE,
4852
SexpOpcodes as Op,
@@ -68,8 +72,7 @@ import type {
6872
} from './builder-interface';
6973

7074
import { compactSexpr } from '../../passes/2-encoding/content';
71-
import { callArgs } from '../../passes/2-encoding/expressions';
72-
import { compact, isGet, isInvokeResolved, needsAtNames } from '../builder';
75+
import { isGet, isInvokeResolved, needsAtNames } from '../builder';
7376
import { normalizeStatement } from './builder-interface';
7477

7578
export function buildStatements(
@@ -158,6 +161,7 @@ export function buildStatement(
158161
}
159162

160163
return [[Op.InvokeDynamicBlock, path, args]];
164+
// ^?
161165
}
162166

163167
case KEYWORD_HEAD: {
@@ -183,7 +187,7 @@ export function buildAppendCautiously(
183187
): WireFormat.Content.SomeAppend | WireFormat.Content.AppendHtmlText {
184188
if (Array.isArray(expr)) {
185189
if (expr[0] === Op.ResolveAsAppendableCallee) {
186-
return [Op.AppendResolvedInvokable, expr[1]];
190+
return [Op.AppendResolvedInvokable, expr[1], [EMPTY_ARGS_OPCODE]];
187191
} else if (isInvokeResolved(expr)) {
188192
const [, callee, args] = expr;
189193

@@ -410,17 +414,17 @@ export function buildExpression(
410414
);
411415

412416
if (builtExpr[0] === Op.CallResolved) {
413-
return [builtExpr[0], builtExpr[1], buildArgs(builtParams, builtHash)];
417+
return [builtExpr[0], builtExpr[1], buildCallArgs(builtParams, builtHash)];
414418
}
415419

416420
if (builtExpr.length === 2) {
417421
switch (builtExpr[0]) {
418422
case Op.ResolveAsAppendableCallee:
419-
return [Op.CallResolved, builtExpr[1], buildArgs(builtParams, builtHash)];
423+
return [Op.CallResolved, builtExpr[1], buildCallArgs(builtParams, builtHash)];
420424
}
421425
}
422426

423-
return [Op.CallDynamicValue, builtExpr, buildArgs(builtParams, builtHash)];
427+
return [Op.CallDynamicValue, builtExpr, buildCallArgs(builtParams, builtHash)];
424428
}
425429

426430
case HAS_BLOCK_EXPR: {
@@ -666,15 +670,27 @@ export function buildBlockArgs(
666670
rawHash: Optional<WireFormat.Core.Hash>,
667671
blocks: Optional<WireFormat.Core.Blocks>,
668672
{ path }: { path: WireFormat.Core.Expression }
669-
): Optional<WireFormat.Core.BlockArgs> {
673+
): WireFormat.Core.BlockArgs {
674+
if (!params && !rawHash && !blocks) {
675+
return [EMPTY_ARGS_OPCODE];
676+
}
677+
670678
const hash: Optional<WireFormat.Core.Hash> =
671679
isGet(path) && needsAtNames(path) ? addAtNames(rawHash) : rawHash;
672680

673-
return compact({
674-
params,
675-
hash,
676-
blocks,
677-
});
681+
if (!blocks) {
682+
return buildCallArgs(params, hash);
683+
}
684+
685+
if (params && hash) {
686+
return [POSITIONAL_AND_NAMED_ARGS_AND_BLOCKS_OPCODE, params, hash, blocks];
687+
} else if (params) {
688+
return [POSITIONAL_AND_BLOCKS_OPCODE, params, blocks];
689+
} else if (hash) {
690+
return [NAMED_ARGS_AND_BLOCKS_OPCODE, hash, blocks];
691+
} else {
692+
return [BLOCKS_OPCODE, blocks];
693+
}
678694
}
679695

680696
function addAtNames(hash: Optional<WireFormat.Core.Hash>): Optional<WireFormat.Core.Hash> {
@@ -685,7 +701,7 @@ function addAtNames(hash: Optional<WireFormat.Core.Hash>): Optional<WireFormat.C
685701
return [keys.map((key) => `@${key}`) as PresentArray<string>, values];
686702
}
687703

688-
function buildArgs(
704+
function buildCallArgs(
689705
params: Optional<WireFormat.Core.Params>,
690706
hash: Optional<WireFormat.Core.Hash>
691707
): WireFormat.Core.CallArgs {

packages/@glimmer/compiler/lib/passes/2-encoding/content.ts

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ import type { BlockSymbolTable } from '@glimmer/syntax';
1515
import { assertPresentArray, exhausted, localAssert } from '@glimmer/debug-util';
1616
import { LOCAL_DEBUG, LOCAL_TRACE_LOGGING } from '@glimmer/local-debug-flags';
1717
import { LOCAL_LOGGER } from '@glimmer/util';
18-
import { isGetLexical, SexpOpcodes as Op } from '@glimmer/wire-format';
18+
import { EMPTY_ARGS_OPCODE, isGetLexical, SexpOpcodes as Op } from '@glimmer/wire-format';
1919

2020
import type { OptionalList } from '../../shared/list';
2121
import type * as mir from './mir';
2222

2323
import {
2424
buildComponentArgs,
25-
compact,
2625
isGet,
2726
isGetSymbolOrPath,
2827
isTupleExpression,
29-
needsAtNames,
3028
} from '../../builder/builder';
3129
import { deflateAttrName, deflateTagName } from '../../utils';
3230
import {
31+
callArgs,
3332
encodeArgs,
33+
encodeComponentBlockArgs,
3434
encodeExpr,
3535
encodeMaybeExpr,
3636
encodeNamedArguments,
@@ -166,28 +166,23 @@ export function InvokeBlockComponent({
166166
return [
167167
Op.InvokeLexicalBlockComponent,
168168
encodeExpr(head),
169-
BlockArgs(args, blocks, { insertAtPrefix: needsAtNames(path) }),
169+
// @todo verify whether insertAtPrefix is variable here
170+
encodeComponentBlockArgs(args.positional, args.named, blocks),
170171
];
171172
} else if (head.type === 'Resolved') {
172173
return [
173174
Op.InvokeResolvedBlockComponent,
174175
encodeExpr(head),
175-
BlockArgs(args, blocks, { insertAtPrefix: needsAtNames(path) }),
176+
// @todo verify whether insertAtPrefix is variable here
177+
encodeComponentBlockArgs(args.positional, args.named, blocks),
176178
];
177179
}
178180

179-
return [Op.InvokeDynamicBlock, path, BlockArgs(args, blocks, { insertAtPrefix: true })];
180-
}
181-
182-
export function BlockArgs(
183-
argsNode: Pick<mir.Args, 'positional' | 'named'>,
184-
blocksNode: Optional<mir.NamedBlocks>,
185-
{ insertAtPrefix }: { insertAtPrefix: boolean }
186-
): Optional<WireFormat.Core.BlockArgs> {
187-
return compact({
188-
...encodeArgs(argsNode, insertAtPrefix),
189-
blocks: blocksNode ? NamedBlocks(blocksNode) : undefined,
190-
});
181+
return [
182+
Op.InvokeDynamicBlock,
183+
path,
184+
encodeComponentBlockArgs(args.positional, args.named, blocks),
185+
];
191186
}
192187

193188
export function AppendTrustedHTML({
@@ -201,7 +196,7 @@ export function AppendValueCautiously({
201196
}: mir.AppendValueCautiously): WireFormat.Content.SomeAppend {
202197
switch (value.type) {
203198
case 'Resolved': {
204-
return [Op.AppendResolvedInvokable, value.symbol];
199+
return [Op.AppendResolvedInvokable, value.symbol, [EMPTY_ARGS_OPCODE]];
205200
}
206201
case 'CallExpression': {
207202
const args = encodeArgs(value.args);
@@ -212,7 +207,11 @@ export function AppendValueCautiously({
212207
throw new Error(`BUG: Resolved ${value.callee.name} is not appendable`);
213208
}
214209

215-
return [Op.AppendResolvedInvokable, value.callee.symbol, args];
210+
return [
211+
Op.AppendResolvedInvokable,
212+
value.callee.symbol,
213+
callArgs(value.args.positional, value.args.named),
214+
];
216215
}
217216

218217
case 'Local': {
@@ -275,12 +274,12 @@ export function AngleBracketComponent({
275274
blocks,
276275
}: mir.AngleBracketComponent): WireFormat.Content.SomeInvokeComponent {
277276
let wireTag = encodeExpr(tag);
278-
let wirePositional = ElementParameters(params);
279-
let wireNamed = encodeNamedArguments(named, false);
277+
let wireSplattributes = ElementParameters(params);
278+
let wireNamed = encodeNamedArguments(named, { insertAtPrefix: false });
280279

281280
let wireNamedBlocks = NamedBlocks(blocks);
282281

283-
const args = buildComponentArgs(wirePositional.toPresentArray(), wireNamed, wireNamedBlocks);
282+
const args = buildComponentArgs(wireSplattributes.toPresentArray(), wireNamed, wireNamedBlocks);
284283

285284
localAssert(
286285
isTupleExpression(wireTag),
@@ -412,7 +411,7 @@ export function WithDynamicVars({
412411
}: mir.WithDynamicVars): WireFormat.Content.WithDynamicVars {
413412
return [
414413
Op.WithDynamicVars,
415-
encodeNamedArguments(named, false),
414+
encodeNamedArguments(named, { insertAtPrefix: false }),
416415
namedBlock(block.body, block.scope),
417416
];
418417
}
@@ -429,15 +428,23 @@ export function InvokeComponentKeyword({
429428
`[BUG] {{component <KW>}} is not a valid node`
430429
);
431430

432-
return [Op.InvokeComponentKeyword, expression, BlockArgs(args, blocks, { insertAtPrefix: true })];
431+
return [
432+
Op.InvokeComponentKeyword,
433+
expression,
434+
encodeComponentBlockArgs(args.positional, args.named, blocks),
435+
];
433436
}
434437

435438
export function InvokeResolvedComponentKeyword({
436439
definition,
437440
args,
438441
blocks,
439442
}: mir.InvokeResolvedComponentKeyword): WireFormat.Content.InvokeComponentKeyword {
440-
return [Op.InvokeComponentKeyword, definition, BlockArgs(args, blocks, { insertAtPrefix: true })];
443+
return [
444+
Op.InvokeComponentKeyword,
445+
definition,
446+
encodeComponentBlockArgs(args.positional, args.named, blocks),
447+
];
441448
}
442449

443450
export type StaticAttrArgs = [name: string | WellKnownAttrName, value: string, namespace?: string];

0 commit comments

Comments
 (0)