From ec622342df5b7f3e9bb6510fd7650cfbdd429629 Mon Sep 17 00:00:00 2001 From: sayomaki Date: Wed, 23 Aug 2023 12:01:26 +0800 Subject: [PATCH 1/3] Fix invalid array access for stepper steps * Resolves bug where no execution steps will lead to access error --- src/stepper/stepper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stepper/stepper.ts b/src/stepper/stepper.ts index 96b6806de..97e958234 100644 --- a/src/stepper/stepper.ts +++ b/src/stepper/stepper.ts @@ -3250,7 +3250,7 @@ export function getEvaluationSteps( reducedWithPath = reduceMain(reducedWithPath[0], context) i += 2 } - if (!limitExceeded) { + if (!limitExceeded && steps.length > 0) { steps[steps.length - 1][2] = 'Evaluation complete' } return steps From 229e7c5e71911a3478629f59fb26b6b6e2b57ca0 Mon Sep 17 00:00:00 2001 From: sayomaki Date: Wed, 23 Aug 2023 12:03:58 +0800 Subject: [PATCH 2/3] Add evaluation message to indicate no evaluation * Returns empty step with message --- src/stepper/stepper.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/stepper/stepper.ts b/src/stepper/stepper.ts index 97e958234..844962c19 100644 --- a/src/stepper/stepper.ts +++ b/src/stepper/stepper.ts @@ -3253,6 +3253,9 @@ export function getEvaluationSteps( if (!limitExceeded && steps.length > 0) { steps[steps.length - 1][2] = 'Evaluation complete' } + if (steps.length === 0) { + steps.push([reducedWithPath[0] as es.Program, [], 'Nothing to evaluate']) + } return steps } catch (error) { context.errors.push(error) From 7a65a50a29b06a1951c4d5946e21524d0b9b609a Mon Sep 17 00:00:00 2001 From: sayomaki Date: Wed, 23 Aug 2023 17:34:36 +0800 Subject: [PATCH 3/3] Test evaluation of empty programs in stepper --- src/stepper/__tests__/__snapshots__/stepper.ts.snap | 2 ++ src/stepper/__tests__/stepper.ts | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/stepper/__tests__/__snapshots__/stepper.ts.snap b/src/stepper/__tests__/__snapshots__/stepper.ts.snap index cbd102e94..2bdcdbed5 100644 --- a/src/stepper/__tests__/__snapshots__/stepper.ts.snap +++ b/src/stepper/__tests__/__snapshots__/stepper.ts.snap @@ -2095,6 +2095,8 @@ false ? 1 : 100 * factorial(100 - 1); " `; +exports[`Evaluation of empty code and imports Evaluate empty program 1`] = `""`; + exports[`Infinite recursion 1`] = ` "function f() { return f(); diff --git a/src/stepper/__tests__/stepper.ts b/src/stepper/__tests__/stepper.ts index f70f9c0f9..d66ec2603 100644 --- a/src/stepper/__tests__/stepper.ts +++ b/src/stepper/__tests__/stepper.ts @@ -1468,6 +1468,16 @@ describe(`#1342: Test the fix of #1341: Stepper limit off by one`, () => { }) }) +describe(`Evaluation of empty code and imports`, () => { + test('Evaluate empty program', () => { + const code = `` + const program = parse(code, mockContext())! + const steps = getEvaluationSteps(program, mockContext(), 1000) + expect(steps.map(x => codify(x[0])).join('\n')).toMatchSnapshot() + expect(getLastStepAsString(steps)).toEqual('') + }) +}) + // describe(`#1223: Stepper: Import statements cause errors`, () => { // test('import a module and invoke its functions', () => { // const code = `