Closed Bug 83051 Opened 24 years ago Closed 22 years ago

A function defined under a with block can't be invoked outside it

Categories

(Rhino Graveyard :: Core, defect)

x86
All
defect
Not set
normal

Tracking

(Not tracked)

VERIFIED FIXED

People

(Reporter: pschwartau, Assigned: norrisboyd)

Details

In SpiderMonkey, I can define f() under a with block and then call it from
top-level code. In Rhino, I cannot:

DEFINITION OF f
js> with(Math) {function f(){return PI}}
function f() {
    return PI;
}


THIS SUCCEEDS:
js> with(Math) {f()}
3.141592653589793


THIS FAILS: 
js> f()
js: "<stdin>", line 10: uncaught JavaScript exception: ReferenceError: "f" is 
not defined. (<stdin>; line 10)



This issue is causing the following testcase to fail:

         js/tests/ecma_3/Function/scope-001.js
Update: I AM able to invoke f globally in Rhino (as well as SpiderMonkey)
if I use a variable assignment instead of a function declaration:

js> with(Math) {var f = function(){return PI}}

js> f()
3.141592653589793
Summary: A function defined under a with block can't be invoked outside it → A function defined under a with block can't be invoked outside it
This may be the explanation:

1st test above: under the with statement there is a function declaration 
2nd test above: under the with statement there is a function expression 


It may be that ECMA does not specifially provide for function declarations
to appear under with statements, and that this is a SpiderMonkey extension
to the language. I will have to do more ECMA research to find out - 
Another puzzle: Rhino gives me different errors on testcase 

         js/tests/ecma_3/Function/scope-001.js

depending on whether or not I use the -f operator or the load() method:


[//d/JS_TRUNK/mozilla/js/tests/ecma_3/Function]
java org.mozilla.javascript.tools.shell.Main  -f ../shell.js -f scope-001.js
js: "D:\JS_trunk\mozilla\js\tests\ecma_3\Function\scope-001.js", line 100: 
uncaught JavaScript exception: ReferenceError: "f"
is not defined. (D:\JS_trunk\mozilla\js\tests\ecma_3\Function\scope-001.js; line 
100)


[//d/JS_TRUNK/mozilla/js/tests/ecma_3/Function]
$ java org.mozilla.javascript.tools.shell.Main
js> load('../shell.js')
js> load('scope-001.js')
BUGNUMBER: (none)
STATUS: Testing that functions are scoped statically, not dynamically
FAILED!: [reported from test()] Section C of test
FAILED!: [reported from test()] Expected value '2', Actual value '1'
FAILED!: [reported from test()]
FAILED!: [reported from test()] Section D of test
FAILED!: [reported from test()] Expected value '3', Actual value '1'
FAILED!: [reported from test()]
FAILED!: [reported from test()] Section E of test
FAILED!: [reported from test()] Expected value '2', Actual value '1'
FAILED!: [reported from test()]
FAILED!: [reported from test()] Section F of test
FAILED!: [reported from test()] Expected value '2', Actual value '1'
FAILED!: [reported from test()]


Any idea why this would be???

Note: in Rhino interpreted mode, I only get the latter type of failure,
whether I use the -f option or the load() method -  
I take that back : now I'm getting the same behavior in the rhinoi shell 
(interpreted mode) as in the rhino shell(compiled mode: 

a discrepancy between the failure under the -f option as opposed 
to the load() method. I don't how to explain what happened above ...


Now I see what I did wrong. When I was using load() to run the two
different tests in the shell, I wasn't exiting the shell in between them. 
The function f is successfully defined by the one test. If I then happened 
to run the other test, f was still in memory and produced output...

Sorry for the confusion -
To summarize: whether using the -f option or load(), both Rhino shells error on

             js/tests/ecma_3/Function/scope-001.js

js: "D:\JS_trunk\mozilla\js\tests\ecma_3\Function\scope-001.js", line 100: 
uncaught JavaScript exception: ReferenceError: "f"
is not defined. (D:\JS_trunk\mozilla\js\tests\ecma_3\Function\scope-001.js; line 
100)


This is the test with the function DECLARATION under the with statement,
where we try to invoke the function globally. SpiderMonkey allows this.



Both Rhino and SpiderMonkey pass the other test:

             js/tests/ecma_3/Function/scope-002.js

This is the test with the function EXPRESSION under the with statement,
where we try to invoke the function globally. Both engines allow this.
This can be market as fixed as the bug 184107 is effectively a duplicate of this
and it is fixed. Now the bug can be removed from the excluding list in
mozilla/js/tests/rhino-n.tests:

Index: rhino-n.tests
===================================================================
RCS file: /cvsroot/mozilla/js/tests/rhino-n.tests,v
retrieving revision 1.50
diff -u -r1.50 rhino-n.tests
--- rhino-n.tests	4 Sep 2002 22:56:39 -0000	1.50
+++ rhino-n.tests	23 Dec 2002 15:14:53 -0000
@@ -6,8 +6,6 @@
 #
 #bug 152646 Will not fix this in Rhino; too much of a corner case
 js1_5/Regress/regress-152646.js
-# bug 83051
-ecma_3/Function/scope-001.js
 # bug 59861
 ecma/Date/15.9.4.3.js
 #See bug number 94506 
Igor is correct.

The testcase, js/tests/ecma_3/Function/scope-001.js, started
passing in Rhino when the fix for bug 184107 was checked in.

Marking FIXED -
Status: NEW → RESOLVED
Closed: 22 years ago
Resolution: --- → FIXED
Marking Verified Fixed.

I have removed these two lines from the rhino-n.tests skip list:

# bug 83051
ecma_3/Function/scope-001.js


[//d/JS_TRUNK/mozilla/js/tests] cvs ci rhino-n.tests
Checking in rhino-n.tests;
/cvsroot/mozilla/js/tests/rhino-n.tests,v  <--  rhino-n.tests
new revision: 1.51; previous revision: 1.50
done
Status: RESOLVED → VERIFIED
Targeting as resolved against 1.5R4
Target Milestone: --- → 1.5R4
You need to log in before you can comment on or make changes to this bug.