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

Exception handler stack is not popped when handler returns a value #7

Open
pablomarx opened this issue Oct 27, 2018 · 3 comments
Open

Comments

@pablomarx
Copy link

It appears that the exception handler stack is not popped when a handler is invoked that returns a value. This would seem to be because the kNBCPopHandlers is generated after the branch to the handler, and thus is not invoked due to the return statement.

This causes subsequent exceptions to not be handled properly.

The following NewtonScript code demonstrates the problem:

func x() begin
	try
		Throw('|evt.ex|, "");
	onexception |evt.ex| do
		return "caught";
	return "not caught";
end;

print("Attempt 1: " & x() & "\n");
print("Attempt 2: " & x() & "\n");

When ran under NEWT/0, the output is:

Attempt 1: caught

(Note: Attempt 2 is never logged)

Under WallyScript/NewtonScript 1.x:

Welcome to WallyScript!

      0 > func x() begin try Throw('|evt.ex|, ""); onexception |evt.ex| do return "caught"; return "not caught"; end;
#2AE5F9   x
      0 > print("Attempt 1: " & x() & "\n");
"Attempt 1: caught
"
#2        NIL
      0 > print("Attempt 2: " & x() & "\n");
"Attempt 2: caught
"
#2        NIL
      0 > 
@pablomarx
Copy link
Author

I've been able to resolve this by removing the call to pop handlers from NBCGenTry():

NBCGenCode(kNBCPopHandlers, 0);

And adding it above this line in NBCGenTryPost():

NBCGenBC_stmt(stree, node->op2, true);

I'm not sure if this a proper fix and would appreciate guidance.

@pguyot
Copy link
Contributor

pguyot commented Jul 9, 2020

Wrote new tests to compare NEWT/0 generated bytecode with NewtonOS built-in compiler. There are differences that may account for what you observed, fixing them one by one. However, the proposed change doesn't fix it.

@pguyot
Copy link
Contributor

pguyot commented Jul 9, 2020

The issue is here:

NBCGenTry(stree, node->op1, node->op2);

Try blocks should be passed ret to handle the pop at the appropriate place.
Will submit a pull-request as soon as tests pass, including with a fix for #8.

pguyot added a commit to pguyot/NEWT0 that referenced this issue Jul 9, 2020
Exceptions are now propagated through saved vm environments (gnue#8).
Pop-handler and pop are generated in the right sequence, following NewtonOS
compiler.
Fix si_set_lex_scope for native functions.
Add unit tests for various cases.
Add BinEqual function for tests.
pguyot added a commit to pguyot/NEWT0 that referenced this issue Jul 9, 2020
Exceptions are now propagated through saved vm environments (gnue#8).
Pop-handler and pop are generated in the right sequence, following NewtonOS
compiler.
Fix si_set_lex_scope for native functions.
Add unit tests for various cases.
Add BinEqual function for tests.
pguyot added a commit to pguyot/NEWT0 that referenced this issue Jul 9, 2020
Exceptions are now propagated through saved vm environments (gnue#8).
Pop-handler and pop are generated in the right sequence, following NewtonOS
compiler.
Fix si_set_lex_scope for native functions.
Add unit tests for various cases.
Add BinEqual function for tests.
gnue added a commit that referenced this issue Jul 10, 2020
Fix several issues related to exceptions, including #7 and #8
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