Assignment to foo should behave the same as assignment to this.foo

RESOLVED INVALID

Status

Rhino
Core
RESOLVED INVALID
11 years ago
11 years ago

People

(Reporter: Jose Solorzano, Unassigned)

Tracking

Details

(Reporter)

Description

11 years ago
User-Agent:       Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
Build Identifier: 1.6R5

As is the case in FireFox and IE implementations of Javascript,
assignments to a variable should behave the same as assigments
to this.variable, e.g. making the following valid:

location = 'http://google.com';

When you assign this.variable, the put()
method of the class in scope is called. When you assign a
variable without the "this." qualifier, the put() method is
not called.


Reproducible: Always

Steps to Reproduce:
The following program reproduces the issue.

package tests;

import org.mozilla.javascript.*;

public class Test1 {
	private static final String code = 
		"myvar1 = 'foo'; " +
		"this.myvar2 = 'bar'; ";
	
	public static void main(String[] args) {
		new Test1().run();
	}
	
	public void run() {
		Context ctx = Context.enter();
		try {
			Scriptable scope = ctx.initStandardObjects();
			Scriptable newScope = new LocalScriptableObject();
			newScope.setParentScope(scope);
			ctx.evaluateString(newScope, code, "code", 1, null);
		} finally {
			Context.exit();
		}
	}
	
	private class LocalScriptableObject extends ScriptableObject {
		@Override
		public String getClassName() {
			return "Window";
		}

		@Override
		public void put(String arg0, Scriptable arg1, Object arg2) {
			System.out.println("put(): name=" + arg0 + ",value=" + arg2);
			super.put(arg0, arg1, arg2);
		}
	}
}

Actual Results:  
The test program gives the following output:

put(): name=myvar2,value=bar

Expected Results:  
It should give the following output instead:

put(): name=myvar1,value=foo
put(): name=myvar2,value=bar
(Reporter)

Comment 1

11 years ago
Additionally, it appears that variables declared with "var" behave as if they had "this." which I believe is not correct either. In other words, "var" and non-"var" are working the opposite of the way each should.

Comment 2

11 years ago
I think that this is not a bug at all but the right behaviour as described in section 8.7.2 of the ECMA spec.

This is what you get in a browser too. Just try:
function foo()
{
  toto = 123;
  alert(window.toto);
}
you will see that the "toto" variable has been set in window's scope and not in the function's scope.
(Reporter)

Comment 3

11 years ago
You're correct, Mark. Thanks. To fix the example code above, scope setup needs to be reversed, as follows.

ScriptableObject windowScope = new WindowScope();
Scriptable scope = ctx.initStandardObjects(windowScope);

Proably needs one more scope object so that 'var' assignments don't go in the window scope, but that works. Closing as invalid.
Status: UNCONFIRMED → RESOLVED
Last Resolved: 11 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.