Closed Bug 193700 Opened 22 years ago Closed 22 years ago

Useless error message when retrieving property of undefined object

Categories

(Rhino Graveyard :: Core, defect)

All
macOS
defect
Not set
normal

Tracking

(Not tracked)

VERIFIED FIXED

People

(Reporter: russgold, Assigned: norrisboyd)

Details

Attachments

(1 file, 1 obsolete file)

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) Build Identifier: When parsing an expression like: base.something.here, if "base.something" is undefined, the error message generated is "The undefined object has no properties" which is not very helpful in tracking down the problem. The following patch should provide a better message: RCS file: /cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/Interpreter.java,v retrieving revision 1.130 diff -u -r1.130 Interpreter.java --- js/rhino/src/org/mozilla/javascript/Interpreter.java 17 Feb 2003 08:50:55 -0000 1.130 +++ js/rhino/src/org/mozilla/javascript/Interpreter.java 17 Feb 2003 15:07:46 -0000 @@ -2032,6 +2032,12 @@ --stackTop; Object lhs = stack[stackTop]; if (lhs == DBL_MRK) lhs = doubleWrap(sDbl[stackTop]); + else if (lhs == undefined) { + // special code for better error message for get property from undefined + int i = getShort(iCode, pc-6); + if (i != -1) lhs=strings[i]; + throw NativeGlobal.typeError1( "msg.is.not.defined", ScriptRuntime.toString(lhs),scope ); + } stack[stackTop] = ScriptRuntime.getProp(lhs, name, scope); break; } Reproducible: Always Steps to Reproduce: 1. Ask Rhino to parse an expression in which the second-to-last element part is undefined 2. 3. Actual Results: It generates the above error message
The submitted patch does not always work... this one may be better: Index: js/rhino/src/org/mozilla/javascript/Interpreter.java =================================================================== RCS file: /cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/Interpreter.java,v retrieving revision 1.130 diff -u -r1.130 Interpreter.java --- js/rhino/src/org/mozilla/javascript/Interpreter.java 17 Feb 2003 08:50:55 -0000 1.130 +++ js/rhino/src/org/mozilla/javascript/Interpreter.java 17 Feb 2003 15:43:53 -0000 @@ -2032,6 +2032,16 @@ --stackTop; Object lhs = stack[stackTop]; if (lhs == DBL_MRK) lhs = doubleWrap(sDbl[stackTop]); + else if (lhs == undefined) { + // special code for better error message for get property from undefined + int j = pc-6; + while (j > 0 && iCode[j] != 0) j--; + int i = getShort(iCode, j); + if (i >= 0 && i < strings.length) { + lhs=strings[i]; + throw NativeGlobal.typeError1( "msg.is.not.defined", ScriptRuntime.toString(lhs),scope ); + } + } stack[stackTop] = ScriptRuntime.getProp(lhs, name, scope); break; }
OS: All → MacOS X
cc'ing Igor to consider this patch -
Although the patch idea is fine, I think it would be better to report the property name for undefined null value, i.e. in x.y.z with undefined y it would be sufficient for debugging to report about accessing the property z of undefined. In this way a reasonable message would be printed for x[0].z or x.y[0].
Patch adds to various getElem/getProp explicit checks for null/undefined and in such cases report which property of null/undefined was accessed. With the patch I have in Rhino shell: js> x = {} [object Object] js> x.y.z js: "<stdin>", line 2: uncaught JavaScript exception: TypeError: Cannot get property "z" from undefined (<stdin>; line 2) js> x[0].z js: "<stdin>", line 3: uncaught JavaScript exception: TypeError: Cannot get property "z" from undefined (<stdin>; line 3) js> x.y[0] js: "<stdin>", line 4: uncaught JavaScript exception: TypeError: Cannot get property "0" from undefined (<stdin>; line 4)
This is a patch extention to cover property assignments as well so I have in Rhino shell: js> x={} [object Object] js> x.y.z js: "<stdin>", line 2: uncaught JavaScript exception: TypeError: Cannot read property "z" from undefined (<stdin>; line 2) js> x.y.z=1 js: "<stdin>", line 3: uncaught JavaScript exception: TypeError: Cannot set property "z" of undefined to "1" (<stdin>; line 3) js> x.y[0]=Math.sin js: "<stdin>", line 4: uncaught JavaScript exception: TypeError: Cannot set property "0" of undefined to "org.mozilla.javascript.IdFunction@93dcd" (<stdin>; line 4) js> x[1][2]=1 js: "<stdin>", line 6: uncaught JavaScript exception: TypeError: Cannot set property "2" of undefined to "1" (<stdin>; line 6)
Attachment #114733 - Attachment is obsolete: true
I commited the patch few days ago.
Status: NEW → RESOLVED
Closed: 22 years ago
Resolution: --- → FIXED
Verified Fixed - js> x = {} [object Object] js> x.y.z js: "<stdin>", line 8: uncaught JavaScript exception: TypeError: Cannot read property "z" from undefined (<stdin>; line 8) js> x.y.z=1 js: "<stdin>", line 9: uncaught JavaScript exception: TypeError: Cannot set property "z" of undefined to "1" (<stdin>; line 9 js> x.y[0]=Math.sin js: "<stdin>", line 10: uncaught JavaScript exception: TypeError: Cannot set property "0" of undefined to "org.mozilla.javasc ipt.IdFunction@1f14ceb" (<stdin>; line 10) js> x[1][2]=1 js: "<stdin>", line 11: uncaught JavaScript exception: TypeError: Cannot set property "2" of undefined to "1" (<stdin>; line 1)
Status: RESOLVED → VERIFIED
Targeting 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.

Attachment

General

Creator:
Created:
Updated:
Size: