refactor(compiler-vapor): move `templateIndex` to dynamic

This commit is contained in:
三咲智子 Kevin Deng 2024-02-22 11:46:40 +08:00
parent 0e0ee5b85e
commit 004edd3bac
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
9 changed files with 42 additions and 28 deletions

View File

@ -44,7 +44,9 @@ describe('compiler: v-for', () => {
index: undefined, index: undefined,
render: { render: {
type: IRNodeTypes.BLOCK, type: IRNodeTypes.BLOCK,
templateIndex: 0, dynamic: {
template: 0,
},
}, },
keyProperty: { keyProperty: {
type: NodeTypes.SIMPLE_EXPRESSION, type: NodeTypes.SIMPLE_EXPRESSION,

View File

@ -43,7 +43,9 @@ describe('compiler: v-if', () => {
}, },
positive: { positive: {
type: IRNodeTypes.BLOCK, type: IRNodeTypes.BLOCK,
templateIndex: 0, dynamic: {
template: 0,
},
}, },
}, },
]) ])
@ -125,11 +127,15 @@ describe('compiler: v-if', () => {
}, },
positive: { positive: {
type: IRNodeTypes.BLOCK, type: IRNodeTypes.BLOCK,
templateIndex: 0, dynamic: {
template: 0,
},
}, },
negative: { negative: {
type: IRNodeTypes.BLOCK, type: IRNodeTypes.BLOCK,
templateIndex: 1, dynamic: {
template: 1,
},
}, },
}, },
]) ])
@ -154,7 +160,9 @@ describe('compiler: v-if', () => {
}, },
positive: { positive: {
type: IRNodeTypes.BLOCK, type: IRNodeTypes.BLOCK,
templateIndex: 0, dynamic: {
template: 0,
},
}, },
negative: { negative: {
type: IRNodeTypes.IF, type: IRNodeTypes.IF,
@ -165,7 +173,9 @@ describe('compiler: v-if', () => {
}, },
positive: { positive: {
type: IRNodeTypes.BLOCK, type: IRNodeTypes.BLOCK,
templateIndex: 1, dynamic: {
template: 1,
},
}, },
}, },
}, },
@ -187,17 +197,23 @@ describe('compiler: v-if', () => {
id: 1, id: 1,
positive: { positive: {
type: IRNodeTypes.BLOCK, type: IRNodeTypes.BLOCK,
templateIndex: 0, dynamic: {
template: 0,
},
}, },
negative: { negative: {
type: IRNodeTypes.IF, type: IRNodeTypes.IF,
positive: { positive: {
type: IRNodeTypes.BLOCK, type: IRNodeTypes.BLOCK,
templateIndex: 1, dynamic: {
template: 1,
},
}, },
negative: { negative: {
type: IRNodeTypes.BLOCK, type: IRNodeTypes.BLOCK,
templateIndex: 2, dynamic: {
template: 2,
},
}, },
}, },
}, },

View File

