scratchfoundation / scratchfoundation/scratch-editor

Stackframe is not cleared in `retireThread`

Open
#685 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
281
Forks
276
Avg merge
7d 8h
Merged PRs (30d)
6

Description

Expected Behavior

  • Thread.stackFrames should be cleared in Sequencer.retireThread.
  • It should be checked whether the thread is done before invoking Thread.goToNextBlock.

Actual Behavior

In scratch-vm, Thread.stackFrames are not cleared correctly in Sequencer.retireThread due to typo problems.
https://github.com/scratchfoundation/scratch-editor/blob/a54b373f5e265283ff533d277f00329f9dd8ea03/packages/scratch-vm/src/engine/sequencer.js#L353-L358

It should be this.stackFrames = [];.

After fixing this typo problem, it will lead to another problem: project with edge-activated hat blocks will raise error in reuseStackForNextBlock. (It can be verified in test/integration/stack-click.js)

TypeError: Cannot read properties of undefined (reading 'reuse')

      246 |     reuseStackForNextBlock (blockId: string | null): void {
      247 |         this.stack[this.stack.length - 1] = blockId;
    > 248 |         this.stackFrames[this.stackFrames.length - 1].reuse();
          |                                                       ^
      249 |     }
      250 |
      251 |     /**

      at Thread.reuse [as reuseStackForNextBlock] (src/engine/thread.ts:248:55)
      at Thread.reuseStackForNextBlock [as goToNextBlock] (src/engine/thread.ts:380:14)
      at goToNextBlock (src/engine/runtime.js:2189:24)
          at Array.forEach (<anonymous>)
      at Runtime.forEach [as startHats] (src/engine/runtime.js:2186:20)
      at Runtime.startHats [as _step] (src/engine/runtime.js:2398:22)
      at Timeout._step [as _onTimeout] (src/engine/runtime.js:2973:18)

The problem is caused by Runtime.startHats. After executing the thread, it's status might be Thread.STATUS_DONE and the thread might have been retired, then thread.stack and thread.stackFrame should be empty array. Then function reuseStackForNextBlock will access this.stack[-1] and this.stackFrames[-1], and try to call undefined.reuse(), which will break the program.

https://github.com/scratchfoundation/scratch-editor/blob/a54b373f5e265283ff533d277f00329f9dd8ea03/packages/scratch-vm/src/engine/runtime.js#L1927-L1930

Previously, since this.stackFrames is not cleared and javascript allows assignment to array[-1], vm looks like running well.

Steps to Reproduce

  1. Replace this.stackFrame = []; with this.stackFrames = []; in function Sequencer.retireThread.
  2. Run test case test/integration/stack-click.js.

Screenshots

None.

System Details

None.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in packages/scratch-vm/src/engine/sequencer.js at Sequencer.retireThread and inspect the related flow in packages/scratch-vm/src/engine/runtime.js and Thread.goToNextBlock. Run test/integration/stack-click.js after correcting the stack-frame handling and checking the thread status before advancing. Done means the test passes without the reuseStackForNextBlock undefined error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.