Compiler chokes on keywords as object properties

RESOLVED FIXED

Status

Rhino
Compiler
RESOLVED FIXED
10 years ago
7 years ago

People

(Reporter: Mark, Assigned: Hannes Wallnoefer)

Tracking

Details

Attachments

(1 attachment)

(Reporter)

Description

10 years ago
User-Agent:       Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12
Build Identifier: rhino1_6R7

The following snippets cause compiler errors, all are valid:

var x = {int:5};
var x = {for:5};
var x = {continue:5};
var x = {while:5};
...etc

var x = {'int': 5}; // passes this, but...
alert(x.int);       // fails

Reproducible: Always

Steps to Reproduce:
1.
2.
3.
(Reporter)

Comment 1

10 years ago
I know alert isn't valid, I wasn't thinking about it.  Change that to whatever, the "x.int" still breaks.
(Reporter)

Updated

10 years ago
Severity: normal → critical
(Reporter)

Comment 2

10 years ago
Okay, looking into this more I found that Rhino follows the ECMAScript Grammar on this while every browser I've tested (FF, Opera, Safari, IE) allows the behavior.  I suppose the question is what Rhino should be following closer  :-)  A simple browser test:

javascript:alert({int:5}.int);



From ECMA-262 E3:

ReservedWord :: See section 7.5.1 
Keyword 
FutureReservedWord 
NullLiteral 
BooleanLiteral 
 
Keyword :: one of See section 7.5.2 
break else new var 
case finally return void 
catch for switch while 
continue function this with 
default if throw 
delete in try 
do instanceof typeof 
 
FutureReservedWord :: one of See section 7.5.3 
abstract enum int short 
boolean export interface static 
byte extends long super 
char final native synchronized 
class float package throws 
const goto private transient 
debugger implements protected volatile 
double import public 

Identifier :: IdentifierName but not ReservedWord
Severity: critical → normal

Comment 3

10 years ago
IE6 permits this:

var foo = {int: 5}

but NOT any of the following:

var foo = {return: 5}
var foo = {for: 5}
var foo = {while: 5}
var foo = {if: 5}

So technically IE is only permitting non-keyword reserved words as identifiers.  (Haven't verified it for all keywords, but that's my hunch.)
(Assignee)

Comment 4

7 years ago
Created attachment 522684 [details] [diff] [review]
Allow keywords as property identifiers

This patch allows all keywords as property ids if Context.FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER is enabled. In the meantime all major browsers allow this, so it's time to catch up.
Assignee: nobody → hannes
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
(Assignee)

Comment 5

7 years ago
Committed my patch.

Checking in src/org/mozilla/javascript/Token.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/Token.java,v  <--  Token.java
new revision: 1.47; previous revision: 1.46
done
Checking in src/org/mozilla/javascript/Parser.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/Parser.java,v  <--  Parser.java
new revision: 1.157; previous revision: 1.156
done
RCS file: /cvsroot/mozilla/js/rhino/testsrc/doctests/423557.doctest,v
done
Checking in testsrc/doctests/423557.doctest;
/cvsroot/mozilla/js/rhino/testsrc/doctests/423557.doctest,v  <--  423557.doctest
initial revision: 1.1
done
Status: ASSIGNED → RESOLVED
Last Resolved: 7 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.