@ -31,16 +31,13 @@ export function genBlockFunction(
} }
export function genBlockFunctionContent( export function genBlockFunctionContent(
{ dynamic, effect, operation, templateIndex, returns }: BlockIRNode, { dynamic, effect, operation, returns }: BlockIRNode,
context: CodegenContext, context: CodegenContext,
customReturns?: (returns: CodeFragment[]) => CodeFragment[], customReturns?: (returns: CodeFragment[]) => CodeFragment[],
): CodeFragment[] { ): CodeFragment[] {
const [frag, push] = buildCodeFragment() const [frag, push] = buildCodeFragment()
if (templateIndex > -1) {
push(NEWLINE, `const n${dynamic.id} = t${templateIndex}()`)
push(...genChildren(dynamic, context, dynamic.id!)) push(...genChildren(dynamic, context, dynamic.id!))
}
const directiveOps = operation.filter( const directiveOps = operation.filter(
(oper): oper is WithDirectiveIRNode => (oper): oper is WithDirectiveIRNode =>

View File

@ -23,7 +23,11 @@ export function genChildren(
const { vaporHelper } = context const { vaporHelper } = context
const [frag, push] = buildCodeFragment() const [frag, push] = buildCodeFragment()
let offset = 0 let offset = 0
const { children } = dynamic const { children, id, template } = dynamic
if (id !== undefined && template !== undefined) {
push(NEWLINE, `const n${id} = t${template}()`)
}
for (const [index, child] of children.entries()) { for (const [index, child] of children.entries()) {
if (child.flags & DynamicFlag.NON_TEMPLATE) { if (child.flags & DynamicFlag.NON_TEMPLATE) {
@ -36,11 +40,11 @@ export function genChildren(
? child.flags & DynamicFlag.INSERT ? child.flags & DynamicFlag.INSERT
? child.anchor ? child.anchor
: child.id : child.id
: null : undefined
const newPaths = [...paths, elementIndex] const newPaths = [...paths, elementIndex]
if (id !== null) { if (id !== undefined) {
push( push(
NEWLINE, NEWLINE,
`const n${id} = `, `const n${id} = `,

View File

@ -45,7 +45,6 @@ export type VaporHelper = keyof typeof import('@vue/runtime-vapor')
export interface BlockIRNode extends BaseIRNode { export interface BlockIRNode extends BaseIRNode {
type: IRNodeTypes.BLOCK type: IRNodeTypes.BLOCK
node: RootNode | TemplateChildNode node: RootNode | TemplateChildNode
templateIndex: number
dynamic: IRDynamicInfo dynamic: IRDynamicInfo
effect: IREffect[] effect: IREffect[]
operation: OperationNode[] operation: OperationNode[]
@ -205,10 +204,11 @@ export enum DynamicFlag {
} }
export interface IRDynamicInfo { export interface IRDynamicInfo {
id: number | null id?: number
flags: DynamicFlag flags: DynamicFlag
anchor: number | null anchor?: number
children: IRDynamicInfo[] children: IRDynamicInfo[]
template?: number
} }
export interface IREffect { export interface IREffect {

View File

@ -139,7 +139,7 @@ function createRootContext(
increaseId: () => globalId++, increaseId: () => globalId++,
reference() { reference() {
if (this.dynamic.id !== null) return this.dynamic.id if (this.dynamic.id !== undefined) return this.dynamic.id
this.dynamic.flags |= DynamicFlag.REFERENCED this.dynamic.flags |= DynamicFlag.REFERENCED
return (this.dynamic.id = this.increaseId()) return (this.dynamic.id = this.increaseId())
}, },
@ -181,11 +181,11 @@ function createRootContext(
template => template === this.template, template => template === this.template,
) )
if (existing !== -1) { if (existing !== -1) {
return (this.block.templateIndex = existing) return (this.dynamic.template = existing)
} }
root.template.push(this.template) root.template.push(this.template)
return (this.block.templateIndex = root.template.length - 1) return (this.dynamic.template = root.template.length - 1)
}, },
registerOperation(...node) { registerOperation(...node) {
this.block.operation.push(...node) this.block.operation.push(...node)
@ -225,7 +225,6 @@ export function transform(
block: { block: {
type: IRNodeTypes.BLOCK, type: IRNodeTypes.BLOCK,
node: root, node: root,
templateIndex: -1,
dynamic: extend(genDefaultDynamic(), { dynamic: extend(genDefaultDynamic(), {
flags: DynamicFlag.REFERENCED, flags: DynamicFlag.REFERENCED,
} satisfies Partial<IRDynamicInfo>), } satisfies Partial<IRDynamicInfo>),

View File

@ -12,9 +12,7 @@ import { extend } from '@vue/shared'
import { DynamicFlag, type IRDynamicInfo } from '../ir' import { DynamicFlag, type IRDynamicInfo } from '../ir'
export const genDefaultDynamic = (): IRDynamicInfo => ({ export const genDefaultDynamic = (): IRDynamicInfo => ({
id: null,
flags: DynamicFlag.NONE, flags: DynamicFlag.NONE,
anchor: null,
children: [], children: [],
}) })

View File

@ -53,7 +53,6 @@ export function processFor(
const render: BlockIRNode = { const render: BlockIRNode = {
type: IRNodeTypes.BLOCK, type: IRNodeTypes.BLOCK,
node, node,
templateIndex: -1,
dynamic: extend(genDefaultDynamic(), { dynamic: extend(genDefaultDynamic(), {
flags: DynamicFlag.REFERENCED, flags: DynamicFlag.REFERENCED,
} satisfies Partial<IRDynamicInfo>), } satisfies Partial<IRDynamicInfo>),

View File

@ -146,7 +146,6 @@ export function createIfBranch(
const branch: BlockIRNode = { const branch: BlockIRNode = {
type: IRNodeTypes.BLOCK, type: IRNodeTypes.BLOCK,
node, node,
templateIndex: -1,
dynamic: extend(genDefaultDynamic(), { dynamic: extend(genDefaultDynamic(), {
flags: DynamicFlag.REFERENCED, flags: DynamicFlag.REFERENCED,
} satisfies Partial<IRDynamicInfo>), } satisfies Partial<IRDynamicInfo>),