closure-callback.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // Simulate nested calls to setTimeout by setting and calling a callback variable
  6. var callback;
  7. callback = (function () {
  8. WScript.Echo('callback 1');
  9. var ran = false;
  10. function startTest() {
  11. WScript.Echo('startTest: ran == ' + ran);
  12. if (!ran) {
  13. ran = true;
  14. try {
  15. callback = (function () {
  16. WScript.Echo('callback 2');
  17. // Needs the timeout nested call to crash
  18. startTest();
  19. callback = null;
  20. });
  21. } catch (e) {
  22. callback = (function () {
  23. WScript.Echo('callback 2');
  24. // Needs the closure reference to e, to crash
  25. var x = e;
  26. callback = null;
  27. });
  28. }
  29. }
  30. }
  31. startTest();
  32. });
  33. while (callback)
  34. callback();