Closed Bug 694286 Opened 13 years ago Closed 5 years ago

Top level variables interfere with importPackage

Categories

(Rhino Graveyard :: Core, defect)

head
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED INACTIVE

People

(Reporter: dhartnoll, Unassigned)

Details

(Keywords: regression, reproducible)

Attachments

(2 files)

Attached file ScriptingBug.java
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1

Steps to reproduce:

When calling the calling the JavaScript engine from Java to process multiple scripts (in the same context), an exception is thrown if the later scripts attempt to use importPackage() to add another nested package of one that was previously imported and if there is also a top level variable in that later script with the same name as the additional level of the nested package. That description may be hard to follow, so this code (also attached) illustrates the issue better.

NB: This code needs to be run against JDK 1.7 (more specifically Rhino 1.7R1+) to reproduce the issue.
-----------------------------------------------
package com.bugreport;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class ScriptingBug {
	private static final String script1 = 
			"importPackage(com.bugreport)\n" +  // comment this line...
			"println ('Hello from script1')\n";

	private static final String script2 = 
			"importPackage(com.bugreport.anything)\n" +
			// anything variable interferes with JavaPackage com.bugreport.anything
			"var anything = 123\n" + // ... or this line to mask the issue
			"println ('Hello from script2')\n";

	public static void main(String[] args) {
		ScriptEngineManager engineManager = new ScriptEngineManager();
		ScriptEngine scriptEngine = engineManager.getEngineByExtension("js");
		
		System.out.println("Hello from main");
		try {
			scriptEngine.eval(script1); // reverse the order of these
			scriptEngine.eval(script2); // lines also masks the issue
		} catch (ScriptException e) {
			e.printStackTrace();
		}
		System.out.println("DONE.");
	}
}
-----------------------------------------------



Actual results:

When the importPackage() is encountered in the second script, an Exception is thrown because com.bugreport.anything evaluates to null. This happens because there is a top-level variable 'anything' in the same script. (The null value has nothing to do with the fact that the com.bugreport.anything package doesn't exist - in my real-world case, the package in question does actually exist, with the same result).


Expected results:

The importPackage should not have thrown an exception, but should have created lazy-loaded JavaPackage in the usual way.

The correct behaviour is observed with the JavaScript engine included in JRE/JDK 1.6.x, but the Exception is thrown when run against JDK 1.7.0. This regression appears to have been caused by the fix to bug 421071.

I have a proposed patch which I'll post separately.
This suggested patch is against the sources for Rhino 1.7 release 3.
It is actually a modification of the fix to bug 421071. That original fix was a performance enhancement to not try loading classes that have previously failed to load, but it also prevented names from later being treated has packages if they have ever failed to load as a class. This improvement only caches against the class loader, but still allows names to become packages when required.

Closing. Bug management is now done here:
https://github.com/mozilla/rhino

Status: UNCONFIRMED → RESOLVED
Closed: 5 years ago
Resolution: --- → INACTIVE
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: