Closed Bug 398396 Opened 18 years ago Closed 18 years ago

Arithmetic mixing boxed (java) numbers and js numbers

Categories

(Rhino Graveyard :: Core, defect)

head
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED DUPLICATE of bug 371443

People

(Reporter: mozilla.bugs, Unassigned)

Details

Attachments

(1 file)

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7 Build Identifier: new java.lang.Double("3") + 1 returns "31". Reproducible: Always Steps to Reproduce: At a rhino shell enter: new java.lang.Double("3") + 1 Actual Results: 31 (String) Expected Results: 4 (Number)
Version: other → head
Oops: WrapFactory.setJavaPrimitiveWrap(false) solves my core problem but not "new java.lang.Double("3") + 1 returns "3.01"".
Okay, so the + operator calls ToPrimitive() on both its left and right side (ECMA spec 11.6.1). Now, if you're *explicitly* creating a java.lang.Double, you're acknowledging that you're creating an object, and unhinted toPrimitive for objects will always call defaultValue on them (9.1), and defaultValue will ultimately invoke toString (Number hint step 5 in 8.6.2.6). Ultimately, setJavaPrimitiveWrap() is used to decide whether to wrap or not return values from methods. If you're explicitly creating objects in your scripts, they're never considered primitive. So, everything operates as expected :-)
Status: UNCONFIRMED → RESOLVED
Closed: 18 years ago
Resolution: --- → DUPLICATE
Bug 371443 gives you further pointers if you want to implement your desired functionality
Thank you Attila for these explanations. As I said: my problem (doubles becoming Doubles because of being boxed to fit in a Map) was solved by using setJavaPrimitiveWrap.
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: