Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /** |
michael@0 | 2 | * Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | * http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | // This function should never run on a too much recursion error. |
michael@0 | 7 | onerror = function(event) { |
michael@0 | 8 | postMessage(event.message); |
michael@0 | 9 | }; |
michael@0 | 10 | |
michael@0 | 11 | // Pure JS recursion |
michael@0 | 12 | function recurse() { |
michael@0 | 13 | recurse(); |
michael@0 | 14 | } |
michael@0 | 15 | |
michael@0 | 16 | // JS -> C++ -> JS -> C++ recursion |
michael@0 | 17 | function recurse2() { |
michael@0 | 18 | var xhr = new XMLHttpRequest(); |
michael@0 | 19 | xhr.onreadystatechange = function() { |
michael@0 | 20 | xhr.open("GET", "nonexistent.file"); |
michael@0 | 21 | } |
michael@0 | 22 | xhr.open("GET", "nonexistent.file"); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | var messageCount = 0; |
michael@0 | 26 | onmessage = function(event) { |
michael@0 | 27 | switch (++messageCount) { |
michael@0 | 28 | case 2: |
michael@0 | 29 | recurse2(); |
michael@0 | 30 | |
michael@0 | 31 | // An exception thrown from an event handler like xhr.onreadystatechange |
michael@0 | 32 | // should not leave an exception pending in the code that generated the |
michael@0 | 33 | // event. |
michael@0 | 34 | postMessage("Done"); |
michael@0 | 35 | return; |
michael@0 | 36 | |
michael@0 | 37 | case 1: |
michael@0 | 38 | recurse(); |
michael@0 | 39 | throw "Exception should have prevented us from getting here!"; |
michael@0 | 40 | |
michael@0 | 41 | default: |
michael@0 | 42 | throw "Weird number of messages: " + messageCount; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | throw "Impossible to get here!"; |
michael@0 | 46 | } |