Closed Bug 371443 Opened 19 years ago Closed 19 years ago

NativeJavaObject hides Boolean and String objects.

Categories

(Rhino Graveyard :: Core, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED INVALID

People

(Reporter: bjervis, Unassigned)

References

Details

Attachments

(1 file)

User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.7) Gecko/20060926 Ubuntu/breezy-security Firefox/1.5.0.7 Build Identifier: Rhino 1.6R6 pre (live sources) If you wrap a java.lang.Boolean or java.lang.String object and then test the object, it reports true, even if the Boolean is false or the String has zero length. Reproducible: Always Steps to Reproduce: 1. Start Rhino shell 2. var x = new java.lang.Boolean(false) 3. if (x) print("true") Actual Results: Prints "true" Expected Results: Prints "false"
I have a fix for this, I'm putting together a patch right now.
As shocking as it might sound, this is a feature, not a bug. By default, every Java object is wrapped into a JS object (NativeJavaObject), and according to ECMA-262 section 9.2, toBoolean applied to an object always evaluates to true. Now, every Context has an instance of WrapFactory class that governs wrapping behavior. You can invoke setJavaPrimitiveWrap(false) on it to explicitly disable wrapping of java.lang.String and java.lang.Boolean objects into NativeJavaObject instances, although it specifically won't work for explicit constructor invocations from script (as it assumes you knew what you were doing when you wrote "new java.lang.Boolean(false)" instead of just "false"), but it'll work for java.lang.Boolean and java.lang.String objects returned from invocations of Java methods. If you want it to work for explicit constructor invocations as well (as in your example), then subclass WrapFactory, and override wrapNewObject() to delegate to wrap() instead of to wrapAsJavaObject().
Status: UNCONFIRMED → RESOLVED
Closed: 19 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: