Converting function to string removes labeled block (1_7R4)

RESOLVED FIXED

Status

--
minor
RESOLVED FIXED
6 years ago
6 years ago

People

(Reporter: pekkakarj, Unassigned)

Tracking

Details

Attachments

(1 attachment)

(Reporter)

Description

6 years ago
Created attachment 640178 [details]
ex.js - the JS program from What did you do? section

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.
(Reporter)

Comment 1

6 years ago
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
Proposed patch filed at https://github.com/mozilla/rhino/pull/60

Comment 3

6 years ago
Thanks André, pull request merged:

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