5

I'm wondering how the sequence shown below could possibly occur.

Here is the function in question:

WebSocketConnector.prototype.sendMessage = function(message) {
    if (socket !== null) {
        socket.send(message);
        console.log('Sent: ' + message);
    } else {
        alert('Failed to send message. WebSocket connection not established.');
    }
};

And here is what happens when I debug a call to this function:

1. Start at line 32.

if condition

2. Step In, advances to line 33.

first line of if block

3. Step In again, advances to line 34.

second line of if block

4. Step in one more time, advances to line 36???

first line of else block

--> How can control possibly go directly from the last line of the if block to the first line of the else block?

Some important facts:

  1. There are no missing steps here.
  2. This really happened.
  3. I'm only calling sendMessage from one place, and I'm logging when that call occurs. There are no unaccounted for sendMessage calls in the log, so I don't believe asynchrony is an explanation.
  4. I also tried the same thing with the Firebug debugger and the same crazy thing happens.

Edit/Followup

If I add a console.log statement to the first line of the else block (pushing the alert down to line 37), the control will go right from line 34 to line 37 (skipping the console.log statement).

Also, I should have mentioned, no alert actually ever appears, even when stepping directly into that code.

Edit 2

Here is the spacing and CRLF's of the sendMessage function:

enter image description here

devuxer
  • 41,681
  • 47
  • 180
  • 292
  • Is your code formatted with newlines exactly the same way in your question, in your IDE, and in firefox debugger? Maybe it's a bug related to the fact that the code is shown in the debugger differently than it is loaded in the VM, and linenumbers don't correspond... – reverse_engineer Oct 17 '14 at 09:26
  • @reverse_engineer, thanks, but that doesn't seem to be the case. I made sure the code was formatted and re-tested it, but the debugger still does the same thing. I also checked the text in MS Word to see the spaces and line breaks (CRLFs), but they all seemed to be correct (see Edit 2 in my question). – devuxer Oct 17 '14 at 22:34
  • I just saw almost this exact same thing, when the line cause an exception. On exception, it seems to display as though it's going to else. Weird. – NoBugs Oct 19 '14 at 03:49

2 Answers2

3

This is because the debugger steps to the last executable line before returning to the calling stack frame. In your case this is line 36 containing the alert() function. It would be clearer if the debugger jumped to the closing curly brace of the function, i.e. line 38.

There is already a report to change this behavior:

https://bugzil.la/1013219

Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132
1

Unfortunately there are some really odd behaviors with the debugger in Firefox. I wouldn't be surprised if what you describe might be related to this bug. The "step" functionality sometimes doesn't do what you would expect, or what Chromium browser does.

NoBugs
  • 9,310
  • 13
  • 80
  • 146