Skip to content

Commit

Permalink
Fix stepper: infinite loop in findMain
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziwen510 committed Aug 7, 2023
1 parent b8a7b7d commit d19dfd8
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/stepper/stepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ function findMain(
| es.BlockStatement
| BlockExpression
| es.FunctionDeclaration
| es.Program
| es.Program,
seenBefore: Map<substituterNodes, substituterNodes>
): string[] {
const params: string[] = []
if (
Expand All @@ -194,7 +195,6 @@ function findMain(
}

const freeNames: any[] = []
const seenBefore: Map<substituterNodes, substituterNodes> = new Map()

const finders = {
Identifier(target: es.Identifier): void {
Expand Down Expand Up @@ -250,7 +250,7 @@ function findMain(

FunctionDeclaration(target: es.FunctionDeclaration): void {
seenBefore.set(target, target)
const freeInNested = findMain(target)
const freeInNested = findMain(target, seenBefore)
for (const free of freeInNested) {
let bound = false
for (const param of params) {
Expand All @@ -266,7 +266,7 @@ function findMain(

ArrowFunctionExpression(target: es.ArrowFunctionExpression): void {
seenBefore.set(target, target)
const freeInNested = findMain(target)
const freeInNested = findMain(target, seenBefore)
for (const free of freeInNested) {
let bound = false
for (const param of params) {
Expand Down Expand Up @@ -585,10 +585,10 @@ function substituteMain(
replacement.type == 'FunctionExpression' ||
replacement.type == 'ArrowFunctionExpression'
) {
freeReplacement = findMain(replacement)
freeReplacement = findMain(replacement, new Map())
boundReplacement = scanOutBoundNames(replacement.body)
}
const freeTarget = findMain(target)
const freeTarget = findMain(target, new Map())
const boundTarget = scanOutBoundNames(target.body)
for (let i = 0; i < target.params.length; i++) {
const param = target.params[i]
Expand Down Expand Up @@ -664,10 +664,10 @@ function substituteMain(
replacement.type == 'FunctionExpression' ||
replacement.type == 'ArrowFunctionExpression'
) {
freeReplacement = findMain(replacement)
freeReplacement = findMain(replacement, new Map())
boundReplacement = scanOutBoundNames(replacement.body)
}
const freeTarget = findMain(target)
const freeTarget = findMain(target, new Map())
const boundTarget = scanOutBoundNames(target.body)
for (let i = 0; i < target.params.length; i++) {
const param = target.params[i]
Expand Down Expand Up @@ -741,9 +741,9 @@ function substituteMain(
replacement.type == 'ArrowFunctionExpression') &&
!re.test(name.name)
) {
const freeTarget: string[] = findMain(target)
const freeTarget: string[] = findMain(target, new Map())
const declaredIds: es.Identifier[] = scanOutDeclarations(target)
const freeReplacement: string[] = findMain(replacement)
const freeReplacement: string[] = findMain(replacement, new Map())
const boundReplacement: es.Identifier[] = scanOutDeclarations(replacement.body)
for (const declaredId of declaredIds) {
if (freeReplacement.includes(declaredId.name)) {
Expand Down Expand Up @@ -826,9 +826,9 @@ function substituteMain(
replacement.type == 'ArrowFunctionExpression') &&
!re.test(name.name)
) {
const freeTarget: string[] = findMain(target)
const freeTarget: string[] = findMain(target, new Map())
const declaredIds: es.Identifier[] = scanOutDeclarations(target)
const freeReplacement: string[] = findMain(replacement)
const freeReplacement: string[] = findMain(replacement, new Map())
const boundReplacement: es.Identifier[] = scanOutDeclarations(replacement.body)
for (const declaredId of declaredIds) {
if (freeReplacement.includes(declaredId.name)) {
Expand Down Expand Up @@ -911,9 +911,9 @@ function substituteMain(
replacement.type == 'ArrowFunctionExpression') &&
!re.test(name.name)
) {
const freeTarget: string[] = findMain(target)
const freeTarget: string[] = findMain(target, new Map())
const declaredIds: es.Identifier[] = scanOutDeclarations(target)
const freeReplacement: string[] = findMain(replacement)
const freeReplacement: string[] = findMain(replacement, new Map())
const boundReplacement: es.Identifier[] = scanOutDeclarations(replacement.body)
for (const declaredId of declaredIds) {
if (freeReplacement.includes(declaredId.name)) {
Expand Down Expand Up @@ -1015,7 +1015,7 @@ function substituteMain(
replacement.type == 'FunctionExpression' ||
replacement.type == 'ArrowFunctionExpression'
) {
freeReplacement = findMain(replacement)
freeReplacement = findMain(replacement, new Map())
boundReplacement = scanOutBoundNames(replacement.body)
}
for (let i = 0; i < target.params.length; i++) {
Expand All @@ -1025,7 +1025,7 @@ function substituteMain(
substedArrow.expression = target.body.type !== 'BlockStatement'
return substedArrow
}
const freeTarget = findMain(target)
const freeTarget = findMain(target, new Map())
const boundTarget = scanOutBoundNames(target.body)
if (param.type == 'Identifier') {
if (freeReplacement.includes(param.name)) {
Expand Down Expand Up @@ -1232,9 +1232,12 @@ function apply(
const arg = args[i]

if (arg.type === 'ArrowFunctionExpression' || arg.type === 'FunctionExpression') {
const freeTarget: string[] = findMain(ast.arrowFunctionExpression(substedParams, substedBody))
const freeTarget: string[] = findMain(
ast.arrowFunctionExpression(substedParams, substedBody),
new Map()
)
const declaredIds: es.Identifier[] = substedParams as es.Identifier[]
const freeReplacement: string[] = findMain(arg)
const freeReplacement: string[] = findMain(arg, new Map())
const boundReplacement: es.Identifier[] = scanOutDeclarations(arg.body)
for (const declaredId of declaredIds) {
if (freeReplacement.includes(declaredId.name)) {
Expand Down

0 comments on commit d19dfd8

Please sign in to comment.