concatenating string and number doesn't work compiled.

VERIFIED FIXED in 1.5R5

Status

P3
major
VERIFIED FIXED
19 years ago
15 years ago

People

(Reporter: vajda, Assigned: norrisboyd)

Tracking

other
1.5R5
x86
Windows NT

Details

Attachments

(1 attachment)

(Reporter)

Description

19 years ago
Given the code:
{
    var i = 0;
    while (i++ < 5) {
       var text = inputText + i;
       ... do stuff ...
}

Interpreted the above code works, that is text is the inputText followed by a 
number.
Compiled (with max optimization level) text becomes NaN.
The interpreted behaviour is the correct one according to the javascript book 
(O'Reilly rhino).

Since this is a discrepancy between interpreted and compiled script it is major. 
The current workaround is to call i.toString() before concatenating.
(Reporter)

Comment 1

19 years ago
This happens on the latest rhinoTip, js.jar is dated 4/25 and is 331802 bytes 
long.
(Assignee)

Updated

19 years ago
Status: NEW → ASSIGNED
(Assignee)

Comment 2

19 years ago
I tried the following:

[rhino] cat test.js
var inputText = "hi";
var i = 0;
while (i++ < 5) {
        var text = inputText + i;
        print(text);
}
[rhino] java org.mozilla.javascript.tools.shell.Main -opt 9 test.js
hi1
hi2
hi3
hi4
hi5

Can you give me more information on the code that fails?
(Assignee)

Comment 3

19 years ago
Wait....

I found this from a mail:
==================================================
I just posted a compiler bug on bugzilla (#39309).
It has to do with concatenating a string and a number.
Interpreted concatenation works. Compiled it produces 'NaN'.

Andi..

for example:

function addSnackItems(textProp, imageProp, len, centerText, 
centerImages)
{
    var fragment = new Fragment(inputNode.doc);

    var i = 0;
    while (++i <= len) 
    {
        var iString = i.toString();
        var name = textProp + iString;
        var imgname = imageProp + iString;

        if (addSnackTextItem(fragment, name, null, 0, 0, centerText))
            addSnackImageItem(fragment, imgname, centerImages);
    }

    return fragment;             
}

If you take the .toString() calls away, concatenation name becomes 
'NaN'.
(Assignee)

Comment 4

19 years ago
Okay: this case generates the error:

function addSnackItems(textProp, len) {
    var i = 0;
    while (++i <= len) {
       var name = textProp + i;
       print(name);
    }
}
addSnackItems("text", 1);

This will print out "NaN" rather than "text1". 

I've tracked down the problem this far: It only occurs with -opt 9 and is caused 
by Optimizer.findSinglyTypedVars calling setIsNumber for the "name" variable. 

Could you take a look at it, Roger, since it's your code?

Assignee: nboyd → rogerl
Status: ASSIGNED → NEW
(Assignee)

Comment 5

19 years ago
Reassign back to me.
Assignee: rogerl → nboyd
(Assignee)

Comment 6

19 years ago
Created attachment 8954 [details] [diff] [review]
proposed patch

Comment 7

19 years ago
Fix checked in + addition to handle case of var being used before def'd.
Status: NEW → RESOLVED
Last Resolved: 19 years ago
Resolution: --- → FIXED

Comment 8

15 years ago
Testcase added to JS testsuite:

      mozilla/js/tests/js1_5/Regress/regress-39309.js

Marking Verified FIXED. The testcase passes in the Rhino shell
on WinNT4.0 with optimization levels set to -1, 0, or 9.
Status: RESOLVED → VERIFIED

Comment 9

15 years ago
Trageting as resolved against 1.5R5
Target Milestone: --- → 1.5R5
You need to log in before you can comment on or make changes to this bug.