8015741: Need a global.load function that starts with a new global scope.

Mon, 03 Jun 2013 08:34:29 -0300

author
jlaskey
date
Mon, 03 Jun 2013 08:34:29 -0300
changeset 317
08a8fda6c0bf
parent 316
295c91f5fdde
child 318
2df08f4c531d

8015741: Need a global.load function that starts with a new global scope.
Reviewed-by: sundar, lagergren
Contributed-by: james.laskey@oracle.com

src/jdk/nashorn/internal/objects/Global.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/Context.java file | annotate | diff | comparison | revisions
test/script/basic/JDK-8015741.js file | annotate | diff | comparison | revisions
test/script/basic/JDK-8015741.js.EXPECTED file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/objects/Global.java	Mon Jun 03 15:58:14 2013 +0530
     1.2 +++ b/src/jdk/nashorn/internal/objects/Global.java	Mon Jun 03 08:34:29 2013 -0300
     1.3 @@ -119,6 +119,10 @@
     1.4      @Property(attributes = Attribute.NOT_ENUMERABLE)
     1.5      public Object load;
     1.6  
     1.7 +    /** Nashorn extension: global.loadWithNewGlobal */
     1.8 +    @Property(attributes = Attribute.NOT_ENUMERABLE)
     1.9 +    public Object loadWithNewGlobal;
    1.10 +
    1.11      /** Nashorn extension: global.exit */
    1.12      @Property(attributes = Attribute.NOT_ENUMERABLE)
    1.13      public Object exit;
    1.14 @@ -364,11 +368,12 @@
    1.15      // Used to store the last RegExp result to support deprecated RegExp constructor properties
    1.16      private RegExpResult lastRegExpResult;
    1.17  
    1.18 -    private static final MethodHandle EVAL    = findOwnMH("eval",    Object.class, Object.class, Object.class);
    1.19 -    private static final MethodHandle PRINT   = findOwnMH("print",   Object.class, Object.class, Object[].class);
    1.20 -    private static final MethodHandle PRINTLN = findOwnMH("println", Object.class, Object.class, Object[].class);
    1.21 -    private static final MethodHandle LOAD    = findOwnMH("load",    Object.class, Object.class, Object.class);
    1.22 -    private static final MethodHandle EXIT    = findOwnMH("exit",    Object.class, Object.class, Object.class);
    1.23 +    private static final MethodHandle EVAL              = findOwnMH("eval",              Object.class, Object.class, Object.class);
    1.24 +    private static final MethodHandle PRINT             = findOwnMH("print",             Object.class, Object.class, Object[].class);
    1.25 +    private static final MethodHandle PRINTLN           = findOwnMH("println",           Object.class, Object.class, Object[].class);
    1.26 +    private static final MethodHandle LOAD              = findOwnMH("load",              Object.class, Object.class, Object.class);
    1.27 +    private static final MethodHandle LOADWITHNEWGLOBAL = findOwnMH("loadWithNewGlobal", Object.class, Object.class, Object.class);
    1.28 +    private static final MethodHandle EXIT              = findOwnMH("exit",              Object.class, Object.class, Object.class);
    1.29  
    1.30      private final Context context;
    1.31  
    1.32 @@ -743,6 +748,21 @@
    1.33      }
    1.34  
    1.35      /**
    1.36 +     * Global loadWithNewGlobal implementation - Nashorn extension
    1.37 +     *
    1.38 +     * @param self    scope
    1.39 +     * @param source  source to load
    1.40 +     *
    1.41 +     * @return result of load (undefined)
    1.42 +     *
    1.43 +     * @throws IOException if source could not be read
    1.44 +     */
    1.45 +    public static Object loadWithNewGlobal(final Object self, final Object source) throws IOException {
    1.46 +        final Global global = Global.instance();
    1.47 +        return global.context.loadWithNewGlobal(source);
    1.48 +    }
    1.49 +
    1.50 +    /**
    1.51       * Global exit and quit implementation - Nashorn extension: perform a {@code System.exit} call from the script
    1.52       *
    1.53       * @param self  self reference
    1.54 @@ -1387,6 +1407,7 @@
    1.55          this.unescape           = ScriptFunctionImpl.makeFunction("unescape",   GlobalFunctions.UNESCAPE);
    1.56          this.print              = ScriptFunctionImpl.makeFunction("print",      env._print_no_newline ? PRINT : PRINTLN);
    1.57          this.load               = ScriptFunctionImpl.makeFunction("load",       LOAD);
    1.58 +        this.loadWithNewGlobal  = ScriptFunctionImpl.makeFunction("loadWithNewGlobal", LOADWITHNEWGLOBAL);
    1.59          this.exit               = ScriptFunctionImpl.makeFunction("exit",       EXIT);
    1.60          this.quit               = ScriptFunctionImpl.makeFunction("quit",       EXIT);
    1.61  
     2.1 --- a/src/jdk/nashorn/internal/runtime/Context.java	Mon Jun 03 15:58:14 2013 +0530
     2.2 +++ b/src/jdk/nashorn/internal/runtime/Context.java	Mon Jun 03 08:34:29 2013 -0300
     2.3 @@ -491,6 +491,28 @@
     2.4      }
     2.5  
     2.6      /**
     2.7 +     * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
     2.8 +     * expression, after creating a new global scope.
     2.9 +     *
    2.10 +     * @param from source expression for script
    2.11 +     *
    2.12 +     * @return return value for load call (undefined)
    2.13 +     *
    2.14 +     * @throws IOException if source cannot be found or loaded
    2.15 +     */
    2.16 +    public Object loadWithNewGlobal(final Object from) throws IOException {
    2.17 +        final ScriptObject oldGlobal = getGlobalTrusted();
    2.18 +        final ScriptObject newGlobal = createGlobal();
    2.19 +        setGlobalTrusted(newGlobal);
    2.20 +
    2.21 +        try {
    2.22 +            return load(newGlobal, from);
    2.23 +        } finally {
    2.24 +            setGlobalTrusted(oldGlobal);
    2.25 +        }
    2.26 +    }
    2.27 +
    2.28 +    /**
    2.29       * Load or get a structure class. Structure class names are based on the number of parameter fields
    2.30       * and {@link AccessorProperty} fields in them. Structure classes are used to represent ScriptObjects
    2.31       *
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/script/basic/JDK-8015741.js	Mon Jun 03 08:34:29 2013 -0300
     3.3 @@ -0,0 +1,57 @@
     3.4 +/*
     3.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + * 
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + * 
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + * 
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + * 
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/**
    3.28 + * JDK-8015741 : Need a global.load function that starts with a new global scope.
    3.29 + *
    3.30 + * @test
    3.31 + * @run
    3.32 + */
    3.33 +
    3.34 +var Thread = java.lang.Thread;
    3.35 +
    3.36 +myGlobal = "#0";
    3.37 +var script1 = {name: "script 1", script: 'myGlobal = "#1"; print(myGlobal);'};
    3.38 +var script2 = {name: "script 2", script: 'myGlobal = "#2"; print(myGlobal);'};
    3.39 +var script3 = {name: "script 3", script: 'myGlobal = "#3"; print(myGlobal);'};
    3.40 +var script4 = {name: "script 4", script: 'myGlobal = "#4"; print(myGlobal);'};
    3.41 +
    3.42 +print(myGlobal);
    3.43 +load(script1);
    3.44 +print(myGlobal);
    3.45 +
    3.46 +print(myGlobal);
    3.47 +var thread1 = new Thread(function() { load(script2); });
    3.48 +thread1.start();
    3.49 +thread1.join();
    3.50 +print(myGlobal);
    3.51 +
    3.52 +print(myGlobal);
    3.53 +loadWithNewGlobal(script3);
    3.54 +print(myGlobal);
    3.55 +
    3.56 +print(myGlobal);
    3.57 +var thread2 = new Thread(function() { loadWithNewGlobal(script4); });
    3.58 +thread2.start();
    3.59 +thread2.join();
    3.60 +print(myGlobal);
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/script/basic/JDK-8015741.js.EXPECTED	Mon Jun 03 08:34:29 2013 -0300
     4.3 @@ -0,0 +1,12 @@
     4.4 +#0
     4.5 +#1
     4.6 +#1
     4.7 +#1
     4.8 +#2
     4.9 +#2
    4.10 +#2
    4.11 +#3
    4.12 +#2
    4.13 +#2
    4.14 +#4
    4.15 +#2

mercurial