Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple gosubs in sequence mess up return locations #184

Open
bishoprook opened this issue Jan 12, 2022 · 1 comment
Open

Multiple gosubs in sequence mess up return locations #184

bishoprook opened this issue Jan 12, 2022 · 1 comment

Comments

@bishoprook
Copy link

To reproduce, run the following script:

level1:
  echo Entering level1.
  gosub level2
  echo Exiting level1.
  exit

level2:
  echo Entering level2.
  gosub level3
  echo Exiting level2.
  return
  echo ERROR: Overflow in level2.
  exit

level3:
  echo Entering level3.
  gosub level4
  gosub level4
  echo Exiting level3.
  return
  echo ERROR: Overflow in level3.
  exit

level4:
  echo Entering level4.
  echo Exiting level4.
  return
  echo ERROR: Overflow in level4.
  exit

The expected output should be:

Entering level1.
Entering level2.
Entering level3.
Entering level4.
Exiting level4.
Entering level4.
Exiting level4.
Exiting level3.
Exiting level2.
Exiting level1.

The actual output (with debugging enabled) is:

[test_sub_nesting(1)]: debuglevel 1
[test_sub_nesting(3)]: passing label level1
Entering level1.
[test_sub_nesting(5)]: gosub level2 
[test_sub_nesting(9)]: passing label level2
Entering level2.
[test_sub_nesting(13)]: gosub level3 
[test_sub_nesting(19)]: passing label level3
Entering level3.
[test_sub_nesting(23)]: gosub level4 
[test_sub_nesting(32)]: passing label level4
Entering level4.
Exiting level4.
[test_sub_nesting(35)]: returning to line 23
[test_sub_nesting(26)]: gosub level4 
[test_sub_nesting(32)]: passing label level4
Entering level4.
Exiting level4.
[test_sub_nesting(35)]: returning to line 26
Exiting level3.
[test_sub_nesting(28)]: returning to line 13
Exiting level2.
[test_sub_nesting(15)]: returning to line 13
ERROR: Overflow in level2.
[test_sub_nesting(17)]: exit

Note how it tried to return to line 13 twice in a row. The first time was correct, the second time was an error, and it should have been returning to the corresponding gosub on line 5.

This always seems to happen on the third context up from the deepest. In this example, the error happens in level 2 of 5. If I add another level, then the error happens on level 3 of 6.

I have isolated this to the case where there are two gosubs in a row, without being separated by a goto.

In other words, you can work around this problem (very painfully) with the following modifications to the script:

level1:
  echo Entering level1.
  gosub level2
  echo Exiting level1.
  exit

level2:
  goto level2_i  # Throw in an extraneous goto
level2_i:
  echo Entering level2.
  gosub level3
  echo Exiting level2.
  return
  echo ERROR: Overflow in level2.
  exit

level3:
  goto level3_i  # Another extraneous goto
level3_i:
  echo Entering level3.
  gosub level4
  goto level3_step2  # And yet another
level3_step2:
  gosub level4
  echo Exiting level3.
  return
  echo ERROR: Overflow in level3.
  exit

level4:
  echo Entering level4.
  echo Exiting level4.
  return
  echo ERROR: Overflow in level4.
  exit

This produces the correct output.

This bug is almost certainly caused by https://github.com/outlander-app/outlander-osx/blob/master/src/Outlander/Scripting/ScriptContext.swift#L211-219

If you are processing a gosub and the most recent thing that set gosubContext was also a gosub, then this method will push two contexts onto the stack instead of one, which would produce the behavior where two sequential returns both try to go back to the same line number.

@joemcbride
Copy link
Member

Thanks for the report! Are you currently using version 0.11.24 of Outlander? This issue appears to be fixed in the recently released Outlander 2 Beta.

Outlander 2 has been re-written using Swift 5 and fixes a lot of issues found in 0.11.24.

https://github.com/outlander-app/outlander/releases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants