refactor(compiler-vapor): required returns

This commit is contained in:
三咲智子 Kevin Deng 2024-02-22 00:18:10 +08:00
parent 66c0e82d4b
commit fa0ca8a5af
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
9 changed files with 44 additions and 38 deletions

View File

@ -164,7 +164,7 @@ exports[`compile > dynamic root 1`] = `
export function render(_ctx) { export function render(_ctx) {
const n1 = _createTextNode() const n1 = _createTextNode()
_setText(n1, 1, 2) _setText(n1, 1, 2)
return [n1] return n1
}" }"
`; `;
@ -186,7 +186,7 @@ exports[`compile > expression parsing > interpolation 1`] = `
"(() => { "(() => {
const n1 = _createTextNode() const n1 = _createTextNode()
_renderEffect(() => _setText(n1, a + b.value)) _renderEffect(() => _setText(n1, a + b.value))
return [n1] return n1
})()" })()"
`; `;
@ -215,7 +215,7 @@ exports[`compile > static + dynamic root 1`] = `
export function render(_ctx) { export function render(_ctx) {
const n1 = _createTextNode() const n1 = _createTextNode()
_setText(n1, 1, 2, "3", 4, 5, "6", 7, 8, "9", 'A', 'B') _setText(n1, 1, 2, "3", 4, 5, "6", 7, 8, "9", 'A', 'B')
return [n1] return n1
}" }"
`; `;

View File

@ -16,7 +16,7 @@ export function render(_ctx) {
_renderEffect(_updateEffect) _renderEffect(_updateEffect)
return [n2, _updateEffect] return [n2, _updateEffect]
}, (item) => (item.id)) }, (item) => (item.id))
return [n1] return n1
}" }"
`; `;
@ -36,7 +36,7 @@ export function render(_ctx) {
_renderEffect(_updateEffect) _renderEffect(_updateEffect)
return [n2, _updateEffect] return [n2, _updateEffect]
}) })
return [n1] return n1
}" }"
`; `;
@ -49,6 +49,6 @@ export function render(_ctx) {
const n2 = t0() const n2 = t0()
return [n2, () => {}] return [n2, () => {}]
}) })
return [n1] return n1
}" }"
`; `;

View File

@ -11,7 +11,7 @@ export function render(_ctx) {
_renderEffect(() => _setText(n3, _ctx.msg)) _renderEffect(() => _setText(n3, _ctx.msg))
return n2 return n2
}) })
return [n1] return n1
}" }"
`; `;
@ -69,7 +69,7 @@ export function render(_ctx) {
_renderEffect(() => _setText(n3, _ctx.msg)) _renderEffect(() => _setText(n3, _ctx.msg))
return n2 return n2
}) })
return [n1] return n1
}" }"
`; `;
@ -86,7 +86,7 @@ export function render(_ctx) {
const n3 = t1() const n3 = t1()
return n3 return n3
}) })
return [n1] return n1
}" }"
`; `;
@ -107,7 +107,7 @@ export function render(_ctx) {
const n4 = t2() const n4 = t2()
return n4 return n4
})) }))
return [n1] return n1
}" }"
`; `;
@ -124,6 +124,6 @@ export function render(_ctx) {
const n3 = t1() const n3 = t1()
return n3 return n3
})) }))
return [n1] return n1
}" }"
`; `;

View File

@ -21,14 +21,14 @@ export function genBlockFunction(
oper: BlockFunctionIRNode, oper: BlockFunctionIRNode,
context: CodegenContext, context: CodegenContext,
args: CodeFragment[] = [], args: CodeFragment[] = [],
returnValue?: () => CodeFragment[], customReturns?: (returns: CodeFragment[]) => CodeFragment[],
): CodeFragment[] { ): CodeFragment[] {
return [ return [
'(', '(',
...args, ...args,
') => {', ') => {',
INDENT_START, INDENT_START,
...genBlockFunctionContent(oper, context, returnValue), ...genBlockFunctionContent(oper, context, customReturns),
INDENT_END, INDENT_END,
NEWLINE, NEWLINE,
'}', '}',
@ -36,18 +36,24 @@ export function genBlockFunction(
} }
export function genBlockFunctionContent( export function genBlockFunctionContent(
ir: BlockFunctionIRNode | RootIRNode, {
dynamic,
effect,
operation,
templateIndex,
returns,
}: BlockFunctionIRNode | RootIRNode,
context: CodegenContext, context: CodegenContext,
returnValue?: () => CodeFragment[], customReturns?: (returns: CodeFragment[]) => CodeFragment[],
): CodeFragment[] { ): CodeFragment[] {
const [frag, push] = buildCodeFragment() const [frag, push] = buildCodeFragment()
if (ir.templateIndex > -1) { if (templateIndex > -1) {
push(NEWLINE, `const n${ir.dynamic.id} = t${ir.templateIndex}()`) push(NEWLINE, `const n${dynamic.id} = t${templateIndex}()`)
push(...genChildren(ir.dynamic, context, ir.dynamic.id!)) push(...genChildren(dynamic, context, dynamic.id!))
} }
const directiveOps = ir.operation.filter( const directiveOps = operation.filter(
(oper): oper is WithDirectiveIRNode => (oper): oper is WithDirectiveIRNode =>
oper.type === IRNodeTypes.WITH_DIRECTIVE, oper.type === IRNodeTypes.WITH_DIRECTIVE,
) )
@ -55,22 +61,16 @@ export function genBlockFunctionContent(
push(...genWithDirective(directives, context)) push(...genWithDirective(directives, context))
} }
push(...genOperations(ir.operation, context)) push(...genOperations(operation, context))
push(...(context.genEffect || genEffects)(ir.effect, context)) push(...(context.genEffect || genEffects)(effect, context))
if (ir.returns) { push(NEWLINE, `return `)
push(
NEWLINE, const returnsCode: CodeFragment[] =
`return `, returns.length > 1
...genMulti(['[', ']', ', '], ...ir.returns.map(n => `n${n}`)), ? genMulti(['[', ']', ', '], ...returns.map(n => `n${n}`))
) : [`n${returns[0]}`]
} else { push(...(customReturns ? customReturns(returnsCode) : returnsCode))
push(
NEWLINE,
'return ',
...(returnValue ? returnValue() : [`n${ir.dynamic.id}`]),
)
}
return frag return frag
} }

View File

@ -31,12 +31,14 @@ export function genFor(
if (rawValue) idMap[rawValue] = `_block.s[0]` if (rawValue) idMap[rawValue] = `_block.s[0]`
if (rawKey) idMap[rawKey] = `_block.s[1]` if (rawKey) idMap[rawKey] = `_block.s[1]`
const blockRet = (): CodeFragment[] => [ const blockReturns = (returns: CodeFragment[]): CodeFragment[] => [
`[n${render.dynamic.id!}, ${updateFn}]`, '[',
...returns,
`, ${updateFn}]`,
] ]
const blockFn = context.withId( const blockFn = context.withId(
() => genBlockFunction(render, context, ['_block'], blockRet), () => genBlockFunction(render, context, ['_block'], blockReturns),
idMap, idMap,
) )

View File

@ -49,7 +49,7 @@ export interface BlockFunctionIRNode extends BaseIRNode {
dynamic: IRDynamicInfo dynamic: IRDynamicInfo
effect: IREffect[] effect: IREffect[]
operation: OperationNode[] operation: OperationNode[]
returns?: number[] returns: number[]
} }
export interface RootIRNode extends Omit<BlockFunctionIRNode, 'type'> { export interface RootIRNode extends Omit<BlockFunctionIRNode, 'type'> {

View File

@ -228,6 +228,7 @@ export function transform(
} satisfies Partial<IRDynamicInfo>), } satisfies Partial<IRDynamicInfo>),
effect: [], effect: [],
operation: [], operation: [],
returns: [],
} }
const context = createRootContext(ir, root, options) const context = createRootContext(ir, root, options)
@ -327,6 +328,7 @@ function processDynamicChildren(
} }
// mixed: insert with anchor // mixed: insert with anchor
context.block.returns = [context.dynamic.id!]
for (const [index, child] of children.entries()) { for (const [index, child] of children.entries()) {
if (child.flags & DynamicFlag.INSERT) { if (child.flags & DynamicFlag.INSERT) {
prevDynamics.push(child) prevDynamics.push(child)

View File

@ -59,6 +59,7 @@ export function processFor(
} satisfies Partial<IRDynamicInfo>), } satisfies Partial<IRDynamicInfo>),
effect: [], effect: [],
operation: [], operation: [],
returns: [],
} }
const exitBlock = context.enterBlock(render) const exitBlock = context.enterBlock(render)
context.reference() context.reference()

View File

@ -152,6 +152,7 @@ export function createIfBranch(
} satisfies Partial<IRDynamicInfo>), } satisfies Partial<IRDynamicInfo>),
effect: [], effect: [],
operation: [], operation: [],
returns: [],
} }
const exitBlock = context.enterBlock(branch) const exitBlock = context.enterBlock(branch)