Skip to main content

Bug Tracker

Side navigation

#15251 closed bug (notabug)

Opened October 02, 2014 06:42PM UTC

Closed October 02, 2014 09:20PM UTC

Loading jquery2.1.1.js into Nashorn JavaScript engine yields error

Reported by: fritzthecat Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 2.1.1
Keywords: Cc:
Blocked by: Blocking:
Description

A problem occurs in jquery2.1.1.js on line 718:

push = { apply: arr.length ?

should be adjusted to

push = { apply: arr && arr.length ?

because "arr" can be undefined here.

Steps to reproduce:

I use Java 8 with the Nashorn JS engine.

I have all HtmlUnit 2.15 JARs in my CLASSPATH, including htmlunit-core-js2.15.jar.

I instantiate the JS engine and execute the script from Java by

final ScriptEngineManager engineManager = new ScriptEngineManager();
final ScriptEngine engine = engineManager.getEngineByMimeType("text/javascript");
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://www.this-page-intentionally-left-blank.org/");
final Window window = (Window) page.getEnclosingWindow().getScriptObject();
bindings.put("window", window);
bindings.put("document", window.getDocument());
bindings.put("navigator", window.getNavigator());
bindings.put("location", window.getLocation());
bindings.put("history", window.getHistory());
bindings.put("screen", window.getScreen());
final URL url = new URL("http://code.jquery.com/jquery-2.1.1.js");
final Reader reader = new InputStreamReader(url.openStream());
engine.eval(new BufferedReader(reader));

Nashorn error message:

Exception in thread "main" javax.script.ScriptException: TypeError: Cannot read property "length" from undefined in at line number 718
Attachments (0)
Change History (2)

Changed October 02, 2014 08:38PM UTC by fritzthecat comment:1

final ScriptEngineManager engineManager = new ScriptEngineManager();
final ScriptEngine engine = engineManager.getEngineByMimeType("text/javascript");
final Bindings bindings = engine.createBindings();
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://www.this-page-intentionally-left-blank.org/");
final Window window = (Window) page.getEnclosingWindow().getScriptObject();
bindings.put("window", window);
bindings.put("document", window.getDocument());
bindings.put("navigator", window.getNavigator());
bindings.put("location", window.getLocation());
bindings.put("history", window.getHistory());
bindings.put("screen", window.getScreen());
engine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
final URL url = new URL("http://code.jquery.com/jquery-2.1.1.js");
final Reader reader = new InputStreamReader(url.openStream());
engine.eval(new BufferedReader(reader));

Changed October 02, 2014 09:20PM UTC by gibson042 comment:2

resolution: → notabug
status: newclosed

arr is defined as [] on line 605 of http://code.jquery.com/jquery-2.1.1.js, so the only way for it to be undefined on line 618 is if that was returned by slice.call( preferredDoc.childNodes ):

// Optimize for push.apply( _, NodeList )
try {
        push.apply(
                (arr = slice.call( preferredDoc.childNodes )),
                preferredDoc.childNodes
        );
        // Support: Android<4.0
        // Detect silently failing push.apply
        arr[ preferredDoc.childNodes.length ].nodeType;
} catch ( e ) {
        push = { apply: arr.length ?

                // Leverage slice if possible
                function( target, els ) {
                        push_native.apply( target, slice.call(els) );
                } :

                // Support: IE<9
                // Otherwise append directly
                function( target, els ) {
                        var j = target.length,
                                i = 0;
                        // Can't trust NodeList.length
                        while ( (target[j++] = els[i++]) ) {}
                        target.length = j - 1;
                }
        };
}

Looks like a Nashorn problem.