8114838: Anonymous functions escape to surrounding scope when defined under "with" statement

Wed, 29 Jul 2015 18:54:56 +0530

author
sundar
date
Wed, 29 Jul 2015 18:54:56 +0530
changeset 1511
c6e9453ed5ef
parent 1510
5e3d8947e95c
child 1512
152cfeee5001

8114838: Anonymous functions escape to surrounding scope when defined under "with" statement
Reviewed-by: jlaskey, mhaupt

samples/javabind.js file | annotate | diff | comparison | revisions
samples/javaconstructorbind.js file | annotate | diff | comparison | revisions
samples/mapwith.js file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/codegen/AssignSymbols.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/codegen/SplitIntoFunctions.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/ir/VarNode.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/parser/Parser.java file | annotate | diff | comparison | revisions
test/script/basic/JDK-8114838.js file | annotate | diff | comparison | revisions
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/javabind.js	Wed Jul 29 18:54:56 2015 +0530
     1.3 @@ -0,0 +1,44 @@
     1.4 +/*
     1.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     1.6 + *
     1.7 + * Redistribution and use in source and binary forms, with or without
     1.8 + * modification, are permitted provided that the following conditions
     1.9 + * are met:
    1.10 + *
    1.11 + *   - Redistributions of source code must retain the above copyright
    1.12 + *     notice, this list of conditions and the following disclaimer.
    1.13 + *
    1.14 + *   - Redistributions in binary form must reproduce the above copyright
    1.15 + *     notice, this list of conditions and the following disclaimer in the
    1.16 + *     documentation and/or other materials provided with the distribution.
    1.17 + *
    1.18 + *   - Neither the name of Oracle nor the names of its
    1.19 + *     contributors may be used to endorse or promote products derived
    1.20 + *     from this software without specific prior written permission.
    1.21 + *
    1.22 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    1.23 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    1.24 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.25 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    1.26 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.27 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.28 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.29 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    1.30 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.31 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.32 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.33 + */
    1.34 +
    1.35 +// bind on a Java method
    1.36 +
    1.37 +// #javascript "bind" function
    1.38 +var bind = Function.prototype.bind;
    1.39 +
    1.40 +// Java console object
    1.41 +var console = java.lang.System.console();
    1.42 +
    1.43 +// arguments "this" and prompt string of Console.readLine method are bound
    1.44 +var readName = bind.call(console.readLine, console, "Your name: ");
    1.45 +
    1.46 +// Now call it like a function that takes no arguments!
    1.47 +print("Hello,", readName());
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/samples/javaconstructorbind.js	Wed Jul 29 18:54:56 2015 +0530
     2.3 @@ -0,0 +1,69 @@
     2.4 +/*
     2.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     2.6 + *
     2.7 + * Redistribution and use in source and binary forms, with or without
     2.8 + * modification, are permitted provided that the following conditions
     2.9 + * are met:
    2.10 + *
    2.11 + *   - Redistributions of source code must retain the above copyright
    2.12 + *     notice, this list of conditions and the following disclaimer.
    2.13 + *
    2.14 + *   - Redistributions in binary form must reproduce the above copyright
    2.15 + *     notice, this list of conditions and the following disclaimer in the
    2.16 + *     documentation and/or other materials provided with the distribution.
    2.17 + *
    2.18 + *   - Neither the name of Oracle nor the names of its
    2.19 + *     contributors may be used to endorse or promote products derived
    2.20 + *     from this software without specific prior written permission.
    2.21 + *
    2.22 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    2.23 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    2.24 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    2.25 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    2.26 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    2.27 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    2.28 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    2.29 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    2.30 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    2.31 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    2.32 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2.33 + */
    2.34 +
    2.35 +// bind on a Java constructor
    2.36 +
    2.37 +// See Function.prototype.bind:
    2.38 +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
    2.39 +var bind = Function.prototype.bind;
    2.40 +
    2.41 +var URL = Java.type("java.net.URL");
    2.42 +
    2.43 +// get the constructor that accepts URL, String parameters.
    2.44 +// constructor signatures are properties of type object.
    2.45 +var newURL = URL["(URL, String)"];
    2.46 +
    2.47 +// bind "context" URL parameter.
    2.48 +var TwitterURL = bind.call(newURL, null, new URL('https://www.twitter.com'));
    2.49 +
    2.50 +// now you can create context relative URLs using the bound constructor
    2.51 +print(new TwitterURL("sundararajan_a"));
    2.52 +
    2.53 +// read the URL content and print (optional part)
    2.54 +
    2.55 +var BufferedReader = Java.type("java.io.BufferedReader");
    2.56 +var InputStreamReader = Java.type("java.io.InputStreamReader");
    2.57 +
    2.58 +// function to retrieve text content of the given URL
    2.59 +function readTextFromURL(url) {
    2.60 +    var str = '';
    2.61 +    var u = new URL(url);
    2.62 +    var reader = new BufferedReader(
    2.63 +        new InputStreamReader(u.openStream()));
    2.64 +    try {
    2.65 +        reader.lines().forEach(function(x) str += x);
    2.66 +        return str;
    2.67 +    } finally {
    2.68 +        reader.close();
    2.69 +    }
    2.70 +}
    2.71 +
    2.72 +print(readTextFromURL(new TwitterURL("sundararajan_a")));
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/mapwith.js	Wed Jul 29 18:54:56 2015 +0530
     3.3 @@ -0,0 +1,47 @@
     3.4 +/*
     3.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     3.6 + *
     3.7 + * Redistribution and use in source and binary forms, with or without
     3.8 + * modification, are permitted provided that the following conditions
     3.9 + * are met:
    3.10 + *
    3.11 + *   - Redistributions of source code must retain the above copyright
    3.12 + *     notice, this list of conditions and the following disclaimer.
    3.13 + *
    3.14 + *   - Redistributions in binary form must reproduce the above copyright
    3.15 + *     notice, this list of conditions and the following disclaimer in the
    3.16 + *     documentation and/or other materials provided with the distribution.
    3.17 + *
    3.18 + *   - Neither the name of Oracle nor the names of its
    3.19 + *     contributors may be used to endorse or promote products derived
    3.20 + *     from this software without specific prior written permission.
    3.21 + *
    3.22 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    3.23 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    3.24 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    3.25 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    3.26 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    3.27 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    3.28 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    3.29 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    3.30 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    3.31 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    3.32 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    3.33 + */
    3.34 +
    3.35 +// Using a Java map with Javascript "with" statement
    3.36 +
    3.37 +var map = new java.util.HashMap();
    3.38 +map.put("foo", 34);
    3.39 +map.put("bar", "hello");
    3.40 +
    3.41 +var obj = {
    3.42 +    __noSuchProperty__: function(name) {
    3.43 +        return map.get(name);
    3.44 +    }
    3.45 +};
    3.46 +
    3.47 +with(obj) {
    3.48 +   print(foo);
    3.49 +   print(bar);
    3.50 +}
     4.1 --- a/src/jdk/nashorn/internal/codegen/AssignSymbols.java	Wed Jul 29 17:00:39 2015 +0530
     4.2 +++ b/src/jdk/nashorn/internal/codegen/AssignSymbols.java	Wed Jul 29 18:54:56 2015 +0530
     4.3 @@ -551,9 +551,7 @@
     4.4      private void defineVarIdent(final VarNode varNode) {
     4.5          final IdentNode ident = varNode.getName();
     4.6          final int flags;
     4.7 -        if (varNode.isAnonymousFunctionDeclaration()) {
     4.8 -            flags = IS_INTERNAL;
     4.9 -        } else if (!varNode.isBlockScoped() && lc.getCurrentFunction().isProgram()) {
    4.10 +        if (!varNode.isBlockScoped() && lc.getCurrentFunction().isProgram()) {
    4.11              flags = IS_SCOPE;
    4.12          } else {
    4.13              flags = 0;
     5.1 --- a/src/jdk/nashorn/internal/codegen/SplitIntoFunctions.java	Wed Jul 29 17:00:39 2015 +0530
     5.2 +++ b/src/jdk/nashorn/internal/codegen/SplitIntoFunctions.java	Wed Jul 29 18:54:56 2015 +0530
     5.3 @@ -308,10 +308,6 @@
     5.4          assert !varNode.isBlockScoped(); //TODO: we must handle these too, but we currently don't
     5.5  
     5.6          final Expression init = varNode.getInit();
     5.7 -        if (varNode.isAnonymousFunctionDeclaration()) {
     5.8 -            // We ain't moving anonymous function declarations.
     5.9 -            return super.enterVarNode(varNode);
    5.10 -        }
    5.11  
    5.12          // Move a declaration-only var statement to the top of the outermost function.
    5.13          getCurrentFunctionState().varStatements.add(varNode.setInit(null));
     6.1 --- a/src/jdk/nashorn/internal/ir/VarNode.java	Wed Jul 29 17:00:39 2015 +0530
     6.2 +++ b/src/jdk/nashorn/internal/ir/VarNode.java	Wed Jul 29 18:54:56 2015 +0530
     6.3 @@ -262,12 +262,4 @@
     6.4      public boolean isFunctionDeclaration() {
     6.5          return init instanceof FunctionNode && ((FunctionNode)init).isDeclared();
     6.6      }
     6.7 -
     6.8 -    /**
     6.9 -     * Returns true if this is an anonymous function declaration.
    6.10 -     * @return true if this is an anonymous function declaration.
    6.11 -     */
    6.12 -    public boolean isAnonymousFunctionDeclaration() {
    6.13 -        return isFunctionDeclaration() && ((FunctionNode)init).isAnonymous();
    6.14 -    }
    6.15  }
     7.1 --- a/src/jdk/nashorn/internal/parser/Parser.java	Wed Jul 29 17:00:39 2015 +0530
     7.2 +++ b/src/jdk/nashorn/internal/parser/Parser.java	Wed Jul 29 18:54:56 2015 +0530
     7.3 @@ -2720,6 +2720,11 @@
     7.4          }
     7.5  
     7.6          if (isStatement) {
     7.7 +            if (isAnonymous) {
     7.8 +                appendStatement(new ExpressionStatement(functionLine, functionToken, finish, functionNode));
     7.9 +                return functionNode;
    7.10 +            }
    7.11 +
    7.12              final int     varFlags = (topLevel || !useBlockScope()) ? 0 : VarNode.IS_LET;
    7.13              final VarNode varNode = new VarNode(functionLine, functionToken, finish, name, functionNode, varFlags);
    7.14              if (topLevel) {
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/test/script/basic/JDK-8114838.js	Wed Jul 29 18:54:56 2015 +0530
     8.3 @@ -0,0 +1,47 @@
     8.4 +/*
     8.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     8.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     8.7 + * 
     8.8 + * This code is free software; you can redistribute it and/or modify it
     8.9 + * under the terms of the GNU General Public License version 2 only, as
    8.10 + * published by the Free Software Foundation.
    8.11 + * 
    8.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    8.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    8.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    8.15 + * version 2 for more details (a copy is included in the LICENSE file that
    8.16 + * accompanied this code).
    8.17 + * 
    8.18 + * You should have received a copy of the GNU General Public License version
    8.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    8.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    8.21 + * 
    8.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    8.23 + * or visit www.oracle.com if you need additional information or have any
    8.24 + * questions.
    8.25 + */
    8.26 +
    8.27 +/**
    8.28 + * JDK-8114838: Anonymous functions escape to surrounding scope when defined under "with" statement
    8.29 + *
    8.30 + * @test
    8.31 + * @run
    8.32 + */
    8.33 +
    8.34 +// do *not* introduce new lines! The next line should be 32
    8.35 +with({}) { function () {} }
    8.36 +if (typeof this["L:32"] != 'undefined') {
    8.37 +    throw new Error("anonymous name spills into global scope");
    8.38 +}
    8.39 +
    8.40 +var func = eval("function() {}");
    8.41 +if (typeof func != 'function') {
    8.42 +    throw new Error("eval of anonymous function does not work!");
    8.43 +}
    8.44 +
    8.45 +var ScriptEngineManager = Java.type("javax.script.ScriptEngineManager");
    8.46 +var engine = new ScriptEngineManager().getEngineByName("nashorn");
    8.47 +var func2 = engine.eval("function() {}");
    8.48 +if (typeof func2 != 'function') {
    8.49 +    throw new Error("eval of anonymous function does not work from script engine!");
    8.50 +}

mercurial