Ensure scope 'this' is properly restored, add unit test.

Close #2632