Closed Bug 772011 Opened 12 years ago Closed 12 years ago

Converting function to string removes labeled block (1_7R4)

Categories

(Rhino Graveyard :: Core, defect)

defect
Not set
minor

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: pekkakarj, Unassigned)

Details

Attachments

(1 file)

User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Build ID: 20110928134238

Steps to reproduce:

I am using the downloadable Rhino package 1_7R4 from http://www.mozilla.org/rhino/download.html. I launch the program with java -jar js.jar <filename> without any other options. The javascript program I am running is as follows.

function x() {
  foo: {
    print("hi");
    break foo;
  }
}

x(); // OK
eval(x.toString()); // not OK??


Actual results:

The program prints hi as expected and then throws a runtime SyntaxError exception. This is caused by the call to eval. It receives a syntactically incorrect javascript function definition.



Expected results:

There should be no syntax error. I expect that I can always turn a valid JS function into a string and eval the string later without the string having incorrect syntax in it. (Even if it is not true in general, I expect it should work with this particular function. SpiderMonkey handles the situation just fine.)

By making the program print the function x convered to a string you will find it prints this:

function x() {
foo:
    print("hi");
    break foo;
}

The difference from the original definition is that the labeled block is removed, but the label remains. This causes the SyntaxError exception.

I believe the behavior that happens in the SpiderMonkey engine (of a recent Firefox) is correct. It does not remove the block from the function and allows you to eval the resulting string without a syntax error.

As far as I can tell, this only happens with labeled blocks that are not used in any kind of looping construct, so it is likely most users will never run into this problem.
Drat, I attached the wrong file. The attached while has a dummy do ... while(false); loop that prevents Rhino from removing the labeled block and causing the SyntaxError exception. The right bit of code is in the bug report itself. Sorry.
Severity: normal → minor
Thanks André, pull request merged:

https://github.com/mozilla/rhino/commit/aef0a286d2b239f5a3122f2233a2465459eba087
Status: UNCONFIRMED → RESOLVED
Closed: 12 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: