Merge
authorlana
Tue, 20 Aug 2013 17:46:45 -0700
changeset 19475 70b75dad34e1
parent 19308 b5ed503c26ad (current diff)
parent 19474 70c91fc38cb4 (diff)
child 19476 df7b5446debc
Merge
nashorn/src/jdk/internal/dynalink/support/Backport.java
nashorn/src/jdk/nashorn/internal/runtime/arrays/ArrayIterator.java
nashorn/src/jdk/nashorn/internal/runtime/arrays/MapIterator.java
nashorn/src/jdk/nashorn/internal/runtime/arrays/ReverseArrayIterator.java
nashorn/src/jdk/nashorn/internal/runtime/arrays/ReverseMapIterator.java
--- a/nashorn/exclude/exclude_list_cc.txt	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/exclude/exclude_list_cc.txt	Tue Aug 20 17:46:45 2013 -0700
@@ -3,4 +3,5 @@
 <excludeList>
   <test id="JDK-8014647.js" />
   <test id="javaclassoverrides.js" />
+  <test id="JDK-8020809.js" />
 </excludeList>
--- a/nashorn/make/project.properties	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/make/project.properties	Tue Aug 20 17:46:45 2013 -0700
@@ -222,11 +222,16 @@
 run.test.user.language=tr
 run.test.user.country=TR
 
-#  -XX:+PrintCompilation -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMethods
-run.test.jvmargs.main=-server -Xmx${run.test.xmx} -XX:+TieredCompilation -ea -Dfile.encoding=UTF-8 -Duser.language=${run.test.user.language} -Duser.country=${run.test.user.country}
+run.test.jvmargs.common=-server -Xmx${run.test.xmx} -XX:+TieredCompilation -Dfile.encoding=UTF-8 -Duser.language=${run.test.user.language} -Duser.country=${run.test.user.country} -XX:+HeapDumpOnOutOfMemoryError
+
+#-XX:-UseCompressedKlassPointers -XX:+PrintHeapAtGC -XX:ClassMetaspaceSize=300M
+# -XX:+PrintCompilation -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMethods
 
-#-XX:+HeapDumpOnOutOfMemoryError -XX:-UseCompressedKlassPointers -XX:+PrintHeapAtGC -XX:ClassMetaspaceSize=300M  
-run.test.jvmargs.octane.main=-Xms${run.test.xms} ${run.test.jvmargs.main}
+# turn on assertions for tests
+run.test.jvmargs.main=${run.test.jvmargs.common} -ea
+
+#-XX:-UseCompressedKlassPointers -XX:+PrintHeapAtGC -XX:ClassMetaspaceSize=300M  
+run.test.jvmargs.octane.main=-Xms${run.test.xms} ${run.test.jvmargs.common}
 
 run.test.jvmsecurityargs=-Xverify:all -Djava.security.properties=${basedir}/make/java.security.override -Djava.security.manager -Djava.security.policy=${basedir}/build/nashorn.policy
 
--- a/nashorn/src/jdk/internal/dynalink/ChainedCallSite.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/internal/dynalink/ChainedCallSite.java	Tue Aug 20 17:46:45 2013 -0700
@@ -85,12 +85,12 @@
 
 import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodType;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.concurrent.atomic.AtomicReference;
 import jdk.internal.dynalink.linker.GuardedInvocation;
 import jdk.internal.dynalink.support.AbstractRelinkableCallSite;
+import jdk.internal.dynalink.support.Lookup;
 
 /**
  * A relinkable call site that maintains a chain of linked method handles. In the default implementation, up to 8 method
@@ -103,6 +103,9 @@
  * handle is always at the start of the chain.
  */
 public class ChainedCallSite extends AbstractRelinkableCallSite {
+    private static final MethodHandle PRUNE = Lookup.findOwnSpecial(MethodHandles.lookup(), "prune", MethodHandle.class,
+            MethodHandle.class);
+
     private final AtomicReference<LinkedList<GuardedInvocation>> invocations = new AtomicReference<>();
 
     /**
@@ -194,18 +197,4 @@
     private MethodHandle prune(MethodHandle relink) {
         return relinkInternal(null, relink, false);
     }
-
-    private static final MethodHandle PRUNE;
-    static {
-        try {
-            PRUNE = MethodHandles.lookup().findSpecial(ChainedCallSite.class, "prune", MethodType.methodType(
-                    MethodHandle.class, MethodHandle.class), ChainedCallSite.class);
-        // NOTE: using two catch blocks so we don't introduce a reference to 1.7 ReflectiveOperationException, allowing
-        // Dynalink to be used on 1.6 JVMs with Remi's backport library.
-        } catch(IllegalAccessException e) {
-            throw new AssertionError(e.getMessage(), e); // Can not happen
-        } catch(NoSuchMethodException e) {
-            throw new AssertionError(e.getMessage(), e); // Can not happen
-        }
-    }
 }
--- a/nashorn/src/jdk/internal/dynalink/DynamicLinkerFactory.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/internal/dynalink/DynamicLinkerFactory.java	Tue Aug 20 17:46:45 2013 -0700
@@ -84,6 +84,8 @@
 package jdk.internal.dynalink;
 
 import java.lang.invoke.MutableCallSite;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -97,6 +99,7 @@
 import jdk.internal.dynalink.linker.LinkRequest;
 import jdk.internal.dynalink.support.AutoDiscovery;
 import jdk.internal.dynalink.support.BottomGuardingDynamicLinker;
+import jdk.internal.dynalink.support.ClassLoaderGetterContextProvider;
 import jdk.internal.dynalink.support.CompositeGuardingDynamicLinker;
 import jdk.internal.dynalink.support.CompositeTypeBasedGuardingDynamicLinker;
 import jdk.internal.dynalink.support.LinkerServicesImpl;
@@ -117,7 +120,9 @@
      */
     public static final int DEFAULT_UNSTABLE_RELINK_THRESHOLD = 8;
 
-    private ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+    private boolean classLoaderExplicitlySet = false;
+    private ClassLoader classLoader;
+
     private List<? extends GuardingDynamicLinker> prioritizedLinkers;
     private List<? extends GuardingDynamicLinker> fallbackLinkers;
     private int runtimeContextArgCount = 0;
@@ -126,12 +131,13 @@
 
     /**
      * Sets the class loader for automatic discovery of available linkers. If not set explicitly, then the thread
-     * context class loader at the time of the constructor invocation will be used.
+     * context class loader at the time of {@link #createLinker()} invocation will be used.
      *
      * @param classLoader the class loader used for the autodiscovery of available linkers.
      */
     public void setClassLoader(ClassLoader classLoader) {
         this.classLoader = classLoader;
+        classLoaderExplicitlySet = true;
     }
 
     /**
@@ -260,7 +266,8 @@
         addClasses(knownLinkerClasses, prioritizedLinkers);
         addClasses(knownLinkerClasses, fallbackLinkers);
 
-        final List<GuardingDynamicLinker> discovered = AutoDiscovery.loadLinkers(classLoader);
+        final ClassLoader effectiveClassLoader = classLoaderExplicitlySet ? classLoader : getThreadContextClassLoader();
+        final List<GuardingDynamicLinker> discovered = AutoDiscovery.loadLinkers(effectiveClassLoader);
         // Now, concatenate ...
         final List<GuardingDynamicLinker> linkers =
                 new ArrayList<>(prioritizedLinkers.size() + discovered.size()
@@ -303,6 +310,15 @@
                 runtimeContextArgCount, syncOnRelink, unstableRelinkThreshold);
     }
 
+    private static ClassLoader getThreadContextClassLoader() {
+        return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
+            @Override
+            public ClassLoader run() {
+                return Thread.currentThread().getContextClassLoader();
+            }
+        }, ClassLoaderGetterContextProvider.GET_CLASS_LOADER_CONTEXT);
+    }
+
     private static void addClasses(Set<Class<? extends GuardingDynamicLinker>> knownLinkerClasses,
             List<? extends GuardingDynamicLinker> linkers) {
         for(GuardingDynamicLinker linker: linkers) {
--- a/nashorn/src/jdk/internal/dynalink/beans/ClassString.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/internal/dynalink/beans/ClassString.java	Tue Aug 20 17:46:45 2013 -0700
@@ -112,10 +112,6 @@
         this(type.parameterArray());
     }
 
-    Class<?>[] getClasses() {
-        return classes;
-    }
-
     @Override
     public boolean equals(Object other) {
         if(!(other instanceof ClassString)) {
--- a/nashorn/src/jdk/internal/dynalink/beans/StaticClassLinker.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/internal/dynalink/beans/StaticClassLinker.java	Tue Aug 20 17:46:45 2013 -0700
@@ -189,15 +189,17 @@
         return type == StaticClass.class;
     }
 
-    /*private*/ static final MethodHandle GET_CLASS = new Lookup(MethodHandles.lookup()).findVirtual(StaticClass.class,
-            "getRepresentedClass", MethodType.methodType(Class.class));
-
-    /*private*/ static final MethodHandle IS_CLASS = new Lookup(MethodHandles.lookup()).findStatic(StaticClassLinker.class,
-            "isClass", MethodType.methodType(Boolean.TYPE, Class.class, Object.class));
-
+    /*private*/ static final MethodHandle GET_CLASS;
+    /*private*/ static final MethodHandle IS_CLASS;
     /*private*/ static final MethodHandle ARRAY_CTOR = Lookup.PUBLIC.findStatic(Array.class, "newInstance",
             MethodType.methodType(Object.class, Class.class, int.class));
 
+    static {
+        final Lookup lookup = new Lookup(MethodHandles.lookup());
+        GET_CLASS = lookup.findVirtual(StaticClass.class, "getRepresentedClass", MethodType.methodType(Class.class));
+        IS_CLASS = lookup.findOwnStatic("isClass", Boolean.TYPE, Class.class, Object.class);
+    }
+
     @SuppressWarnings("unused")
     private static boolean isClass(Class<?> clazz, Object obj) {
         return obj instanceof StaticClass && ((StaticClass)obj).getRepresentedClass() == clazz;
--- a/nashorn/src/jdk/internal/dynalink/support/Backport.java	Wed Jul 05 19:08:14 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,99 +0,0 @@
-/*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-/*
- * This file is available under and governed by the GNU General Public
- * License version 2 only, as published by the Free Software Foundation.
- * However, the following notice accompanied the original version of this
- * file, and Oracle licenses the original version of this file under the BSD
- * license:
- */
-/*
-   Copyright 2009-2013 Attila Szegedi
-
-   Licensed under both the Apache License, Version 2.0 (the "Apache License")
-   and the BSD License (the "BSD License"), with licensee being free to
-   choose either of the two at their discretion.
-
-   You may not use this file except in compliance with either the Apache
-   License or the BSD License.
-
-   If you choose to use this file in compliance with the Apache License, the
-   following notice applies to you:
-
-       You may obtain a copy of the Apache License at
-
-           http://www.apache.org/licenses/LICENSE-2.0
-
-       Unless required by applicable law or agreed to in writing, software
-       distributed under the License is distributed on an "AS IS" BASIS,
-       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
-       implied. See the License for the specific language governing
-       permissions and limitations under the License.
-
-   If you choose to use this file in compliance with the BSD License, the
-   following notice applies to you:
-
-       Redistribution and use in source and binary forms, with or without
-       modification, are permitted provided that the following conditions are
-       met:
-       * Redistributions of source code must retain the above copyright
-         notice, this list of conditions and the following disclaimer.
-       * Redistributions in binary form must reproduce the above copyright
-         notice, this list of conditions and the following disclaimer in the
-         documentation and/or other materials provided with the distribution.
-       * Neither the name of the copyright holder nor the names of
-         contributors may be used to endorse or promote products derived from
-         this software without specific prior written permission.
-
-       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
-       IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
-       TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
-       PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
-       BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-       CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-       SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
-       BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-       WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-       OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-       ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-
-package jdk.internal.dynalink.support;
-
-import java.lang.invoke.MethodHandles;
-
-/**
- * @author Attila Szegedi
- */
-public class Backport {
-    /**
-     * True if Remi's JSR-292 backport agent is active; false if we're using native OpenJDK JSR-292 support.
-     */
-    public static final boolean inUse = MethodHandles.class.getName().startsWith("jsr292");
-
-    private Backport() {
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/src/jdk/internal/dynalink/support/ClassLoaderGetterContextProvider.java	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * This file is available under and governed by the GNU General Public
+ * License version 2 only, as published by the Free Software Foundation.
+ * However, the following notice accompanied the original version of this
+ * file, and Oracle licenses the original version of this file under the BSD
+ * license:
+ */
+/*
+   Copyright 2009-2013 Attila Szegedi
+
+   Licensed under both the Apache License, Version 2.0 (the "Apache License")
+   and the BSD License (the "BSD License"), with licensee being free to
+   choose either of the two at their discretion.
+
+   You may not use this file except in compliance with either the Apache
+   License or the BSD License.
+
+   If you choose to use this file in compliance with the Apache License, the
+   following notice applies to you:
+
+       You may obtain a copy of the Apache License at
+
+           http://www.apache.org/licenses/LICENSE-2.0
+
+       Unless required by applicable law or agreed to in writing, software
+       distributed under the License is distributed on an "AS IS" BASIS,
+       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+       implied. See the License for the specific language governing
+       permissions and limitations under the License.
+
+   If you choose to use this file in compliance with the BSD License, the
+   following notice applies to you:
+
+       Redistribution and use in source and binary forms, with or without
+       modification, are permitted provided that the following conditions are
+       met:
+       * Redistributions of source code must retain the above copyright
+         notice, this list of conditions and the following disclaimer.
+       * Redistributions in binary form must reproduce the above copyright
+         notice, this list of conditions and the following disclaimer in the
+         documentation and/or other materials provided with the distribution.
+       * Neither the name of the copyright holder nor the names of
+         contributors may be used to endorse or promote products derived from
+         this software without specific prior written permission.
+
+       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+       IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+       TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+       PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
+       BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+       CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+       SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+       BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+       WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+       OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+       ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+package jdk.internal.dynalink.support;
+
+import java.security.AccessControlContext;
+import java.security.Permissions;
+import java.security.ProtectionDomain;
+
+/**
+ * This class exposes a canonical {@link AccessControlContext} with a single {@link RuntimePermission} for
+ * {@code "getClassLoader"} permission that is used by other parts of the code to narrow their set of permissions when
+ * they're retrieving class loaders in privileged blocks.
+ */
+public class ClassLoaderGetterContextProvider {
+    /**
+     * Canonical instance of {@link AccessControlContext} with a single {@link RuntimePermission} for
+     * {@code "getClassLoader"} permission.
+     */
+    public static final AccessControlContext GET_CLASS_LOADER_CONTEXT;
+    static {
+        final Permissions perms = new Permissions();
+        perms.add(new RuntimePermission("getClassLoader"));
+        GET_CLASS_LOADER_CONTEXT = new AccessControlContext(
+                new ProtectionDomain[] { new ProtectionDomain(null, perms) });
+    }
+}
--- a/nashorn/src/jdk/internal/dynalink/support/ClassMap.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/internal/dynalink/support/ClassMap.java	Tue Aug 20 17:46:45 2013 -0700
@@ -85,6 +85,8 @@
 
 import java.lang.ref.Reference;
 import java.lang.ref.SoftReference;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.Map;
 import java.util.WeakHashMap;
 import java.util.concurrent.ConcurrentHashMap;
@@ -122,21 +124,12 @@
     protected abstract T computeValue(Class<?> clazz);
 
     /**
-     * Returns the class loader that governs the strong referenceability of this class map.
-     *
-     * @return the class loader that governs the strong referenceability of this class map.
-     */
-    public ClassLoader getClassLoader() {
-        return classLoader;
-    }
-
-    /**
      * Returns the value associated with the class
      *
      * @param clazz the class
      * @return the value associated with the class
      */
-    public T get(Class<?> clazz) {
+    public T get(final Class<?> clazz) {
         // Check in fastest first - objects we're allowed to strongly reference
         final T v = map.get(clazz);
         if(v != null) {
@@ -156,8 +149,16 @@
         // Not found in either place; create a new value
         final T newV = computeValue(clazz);
         assert newV != null;
+
+        final ClassLoader clazzLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
+            @Override
+            public ClassLoader run() {
+                return clazz.getClassLoader();
+            }
+        }, ClassLoaderGetterContextProvider.GET_CLASS_LOADER_CONTEXT);
+
         // If allowed to strongly reference, put it in the fast map
-        if(Guards.canReferenceDirectly(classLoader, clazz.getClassLoader())) {
+        if(Guards.canReferenceDirectly(classLoader, clazzLoader)) {
             final T oldV = map.putIfAbsent(clazz, newV);
             return oldV != null ? oldV : newV;
         }
--- a/nashorn/src/jdk/internal/dynalink/support/Guards.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/internal/dynalink/support/Guards.java	Tue Aug 20 17:46:45 2013 -0700
@@ -258,23 +258,24 @@
                 type.changeReturnType(Boolean.TYPE), new int[] { pos });
     }
 
-    private static final MethodHandle IS_OF_CLASS = new Lookup(MethodHandles.lookup()).findStatic(Guards.class,
-            "isOfClass", MethodType.methodType(Boolean.TYPE, Class.class, Object.class));
-
     private static final MethodHandle IS_INSTANCE = Lookup.PUBLIC.findVirtual(Class.class, "isInstance",
             MethodType.methodType(Boolean.TYPE, Object.class));
 
-    private static final MethodHandle IS_ARRAY = new Lookup(MethodHandles.lookup()).findStatic(Guards.class, "isArray",
-            MethodType.methodType(Boolean.TYPE, Object.class));
-
-    private static final MethodHandle IS_IDENTICAL = new Lookup(MethodHandles.lookup()).findStatic(Guards.class,
-            "isIdentical", MethodType.methodType(Boolean.TYPE, Object.class, Object.class));
+    private static final MethodHandle IS_OF_CLASS;
+    private static final MethodHandle IS_ARRAY;
+    private static final MethodHandle IS_IDENTICAL;
+    private static final MethodHandle IS_NULL;
+    private static final MethodHandle IS_NOT_NULL;
 
-    private static final MethodHandle IS_NULL = new Lookup(MethodHandles.lookup()).findStatic(Guards.class,
-            "isNull", MethodType.methodType(Boolean.TYPE, Object.class));
+    static {
+        final Lookup lookup = new Lookup(MethodHandles.lookup());
 
-    private static final MethodHandle IS_NOT_NULL = new Lookup(MethodHandles.lookup()).findStatic(Guards.class,
-            "isNotNull", MethodType.methodType(Boolean.TYPE, Object.class));
+        IS_OF_CLASS  = lookup.findOwnStatic("isOfClass",   Boolean.TYPE, Class.class, Object.class);
+        IS_ARRAY     = lookup.findOwnStatic("isArray",     Boolean.TYPE, Object.class);
+        IS_IDENTICAL = lookup.findOwnStatic("isIdentical", Boolean.TYPE, Object.class, Object.class);
+        IS_NULL      = lookup.findOwnStatic("isNull",      Boolean.TYPE, Object.class);
+        IS_NOT_NULL  = lookup.findOwnStatic("isNotNull",   Boolean.TYPE, Object.class);
+    }
 
     /**
      * Creates a guard method that tests its only argument for being of an exact particular class.
--- a/nashorn/src/jdk/internal/dynalink/support/Lookup.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/internal/dynalink/support/Lookup.java	Tue Aug 20 17:46:45 2013 -0700
@@ -89,7 +89,6 @@
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
 
 /**
  * A wrapper around MethodHandles.Lookup that masks checked exceptions in those cases when you're looking up methods
@@ -235,9 +234,8 @@
     }
 
     /**
-     * Performs a findSpecial on the underlying lookup, except for the backport where it rather uses unreflect. Converts
-     * any encountered {@link IllegalAccessException} into an {@link IllegalAccessError} and a
-     * {@link NoSuchMethodException} into a {@link NoSuchMethodError}.
+     * Performs a findSpecial on the underlying lookup. Converts any encountered {@link IllegalAccessException} into an
+     * {@link IllegalAccessError} and a {@link NoSuchMethodException} into a {@link NoSuchMethodError}.
      *
      * @param declaringClass class declaring the method
      * @param name the name of the method
@@ -248,13 +246,6 @@
      */
     public MethodHandle findSpecial(Class<?> declaringClass, String name, MethodType type) {
         try {
-            if(Backport.inUse) {
-                final Method m = declaringClass.getDeclaredMethod(name, type.parameterArray());
-                if(!Modifier.isPublic(declaringClass.getModifiers()) || !Modifier.isPublic(m.getModifiers())) {
-                    m.setAccessible(true);
-                }
-                return unreflect(m);
-            }
             return lookup.findSpecial(declaringClass, name, type, declaringClass);
         } catch(IllegalAccessException e) {
             final IllegalAccessError ee = new IllegalAccessError("Failed to access special method " + methodDescription(
--- a/nashorn/src/jdk/internal/dynalink/support/TypeConverterFactory.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/internal/dynalink/support/TypeConverterFactory.java	Tue Aug 20 17:46:45 2013 -0700
@@ -87,6 +87,8 @@
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
 import java.lang.invoke.WrongMethodTypeException;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.LinkedList;
 import java.util.List;
 import jdk.internal.dynalink.linker.ConversionComparator;
@@ -110,7 +112,7 @@
     private final ClassValue<ClassMap<MethodHandle>> converterMap = new ClassValue<ClassMap<MethodHandle>>() {
         @Override
         protected ClassMap<MethodHandle> computeValue(final Class<?> sourceType) {
-            return new ClassMap<MethodHandle>(sourceType.getClassLoader()) {
+            return new ClassMap<MethodHandle>(getClassLoader(sourceType)) {
                 @Override
                 protected MethodHandle computeValue(Class<?> targetType) {
                     try {
@@ -128,7 +130,7 @@
     private final ClassValue<ClassMap<MethodHandle>> converterIdentityMap = new ClassValue<ClassMap<MethodHandle>>() {
         @Override
         protected ClassMap<MethodHandle> computeValue(final Class<?> sourceType) {
-            return new ClassMap<MethodHandle>(sourceType.getClassLoader()) {
+            return new ClassMap<MethodHandle>(getClassLoader(sourceType)) {
                 @Override
                 protected MethodHandle computeValue(Class<?> targetType) {
                     if(!canAutoConvert(sourceType, targetType)) {
@@ -143,6 +145,15 @@
         }
     };
 
+    private static final ClassLoader getClassLoader(final Class<?> clazz) {
+        return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
+            @Override
+            public ClassLoader run() {
+                return clazz.getClassLoader();
+            }
+        }, ClassLoaderGetterContextProvider.GET_CLASS_LOADER_CONTEXT);
+    }
+
     /**
      * Creates a new type converter factory from the available {@link GuardingTypeConverterFactory} instances.
      *
--- a/nashorn/src/jdk/nashorn/api/scripting/NashornScriptEngine.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/api/scripting/NashornScriptEngine.java	Tue Aug 20 17:46:45 2013 -0700
@@ -36,10 +36,13 @@
 import java.lang.reflect.Modifier;
 import java.net.URL;
 import java.nio.charset.Charset;
+import java.security.AccessControlContext;
 import java.security.AccessController;
+import java.security.Permissions;
 import java.security.PrivilegedAction;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
+import java.security.ProtectionDomain;
 import java.text.MessageFormat;
 import java.util.Locale;
 import java.util.ResourceBundle;
@@ -71,6 +74,14 @@
  */
 
 public final class NashornScriptEngine extends AbstractScriptEngine implements Compilable, Invocable {
+    private static AccessControlContext createPermAccCtxt(final String permName) {
+        final Permissions perms = new Permissions();
+        perms.add(new RuntimePermission(permName));
+        return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
+    }
+
+    private static final AccessControlContext CREATE_CONTEXT_ACC_CTXT = createPermAccCtxt(Context.NASHORN_CREATE_CONTEXT);
+    private static final AccessControlContext CREATE_GLOBAL_ACC_CTXT  = createPermAccCtxt(Context.NASHORN_CREATE_GLOBAL);
 
     private final ScriptEngineFactory factory;
     private final Context             nashornContext;
@@ -84,16 +95,9 @@
 
     private static final String MESSAGES_RESOURCE = "jdk.nashorn.api.scripting.resources.Messages";
 
-    // Without do privileged, under security manager messages can not be loaded.
     private static final ResourceBundle MESSAGES_BUNDLE;
     static {
-        MESSAGES_BUNDLE = AccessController.doPrivileged(
-        new PrivilegedAction<ResourceBundle>() {
-            @Override
-            public ResourceBundle run() {
-                return ResourceBundle.getBundle(MESSAGES_RESOURCE, Locale.getDefault());
-            }
-        });
+        MESSAGES_BUNDLE = ResourceBundle.getBundle(MESSAGES_RESOURCE, Locale.getDefault());
     }
 
     private static String getMessage(final String msgId, final String... args) {
@@ -128,7 +132,7 @@
                     throw e;
                 }
             }
-        });
+        }, CREATE_CONTEXT_ACC_CTXT);
 
         // create new global object
         this.global = createNashornGlobal();
@@ -340,7 +344,7 @@
                     throw e;
                 }
             }
-        });
+        }, CREATE_GLOBAL_ACC_CTXT);
 
         nashornContext.initGlobal(newGlobal);
 
@@ -362,10 +366,8 @@
     }
 
     private void evalEngineScript() throws ScriptException {
-        evalSupportScript("resources/engine.js", NashornException.ENGINE_SCRIPT_SOURCE_NAME);
-    }
-
-    private void evalSupportScript(final String script, final String name) throws ScriptException {
+        final String script = "resources/engine.js";
+        final String name   = NashornException.ENGINE_SCRIPT_SOURCE_NAME;
         try {
             final InputStream is = AccessController.doPrivileged(
                     new PrivilegedExceptionAction<InputStream>() {
@@ -380,6 +382,9 @@
                 eval(isr);
             }
         } catch (final PrivilegedActionException | IOException e) {
+            if (Context.DEBUG) {
+                e.printStackTrace();
+            }
             throw new ScriptException(e);
         } finally {
             put(ScriptEngine.FILENAME, null);
--- a/nashorn/src/jdk/nashorn/api/scripting/NashornScriptEngineFactory.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/api/scripting/NashornScriptEngineFactory.java	Tue Aug 20 17:46:45 2013 -0700
@@ -30,6 +30,7 @@
 import java.util.List;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineFactory;
+import jdk.nashorn.internal.runtime.Context;
 import jdk.nashorn.internal.runtime.Version;
 
 /**
@@ -136,7 +137,14 @@
 
     @Override
     public ScriptEngine getScriptEngine() {
-        return new NashornScriptEngine(this, getAppClassLoader());
+        try {
+            return new NashornScriptEngine(this, getAppClassLoader());
+        } catch (final RuntimeException e) {
+            if (Context.DEBUG) {
+                e.printStackTrace();
+            }
+            throw e;
+        }
     }
 
     /**
@@ -178,7 +186,7 @@
     private static void checkConfigPermission() {
         final SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
-            sm.checkPermission(new RuntimePermission("nashorn.setConfig"));
+            sm.checkPermission(new RuntimePermission(Context.NASHORN_SET_CONFIG));
         }
     }
 
--- a/nashorn/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java	Tue Aug 20 17:46:45 2013 -0700
@@ -25,14 +25,17 @@
 
 package jdk.nashorn.api.scripting;
 
+import java.security.AccessControlContext;
 import java.security.AccessController;
+import java.security.Permissions;
 import java.security.PrivilegedAction;
+import java.security.ProtectionDomain;
 import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Iterator;
 import java.util.LinkedHashSet;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -49,6 +52,14 @@
  * netscape.javascript.JSObject interface.
  */
 public final class ScriptObjectMirror extends JSObject implements Bindings {
+    private static AccessControlContext getContextAccCtxt() {
+        final Permissions perms = new Permissions();
+        perms.add(new RuntimePermission(Context.NASHORN_GET_CONTEXT));
+        return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
+    }
+
+    private static final AccessControlContext GET_CONTEXT_ACC_CTXT = getContextAccCtxt();
+
     private final ScriptObject sobj;
     private final ScriptObject global;
 
@@ -144,7 +155,7 @@
                             public Context run() {
                                 return Context.getContext();
                             }
-                        });
+                        }, GET_CONTEXT_ACC_CTXT);
                 return wrap(context.eval(global, s, null, null, false), global);
             }
         });
--- a/nashorn/src/jdk/nashorn/internal/codegen/CompileUnit.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/codegen/CompileUnit.java	Tue Aug 20 17:46:45 2013 -0700
@@ -33,7 +33,7 @@
     private final String className;
 
     /** Current class generator */
-    private final ClassEmitter classEmitter;
+    private ClassEmitter classEmitter;
 
     private long weight;
 
@@ -64,7 +64,11 @@
      * @param clazz class with code for this compile unit
      */
     void setCode(final Class<?> clazz) {
+        clazz.getClass(); // null check
         this.clazz = clazz;
+        // Revisit this - refactor to avoid null-ed out non-final fields
+        // null out emitter
+        this.classEmitter = null;
     }
 
     /**
--- a/nashorn/src/jdk/nashorn/internal/codegen/CompilerConstants.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/codegen/CompilerConstants.java	Tue Aug 20 17:46:45 2013 -0700
@@ -489,20 +489,6 @@
     }
 
     /**
-     * Create a static call, looking up the method handle for it at the same time
-     *
-     * @param clazz  the class
-     * @param name   the name of the method
-     * @param rtype  the return type of the method
-     * @param ptypes the parameter types of the method
-     *
-     * @return the call object representing the static call
-     */
-    public static Call staticCall(final Class<?> clazz, final String name, final Class<?> rtype, final Class<?>... ptypes) {
-        return staticCall(MethodHandles.publicLookup(), clazz, name, rtype, ptypes);
-    }
-
-    /**
      * Create a static call, given an explicit lookup, looking up the method handle for it at the same time
      *
      * @param lookup the lookup
@@ -523,20 +509,6 @@
     }
 
     /**
-     * Create a virtual call, looking up the method handle for it at the same time
-     *
-     * @param clazz  the class
-     * @param name   the name of the method
-     * @param rtype  the return type of the method
-     * @param ptypes the parameter types of the method
-     *
-     * @return the call object representing the virtual call
-     */
-    public static Call virtualCall(final Class<?> clazz, final String name, final Class<?> rtype, final Class<?>... ptypes) {
-        return virtualCall(MethodHandles.publicLookup(), clazz, name, rtype, ptypes);
-    }
-
-    /**
      * Create a virtual call, given an explicit lookup, looking up the method handle for it at the same time
      *
      * @param lookup the lookup
--- a/nashorn/src/jdk/nashorn/internal/codegen/ObjectClassGenerator.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/codegen/ObjectClassGenerator.java	Tue Aug 20 17:46:45 2013 -0700
@@ -317,7 +317,8 @@
         final String       className    = getClassName(fieldCount);
         final String       superName    = className(ScriptObject.class);
         final ClassEmitter classEmitter = newClassEmitter(className, superName);
-        final List<String> initFields   = addFields(classEmitter, fieldCount);
+
+        addFields(classEmitter, fieldCount);
 
         final MethodEmitter init = newInitMethod(classEmitter);
         init.returnVoid();
--- a/nashorn/src/jdk/nashorn/internal/codegen/ObjectCreator.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/codegen/ObjectCreator.java	Tue Aug 20 17:46:45 2013 -0700
@@ -45,9 +45,11 @@
     /** Code generator */
     protected final CodeGenerator codegen;
 
-    private   final boolean       isScope;
-    private   final boolean       hasArguments;
-    protected       PropertyMap   propertyMap;
+    /** Property map */
+    protected PropertyMap   propertyMap;
+
+    private final boolean       isScope;
+    private final boolean       hasArguments;
 
     /**
      * Constructor
--- a/nashorn/src/jdk/nashorn/internal/ir/BinaryNode.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/ir/BinaryNode.java	Tue Aug 20 17:46:45 2013 -0700
@@ -99,6 +99,7 @@
         case DIV:
         case MOD:
         case MUL:
+        case SUB:
         case ASSIGN_DIV:
         case ASSIGN_MOD:
         case ASSIGN_MUL:
--- a/nashorn/src/jdk/nashorn/internal/ir/BreakableNode.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/ir/BreakableNode.java	Tue Aug 20 17:46:45 2013 -0700
@@ -33,6 +33,14 @@
  * a {@code break} statement
  */
 public interface BreakableNode extends LexicalContextNode {
+    /**
+     * Ensure that any labels in this breakable node are unique so
+     * that new jumps won't go to old parts of the tree. Used for
+     * example for cloning finally blocks
+     *
+     * @param lc the lexical context
+     * @return node after labels have been made unique
+     */
     public abstract Node ensureUniqueLabels(final LexicalContext lc);
 
     /**
--- a/nashorn/src/jdk/nashorn/internal/ir/IdentNode.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/ir/IdentNode.java	Tue Aug 20 17:46:45 2013 -0700
@@ -161,13 +161,13 @@
      * converting to object, for example if the symbol is used as the left hand side of an
      * assignment such as in the code below.</p>
      *
-     * <pre>{@code
+     * <pre>
      *   try {
      *     return 2;
      *   } finally {
      *     return 3;
      *   }
-     * }</pre>
+     * }
      *
      * @return true if can have callsite type
      */
--- a/nashorn/src/jdk/nashorn/internal/ir/LexicalContextNode.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/ir/LexicalContextNode.java	Tue Aug 20 17:46:45 2013 -0700
@@ -44,8 +44,14 @@
     Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor);
 
     // Would be a default method on Java 8
+    /**
+     * Helper class for accept for items of this lexical context, delegates to the
+     * subclass accept and makes sure that the node is on the context before accepting
+     * and gets popped after accepting (and that the stack is consistent in that the
+     * node has been replaced with the possible new node resulting in visitation)
+     */
     static class Acceptor {
-        static Node accept(LexicalContextNode node, final NodeVisitor<? extends LexicalContext> visitor) {
+        static Node accept(final LexicalContextNode node, final NodeVisitor<? extends LexicalContext> visitor) {
             final LexicalContext lc = visitor.getLexicalContext();
             lc.push(node);
             final LexicalContextNode newNode = (LexicalContextNode)node.accept(lc, visitor);
--- a/nashorn/src/jdk/nashorn/internal/objects/Global.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/Global.java	Tue Aug 20 17:46:45 2013 -0700
@@ -35,12 +35,12 @@
 import java.lang.invoke.MethodHandles;
 import java.lang.ref.SoftReference;
 import java.lang.reflect.Field;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
 import java.util.Arrays;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ConcurrentHashMap;
 import jdk.internal.dynalink.linker.GuardedInvocation;
 import jdk.internal.dynalink.linker.LinkRequest;
 import jdk.nashorn.internal.objects.annotations.Attribute;
@@ -72,8 +72,8 @@
  */
 @ScriptClass("Global")
 public final class Global extends ScriptObject implements GlobalObject, Scope {
-    private static final InvokeByName TO_STRING = new InvokeByName("toString", ScriptObject.class);
-    private static final InvokeByName VALUE_OF  = new InvokeByName("valueOf",  ScriptObject.class);
+    private final InvokeByName TO_STRING = new InvokeByName("toString", ScriptObject.class);
+    private final InvokeByName VALUE_OF  = new InvokeByName("valueOf",  ScriptObject.class);
 
     /** ECMA 15.1.2.2 parseInt (string , radix) */
     @Property(attributes = Attribute.NOT_ENUMERABLE)
@@ -418,7 +418,7 @@
         // security check first
         final SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
-            sm.checkPermission(new RuntimePermission("nashorn.newGlobal"));
+            sm.checkPermission(new RuntimePermission(Context.NASHORN_CREATE_GLOBAL));
         }
 
         // null check on context
@@ -709,6 +709,35 @@
         classCache.put(source, new SoftReference<Class<?>>(clazz));
     }
 
+    private static <T> T getLazilyCreatedValue(final Object key, final Callable<T> creator, final Map<Object, T> map) {
+        final T obj = map.get(key);
+        if (obj != null) {
+            return obj;
+        }
+
+        try {
+            final T newObj = creator.call();
+            final T existingObj = map.putIfAbsent(key, newObj);
+            return existingObj != null ? existingObj : newObj;
+        } catch (final Exception exp) {
+            throw new RuntimeException(exp);
+        }
+    }
+
+    private final Map<Object, InvokeByName> namedInvokers = new ConcurrentHashMap<>();
+
+    @Override
+    public InvokeByName getInvokeByName(final Object key, final Callable<InvokeByName> creator) {
+        return getLazilyCreatedValue(key, creator, namedInvokers);
+    }
+
+    private final Map<Object, MethodHandle> dynamicInvokers = new ConcurrentHashMap<>();
+
+    @Override
+    public MethodHandle getDynamicInvoker(final Object key, final Callable<MethodHandle> creator) {
+        return getLazilyCreatedValue(key, creator, dynamicInvokers);
+    }
+
     /**
      * This is the eval used when 'indirect' eval call is made.
      *
@@ -1749,19 +1778,13 @@
     }
 
     private static void copyOptions(final ScriptObject options, final ScriptEnvironment scriptEnv) {
-        AccessController.doPrivileged(new PrivilegedAction<Void>() {
-            @Override
-            public Void run() {
-                for (Field f : scriptEnv.getClass().getFields()) {
-                    try {
-                        options.set(f.getName(), f.get(scriptEnv), false);
-                    } catch (final IllegalArgumentException | IllegalAccessException exp) {
-                        throw new RuntimeException(exp);
-                    }
-                }
-                return null;
+        for (Field f : scriptEnv.getClass().getFields()) {
+            try {
+                options.set(f.getName(), f.get(scriptEnv), false);
+            } catch (final IllegalArgumentException | IllegalAccessException exp) {
+                throw new RuntimeException(exp);
             }
-        });
+        }
     }
 
     private void initTypedArray() {
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeArguments.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeArguments.java	Tue Aug 20 17:46:45 2013 -0700
@@ -76,36 +76,21 @@
 
     private Object length;
     private Object callee;
-    private ArrayData namedArgs;
-    // This is lazily initialized - only when delete is invoked at all
+    private final int numMapped;
+    private final int numParams;
+
+    // These are lazily initialized when delete is invoked on a mapped arg or an unmapped argument is set.
+    private ArrayData unmappedArgs;
     private BitSet deleted;
 
     NativeArguments(final Object[] arguments, final Object callee, final int numParams, final ScriptObject proto, final PropertyMap map) {
         super(proto, map);
         setIsArguments();
-
         setArray(ArrayData.allocate(arguments));
         this.length = arguments.length;
         this.callee = callee;
-
-        /**
-         * Declared number of parameters may be more or less than the actual passed
-         * runtime arguments count. We need to truncate or extend with undefined values.
-         *
-         * Example:
-         *
-         * // less declared params
-         * (function (x) { print(arguments); })(20, 44);
-         *
-         * // more declared params
-         * (function (x, y) { print(arguments); })(3);
-         */
-        final Object[] newValues = new Object[numParams];
-        if (numParams > arguments.length) {
-            Arrays.fill(newValues, UNDEFINED);
-        }
-        System.arraycopy(arguments, 0, newValues, 0, Math.min(newValues.length, arguments.length));
-        this.namedArgs = ArrayData.allocate(newValues);
+        this.numMapped = Math.min(numParams, arguments.length);
+        this.numParams = numParams;
     }
 
     @Override
@@ -118,7 +103,8 @@
      */
     @Override
     public Object getArgument(final int key) {
-        return namedArgs.has(key) ? namedArgs.getObject(key) : UNDEFINED;
+        assert key >= 0 && key < numParams : "invalid argument index";
+        return isMapped(key) ? getArray().getObject(key) : getUnmappedArg(key);
     }
 
     /**
@@ -126,353 +112,36 @@
      */
     @Override
     public void setArgument(final int key, final Object value) {
-        if (namedArgs.has(key)) {
-            namedArgs = namedArgs.set(key, value, false);
-        }
-    }
-
-    @Override
-    public int getInt(final Object key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getInt(index) : super.getInt(key);
-    }
-
-    @Override
-    public int getInt(final double key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getInt(index) : super.getInt(key);
-    }
-
-    @Override
-    public int getInt(final long key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getInt(index) : super.getInt(key);
-    }
-
-    @Override
-    public int getInt(final int key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getInt(index) : super.getInt(key);
-    }
-
-    @Override
-    public long getLong(final Object key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getLong(index) : super.getLong(key);
-    }
-
-    @Override
-    public long getLong(final double key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getLong(index) : super.getLong(key);
-    }
-
-    @Override
-    public long getLong(final long key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getLong(index) : super.getLong(key);
-    }
-
-    @Override
-    public long getLong(final int key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getLong(index) : super.getLong(key);
-    }
-
-    @Override
-    public double getDouble(final Object key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getDouble(index) : super.getDouble(key);
-    }
-
-    @Override
-    public double getDouble(final double key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getDouble(index) : super.getDouble(key);
-    }
-
-    @Override
-    public double getDouble(final long key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getDouble(index) : super.getDouble(key);
-    }
-
-    @Override
-    public double getDouble(final int key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getDouble(index) : super.getDouble(key);
-    }
-
-    @Override
-    public Object get(final Object key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getObject(index) : super.get(key);
-    }
-
-    @Override
-    public Object get(final double key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getObject(index) : super.get(key);
-    }
-
-    @Override
-    public Object get(final long key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getObject(index) : super.get(key);
-    }
-
-    @Override
-    public Object get(final int key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) ? namedArgs.getObject(index) : super.get(key);
-    }
-
-    @Override
-    public void set(final Object key, final int value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
+        assert key >= 0 && key < numParams : "invalid argument index";
+        if (isMapped(key)) {
+            setArray(getArray().set(key, value, false));
         } else {
-            super.set(key, value, strict);
-        }
-    }
-
-    @Override
-    public void set(final Object key, final long value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
-        } else {
-            super.set(key, value, strict);
-        }
-    }
-
-    @Override
-    public void set(final Object key, final double value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
-        } else {
-            super.set(key, value, strict);
-        }
-    }
-
-    @Override
-    public void set(final Object key, final Object value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
-        } else {
-            super.set(key, value, strict);
-        }
-    }
-
-    @Override
-    public void set(final double key, final int value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
-        } else {
-            super.set(key, value, strict);
+            setUnmappedArg(key, value);
         }
     }
 
     @Override
-    public void set(final double key, final long value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
-        } else {
-            super.set(key, value, strict);
-        }
-    }
-
-    @Override
-    public void set(final double key, final double value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
-        } else {
-            super.set(key, value, strict);
-        }
-    }
-
-    @Override
-    public void set(final double key, final Object value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
-        } else {
-            super.set(key, value, strict);
-        }
-    }
-
-    @Override
-    public void set(final long key, final int value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
-        } else {
-            super.set(key, value, strict);
-        }
-    }
-
-    @Override
-    public void set(final long key, final long value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
-        } else {
-            super.set(key, value, strict);
-        }
-    }
-
-    @Override
-    public void set(final long key, final double value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
-        } else {
-            super.set(key, value, strict);
-        }
-    }
-
-    @Override
-    public void set(final long key, final Object value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
-        } else {
-            super.set(key, value, strict);
-        }
-    }
-
-    @Override
-    public void set(final int key, final int value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
-        } else {
-            super.set(key, value, strict);
-        }
-    }
-
-    @Override
-    public void set(final int key, final long value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
-        } else {
-            super.set(key, value, strict);
-        }
-    }
-
-    @Override
-    public void set(final int key, final double value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
-        } else {
-            super.set(key, value, strict);
-        }
-    }
-
-    @Override
-    public void set(final int key, final Object value, final boolean strict) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        if (isMapped(index)) {
-            namedArgs = namedArgs.set(index, value, strict);
-        } else {
-            super.set(key, value, strict);
-        }
-    }
-
-    @Override
-    public boolean has(final Object key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) || super.has(key);
-    }
-
-    @Override
-    public boolean has(final double key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) || super.has(key);
-    }
-
-    @Override
-    public boolean has(final long key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) || super.has(key);
-    }
-
-    @Override
-    public boolean has(final int key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) || super.has(key);
-    }
-
-    @Override
-    public boolean hasOwnProperty(final Object key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) || super.hasOwnProperty(key);
-    }
-
-    @Override
-    public boolean hasOwnProperty(final int key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) || super.hasOwnProperty(key);
-    }
-
-    @Override
-    public boolean hasOwnProperty(final long key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) || super.hasOwnProperty(key);
-    }
-
-    @Override
-    public boolean hasOwnProperty(final double key) {
-        final int index = ArrayIndex.getArrayIndex(key);
-        return isMapped(index) || super.hasOwnProperty(key);
-    }
-
-    @Override
     public boolean delete(final int key, final boolean strict) {
         final int index = ArrayIndex.getArrayIndex(key);
-        final boolean success = super.delete(key, strict);
-        if (success && namedArgs.has(index)) {
-            setDeleted(index);
-        }
-        return success;
+        return isMapped(index) ? deleteMapped(index, strict) : super.delete(key, strict);
     }
 
     @Override
     public boolean delete(final long key, final boolean strict) {
         final int index = ArrayIndex.getArrayIndex(key);
-        final boolean success = super.delete(key, strict);
-        if (success && namedArgs.has(index)) {
-            setDeleted(index);
-        }
-        return success;
+        return isMapped(index) ? deleteMapped(index, strict) : super.delete(key, strict);
     }
 
     @Override
     public boolean delete(final double key, final boolean strict) {
         final int index = ArrayIndex.getArrayIndex(key);
-        final boolean success = super.delete(key, strict);
-        if (success && namedArgs.has(index)) {
-            setDeleted(index);
-        }
-        return success;
+        return isMapped(index) ? deleteMapped(index, strict) : super.delete(key, strict);
     }
 
     @Override
     public boolean delete(final Object key, final boolean strict) {
         final int index = ArrayIndex.getArrayIndex(key);
-        final boolean success = super.delete(key, strict);
-        if (success && namedArgs.has(index)) {
-            setDeleted(index);
-        }
-        return success;
+        return isMapped(index) ? deleteMapped(index, strict) : super.delete(key, strict);
     }
 
     /**
@@ -483,29 +152,27 @@
     public boolean defineOwnProperty(final String key, final Object propertyDesc, final boolean reject) {
         final int index = ArrayIndex.getArrayIndex(key);
         if (index >= 0) {
-            final boolean allowed = super.defineOwnProperty(key, propertyDesc, false);
-            if (!allowed) {
+            final boolean isMapped = isMapped(index);
+            final Object oldValue = isMapped ? getArray().getObject(index) : null;
+
+            if (!super.defineOwnProperty(key, propertyDesc, false)) {
                 if (reject) {
                     throw typeError("cant.redefine.property",  key, ScriptRuntime.safeToString(this));
                 }
                 return false;
             }
 
-            if (isMapped(index)) {
+            if (isMapped) {
                 // When mapped argument is redefined, if new descriptor is accessor property
                 // or data-non-writable property, we have to "unmap" (unlink).
                 final PropertyDescriptor desc = toPropertyDescriptor(Global.instance(), propertyDesc);
                 if (desc.type() == PropertyDescriptor.ACCESSOR) {
-                    setDeleted(index);
-                } else {
-                    // set "value" from new descriptor to named args
-                    if (desc.has(PropertyDescriptor.VALUE)) {
-                        namedArgs = namedArgs.set(index, desc.getValue(), false);
-                    }
-
-                    if (desc.has(PropertyDescriptor.WRITABLE) && !desc.isWritable()) {
-                        setDeleted(index);
-                    }
+                    setDeleted(index, oldValue);
+                } else if (desc.has(PropertyDescriptor.WRITABLE) && !desc.isWritable()) {
+                    // delete and set value from new descriptor if it has one, otherwise use old value
+                    setDeleted(index, desc.has(PropertyDescriptor.VALUE) ? desc.getValue() : oldValue);
+                } else if (desc.has(PropertyDescriptor.VALUE)) {
+                    setArray(getArray().set(index, desc.getValue(), false));
                 }
             }
 
@@ -519,31 +186,72 @@
 
     // We track deletions using a bit set (delete arguments[index])
     private boolean isDeleted(final int index) {
-        return (deleted != null) ? deleted.get(index) : false;
+        return deleted != null && deleted.get(index);
+    }
+
+    private void setDeleted(final int index, final Object unmappedValue) {
+        if (deleted == null) {
+            deleted = new BitSet(numMapped);
+        }
+        deleted.set(index, true);
+        setUnmappedArg(index, unmappedValue);
+    }
+
+    private boolean deleteMapped(final int index, final boolean strict) {
+        final Object value = getArray().getObject(index);
+        final boolean success = super.delete(index, strict);
+        if (success) {
+            setDeleted(index, value);
+        }
+        return success;
+    }
+
+    private Object getUnmappedArg(final int key) {
+        assert key >= 0 && key < numParams;
+        return unmappedArgs == null ? UNDEFINED : unmappedArgs.getObject(key);
     }
 
-    private void setDeleted(final int index) {
-        if (deleted == null) {
-            deleted = new BitSet((int)namedArgs.length());
+    private void setUnmappedArg(final int key, final Object value) {
+        assert key >= 0 && key < numParams;
+        if (unmappedArgs == null) {
+            /*
+             * Declared number of parameters may be more or less than the actual passed
+             * runtime arguments count. We need to truncate or extend with undefined values.
+             *
+             * Example:
+             *
+             * // less declared params
+             * (function (x) { print(arguments); })(20, 44);
+             *
+             * // more declared params
+             * (function (x, y) { print(arguments); })(3);
+             */
+            final Object[] newValues = new Object[numParams];
+            System.arraycopy(getArray().asObjectArray(), 0, newValues, 0, numMapped);
+            if (numMapped < numParams) {
+                Arrays.fill(newValues, numMapped, numParams, UNDEFINED);
+            }
+            this.unmappedArgs = ArrayData.allocate(newValues);
         }
-        deleted.set(index, true);
+        // Set value of argument
+        unmappedArgs = unmappedArgs.set(key, value, false);
     }
 
     /**
      * Are arguments[index] and corresponding named parameter linked?
      *
-     * In non-strict mode, arguments[index] and corresponding named param
-     * are "linked" or "mapped". Modifications are tacked b/w each other - till
-     * (delete arguments[index]) is used. Once deleted, the corresponding arg
-     * is no longer 'mapped'. Please note that delete can happen only through
-     * the arguments array - named param can not be deleted. (delete is one-way).
+     * In non-strict mode, arguments[index] and corresponding named param are "linked" or "mapped"
+     * if the argument is provided by the caller. Modifications are tacked b/w each other - until
+     * (delete arguments[index]) is used. Once deleted, the corresponding arg is no longer 'mapped'.
+     * Please note that delete can happen only through the arguments array - named param can not
+     * be deleted. (delete is one-way).
      */
     private boolean isMapped(final int index) {
-        // in named args and not marked as "deleted"
-        return namedArgs.has(index) && !isDeleted(index);
+        // in mapped named args and not marked as "deleted"
+        return index >= 0 && index < numMapped && !isDeleted(index);
     }
 
-        /**
+    /**
      * Factory to create correct Arguments object based on strict mode.
      *
      * @param arguments the actual arguments array passed
@@ -558,9 +266,8 @@
         final ScriptObject proto = global.getObjectPrototype();
         if (isStrict) {
             return new NativeStrictArguments(arguments, numParams, proto, global.getStrictArgumentsMap());
-        } else {
-            return new NativeArguments(arguments, callee, numParams, proto, global.getArgumentsMap());
         }
+        return new NativeArguments(arguments, callee, numParams, proto, global.getArgumentsMap());
     }
 
     /**
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeArray.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeArray.java	Tue Aug 20 17:46:45 2013 -0700
@@ -39,6 +39,7 @@
 import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
+import java.util.concurrent.Callable;
 import jdk.nashorn.api.scripting.ScriptObjectMirror;
 import jdk.nashorn.internal.objects.annotations.Attribute;
 import jdk.nashorn.internal.objects.annotations.Constructor;
@@ -68,20 +69,88 @@
  */
 @ScriptClass("Array")
 public final class NativeArray extends ScriptObject {
-    private static final InvokeByName JOIN = new InvokeByName("join", ScriptObject.class);
+    private static final Object JOIN                     = new Object();
+    private static final Object EVERY_CALLBACK_INVOKER   = new Object();
+    private static final Object SOME_CALLBACK_INVOKER    = new Object();
+    private static final Object FOREACH_CALLBACK_INVOKER = new Object();
+    private static final Object MAP_CALLBACK_INVOKER     = new Object();
+    private static final Object FILTER_CALLBACK_INVOKER  = new Object();
+    private static final Object REDUCE_CALLBACK_INVOKER  = new Object();
+    private static final Object CALL_CMP                 = new Object();
+    private static final Object TO_LOCALE_STRING         = new Object();
+
+    private static InvokeByName getJOIN() {
+        return Global.instance().getInvokeByName(JOIN,
+                new Callable<InvokeByName>() {
+                    @Override
+                    public InvokeByName call() {
+                        return new InvokeByName("join", ScriptObject.class);
+                    }
+                });
+    }
+
+    private static MethodHandle createIteratorCallbackInvoker(final Object key, final Class<?> rtype) {
+        return Global.instance().getDynamicInvoker(key,
+            new Callable<MethodHandle>() {
+                @Override
+                public MethodHandle call() {
+                    return Bootstrap.createDynamicInvoker("dyn:call", rtype, Object.class, Object.class, Object.class,
+                        long.class, Object.class);
+                }
+            });
+    }
+
+    private static MethodHandle getEVERY_CALLBACK_INVOKER() {
+        return createIteratorCallbackInvoker(EVERY_CALLBACK_INVOKER, boolean.class);
+    }
+
+    private static MethodHandle getSOME_CALLBACK_INVOKER() {
+        return createIteratorCallbackInvoker(SOME_CALLBACK_INVOKER, boolean.class);
+    }
 
-    private static final MethodHandle EVERY_CALLBACK_INVOKER   = createIteratorCallbackInvoker(boolean.class);
-    private static final MethodHandle SOME_CALLBACK_INVOKER    = createIteratorCallbackInvoker(boolean.class);
-    private static final MethodHandle FOREACH_CALLBACK_INVOKER = createIteratorCallbackInvoker(void.class);
-    private static final MethodHandle MAP_CALLBACK_INVOKER     = createIteratorCallbackInvoker(Object.class);
-    private static final MethodHandle FILTER_CALLBACK_INVOKER  = createIteratorCallbackInvoker(boolean.class);
+    private static MethodHandle getFOREACH_CALLBACK_INVOKER() {
+        return createIteratorCallbackInvoker(FOREACH_CALLBACK_INVOKER, void.class);
+    }
+
+    private static MethodHandle getMAP_CALLBACK_INVOKER() {
+        return createIteratorCallbackInvoker(MAP_CALLBACK_INVOKER, Object.class);
+    }
+
+    private static MethodHandle getFILTER_CALLBACK_INVOKER() {
+        return createIteratorCallbackInvoker(FILTER_CALLBACK_INVOKER, boolean.class);
+    }
 
-    private static final MethodHandle REDUCE_CALLBACK_INVOKER = Bootstrap.createDynamicInvoker("dyn:call", Object.class,
-            Object.class, Undefined.class, Object.class, Object.class, long.class, Object.class);
-    private static final MethodHandle CALL_CMP                = Bootstrap.createDynamicInvoker("dyn:call", double.class,
-            ScriptFunction.class, Object.class, Object.class, Object.class);
+    private static MethodHandle getREDUCE_CALLBACK_INVOKER() {
+        return Global.instance().getDynamicInvoker(REDUCE_CALLBACK_INVOKER,
+                new Callable<MethodHandle>() {
+                    @Override
+                    public MethodHandle call() {
+                        return Bootstrap.createDynamicInvoker("dyn:call", Object.class, Object.class,
+                             Undefined.class, Object.class, Object.class, long.class, Object.class);
+                    }
+                });
+    }
 
-    private static final InvokeByName TO_LOCALE_STRING = new InvokeByName("toLocaleString", ScriptObject.class, String.class);
+    private static MethodHandle getCALL_CMP() {
+        return Global.instance().getDynamicInvoker(CALL_CMP,
+                new Callable<MethodHandle>() {
+                    @Override
+                    public MethodHandle call() {
+                        return Bootstrap.createDynamicInvoker("dyn:call", double.class,
+                            ScriptFunction.class, Object.class, Object.class, Object.class);
+                    }
+                });
+    }
+
+    private static InvokeByName getTO_LOCALE_STRING() {
+        return Global.instance().getInvokeByName(TO_LOCALE_STRING,
+                new Callable<InvokeByName>() {
+                    @Override
+                    public InvokeByName call() {
+                        return new InvokeByName("toLocaleString", ScriptObject.class, String.class);
+                    }
+                });
+    }
 
     // initialized by nasgen
     private static PropertyMap $nasgenmap$;
@@ -357,11 +426,12 @@
     public static Object toString(final Object self) {
         final Object obj = Global.toObject(self);
         if (obj instanceof ScriptObject) {
+            final InvokeByName joinInvoker = getJOIN();
             final ScriptObject sobj = (ScriptObject)obj;
             try {
-                final Object join = JOIN.getGetter().invokeExact(sobj);
+                final Object join = joinInvoker.getGetter().invokeExact(sobj);
                 if (Bootstrap.isCallable(join)) {
-                    return JOIN.getInvoker().invokeExact(join, sobj);
+                    return joinInvoker.getInvoker().invokeExact(join, sobj);
                 }
             } catch (final RuntimeException | Error e) {
                 throw e;
@@ -393,11 +463,12 @@
 
                 try {
                     if (val instanceof ScriptObject) {
+                        final InvokeByName localeInvoker = getTO_LOCALE_STRING();
                         final ScriptObject sobj           = (ScriptObject)val;
-                        final Object       toLocaleString = TO_LOCALE_STRING.getGetter().invokeExact(sobj);
+                        final Object       toLocaleString = localeInvoker.getGetter().invokeExact(sobj);
 
                         if (Bootstrap.isCallable(toLocaleString)) {
-                            sb.append((String)TO_LOCALE_STRING.getInvoker().invokeExact(toLocaleString, sobj));
+                            sb.append((String)localeInvoker.getInvoker().invokeExact(toLocaleString, sobj));
                         } else {
                             throw typeError("not.a.function", "toLocaleString");
                         }
@@ -567,9 +638,9 @@
         if (isScriptArray || obj instanceof Iterable || (obj != null && obj.getClass().isArray())) {
             final Iterator<Object> iter = arrayLikeIterator(obj, true);
             if (iter.hasNext()) {
-                for(int i = 0; iter.hasNext(); ++i) {
+                for (int i = 0; iter.hasNext(); ++i) {
                     final Object value = iter.next();
-                    if(value == ScriptRuntime.UNDEFINED && isScriptObject && !((ScriptObject)obj).has(i)) {
+                    if (value == ScriptRuntime.UNDEFINED && isScriptObject && !((ScriptObject)obj).has(i)) {
                         // TODO: eventually rewrite arrayLikeIterator to use a three-state enum for handling
                         // UNDEFINED instead of an "includeUndefined" boolean with states SKIP, INCLUDE,
                         // RETURN_EMPTY. Until then, this is how we'll make sure that empty elements don't make it
@@ -814,6 +885,7 @@
         final Object cmpThis = cmp == null || cmp.isStrict() ? ScriptRuntime.UNDEFINED : Global.instance();
 
         Collections.sort(list, new Comparator<Object>() {
+            private final MethodHandle call_cmp = getCALL_CMP();
             @Override
             public int compare(final Object x, final Object y) {
                 if (x == ScriptRuntime.UNDEFINED && y == ScriptRuntime.UNDEFINED) {
@@ -826,7 +898,7 @@
 
                 if (cmp != null) {
                     try {
-                        return (int)Math.signum((double)CALL_CMP.invokeExact(cmp, cmpThis, x, y));
+                        return (int)Math.signum((double)call_cmp.invokeExact(cmp, cmpThis, x, y));
                     } catch (final RuntimeException | Error e) {
                         throw e;
                     } catch (final Throwable t) {
@@ -1103,9 +1175,11 @@
 
     private static boolean applyEvery(final Object self, final Object callbackfn, final Object thisArg) {
         return new IteratorAction<Boolean>(Global.toObject(self), callbackfn, thisArg, true) {
+            private final MethodHandle everyInvoker = getEVERY_CALLBACK_INVOKER();
+
             @Override
             protected boolean forEach(final Object val, final long i) throws Throwable {
-                return (result = (boolean)EVERY_CALLBACK_INVOKER.invokeExact(callbackfn, thisArg, val, i, self));
+                return (result = (boolean)everyInvoker.invokeExact(callbackfn, thisArg, val, i, self));
             }
         }.apply();
     }
@@ -1121,9 +1195,11 @@
     @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
     public static Object some(final Object self, final Object callbackfn, final Object thisArg) {
         return new IteratorAction<Boolean>(Global.toObject(self), callbackfn, thisArg, false) {
+            private final MethodHandle someInvoker = getSOME_CALLBACK_INVOKER();
+
             @Override
             protected boolean forEach(final Object val, final long i) throws Throwable {
-                return !(result = (boolean)SOME_CALLBACK_INVOKER.invokeExact(callbackfn, thisArg, val, i, self));
+                return !(result = (boolean)someInvoker.invokeExact(callbackfn, thisArg, val, i, self));
             }
         }.apply();
     }
@@ -1139,9 +1215,11 @@
     @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
     public static Object forEach(final Object self, final Object callbackfn, final Object thisArg) {
         return new IteratorAction<Object>(Global.toObject(self), callbackfn, thisArg, ScriptRuntime.UNDEFINED) {
+            private final MethodHandle forEachInvoker = getFOREACH_CALLBACK_INVOKER();
+
             @Override
             protected boolean forEach(final Object val, final long i) throws Throwable {
-                FOREACH_CALLBACK_INVOKER.invokeExact(callbackfn, thisArg, val, i, self);
+                forEachInvoker.invokeExact(callbackfn, thisArg, val, i, self);
                 return true;
             }
         }.apply();
@@ -1158,9 +1236,11 @@
     @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
     public static Object map(final Object self, final Object callbackfn, final Object thisArg) {
         return new IteratorAction<NativeArray>(Global.toObject(self), callbackfn, thisArg, null) {
+            private final MethodHandle mapInvoker = getMAP_CALLBACK_INVOKER();
+
             @Override
             protected boolean forEach(final Object val, final long i) throws Throwable {
-                final Object r = MAP_CALLBACK_INVOKER.invokeExact(callbackfn, thisArg, val, i, self);
+                final Object r = mapInvoker.invokeExact(callbackfn, thisArg, val, i, self);
                 result.defineOwnProperty(ArrayIndex.getArrayIndex(index), r);
                 return true;
             }
@@ -1186,10 +1266,11 @@
     public static Object filter(final Object self, final Object callbackfn, final Object thisArg) {
         return new IteratorAction<NativeArray>(Global.toObject(self), callbackfn, thisArg, new NativeArray()) {
             private long to = 0;
+            private final MethodHandle filterInvoker = getFILTER_CALLBACK_INVOKER();
 
             @Override
             protected boolean forEach(final Object val, final long i) throws Throwable {
-                if ((boolean)FILTER_CALLBACK_INVOKER.invokeExact(callbackfn, thisArg, val, i, self)) {
+                if ((boolean)filterInvoker.invokeExact(callbackfn, thisArg, val, i, self)) {
                     result.defineOwnProperty(ArrayIndex.getArrayIndex(to++), val);
                 }
                 return true;
@@ -1217,10 +1298,12 @@
 
         //if initial value is ScriptRuntime.UNDEFINED - step forward once.
         return new IteratorAction<Object>(Global.toObject(self), callbackfn, ScriptRuntime.UNDEFINED, initialValue, iter) {
+            private final MethodHandle reduceInvoker = getREDUCE_CALLBACK_INVOKER();
+
             @Override
             protected boolean forEach(final Object val, final long i) throws Throwable {
                 // TODO: why can't I declare the second arg as Undefined.class?
-                result = REDUCE_CALLBACK_INVOKER.invokeExact(callbackfn, ScriptRuntime.UNDEFINED, result, val, i, self);
+                result = reduceInvoker.invokeExact(callbackfn, ScriptRuntime.UNDEFINED, result, val, i, self);
                 return true;
             }
         }.apply();
@@ -1273,10 +1356,4 @@
 
         return false;
     }
-
-    private static MethodHandle createIteratorCallbackInvoker(final Class<?> rtype) {
-        return Bootstrap.createDynamicInvoker("dyn:call", rtype, Object.class, Object.class, Object.class,
-                long.class, Object.class);
-
-    }
 }
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeDate.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeDate.java	Tue Aug 20 17:46:45 2013 -0700
@@ -33,6 +33,7 @@
 
 import java.util.Locale;
 import java.util.TimeZone;
+import java.util.concurrent.Callable;
 import jdk.nashorn.internal.objects.annotations.Attribute;
 import jdk.nashorn.internal.objects.annotations.Constructor;
 import jdk.nashorn.internal.objects.annotations.Function;
@@ -95,8 +96,17 @@
             "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
     };
 
-    private static final InvokeByName TO_ISO_STRING = new InvokeByName("toISOString", ScriptObject.class, Object.class,
-            Object.class);
+    private static final Object TO_ISO_STRING = new Object();
+
+    private static InvokeByName getTO_ISO_STRING() {
+        return Global.instance().getInvokeByName(TO_ISO_STRING,
+                new Callable<InvokeByName>() {
+                    @Override
+                    public InvokeByName call() {
+                        return new InvokeByName("toISOString", ScriptObject.class, Object.class, Object.class);
+                    }
+                });
+    }
 
     private double time;
     private final TimeZone timezone;
@@ -861,9 +871,10 @@
         }
 
         try {
-            final Object func = TO_ISO_STRING.getGetter().invokeExact(sobj);
+            final InvokeByName toIsoString = getTO_ISO_STRING();
+            final Object func = toIsoString.getGetter().invokeExact(sobj);
             if (Bootstrap.isCallable(func)) {
-                return TO_ISO_STRING.getInvoker().invokeExact(func, sobj, key);
+                return toIsoString.getInvoker().invokeExact(func, sobj, key);
             }
             throw typeError("not.a.function", ScriptRuntime.safeToString(func));
         } catch (final RuntimeException | Error e) {
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeDebug.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeDebug.java	Tue Aug 20 17:46:45 2013 -0700
@@ -72,7 +72,7 @@
     public static Object getContext(final Object self) {
         final SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
-            sm.checkPermission(new RuntimePermission("nashorn.getContext"));
+            sm.checkPermission(new RuntimePermission(Context.NASHORN_GET_CONTEXT));
         }
         return Global.getThisContext();
     }
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeJSON.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeJSON.java	Tue Aug 20 17:46:45 2013 -0700
@@ -35,6 +35,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.Callable;
 import jdk.nashorn.internal.objects.annotations.Attribute;
 import jdk.nashorn.internal.objects.annotations.Function;
 import jdk.nashorn.internal.objects.annotations.ScriptClass;
@@ -55,9 +56,31 @@
  */
 @ScriptClass("JSON")
 public final class NativeJSON extends ScriptObject {
-    private static final InvokeByName TO_JSON = new InvokeByName("toJSON", ScriptObject.class, Object.class, Object.class);
-    private static final MethodHandle REPLACER_INVOKER = Bootstrap.createDynamicInvoker("dyn:call", Object.class,
-            ScriptFunction.class, ScriptObject.class, Object.class, Object.class);
+    private static final Object TO_JSON = new Object();
+
+    private static InvokeByName getTO_JSON() {
+        return Global.instance().getInvokeByName(TO_JSON,
+                new Callable<InvokeByName>() {
+                    @Override
+                    public InvokeByName call() {
+                        return new InvokeByName("toJSON", ScriptObject.class, Object.class, Object.class);
+                    }
+                });
+    }
+
+
+    private static final Object REPLACER_INVOKER = new Object();
+
+    private static MethodHandle getREPLACER_INVOKER() {
+        return Global.instance().getDynamicInvoker(REPLACER_INVOKER,
+                new Callable<MethodHandle>() {
+                    @Override
+                    public MethodHandle call() {
+                        return Bootstrap.createDynamicInvoker("dyn:call", Object.class,
+                            ScriptFunction.class, ScriptObject.class, Object.class, Object.class);
+                    }
+                });
+    }
 
     // initialized by nasgen
     @SuppressWarnings("unused")
@@ -187,15 +210,16 @@
 
         try {
             if (value instanceof ScriptObject) {
+                final InvokeByName toJSONInvoker = getTO_JSON();
                 final ScriptObject svalue = (ScriptObject)value;
-                final Object toJSON = TO_JSON.getGetter().invokeExact(svalue);
+                final Object toJSON = toJSONInvoker.getGetter().invokeExact(svalue);
                 if (Bootstrap.isCallable(toJSON)) {
-                    value = TO_JSON.getInvoker().invokeExact(toJSON, svalue, key);
+                    value = toJSONInvoker.getInvoker().invokeExact(toJSON, svalue, key);
                 }
             }
 
             if (state.replacerFunction != null) {
-                value = REPLACER_INVOKER.invokeExact(state.replacerFunction, holder, key, value);
+                value = getREPLACER_INVOKER().invokeExact(state.replacerFunction, holder, key, value);
             }
         } catch(Error|RuntimeException t) {
             throw t;
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeObject.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeObject.java	Tue Aug 20 17:46:45 2013 -0700
@@ -36,6 +36,7 @@
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.Callable;
 import jdk.internal.dynalink.beans.BeansLinker;
 import jdk.internal.dynalink.beans.StaticClass;
 import jdk.internal.dynalink.linker.GuardedInvocation;
@@ -70,7 +71,18 @@
  */
 @ScriptClass("Object")
 public final class NativeObject {
-    private static final InvokeByName TO_STRING = new InvokeByName("toString", ScriptObject.class);
+    private static final Object TO_STRING = new Object();
+
+    private static InvokeByName getTO_STRING() {
+        return Global.instance().getInvokeByName(TO_STRING,
+                new Callable<InvokeByName>() {
+                    @Override
+                    public InvokeByName call() {
+                        return new InvokeByName("toString", ScriptObject.class);
+                    }
+                });
+    }
+
     private static final MethodType MIRROR_GETTER_TYPE = MethodType.methodType(Object.class, ScriptObjectMirror.class);
     private static final MethodType MIRROR_SETTER_TYPE = MethodType.methodType(Object.class, ScriptObjectMirror.class, Object.class);
 
@@ -101,6 +113,13 @@
         } else if (obj instanceof ScriptObjectMirror) {
             return ((ScriptObjectMirror)obj).getProto();
         } else {
+            final JSType type = JSType.of(obj);
+            if (type == JSType.OBJECT) {
+                // host (Java) objects have null __proto__
+                return null;
+            }
+
+            // must be some JS primitive
             throw notAnObject(obj);
         }
     }
@@ -402,12 +421,13 @@
     public static Object toLocaleString(final Object self) {
         final Object obj = JSType.toScriptObject(self);
         if (obj instanceof ScriptObject) {
+            final InvokeByName toStringInvoker = getTO_STRING();
             final ScriptObject sobj = (ScriptObject)self;
             try {
-                final Object toString = TO_STRING.getGetter().invokeExact(sobj);
+                final Object toString = toStringInvoker.getGetter().invokeExact(sobj);
 
                 if (Bootstrap.isCallable(toString)) {
-                    return TO_STRING.getInvoker().invokeExact(toString, sobj);
+                    return toStringInvoker.getInvoker().invokeExact(toString, sobj);
                 }
             } catch (final RuntimeException | Error e) {
                 throw e;
--- a/nashorn/src/jdk/nashorn/internal/parser/DateParser.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/parser/DateParser.java	Tue Aug 20 17:46:45 2013 -0700
@@ -141,7 +141,7 @@
      * Try parsing the date string according to the rules laid out in ES5 15.9.1.15.
      * The date string must conform to the following format:
      *
-     * <pre>  [('-'|'+')yy]yyyy[-MM[-dd]][hh:mm[:ss[.sss]][Z|(+|-)hh:mm]] </pre>
+     * <pre>  [('-'|'+')yy]yyyy[-MM[-dd]][Thh:mm[:ss[.sss]][Z|(+|-)hh:mm]] </pre>
      *
      * <p>If the string does not contain a time zone offset, the <tt>TIMEZONE</tt> field
      * is set to <tt>0</tt> (GMT).</p>
@@ -249,7 +249,7 @@
 
             switch (token) {
                 case NUMBER:
-                    if (skip(':')) {
+                    if (skipDelimiter(':')) {
                         // A number followed by ':' is parsed as time
                         if (!setTimeField(numValue)) {
                             return false;
@@ -260,14 +260,14 @@
                             if (token != Token.NUMBER || !setTimeField(numValue)) {
                                 return false;
                             }
-                        } while (skip(isSet(SECOND) ? '.' : ':'));
+                        } while (skipDelimiter(isSet(SECOND) ? '.' : ':'));
 
                     } else {
                         // Parse as date token
                         if (!setDateField(numValue)) {
                             return false;
                         }
-                        skip('-');
+                        skipDelimiter('-');
                     }
                     break;
 
@@ -297,7 +297,7 @@
                             break;
                     }
                     if (nameValue.type != Name.TIMEZONE_ID) {
-                        skip('-');
+                        skipDelimiter('-');
                     }
                     break;
 
@@ -359,7 +359,18 @@
         return pos < length ? string.charAt(pos) : -1;
     }
 
-    private boolean skip(final char c) {
+    // Skip delimiter if followed by a number. Used for ISO 8601 formatted dates
+    private boolean skipNumberDelimiter(final char c) {
+        if (pos < length - 1 && string.charAt(pos) == c
+                && Character.getType(string.charAt(pos + 1)) == DECIMAL_DIGIT_NUMBER) {
+            token = null;
+            pos++;
+            return true;
+        }
+        return false;
+    }
+
+    private boolean skipDelimiter(final char c) {
         if (pos < length && string.charAt(pos) == c) {
             token = null;
             pos++;
@@ -452,14 +463,14 @@
         switch (currentField) {
             case YEAR:
             case MONTH:
-                return skip('-') || peek() == 'T' || peek() == -1;
+                return skipNumberDelimiter('-') || peek() == 'T' || peek() == -1;
             case DAY:
                 return peek() == 'T' || peek() == -1;
             case HOUR:
             case MINUTE:
-                return skip(':') || endOfTime();
+                return skipNumberDelimiter(':') || endOfTime();
             case SECOND:
-                return skip('.') || endOfTime();
+                return skipNumberDelimiter('.') || endOfTime();
             default:
                 return true;
         }
@@ -515,7 +526,7 @@
     private int readTimeZoneOffset() {
         final int sign = string.charAt(pos - 1) == '+' ? 1 : -1;
         int offset = readNumber(2);
-        skip(':');
+        skipDelimiter(':');
         offset = offset * 60 + readNumber(2);
         return sign * offset;
     }
--- a/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Tue Aug 20 17:46:45 2013 -0700
@@ -160,10 +160,10 @@
         if (this.scripting) {
             this.lineInfoReceiver = new Lexer.LineInfoReceiver() {
                 @Override
-                public void lineInfo(final int line, final int linePosition) {
+                public void lineInfo(final int receiverLine, final int receiverLinePosition) {
                     // update the parser maintained line information
-                    Parser.this.line = line;
-                    Parser.this.linePosition = linePosition;
+                    Parser.this.line = receiverLine;
+                    Parser.this.linePosition = receiverLinePosition;
                 }
             };
         } else {
--- a/nashorn/src/jdk/nashorn/internal/runtime/Context.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/Context.java	Tue Aug 20 17:46:45 2013 -0700
@@ -48,6 +48,7 @@
 import java.security.PrivilegedAction;
 import java.security.ProtectionDomain;
 import java.util.Map;
+
 import jdk.internal.org.objectweb.asm.ClassReader;
 import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
 import jdk.nashorn.api.scripting.ScriptObjectMirror;
@@ -64,6 +65,31 @@
  * This class manages the global state of execution. Context is immutable.
  */
 public final class Context {
+    // nashorn specific security runtime access permission names
+    /**
+     * Permission needed to pass arbitrary nashorn command line options when creating Context.
+     */
+    public static final String NASHORN_SET_CONFIG      = "nashorn.setConfig";
+
+    /**
+     * Permission needed to create Nashorn Context instance.
+     */
+    public static final String NASHORN_CREATE_CONTEXT  = "nashorn.createContext";
+
+    /**
+     * Permission needed to create Nashorn Global instance.
+     */
+    public static final String NASHORN_CREATE_GLOBAL   = "nashorn.createGlobal";
+
+    /**
+     * Permission to get current Nashorn Context from thread local storage.
+     */
+    public static final String NASHORN_GET_CONTEXT     = "nashorn.getContext";
+
+    /**
+     * Permission to use Java reflection/jsr292 from script code.
+     */
+    public static final String NASHORN_JAVA_REFLECTION = "nashorn.JavaReflection";
 
     /**
      * ContextCodeInstaller that has the privilege of installing classes in the Context.
@@ -139,7 +165,7 @@
     public static Context getContext() {
         final SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
-            sm.checkPermission(new RuntimePermission("nashorn.getContext"));
+            sm.checkPermission(new RuntimePermission(NASHORN_GET_CONTEXT));
         }
         return getContextTrusted();
     }
@@ -204,7 +230,20 @@
 
     private static final ClassLoader myLoader = Context.class.getClassLoader();
     private static final StructureLoader sharedLoader;
-    private static final AccessControlContext NO_PERMISSIONS_CONTEXT;
+
+    private static AccessControlContext createNoPermAccCtxt() {
+        return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, new Permissions()) });
+    }
+
+    private static AccessControlContext createPermAccCtxt(final String permName) {
+        final Permissions perms = new Permissions();
+        perms.add(new RuntimePermission(permName));
+        return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
+    }
+
+    private static final AccessControlContext NO_PERMISSIONS_ACC_CTXT = createNoPermAccCtxt();
+    private static final AccessControlContext CREATE_LOADER_ACC_CTXT  = createPermAccCtxt("createClassLoader");
+    private static final AccessControlContext CREATE_GLOBAL_ACC_CTXT  = createPermAccCtxt(NASHORN_CREATE_GLOBAL);
 
     static {
         sharedLoader = AccessController.doPrivileged(new PrivilegedAction<StructureLoader>() {
@@ -212,8 +251,7 @@
             public StructureLoader run() {
                 return new StructureLoader(myLoader, null);
             }
-        });
-        NO_PERMISSIONS_CONTEXT = new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, new Permissions()) });
+        }, CREATE_LOADER_ACC_CTXT);
     }
 
     /**
@@ -254,7 +292,7 @@
     public Context(final Options options, final ErrorManager errors, final PrintWriter out, final PrintWriter err, final ClassLoader appLoader) {
         final SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
-            sm.checkPermission(new RuntimePermission("nashorn.createContext"));
+            sm.checkPermission(new RuntimePermission(NASHORN_CREATE_CONTEXT));
         }
 
         this.env       = new ScriptEnvironment(options, out, err);
@@ -516,7 +554,7 @@
            @Override
            public ScriptObject run() {
                try {
-                   return createGlobal();
+                   return newGlobal();
                } catch (final RuntimeException e) {
                    if (Context.DEBUG) {
                        e.printStackTrace();
@@ -524,7 +562,9 @@
                    throw e;
                }
            }
-        });
+        }, CREATE_GLOBAL_ACC_CTXT);
+        // initialize newly created Global instance
+        initGlobal(newGlobal);
         setGlobalTrusted(newGlobal);
 
         final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY :  ScriptObjectMirror.wrapArray(args, oldGlobal);
@@ -577,7 +617,7 @@
                         sm.checkPackageAccess(fullName.substring(0, index));
                         return null;
                     }
-                }, NO_PERMISSIONS_CONTEXT);
+                }, NO_PERMISSIONS_ACC_CTXT);
             }
         }
     }
@@ -849,6 +889,7 @@
         return script;
     }
 
+    @SuppressWarnings("static-method")
     private ScriptLoader createNewLoader() {
         return AccessController.doPrivileged(
              new PrivilegedAction<ScriptLoader>() {
@@ -856,7 +897,7 @@
                 public ScriptLoader run() {
                     return new ScriptLoader(sharedLoader, Context.this);
                 }
-             });
+             }, CREATE_LOADER_ACC_CTXT);
     }
 
     private long getUniqueScriptId() {
--- a/nashorn/src/jdk/nashorn/internal/runtime/ECMAErrors.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/ECMAErrors.java	Tue Aug 20 17:46:45 2013 -0700
@@ -25,8 +25,6 @@
 
 package jdk.nashorn.internal.runtime;
 
-import java.security.AccessController;
-import java.security.PrivilegedAction;
 import java.text.MessageFormat;
 import java.util.Locale;
 import java.util.ResourceBundle;
@@ -40,16 +38,9 @@
 public final class ECMAErrors {
     private static final String MESSAGES_RESOURCE = "jdk.nashorn.internal.runtime.resources.Messages";
 
-    // Without do privileged, under security manager messages can not be loaded.
     private static final ResourceBundle MESSAGES_BUNDLE;
     static {
-        MESSAGES_BUNDLE = AccessController.doPrivileged(
-        new PrivilegedAction<ResourceBundle>() {
-            @Override
-            public ResourceBundle run() {
-                return ResourceBundle.getBundle(MESSAGES_RESOURCE, Locale.getDefault());
-            }
-        });
+        MESSAGES_BUNDLE = ResourceBundle.getBundle(MESSAGES_RESOURCE, Locale.getDefault());
     }
 
     /** We assume that compiler generates script classes into the known package. */
--- a/nashorn/src/jdk/nashorn/internal/runtime/GlobalObject.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/GlobalObject.java	Tue Aug 20 17:46:45 2013 -0700
@@ -26,8 +26,10 @@
 package jdk.nashorn.internal.runtime;
 
 import java.lang.invoke.MethodHandle;
+import java.util.concurrent.Callable;
 import jdk.internal.dynalink.linker.GuardedInvocation;
 import jdk.internal.dynalink.linker.LinkRequest;
+import jdk.nashorn.internal.runtime.linker.InvokeByName;
 
 /**
  * Runtime interface to the global scope objects.
@@ -210,4 +212,20 @@
      * @param clazz compiled Class object for the source
      */
     public void cacheClass(Source source, Class<?> clazz);
+
+    /**
+     * Get cached InvokeByName object for the given key
+     * @param key key to be associated with InvokeByName object
+     * @param creator if InvokeByName is absent 'creator' is called to make one (lazy init)
+     * @return InvokeByName object associated with the key.
+     */
+    public InvokeByName getInvokeByName(final Object key, final Callable<InvokeByName> creator);
+
+    /**
+     * Get cached dynamic method handle for the given key
+     * @param key key to be associated with dynamic method handle
+     * @param creator if method handle is absent 'creator' is called to make one (lazy init)
+     * @return dynamic method handle associated with the key.
+     */
+    public MethodHandle getDynamicInvoker(final Object key, final Callable<MethodHandle> creator);
 }
--- a/nashorn/src/jdk/nashorn/internal/runtime/JSONFunctions.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/JSONFunctions.java	Tue Aug 20 17:46:45 2013 -0700
@@ -27,6 +27,7 @@
 
 import java.lang.invoke.MethodHandle;
 import java.util.Iterator;
+import java.util.concurrent.Callable;
 import jdk.nashorn.internal.ir.LiteralNode;
 import jdk.nashorn.internal.ir.Node;
 import jdk.nashorn.internal.ir.ObjectNode;
@@ -42,8 +43,19 @@
  */
 public final class JSONFunctions {
     private JSONFunctions() {}
-    private static final MethodHandle REVIVER_INVOKER = Bootstrap.createDynamicInvoker("dyn:call", Object.class,
-            ScriptFunction.class, ScriptObject.class, String.class, Object.class);
+
+    private static final Object REVIVER_INVOKER = new Object();
+
+    private static MethodHandle getREVIVER_INVOKER() {
+        return ((GlobalObject)Context.getGlobal()).getDynamicInvoker(REVIVER_INVOKER,
+                new Callable<MethodHandle>() {
+                    @Override
+                    public MethodHandle call() {
+                        return Bootstrap.createDynamicInvoker("dyn:call", Object.class,
+                            ScriptFunction.class, ScriptObject.class, String.class, Object.class);
+                    }
+                });
+    }
 
     /**
      * Returns JSON-compatible quoted version of the given string.
@@ -117,7 +129,7 @@
 
         try {
              // Object.class, ScriptFunction.class, ScriptObject.class, String.class, Object.class);
-             return REVIVER_INVOKER.invokeExact(reviver, holder, JSType.toString(name), val);
+             return getREVIVER_INVOKER().invokeExact(reviver, holder, JSType.toString(name), val);
         } catch(Error|RuntimeException t) {
             throw t;
         } catch(final Throwable t) {
--- a/nashorn/src/jdk/nashorn/internal/runtime/JSType.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/JSType.java	Tue Aug 20 17:46:45 2013 -0700
@@ -28,6 +28,7 @@
 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall;
 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
 
+import java.lang.invoke.MethodHandles;
 import java.util.Locale;
 import jdk.internal.dynalink.beans.BeansLinker;
 import jdk.internal.dynalink.beans.StaticClass;
@@ -63,47 +64,49 @@
     /** Max value for an uint32 in JavaScript */
     public static final long MAX_UINT = 0xFFFF_FFFFL;
 
+    private static final MethodHandles.Lookup myLookup = MethodHandles.lookup();
+
     /** JavaScript compliant conversion function from Object to boolean */
-    public static final Call TO_BOOLEAN = staticCall(JSType.class, "toBoolean", boolean.class, Object.class);
+    public static final Call TO_BOOLEAN = staticCall(myLookup, JSType.class, "toBoolean", boolean.class, Object.class);
 
     /** JavaScript compliant conversion function from number to boolean */
-    public static final Call TO_BOOLEAN_D = staticCall(JSType.class, "toBoolean", boolean.class, double.class);
+    public static final Call TO_BOOLEAN_D = staticCall(myLookup, JSType.class, "toBoolean", boolean.class, double.class);
 
     /** JavaScript compliant conversion function from Object to integer */
-    public static final Call TO_INTEGER = staticCall(JSType.class, "toInteger", int.class, Object.class);
+    public static final Call TO_INTEGER = staticCall(myLookup, JSType.class, "toInteger", int.class, Object.class);
 
     /** JavaScript compliant conversion function from Object to long */
-    public static final Call TO_LONG = staticCall(JSType.class, "toLong", long.class, Object.class);
+    public static final Call TO_LONG = staticCall(myLookup, JSType.class, "toLong", long.class, Object.class);
 
     /** JavaScript compliant conversion function from Object to number */
-    public static final Call TO_NUMBER = staticCall(JSType.class, "toNumber", double.class, Object.class);
+    public static final Call TO_NUMBER = staticCall(myLookup, JSType.class, "toNumber", double.class, Object.class);
 
     /** JavaScript compliant conversion function from Object to int32 */
-    public static final Call TO_INT32 = staticCall(JSType.class, "toInt32", int.class, Object.class);
+    public static final Call TO_INT32 = staticCall(myLookup, JSType.class, "toInt32", int.class, Object.class);
 
     /** JavaScript compliant conversion function from double to int32 */
-    public static final Call TO_INT32_D = staticCall(JSType.class, "toInt32", int.class, double.class);
+    public static final Call TO_INT32_D = staticCall(myLookup, JSType.class, "toInt32", int.class, double.class);
 
     /** JavaScript compliant conversion function from Object to uint32 */
-    public static final Call TO_UINT32 = staticCall(JSType.class, "toUint32", long.class, Object.class);
+    public static final Call TO_UINT32 = staticCall(myLookup, JSType.class, "toUint32", long.class, Object.class);
 
     /** JavaScript compliant conversion function from number to uint32 */
-    public static final Call TO_UINT32_D = staticCall(JSType.class, "toUint32", long.class, double.class);
+    public static final Call TO_UINT32_D = staticCall(myLookup, JSType.class, "toUint32", long.class, double.class);
 
     /** JavaScript compliant conversion function from Object to int64 */
-    public static final Call TO_INT64 = staticCall(JSType.class, "toInt64", long.class, Object.class);
+    public static final Call TO_INT64 = staticCall(myLookup, JSType.class, "toInt64", long.class, Object.class);
 
     /** JavaScript compliant conversion function from number to int64 */
-    public static final Call TO_INT64_D = staticCall(JSType.class, "toInt64", long.class, double.class);
+    public static final Call TO_INT64_D = staticCall(myLookup, JSType.class, "toInt64", long.class, double.class);
 
     /** JavaScript compliant conversion function from Object to String */
-    public static final Call TO_STRING = staticCall(JSType.class, "toString", String.class, Object.class);
+    public static final Call TO_STRING = staticCall(myLookup, JSType.class, "toString", String.class, Object.class);
 
     /** JavaScript compliant conversion function from number to String */
-    public static final Call TO_STRING_D = staticCall(JSType.class, "toString", String.class, double.class);
+    public static final Call TO_STRING_D = staticCall(myLookup, JSType.class, "toString", String.class, double.class);
 
     /** JavaScript compliant conversion function from Object to primitive */
-    public static final Call TO_PRIMITIVE = staticCall(JSType.class, "toPrimitive", Object.class,  Object.class);
+    public static final Call TO_PRIMITIVE = staticCall(myLookup, JSType.class, "toPrimitive", Object.class,  Object.class);
 
     private static final double INT32_LIMIT = 4294967296.0;
 
--- a/nashorn/src/jdk/nashorn/internal/runtime/ListAdapter.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/ListAdapter.java	Tue Aug 20 17:46:45 2013 -0700
@@ -31,6 +31,7 @@
 import java.util.ListIterator;
 import java.util.NoSuchElementException;
 import java.util.RandomAccess;
+import java.util.concurrent.Callable;
 import jdk.nashorn.internal.runtime.linker.Bootstrap;
 import jdk.nashorn.internal.runtime.linker.InvokeByName;
 
@@ -49,16 +50,73 @@
  */
 public final class ListAdapter extends AbstractList<Object> implements RandomAccess, Deque<Object> {
     // These add to the back and front of the list
-    private static final InvokeByName PUSH    = new InvokeByName("push",    ScriptObject.class, void.class, Object.class);
-    private static final InvokeByName UNSHIFT = new InvokeByName("unshift", ScriptObject.class, void.class, Object.class);
+    private static final Object PUSH    = new Object();
+    private static InvokeByName getPUSH() {
+        return ((GlobalObject)Context.getGlobal()).getInvokeByName(PUSH,
+                new Callable<InvokeByName>() {
+                    @Override
+                    public InvokeByName call() {
+                        return new InvokeByName("push", ScriptObject.class, void.class, Object.class);
+                    }
+                });
+    }
+
+    private static final Object UNSHIFT = new Object();
+    private static InvokeByName getUNSHIFT() {
+        return ((GlobalObject)Context.getGlobal()).getInvokeByName(UNSHIFT,
+                new Callable<InvokeByName>() {
+                    @Override
+                    public InvokeByName call() {
+                        return new InvokeByName("unshift", ScriptObject.class, void.class, Object.class);
+                    }
+                });
+    }
 
     // These remove from the back and front of the list
-    private static final InvokeByName POP   = new InvokeByName("pop",   ScriptObject.class, Object.class);
-    private static final InvokeByName SHIFT = new InvokeByName("shift", ScriptObject.class, Object.class);
+    private static final Object POP = new Object();
+    private static InvokeByName getPOP() {
+        return ((GlobalObject)Context.getGlobal()).getInvokeByName(POP,
+                new Callable<InvokeByName>() {
+                    @Override
+                    public InvokeByName call() {
+                        return new InvokeByName("pop", ScriptObject.class, Object.class);
+                    }
+                });
+    }
+
+    private static final Object SHIFT = new Object();
+    private static InvokeByName getSHIFT() {
+        return ((GlobalObject)Context.getGlobal()).getInvokeByName(SHIFT,
+                new Callable<InvokeByName>() {
+                    @Override
+                    public InvokeByName call() {
+                        return new InvokeByName("shift", ScriptObject.class, Object.class);
+                    }
+                });
+    }
 
     // These insert and remove in the middle of the list
-    private static final InvokeByName SPLICE_ADD    = new InvokeByName("splice", ScriptObject.class, void.class, int.class, int.class, Object.class);
-    private static final InvokeByName SPLICE_REMOVE = new InvokeByName("splice", ScriptObject.class, void.class, int.class, int.class);
+    private static final Object SPLICE_ADD = new Object();
+    private static InvokeByName getSPLICE_ADD() {
+        return ((GlobalObject)Context.getGlobal()).getInvokeByName(SPLICE_ADD,
+                new Callable<InvokeByName>() {
+                    @Override
+                    public InvokeByName call() {
+                        return new InvokeByName("splice", ScriptObject.class, void.class, int.class, int.class, Object.class);
+                    }
+                });
+    }
+
+    private static final Object SPLICE_REMOVE = new Object();
+    private static InvokeByName getSPLICE_REMOVE() {
+        return ((GlobalObject)Context.getGlobal()).getInvokeByName(SPLICE_REMOVE,
+                new Callable<InvokeByName>() {
+                    @Override
+                    public InvokeByName call() {
+                        return new InvokeByName("splice", ScriptObject.class, void.class, int.class, int.class);
+                    }
+                });
+    }
 
     private final ScriptObject obj;
 
@@ -109,9 +167,10 @@
     @Override
     public void addFirst(Object e) {
         try {
-            final Object fn = UNSHIFT.getGetter().invokeExact(obj);
-            checkFunction(fn, UNSHIFT);
-            UNSHIFT.getInvoker().invokeExact(fn, obj, e);
+            final InvokeByName unshiftInvoker = getUNSHIFT();
+            final Object fn = unshiftInvoker.getGetter().invokeExact(obj);
+            checkFunction(fn, unshiftInvoker);
+            unshiftInvoker.getInvoker().invokeExact(fn, obj, e);
         } catch(RuntimeException | Error ex) {
             throw ex;
         } catch(Throwable t) {
@@ -122,9 +181,10 @@
     @Override
     public void addLast(Object e) {
         try {
-            final Object fn = PUSH.getGetter().invokeExact(obj);
-            checkFunction(fn, PUSH);
-            PUSH.getInvoker().invokeExact(fn, obj, e);
+            final InvokeByName pushInvoker = getPUSH();
+            final Object fn = pushInvoker.getGetter().invokeExact(obj);
+            checkFunction(fn, pushInvoker);
+            pushInvoker.getInvoker().invokeExact(fn, obj, e);
         } catch(RuntimeException | Error ex) {
             throw ex;
         } catch(Throwable t) {
@@ -142,9 +202,10 @@
             } else {
                 final int size = size();
                 if(index < size) {
-                    final Object fn = SPLICE_ADD.getGetter().invokeExact(obj);
-                    checkFunction(fn, SPLICE_ADD);
-                    SPLICE_ADD.getInvoker().invokeExact(fn, obj, index, 0, e);
+                    final InvokeByName spliceAddInvoker = getSPLICE_ADD();
+                    final Object fn = spliceAddInvoker.getGetter().invokeExact(obj);
+                    checkFunction(fn, spliceAddInvoker);
+                    spliceAddInvoker.getInvoker().invokeExact(fn, obj, index, 0, e);
                 } else if(index == size) {
                     addLast(e);
                 } else {
@@ -234,9 +295,10 @@
 
     private Object invokeShift() {
         try {
-            final Object fn = SHIFT.getGetter().invokeExact(obj);
-            checkFunction(fn, SHIFT);
-            return SHIFT.getInvoker().invokeExact(fn, obj);
+            final InvokeByName shiftInvoker = getSHIFT();
+            final Object fn = shiftInvoker.getGetter().invokeExact(obj);
+            checkFunction(fn, shiftInvoker);
+            return shiftInvoker.getInvoker().invokeExact(fn, obj);
         } catch(RuntimeException | Error ex) {
             throw ex;
         } catch(Throwable t) {
@@ -246,9 +308,10 @@
 
     private Object invokePop() {
         try {
-            final Object fn = POP.getGetter().invokeExact(obj);
-            checkFunction(fn, POP);
-            return POP.getInvoker().invokeExact(fn, obj);
+            final InvokeByName popInvoker = getPOP();
+            final Object fn = popInvoker.getGetter().invokeExact(obj);
+            checkFunction(fn, popInvoker);
+            return popInvoker.getInvoker().invokeExact(fn, obj);
         } catch(RuntimeException | Error ex) {
             throw ex;
         } catch(Throwable t) {
@@ -263,9 +326,10 @@
 
     private void invokeSpliceRemove(int fromIndex, int count) {
         try {
-            final Object fn = SPLICE_REMOVE.getGetter().invokeExact(obj);
-            checkFunction(fn, SPLICE_REMOVE);
-            SPLICE_REMOVE.getInvoker().invokeExact(fn, obj, fromIndex, count);
+            final InvokeByName spliceRemoveInvoker = getSPLICE_REMOVE();
+            final Object fn = spliceRemoveInvoker.getGetter().invokeExact(obj);
+            checkFunction(fn, spliceRemoveInvoker);
+            spliceRemoveInvoker.getInvoker().invokeExact(fn, obj, fromIndex, count);
         } catch(RuntimeException | Error ex) {
             throw ex;
         } catch(Throwable t) {
--- a/nashorn/src/jdk/nashorn/internal/runtime/Logging.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/Logging.java	Tue Aug 20 17:46:45 2013 -0700
@@ -25,6 +25,11 @@
 
 package jdk.nashorn.internal.runtime;
 
+import java.security.AccessControlContext;
+import java.security.AccessController;
+import java.security.Permissions;
+import java.security.PrivilegedAction;
+import java.security.ProtectionDomain;
 import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
@@ -35,6 +40,7 @@
 import java.util.logging.Level;
 import java.util.logging.LogRecord;
 import java.util.logging.Logger;
+import java.util.logging.LoggingPermission;
 
 /**
  * Logging system for getting loggers for arbitrary subsystems as
@@ -50,12 +56,20 @@
 
     private static final Logger disabledLogger = Logger.getLogger("disabled");
 
+    private static AccessControlContext createLoggerControlAccCtxt() {
+        final Permissions perms = new Permissions();
+        perms.add(new LoggingPermission("control", null));
+        return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
+    }
+
     static {
-        try {
-            Logging.disabledLogger.setLevel(Level.OFF);
-        } catch (final SecurityException e) {
-            //ignored
-        }
+        AccessController.doPrivileged(new PrivilegedAction<Void>() {
+            @Override
+            public Void run() {
+                Logging.disabledLogger.setLevel(Level.OFF);
+                return null;
+            }
+        }, createLoggerControlAccCtxt());
     }
 
     /** Maps logger name to loggers. Names are typically per package */
--- a/nashorn/src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java	Tue Aug 20 17:46:45 2013 -0700
@@ -47,18 +47,24 @@
  * This is a subclass that represents a script function that may be regenerated,
  * for example with specialization based on call site types, or lazily generated.
  * The common denominator is that it can get new invokers during its lifespan,
- * unlike {@link FinalScriptFunctionData}
+ * unlike {@code FinalScriptFunctionData}
  */
 public final class RecompilableScriptFunctionData extends ScriptFunctionData {
 
     /** FunctionNode with the code for this ScriptFunction */
-    private FunctionNode functionNode;
+    private volatile FunctionNode functionNode;
+
+    /** Source from which FunctionNode was parsed. */
+    private final Source source;
+
+    /** Token of this function within the source. */
+    private final long token;
 
     /** Allocator map from makeMap() */
     private final PropertyMap allocatorMap;
 
     /** Code installer used for all further recompilation/specialization of this ScriptFunction */
-    private final CodeInstaller<ScriptEnvironment> installer;
+    private volatile CodeInstaller<ScriptEnvironment> installer;
 
     /** Name of class where allocator function resides */
     private final String allocatorClassName;
@@ -103,6 +109,8 @@
               true);
 
         this.functionNode       = functionNode;
+        this.source             = functionNode.getSource();
+        this.token              = tokenFor(functionNode);
         this.installer          = installer;
         this.allocatorClassName = allocatorClassName;
         this.allocatorMap       = allocatorMap;
@@ -110,9 +118,6 @@
 
     @Override
     String toSource() {
-        final Source source = functionNode.getSource();
-        final long   token  = tokenFor(functionNode);
-
         if (source != null && token != 0) {
             return source.getString(Token.descPosition(token), Token.descLength(token));
         }
@@ -123,8 +128,6 @@
     @Override
     public String toString() {
         final StringBuilder sb = new StringBuilder();
-        final Source source = functionNode.getSource();
-        final long   token  = tokenFor(functionNode);
 
         if (source != null) {
             sb.append(source.getName())
@@ -190,6 +193,12 @@
 
          // code exists - look it up and add it into the automatically sorted invoker list
          addCode(functionNode);
+
+         if (! functionNode.canSpecialize()) {
+             // allow GC to claim IR stuff that is not needed anymore
+             functionNode = null;
+             installer = null;
+         }
     }
 
     private MethodHandle addCode(final FunctionNode fn) {
@@ -325,7 +334,7 @@
          * footprint too large to store a parse snapshot, or if it is meaningless
          * to do so, such as e.g. for runScript
          */
-        if (!functionNode.canSpecialize()) {
+        if (functionNode == null || !functionNode.canSpecialize()) {
             return mh;
         }
 
--- a/nashorn/src/jdk/nashorn/internal/runtime/ScriptFunction.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/ScriptFunction.java	Tue Aug 20 17:46:45 2013 -0700
@@ -496,32 +496,24 @@
         MethodHandle boundHandle;
         MethodHandle guard = null;
 
+        final boolean scopeCall = NashornCallSiteDescriptor.isScope(desc);
+
         if (data.needsCallee()) {
             final MethodHandle callHandle = getBestInvoker(type, request.getArguments());
-            if (NashornCallSiteDescriptor.isScope(desc)) {
+            if (scopeCall) {
                 // Make a handle that drops the passed "this" argument and substitutes either Global or Undefined
                 // (callee, this, args...) => (callee, args...)
                 boundHandle = MH.insertArguments(callHandle, 1, needsWrappedThis() ? Context.getGlobalTrusted() : ScriptRuntime.UNDEFINED);
                 // (callee, args...) => (callee, [this], args...)
                 boundHandle = MH.dropArguments(boundHandle, 1, Object.class);
+
             } else {
                 // It's already (callee, this, args...), just what we need
                 boundHandle = callHandle;
-
-                // For non-strict functions, check whether this-object is primitive type.
-                // If so add a to-object-wrapper argument filter.
-                // Else install a guard that will trigger a relink when the argument becomes primitive.
-                if (needsWrappedThis()) {
-                    if (ScriptFunctionData.isPrimitiveThis(request.getArguments()[1])) {
-                        boundHandle = MH.filterArguments(boundHandle, 1, WRAPFILTER);
-                    } else {
-                        guard = getNonStrictFunctionGuard(this);
-                    }
-                }
             }
         } else {
             final MethodHandle callHandle = getBestInvoker(type.dropParameterTypes(0, 1), request.getArguments());
-            if (NashornCallSiteDescriptor.isScope(desc)) {
+            if (scopeCall) {
                 // Make a handle that drops the passed "this" argument and substitutes either Global or Undefined
                 // (this, args...) => (args...)
                 boundHandle = MH.bindTo(callHandle, needsWrappedThis() ? Context.getGlobalTrusted() : ScriptRuntime.UNDEFINED);
@@ -533,6 +525,17 @@
             }
         }
 
+        // For non-strict functions, check whether this-object is primitive type.
+        // If so add a to-object-wrapper argument filter.
+        // Else install a guard that will trigger a relink when the argument becomes primitive.
+        if (!scopeCall && needsWrappedThis()) {
+            if (ScriptFunctionData.isPrimitiveThis(request.getArguments()[1])) {
+                boundHandle = MH.filterArguments(boundHandle, 1, WRAPFILTER);
+            } else {
+                guard = getNonStrictFunctionGuard(this);
+            }
+        }
+
         boundHandle = pairArguments(boundHandle, type);
 
         return new GuardedInvocation(boundHandle, guard == null ? getFunctionGuard(this) : guard);
@@ -550,19 +553,18 @@
     private static MethodHandle bindToNameIfNeeded(final MethodHandle methodHandle, final String bindName) {
         if (bindName == null) {
             return methodHandle;
-        } else {
-            // if it is vararg method, we need to extend argument array with
-            // a new zeroth element that is set to bindName value.
-            final MethodType methodType = methodHandle.type();
-            final int parameterCount = methodType.parameterCount();
-            final boolean isVarArg = parameterCount > 0 && methodType.parameterType(parameterCount - 1).isArray();
+        }
 
-            if (isVarArg) {
-                return MH.filterArguments(methodHandle, 1, MH.insertArguments(ADD_ZEROTH_ELEMENT, 1, bindName));
-            } else {
-                return MH.insertArguments(methodHandle, 1, bindName);
-            }
+        // if it is vararg method, we need to extend argument array with
+        // a new zeroth element that is set to bindName value.
+        final MethodType methodType = methodHandle.type();
+        final int parameterCount = methodType.parameterCount();
+        final boolean isVarArg = parameterCount > 0 && methodType.parameterType(parameterCount - 1).isArray();
+
+        if (isVarArg) {
+            return MH.filterArguments(methodHandle, 1, MH.insertArguments(ADD_ZEROTH_ELEMENT, 1, bindName));
         }
+        return MH.insertArguments(methodHandle, 1, bindName);
     }
 
     /**
--- a/nashorn/src/jdk/nashorn/internal/runtime/ScriptFunctionData.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/ScriptFunctionData.java	Tue Aug 20 17:46:45 2013 -0700
@@ -250,9 +250,18 @@
         final int length = args == null ? 0 : args.length;
 
         CompiledFunctions boundList = new CompiledFunctions();
-        for (final CompiledFunction inv : code) {
+        if (code.size() == 1) {
+            // only one variant - bind that
+            boundList.add(bind(code.first(), fn, self, allArgs));
+        } else {
+            // There are specialized versions. Get the most generic one.
+            // This is to avoid ambiguous overloaded versions of bound and
+            // specialized variants and choosing wrong overload.
+            final MethodHandle genInvoker = getGenericInvoker();
+            final CompiledFunction inv = new CompiledFunction(genInvoker.type(), genInvoker, getGenericConstructor());
             boundList.add(bind(inv, fn, self, allArgs));
         }
+
         ScriptFunctionData boundData = new FinalScriptFunctionData(name, arity == -1 ? -1 : Math.max(0, arity - length), boundList, isStrict(), isBuiltin(), isConstructor());
         return boundData;
     }
--- a/nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java	Tue Aug 20 17:46:45 2013 -0700
@@ -138,10 +138,10 @@
     private static final MethodHandle KNOWNFUNCPROPGUARD = findOwnMH("knownFunctionPropertyGuard", boolean.class, Object.class, PropertyMap.class, MethodHandle.class, Object.class, ScriptFunction.class);
 
     /** Method handle for getting a function argument at a given index. Used from MapCreator */
-    public static final Call GET_ARGUMENT       = virtualCall(ScriptObject.class, "getArgument", Object.class, int.class);
+    public static final Call GET_ARGUMENT       = virtualCall(MethodHandles.lookup(), ScriptObject.class, "getArgument", Object.class, int.class);
 
     /** Method handle for setting a function argument at a given index. Used from MapCreator */
-    public static final Call SET_ARGUMENT       = virtualCall(ScriptObject.class, "setArgument", void.class, int.class, Object.class);
+    public static final Call SET_ARGUMENT       = virtualCall(MethodHandles.lookup(), ScriptObject.class, "setArgument", void.class, int.class, Object.class);
 
     /** Method handle for getting the proto of a ScriptObject */
     public static final Call GET_PROTO          = virtualCallNoLookup(ScriptObject.class, "getProto", ScriptObject.class);
@@ -150,7 +150,7 @@
     public static final Call SET_PROTO          = virtualCallNoLookup(ScriptObject.class, "setProto", void.class, ScriptObject.class);
 
     /** Method handle for setting the user accessors of a ScriptObject */
-    public static final Call SET_USER_ACCESSORS = virtualCall(ScriptObject.class, "setUserAccessors", void.class, String.class, ScriptFunction.class, ScriptFunction.class);
+    public static final Call SET_USER_ACCESSORS = virtualCall(MethodHandles.lookup(), ScriptObject.class, "setUserAccessors", void.class, String.class, ScriptFunction.class, ScriptFunction.class);
 
     /**
      * Constructor
@@ -2012,9 +2012,10 @@
         final boolean scopeAccess = isScope() && NashornCallSiteDescriptor.isScope(desc);
 
         if (find != null) {
-            final Object value = getObjectValue(find);
-            ScriptFunction func = null;
-            MethodHandle methodHandle = null;
+            final Object   value        = getObjectValue(find);
+            ScriptFunction func         = null;
+            MethodHandle   methodHandle = null;
+
             if (value instanceof ScriptFunction) {
                 func = (ScriptFunction)value;
                 methodHandle = getCallMethodHandle(func, desc.getMethodType(), name);
@@ -3219,6 +3220,11 @@
         return property;
     }
 
+    /**
+     * Write a value to a spill slot
+     * @param slot  the slot index
+     * @param value the value
+     */
     protected final void setSpill(final int slot, final Object value) {
         if (spill == null) {
             // create new spill.
@@ -3233,6 +3239,11 @@
         spill[slot] = value;
     }
 
+    /**
+     * Get a value from a spill slot
+     * @param slot the slot index
+     * @return the value in the spill slot with the given index
+     */
     protected Object getSpill(final int slot) {
         return spill != null && slot < spill.length ? spill[slot] : null;
     }
--- a/nashorn/src/jdk/nashorn/internal/runtime/ScriptRuntime.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/ScriptRuntime.java	Tue Aug 20 17:46:45 2013 -0700
@@ -33,6 +33,7 @@
 import static jdk.nashorn.internal.runtime.JSType.isRepresentableAsInt;
 
 import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
 import java.lang.reflect.Array;
 import java.util.Collections;
 import java.util.Iterator;
@@ -100,7 +101,7 @@
       * call sites that are known to be megamorphic. Using an invoke dynamic here would
       * lead to the JVM deoptimizing itself to death
       */
-    public static final Call APPLY = staticCall(ScriptRuntime.class, "apply", Object.class, ScriptFunction.class, Object.class, Object[].class);
+    public static final Call APPLY = staticCall(MethodHandles.lookup(), ScriptRuntime.class, "apply", Object.class, ScriptFunction.class, Object.class, Object[].class);
 
     /**
      * Converts a switch tag value to a simple integer. deflt value if it can't.
--- a/nashorn/src/jdk/nashorn/internal/runtime/UserAccessorProperty.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/UserAccessorProperty.java	Tue Aug 20 17:46:45 2013 -0700
@@ -27,6 +27,7 @@
 
 import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodHandles;
+import java.util.concurrent.Callable;
 
 import jdk.nashorn.internal.codegen.CompilerConstants;
 import jdk.nashorn.internal.lookup.Lookup;
@@ -68,12 +69,32 @@
             "userAccessorSetter", void.class, ScriptObject.class, int.class, String.class, Object.class, Object.class);
 
     /** Dynamic invoker for getter */
-    private static final MethodHandle INVOKE_UA_GETTER = Bootstrap.createDynamicInvoker("dyn:call", Object.class,
-            Object.class, Object.class);
+    private static final Object INVOKE_UA_GETTER = new Object();
+
+    private static MethodHandle getINVOKE_UA_GETTER() {
+
+        return ((GlobalObject)Context.getGlobal()).getDynamicInvoker(INVOKE_UA_GETTER,
+                new Callable<MethodHandle>() {
+                    @Override
+                    public MethodHandle call() {
+                        return Bootstrap.createDynamicInvoker("dyn:call", Object.class,
+                            Object.class, Object.class);
+                    }
+                });
+    }
 
     /** Dynamic invoker for setter */
-    private static final MethodHandle INVOKE_UA_SETTER = Bootstrap.createDynamicInvoker("dyn:call", void.class,
-            Object.class, Object.class, Object.class);
+    private static Object INVOKE_UA_SETTER = new Object();
+    private static MethodHandle getINVOKE_UA_SETTER() {
+        return ((GlobalObject)Context.getGlobal()).getDynamicInvoker(INVOKE_UA_SETTER,
+                new Callable<MethodHandle>() {
+                    @Override
+                    public MethodHandle call() {
+                        return Bootstrap.createDynamicInvoker("dyn:call", void.class,
+                            Object.class, Object.class, Object.class);
+                    }
+                });
+    }
 
     /**
      * Constructor
@@ -191,7 +212,7 @@
 
         if (func instanceof ScriptFunction) {
             try {
-                return INVOKE_UA_GETTER.invokeExact(func, self);
+                return getINVOKE_UA_GETTER().invokeExact(func, self);
             } catch(final Error|RuntimeException t) {
                 throw t;
             } catch(final Throwable t) {
@@ -208,7 +229,7 @@
 
         if (func instanceof ScriptFunction) {
             try {
-                INVOKE_UA_SETTER.invokeExact(func, self, value);
+                getINVOKE_UA_SETTER().invokeExact(func, self, value);
             } catch(final Error|RuntimeException t) {
                 throw t;
             } catch(final Throwable t) {
--- a/nashorn/src/jdk/nashorn/internal/runtime/arrays/ArrayIterator.java	Wed Jul 05 19:08:14 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,88 +0,0 @@
-/*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package jdk.nashorn.internal.runtime.arrays;
-
-import jdk.nashorn.internal.runtime.ScriptObject;
-
-/**
- * Iterator over a NativeArray
- */
-class ArrayIterator extends ArrayLikeIterator<Object> {
-
-    /** Array {@link ScriptObject} to iterate over */
-    protected final ScriptObject array;
-
-    /** length of array */
-    protected final long length;
-
-    /**
-     * Constructor
-     * @param array array to iterate over
-     * @param includeUndefined should undefined elements be included in iteration
-     */
-    protected ArrayIterator(final ScriptObject array, final boolean includeUndefined) {
-        super(includeUndefined);
-        this.array = array;
-        this.length = array.getArray().length();
-    }
-
-    /**
-     * Is the current index still inside the array
-     * @return true if inside the array
-     */
-    protected boolean indexInArray() {
-        return index < length;
-    }
-
-    @Override
-    public Object next() {
-        return array.get(bumpIndex());
-    }
-
-    @Override
-    public long getLength() {
-        return length;
-    }
-
-    @Override
-    public boolean hasNext() {
-        if (!includeUndefined) {
-            while (indexInArray()) {
-                if (array.has(index)) {
-                    break;
-                }
-                bumpIndex();
-            }
-        }
-
-        return indexInArray();
-    }
-
-    @Override
-    public void remove() {
-        array.delete(index, false);
-    }
-}
--- a/nashorn/src/jdk/nashorn/internal/runtime/arrays/ArrayLikeIterator.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/arrays/ArrayLikeIterator.java	Tue Aug 20 17:46:45 2013 -0700
@@ -26,6 +26,7 @@
 package jdk.nashorn.internal.runtime.arrays;
 
 import java.util.Iterator;
+import java.util.List;
 import jdk.nashorn.api.scripting.ScriptObjectMirror;
 import jdk.nashorn.internal.runtime.JSType;
 import jdk.nashorn.internal.runtime.ScriptObject;
@@ -49,7 +50,7 @@
      *
      * @param includeUndefined should undefined elements be included in the iteration?
      */
-    protected ArrayLikeIterator(final boolean includeUndefined) {
+    ArrayLikeIterator(final boolean includeUndefined) {
         this.includeUndefined = includeUndefined;
         this.index = 0;
     }
@@ -118,18 +119,26 @@
         Object obj = object;
 
         if (ScriptObject.isArray(obj)) {
-            return new ArrayIterator((ScriptObject) obj, includeUndefined);
+            return new ScriptArrayIterator((ScriptObject) obj, includeUndefined);
         }
 
         obj = JSType.toScriptObject(obj);
         if (obj instanceof ScriptObject) {
-            return new MapIterator((ScriptObject)obj, includeUndefined);
+            return new ScriptObjectIterator((ScriptObject)obj, includeUndefined);
         }
 
         if (obj instanceof ScriptObjectMirror) {
             return new ScriptObjectMirrorIterator((ScriptObjectMirror)obj, includeUndefined);
         }
 
+        if (obj instanceof List) {
+            return new JavaListIterator((List<?>)obj, includeUndefined);
+        }
+
+        if (obj != null && obj.getClass().isArray()) {
+            return new JavaArrayIterator(obj, includeUndefined);
+        }
+
         return new EmptyArrayLikeIterator();
     }
 
@@ -143,19 +152,25 @@
         Object obj = object;
 
         if (ScriptObject.isArray(obj)) {
-            return new ReverseArrayIterator((ScriptObject) obj, includeUndefined);
+            return new ReverseScriptArrayIterator((ScriptObject) obj, includeUndefined);
         }
 
         obj = JSType.toScriptObject(obj);
         if (obj instanceof ScriptObject) {
-            return new ReverseMapIterator((ScriptObject)obj, includeUndefined);
+            return new ReverseScriptObjectIterator((ScriptObject)obj, includeUndefined);
         }
 
         if (obj instanceof ScriptObjectMirror) {
             return new ReverseScriptObjectMirrorIterator((ScriptObjectMirror)obj, includeUndefined);
         }
 
-        assert !obj.getClass().isArray();
+        if (obj instanceof List) {
+            return new ReverseJavaListIterator((List<?>)obj, includeUndefined);
+        }
+
+        if (obj != null && obj.getClass().isArray()) {
+            return new ReverseJavaArrayIterator(obj, includeUndefined);
+        }
 
         return new EmptyArrayLikeIterator();
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/src/jdk/nashorn/internal/runtime/arrays/JavaArrayIterator.java	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.nashorn.internal.runtime.arrays;
+
+import java.lang.reflect.Array;
+
+/**
+  * Iterator over a Java List.
+ */
+class JavaArrayIterator extends ArrayLikeIterator<Object> {
+
+    /** Array to iterate over */
+    protected final Object array;
+
+    /** length of array */
+    protected final long length;
+
+    /**
+     * Constructor
+     * @param array array to iterate over
+     * @param includeUndefined should undefined elements be included in iteration
+     */
+    protected JavaArrayIterator(final Object array, final boolean includeUndefined) {
+        super(includeUndefined);
+        assert array.getClass().isArray() : "expecting Java array object";
+        this.array = array;
+        this.length = Array.getLength(array);
+    }
+
+    /**
+     * Is the current index still inside the array
+     * @return true if inside the array
+     */
+    protected boolean indexInArray() {
+        return index < length;
+    }
+
+    @Override
+    public Object next() {
+        return Array.get(array, (int)bumpIndex());
+    }
+
+    @Override
+    public long getLength() {
+        return length;
+    }
+
+    @Override
+    public boolean hasNext() {
+        return indexInArray();
+    }
+
+    @Override
+    public void remove() {
+        throw new UnsupportedOperationException("remove");
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/src/jdk/nashorn/internal/runtime/arrays/JavaListIterator.java	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.nashorn.internal.runtime.arrays;
+
+import java.util.List;
+
+/**
+  * Iterator over a Java List.
+ */
+class JavaListIterator extends ArrayLikeIterator<Object> {
+
+    /** {@link java.util.List} to iterate over */
+    protected final List<?> list;
+
+    /** length of array */
+    protected final long length;
+
+    /**
+     * Constructor
+     * @param list list to iterate over
+     * @param includeUndefined should undefined elements be included in iteration
+     */
+    protected JavaListIterator(final List<?> list, final boolean includeUndefined) {
+        super(includeUndefined);
+        this.list = list;
+        this.length = list.size();
+    }
+
+    /**
+     * Is the current index still inside the array
+     * @return true if inside the array
+     */
+    protected boolean indexInArray() {
+        return index < length;
+    }
+
+    @Override
+    public Object next() {
+        return list.get((int)bumpIndex());
+    }
+
+    @Override
+    public long getLength() {
+        return length;
+    }
+
+    @Override
+    public boolean hasNext() {
+        return indexInArray();
+    }
+
+    @Override
+    public void remove() {
+        list.remove(index);
+    }
+}
--- a/nashorn/src/jdk/nashorn/internal/runtime/arrays/LongArrayData.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/arrays/LongArrayData.java	Tue Aug 20 17:46:45 2013 -0700
@@ -98,9 +98,8 @@
         final int length = (int) length();
         if (type == Double.class) {
             return new NumberArrayData(LongArrayData.toDoubleArray(array, length), length);
-        } else {
-            return new ObjectArrayData(LongArrayData.toObjectArray(array, length), length);
         }
+        return new ObjectArrayData(LongArrayData.toObjectArray(array, length), length);
     }
 
     @Override
--- a/nashorn/src/jdk/nashorn/internal/runtime/arrays/MapIterator.java	Wed Jul 05 19:08:14 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,80 +0,0 @@
-/*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package jdk.nashorn.internal.runtime.arrays;
-
-import java.util.NoSuchElementException;
-import jdk.nashorn.internal.runtime.JSType;
-import jdk.nashorn.internal.runtime.ScriptObject;
-
-/**
- * Iterator over a map
- */
-class MapIterator extends ArrayLikeIterator<Object> {
-
-    protected final ScriptObject obj;
-    private final long length;
-
-    MapIterator(final ScriptObject obj, final boolean includeUndefined) {
-        super(includeUndefined);
-        this.obj    = obj;
-        this.length = JSType.toUint32(obj.getLength());
-        this.index  = 0;
-    }
-
-    protected boolean indexInArray() {
-        return index < length;
-    }
-
-    @Override
-    public long getLength() {
-        return length;
-    }
-
-    @Override
-    public boolean hasNext() {
-        if (length == 0L) {
-            return false; //return empty string if toUint32(length) == 0
-        }
-
-        while (indexInArray()) {
-            if (obj.has(index) || includeUndefined) {
-                break;
-            }
-            bumpIndex();
-        }
-
-        return indexInArray();
-    }
-
-    @Override
-    public Object next() {
-        if (indexInArray()) {
-            return obj.get(bumpIndex());
-        }
-
-        throw new NoSuchElementException();
-    }
-}
--- a/nashorn/src/jdk/nashorn/internal/runtime/arrays/ReverseArrayIterator.java	Wed Jul 05 19:08:14 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,59 +0,0 @@
-/*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package jdk.nashorn.internal.runtime.arrays;
-
-import jdk.nashorn.internal.runtime.ScriptObject;
-
-/**
- * Reverse iterator over a NativeArray
- */
-final class ReverseArrayIterator extends ArrayIterator {
-
-    /**
-     * Constructor
-     * @param array array to iterate over
-     * @param includeUndefined should undefined elements be included in iteration
-     */
-    public ReverseArrayIterator(final ScriptObject array, final boolean includeUndefined) {
-        super(array, includeUndefined);
-        this.index = array.getArray().length() - 1;
-    }
-
-    @Override
-    public boolean isReverse() {
-        return true;
-    }
-
-    @Override
-    protected boolean indexInArray() {
-        return index >= 0;
-    }
-
-    @Override
-    protected long bumpIndex() {
-        return index--;
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/src/jdk/nashorn/internal/runtime/arrays/ReverseJavaArrayIterator.java	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.nashorn.internal.runtime.arrays;
+
+import java.lang.reflect.Array;
+
+/**
+ * Reverse iterator over a array
+ */
+final class ReverseJavaArrayIterator extends JavaArrayIterator {
+    /**
+     * Constructor
+     * @param array array to iterate over
+     * @param includeUndefined should undefined elements be included in iteration
+     */
+    public ReverseJavaArrayIterator(final Object array, final boolean includeUndefined) {
+        super(array, includeUndefined);
+        this.index = Array.getLength(array) - 1;
+    }
+
+    @Override
+    public boolean isReverse() {
+        return true;
+    }
+
+    @Override
+    protected boolean indexInArray() {
+        return index >= 0;
+    }
+
+    @Override
+    protected long bumpIndex() {
+        return index--;
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/src/jdk/nashorn/internal/runtime/arrays/ReverseJavaListIterator.java	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.nashorn.internal.runtime.arrays;
+
+import java.util.List;
+
+/**
+ * Reverse iterator over a List
+ */
+final class ReverseJavaListIterator extends JavaListIterator {
+    /**
+     * Constructor
+     * @param list list to iterate over
+     * @param includeUndefined should undefined elements be included in iteration
+     */
+    public ReverseJavaListIterator(final List<?> list, final boolean includeUndefined) {
+        super(list, includeUndefined);
+        this.index = list.size() - 1;
+    }
+
+    @Override
+    public boolean isReverse() {
+        return true;
+    }
+
+    @Override
+    protected boolean indexInArray() {
+        return index >= 0;
+    }
+
+    @Override
+    protected long bumpIndex() {
+        return index--;
+    }
+}
--- a/nashorn/src/jdk/nashorn/internal/runtime/arrays/ReverseMapIterator.java	Wed Jul 05 19:08:14 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,55 +0,0 @@
-/*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package jdk.nashorn.internal.runtime.arrays;
-
-import jdk.nashorn.internal.runtime.JSType;
-import jdk.nashorn.internal.runtime.ScriptObject;
-
-/**
- * Reverse iterator over a map
- */
-final class ReverseMapIterator extends MapIterator {
-
-    ReverseMapIterator(final ScriptObject obj, final boolean includeUndefined) {
-        super(obj, includeUndefined);
-        this.index = JSType.toUint32(obj.getLength()) - 1;
-    }
-
-    @Override
-    public boolean isReverse() {
-        return true;
-    }
-
-    @Override
-    protected boolean indexInArray() {
-        return index >= 0;
-    }
-
-    @Override
-    protected long bumpIndex() {
-        return index--;
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/src/jdk/nashorn/internal/runtime/arrays/ReverseScriptArrayIterator.java	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.nashorn.internal.runtime.arrays;
+
+import jdk.nashorn.internal.runtime.ScriptObject;
+
+/**
+ * Reverse iterator over a NativeArray
+ */
+final class ReverseScriptArrayIterator extends ScriptArrayIterator {
+
+    /**
+     * Constructor
+     * @param array array to iterate over
+     * @param includeUndefined should undefined elements be included in iteration
+     */
+    public ReverseScriptArrayIterator(final ScriptObject array, final boolean includeUndefined) {
+        super(array, includeUndefined);
+        this.index = array.getArray().length() - 1;
+    }
+
+    @Override
+    public boolean isReverse() {
+        return true;
+    }
+
+    @Override
+    protected boolean indexInArray() {
+        return index >= 0;
+    }
+
+    @Override
+    protected long bumpIndex() {
+        return index--;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/src/jdk/nashorn/internal/runtime/arrays/ReverseScriptObjectIterator.java	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.nashorn.internal.runtime.arrays;
+
+import jdk.nashorn.internal.runtime.JSType;
+import jdk.nashorn.internal.runtime.ScriptObject;
+
+/**
+ * Reverse iterator over a map
+ */
+final class ReverseScriptObjectIterator extends ScriptObjectIterator {
+
+    ReverseScriptObjectIterator(final ScriptObject obj, final boolean includeUndefined) {
+        super(obj, includeUndefined);
+        this.index = JSType.toUint32(obj.getLength()) - 1;
+    }
+
+    @Override
+    public boolean isReverse() {
+        return true;
+    }
+
+    @Override
+    protected boolean indexInArray() {
+        return index >= 0;
+    }
+
+    @Override
+    protected long bumpIndex() {
+        return index--;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/src/jdk/nashorn/internal/runtime/arrays/ScriptArrayIterator.java	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.nashorn.internal.runtime.arrays;
+
+import jdk.nashorn.internal.runtime.ScriptObject;
+
+/**
+ * Iterator over a NativeArray
+ */
+class ScriptArrayIterator extends ArrayLikeIterator<Object> {
+
+    /** Array {@link ScriptObject} to iterate over */
+    protected final ScriptObject array;
+
+    /** length of array */
+    protected final long length;
+
+    /**
+     * Constructor
+     * @param array array to iterate over
+     * @param includeUndefined should undefined elements be included in iteration
+     */
+    protected ScriptArrayIterator(final ScriptObject array, final boolean includeUndefined) {
+        super(includeUndefined);
+        this.array = array;
+        this.length = array.getArray().length();
+    }
+
+    /**
+     * Is the current index still inside the array
+     * @return true if inside the array
+     */
+    protected boolean indexInArray() {
+        return index < length;
+    }
+
+    @Override
+    public Object next() {
+        return array.get(bumpIndex());
+    }
+
+    @Override
+    public long getLength() {
+        return length;
+    }
+
+    @Override
+    public boolean hasNext() {
+        if (!includeUndefined) {
+            while (indexInArray()) {
+                if (array.has(index)) {
+                    break;
+                }
+                bumpIndex();
+            }
+        }
+
+        return indexInArray();
+    }
+
+    @Override
+    public void remove() {
+        array.delete(index, false);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/src/jdk/nashorn/internal/runtime/arrays/ScriptObjectIterator.java	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.nashorn.internal.runtime.arrays;
+
+import java.util.NoSuchElementException;
+import jdk.nashorn.internal.runtime.JSType;
+import jdk.nashorn.internal.runtime.ScriptObject;
+
+/**
+ * Iterator over a map
+ */
+class ScriptObjectIterator extends ArrayLikeIterator<Object> {
+
+    protected final ScriptObject obj;
+    private final long length;
+
+    ScriptObjectIterator(final ScriptObject obj, final boolean includeUndefined) {
+        super(includeUndefined);
+        this.obj    = obj;
+        this.length = JSType.toUint32(obj.getLength());
+        this.index  = 0;
+    }
+
+    protected boolean indexInArray() {
+        return index < length;
+    }
+
+    @Override
+    public long getLength() {
+        return length;
+    }
+
+    @Override
+    public boolean hasNext() {
+        if (length == 0L) {
+            return false; //return empty string if toUint32(length) == 0
+        }
+
+        while (indexInArray()) {
+            if (obj.has(index) || includeUndefined) {
+                break;
+            }
+            bumpIndex();
+        }
+
+        return indexInArray();
+    }
+
+    @Override
+    public Object next() {
+        if (indexInArray()) {
+            return obj.get(bumpIndex());
+        }
+
+        throw new NoSuchElementException();
+    }
+}
--- a/nashorn/src/jdk/nashorn/internal/runtime/arrays/SparseArrayData.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/arrays/SparseArrayData.java	Tue Aug 20 17:46:45 2013 -0700
@@ -60,7 +60,7 @@
 
     @Override
     public ArrayData copy() {
-        return new SparseArrayData(underlying.copy(), length(), new TreeMap<Long, Object>(sparseMap));
+        return new SparseArrayData(underlying.copy(), length(), new TreeMap<>(sparseMap));
     }
 
     @Override
--- a/nashorn/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java	Tue Aug 20 17:46:45 2013 -0700
@@ -68,6 +68,10 @@
         if (relinkThreshold > -1) {
             factory.setUnstableRelinkThreshold(relinkThreshold);
         }
+
+        // Linkers for any additional language runtimes deployed alongside Nashorn will be picked up by the factory.
+        factory.setClassLoader(Bootstrap.class.getClassLoader());
+
         dynamicLinker = factory.createLinker();
     }
 
--- a/nashorn/src/jdk/nashorn/internal/runtime/linker/BoundDynamicMethod.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/linker/BoundDynamicMethod.java	Tue Aug 20 17:46:45 2013 -0700
@@ -29,7 +29,7 @@
 
 /**
  * Represents a Dynalink dynamic method bound to a receiver. Note that objects of this class are just the tuples of
- * a method and a bound this, without any behavior. All the behavior is defined in the {@link BoundDynamicMethodLinker}.
+ * a method and a bound this, without any behavior. All the behavior is defined in the {@code BoundDynamicMethodLinker}.
  */
 final class BoundDynamicMethod {
     private final Object dynamicMethod;
--- a/nashorn/src/jdk/nashorn/internal/runtime/linker/BoundDynamicMethodLinker.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/linker/BoundDynamicMethodLinker.java	Tue Aug 20 17:46:45 2013 -0700
@@ -37,7 +37,7 @@
 import jdk.internal.dynalink.support.Guards;
 
 /**
- * Links {@link BoundDynamicMethod} objects. Passes through to Dynalink's BeansLinker for linking a dynamic method
+ * Links {@code BoundDynamicMethod} objects. Passes through to Dynalink's BeansLinker for linking a dynamic method
  * (they only respond to "dyn:call"), and modifies the returned invocation to deal with the receiver binding.
  */
 final class BoundDynamicMethodLinker implements TypeBasedGuardingDynamicLinker {
--- a/nashorn/src/jdk/nashorn/internal/runtime/linker/ClassAndLoader.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/linker/ClassAndLoader.java	Tue Aug 20 17:46:45 2013 -0700
@@ -27,8 +27,11 @@
 
 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
 
+import java.security.AccessControlContext;
 import java.security.AccessController;
+import java.security.Permissions;
 import java.security.PrivilegedAction;
+import java.security.ProtectionDomain;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
@@ -43,6 +46,16 @@
  * used to determine if one loader can see the other loader's classes.
  */
 final class ClassAndLoader {
+    static AccessControlContext createPermAccCtxt(final String... permNames) {
+        final Permissions perms = new Permissions();
+        for (final String permName : permNames) {
+            perms.add(new RuntimePermission(permName));
+        }
+        return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
+    }
+
+    private static final AccessControlContext GET_LOADER_ACC_CTXT = createPermAccCtxt("getClassLoader");
+
     private final Class<?> representativeClass;
     // Don't access this directly; most of the time, use getRetrievedLoader(), or if you know what you're doing,
     // getLoader().
@@ -116,7 +129,7 @@
             public ClassAndLoader run() {
                 return getDefiningClassAndLoaderPrivileged(types);
             }
-        });
+        }, GET_LOADER_ACC_CTXT);
     }
 
     static ClassAndLoader getDefiningClassAndLoaderPrivileged(final Class<?>[] types) {
--- a/nashorn/src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java	Tue Aug 20 17:46:45 2013 -0700
@@ -49,6 +49,7 @@
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.security.AccessControlContext;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.Arrays;
@@ -868,6 +869,8 @@
         }
     }
 
+    private static final AccessControlContext GET_DECLARED_MEMBERS_ACC_CTXT = ClassAndLoader.createPermAccCtxt("accessDeclaredMembers");
+
     /**
      * Creates a collection of methods that are not final, but we still never allow them to be overridden in adapters,
      * as explicitly declaring them automatically is a bad idea. Currently, this means {@code Object.finalize()} and
@@ -886,7 +889,7 @@
                     throw new AssertionError(e);
                 }
             }
-        });
+        }, GET_DECLARED_MEMBERS_ACC_CTXT);
     }
 
     private String getCommonSuperClass(final String type1, final String type2) {
--- a/nashorn/src/jdk/nashorn/internal/runtime/linker/JavaAdapterClassLoader.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/linker/JavaAdapterClassLoader.java	Tue Aug 20 17:46:45 2013 -0700
@@ -25,6 +25,7 @@
 
 package jdk.nashorn.internal.runtime.linker;
 
+import java.security.AccessControlContext;
 import java.security.AccessController;
 import java.security.AllPermission;
 import java.security.CodeSigner;
@@ -46,9 +47,10 @@
 @SuppressWarnings("javadoc")
 final class JavaAdapterClassLoader {
     private static final ProtectionDomain GENERATED_PROTECTION_DOMAIN = createGeneratedProtectionDomain();
+    private static final AccessControlContext CREATE_LOADER_ACC_CTXT = ClassAndLoader.createPermAccCtxt("createClassLoader");
 
     private final String className;
-    private final byte[] classBytes;
+    private volatile byte[] classBytes;
 
     JavaAdapterClassLoader(String className, byte[] classBytes) {
         this.className = className.replace('/', '.');
@@ -56,6 +58,13 @@
     }
 
     /**
+     * clear classBytes after loading class.
+     */
+    void clearClassBytes() {
+       this.classBytes = null;
+    }
+
+    /**
      * Loads the generated adapter class into the JVM.
      * @param parentLoader the parent class loader for the generated class loader
      * @return the generated adapter class
@@ -70,7 +79,7 @@
                     throw new AssertionError(e); // cannot happen
                 }
             }
-        });
+        }, CREATE_LOADER_ACC_CTXT);
     }
 
     // Note that the adapter class is created in the protection domain of the class/interface being
@@ -103,10 +112,10 @@
             @Override
             protected Class<?> findClass(final String name) throws ClassNotFoundException {
                 if(name.equals(className)) {
+                    assert classBytes != null : "what? already cleared .class bytes!!";
                     return defineClass(name, classBytes, 0, classBytes.length, GENERATED_PROTECTION_DOMAIN);
-                } else {
-                    throw new ClassNotFoundException(name);
                 }
+                throw new ClassNotFoundException(name);
             }
         };
     }
--- a/nashorn/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java	Tue Aug 20 17:46:45 2013 -0700
@@ -31,9 +31,9 @@
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
 import java.lang.reflect.Modifier;
+import java.security.AccessControlContext;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
-import java.security.PrivilegedExceptionAction;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -70,6 +70,11 @@
 
 @SuppressWarnings("javadoc")
 public final class JavaAdapterFactory {
+    // context with permissions needs for AdapterInfo creation
+    private static final AccessControlContext CREATE_ADAPTER_INFO_ACC_CTXT =
+        ClassAndLoader.createPermAccCtxt("createClassLoader", "getClassLoader",
+            "accessDeclaredMembers", "accessClassInPackage.jdk.nashorn.internal.runtime");
+
     /**
      * A mapping from an original Class object to AdapterInfo representing the adapter for the class it represents.
      */
@@ -124,17 +129,10 @@
      */
     public static MethodHandle getConstructor(final Class<?> sourceType, final Class<?> targetType) throws Exception {
         final StaticClass adapterClass = getAdapterClassFor(new Class<?>[] { targetType }, null);
-        return AccessController.doPrivileged(new PrivilegedExceptionAction<MethodHandle>() {
-            @Override
-            public MethodHandle run() throws Exception {
-                // NOTE: we use publicLookup(), but none of our adapter constructors are caller sensitive, so this is
-                // okay, we won't artificially limit access.
-                return  MH.bindTo(Bootstrap.getLinkerServices().getGuardedInvocation(new LinkRequestImpl(
-                        NashornCallSiteDescriptor.get(MethodHandles.publicLookup(),  "dyn:new",
-                                MethodType.methodType(targetType, StaticClass.class, sourceType), 0), false,
-                                adapterClass, null)).getInvocation(), adapterClass);
-            }
-        });
+        return MH.bindTo(Bootstrap.getLinkerServices().getGuardedInvocation(new LinkRequestImpl(
+                NashornCallSiteDescriptor.get(MethodHandles.publicLookup(),  "dyn:new",
+                        MethodType.methodType(targetType, StaticClass.class, sourceType), 0), false,
+                        adapterClass, null)).getInvocation(), adapterClass);
     }
 
     /**
@@ -171,7 +169,7 @@
         return (List)Collections.singletonList(clazz);
     }
 
-    /**
+   /**
      * For a given class, create its adapter class and associated info.
      * @param type the class for which the adapter is created
      * @return the adapter info for the class.
@@ -190,12 +188,19 @@
                 }
                 superClass = t;
             } else {
+                if (interfaces.size() > 65535) {
+                    throw new IllegalArgumentException("interface limit exceeded");
+                }
+
                 interfaces.add(t);
             }
+
             if(!Modifier.isPublic(mod)) {
                 return new AdapterInfo(AdaptationResult.Outcome.ERROR_NON_PUBLIC_CLASS, t.getCanonicalName());
             }
         }
+
+
         final Class<?> effectiveSuperClass = superClass == null ? Object.class : superClass;
         return AccessController.doPrivileged(new PrivilegedAction<AdapterInfo>() {
             @Override
@@ -206,7 +211,7 @@
                     return new AdapterInfo(e.getAdaptationResult());
                 }
             }
-        });
+        }, CREATE_ADAPTER_INFO_ACC_CTXT);
     }
 
     private static class AdapterInfo {
@@ -224,7 +229,10 @@
             this.commonLoader = findCommonLoader(definingLoader);
             final JavaAdapterBytecodeGenerator gen = new JavaAdapterBytecodeGenerator(superClass, interfaces, commonLoader, false);
             this.autoConvertibleFromFunction = gen.isAutoConvertibleFromFunction();
-            this.instanceAdapterClass = gen.createAdapterClassLoader().generateClass(commonLoader);
+            final JavaAdapterClassLoader jacl = gen.createAdapterClassLoader();
+            this.instanceAdapterClass = jacl.generateClass(commonLoader);
+            // loaded Class - no need to keep class bytes around
+            jacl.clearClassBytes();
             this.adapterGenerator = new JavaAdapterBytecodeGenerator(superClass, interfaces, commonLoader, true).createAdapterClassLoader();
             this.adaptationResult = AdaptationResult.SUCCESSFUL_RESULT;
         }
--- a/nashorn/src/jdk/nashorn/internal/runtime/linker/ReflectionCheckLinker.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/linker/ReflectionCheckLinker.java	Tue Aug 20 17:46:45 2013 -0700
@@ -88,6 +88,6 @@
     }
 
     private static void checkReflectionPermission(final SecurityManager sm) {
-        sm.checkPermission(new RuntimePermission("nashorn.JavaReflection"));
+        sm.checkPermission(new RuntimePermission(Context.NASHORN_JAVA_REFLECTION));
     }
 }
--- a/nashorn/src/jdk/nashorn/internal/runtime/options/Options.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/options/Options.java	Tue Aug 20 17:46:45 2013 -0700
@@ -26,8 +26,11 @@
 package jdk.nashorn.internal.runtime.options;
 
 import java.io.PrintWriter;
+import java.security.AccessControlContext;
 import java.security.AccessController;
+import java.security.Permissions;
 import java.security.PrivilegedAction;
+import java.security.ProtectionDomain;
 import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -39,6 +42,7 @@
 import java.util.Locale;
 import java.util.Map;
 import java.util.MissingResourceException;
+import java.util.PropertyPermission;
 import java.util.ResourceBundle;
 import java.util.StringTokenizer;
 import java.util.TimeZone;
@@ -51,6 +55,15 @@
  * Manages global runtime options.
  */
 public final class Options {
+    // permission to just read nashorn.* System properties
+    private static AccessControlContext createPropertyReadAccCtxt() {
+        final Permissions perms = new Permissions();
+        perms.add(new PropertyPermission("nashorn.*", "read"));
+        return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
+    }
+
+    private static final AccessControlContext READ_PROPERTY_ACC_CTXT = createPropertyReadAccCtxt();
+
     /** Resource tag. */
     private final String resource;
 
@@ -144,7 +157,7 @@
                             return false;
                         }
                     }
-                });
+                }, READ_PROPERTY_ACC_CTXT);
     }
 
     /**
@@ -171,7 +184,7 @@
                             return defValue;
                         }
                     }
-                });
+                }, READ_PROPERTY_ACC_CTXT);
     }
 
     /**
@@ -198,7 +211,7 @@
                             return defValue;
                         }
                     }
-                });
+                }, READ_PROPERTY_ACC_CTXT);
     }
 
     /**
@@ -395,13 +408,13 @@
         final LinkedList<String> argList = new LinkedList<>();
         Collections.addAll(argList, args);
 
-    final String extra = getStringProperty(NASHORN_ARGS_PROPERTY, null);
-    if (extra != null) {
-        final StringTokenizer st = new StringTokenizer(extra);
-        while (st.hasMoreTokens()) {
-        argList.add(st.nextToken());
+        final String extra = getStringProperty(NASHORN_ARGS_PROPERTY, null);
+        if (extra != null) {
+            final StringTokenizer st = new StringTokenizer(extra);
+            while (st.hasMoreTokens()) {
+                argList.add(st.nextToken());
+            }
         }
-    }
 
         while (!argList.isEmpty()) {
             final String arg = argList.remove(0);
@@ -418,8 +431,9 @@
                 continue;
             }
 
-            // if it doesn't start with -, it's a file
-            if (!arg.startsWith("-")) {
+            // If it doesn't start with -, it's a file. But, if it is just "-",
+            // then it is a file representing standard input.
+            if (!arg.startsWith("-") || arg.length() == 1) {
                 files.add(arg);
                 continue;
             }
@@ -567,15 +581,7 @@
     private static String definePropPrefix;
 
     static {
-        // Without do privileged, under security manager messages can not be
-        // loaded.
-        Options.bundle = AccessController.doPrivileged(new PrivilegedAction<ResourceBundle>() {
-            @Override
-            public ResourceBundle run() {
-                return ResourceBundle.getBundle(Options.MESSAGES_RESOURCE, Locale.getDefault());
-            }
-        });
-
+        Options.bundle = ResourceBundle.getBundle(Options.MESSAGES_RESOURCE, Locale.getDefault());
         Options.validOptions = new TreeSet<>();
         Options.usage        = new HashMap<>();
 
--- a/nashorn/src/jdk/nashorn/internal/runtime/resources/Messages.properties	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/resources/Messages.properties	Tue Aug 20 17:46:45 2013 -0700
@@ -100,7 +100,7 @@
 type.error.inconsistent.property.descriptor=inconsistent property descriptor
 type.error.bad.default.value=bad default value: {0}
 type.error.function.apply.expects.array=Function.prototype.apply expects an Array for second argument
-type.error.instanceof.on.non.object=instanceof cannot be used on objects without [[HasInstance]]
+type.error.instanceof.on.non.object=instanceof must be called with a javascript or java object as the right-hand argument
 type.error.cannot.convert.to.interface=object {0} cannot be converted to {1} due to "{2}"
 type.error.array.reduce.invalid.init=invalid initialValue for Array.prototype.reduce
 type.error.array.reduceright.invalid.init=invalid initialValue for Array.prototype.reduceRight
--- a/nashorn/src/jdk/nashorn/tools/Shell.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/src/jdk/nashorn/tools/Shell.java	Tue Aug 20 17:46:45 2013 -0700
@@ -34,8 +34,6 @@
 import java.io.OutputStream;
 import java.io.PrintStream;
 import java.io.PrintWriter;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
 import java.util.List;
 import java.util.Locale;
 import java.util.ResourceBundle;
@@ -68,18 +66,7 @@
     /**
      * Shell message bundle.
      */
-    private static ResourceBundle bundle;
-
-    static {
-        // Without do privileged, under security manager messages can not be
-        // loaded.
-        bundle = AccessController.doPrivileged(new PrivilegedAction<ResourceBundle>() {
-            @Override
-            public ResourceBundle run() {
-                return ResourceBundle.getBundle(MESSAGE_RESOURCE, Locale.getDefault());
-            }
-        });
-    }
+    private static final ResourceBundle bundle = ResourceBundle.getBundle(MESSAGE_RESOURCE, Locale.getDefault());
 
     /**
      * Exit code for command line tool - successful
@@ -305,6 +292,14 @@
 
             // For each file on the command line.
             for (final String fileName : files) {
+                if ("-".equals(fileName)) {
+                    final int res = readEvalPrint(context, global);
+                    if (res != SUCCESS) {
+                        return res;
+                    }
+                    continue;
+                }
+
                 final File file = new File(fileName);
                 final ScriptFunction script = context.compileScript(new Source(fileName, file.toURI().toURL()), global);
                 if (script == null || errors.getNumberOfErrors() != 0) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8019985.js	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8019985: Date.parse("2000-01-01T00:00:00.Z") should return NaN
+ *
+ * @test
+ * @run
+ */
+
+function testFail(str) {
+    if (!isNaN(Date.parse(str))) {
+        throw new Error("Parsed invalid date string: " + str);
+    }
+}
+
+function testOk(str) {
+    if (isNaN(Date.parse(str))) {
+        throw new Error("Failed to parse valid date string: " + str);
+    }
+}
+
+testFail("2000-01-01T00:00:00.Z");
+testFail("2000-01-01T00:00:Z");
+testFail("2000-01-01T00:Z");
+testFail("2000-01-01T00Z");
+testOk("2000-01-01T00:00:00.000Z");
+testOk("2000-01-01T00:00:00.0Z");
+testOk("2000-01-01T00:00:00Z");
+testOk("2000-01-01T00:00Z");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8020355.js	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8020355: bind on built-in constructors don't use bound argument values
+ *
+ * @test
+ * @run
+ */
+
+if (Array.bind(null, 2)().length != 2) {
+    fail("Expected Array.bind(null, 2)().length to be 2");
+}
+
+if (RegExp.bind(null, "a")().source.length != 1) {
+    fail("Expected RegExp.bind(null, 'a')().source.length to be 1");
+}
+
+// check user defined functions as well
+
+var res = (function(x, y) { return x*y }).bind(null, 20, 30)();
+if (res != 600) {
+    fail("Expected 600, but got " + res);
+}
+
+var obj = new ((function(x, y) { this.foo = x*y }).bind({}, 20, 30))();
+if (obj.foo != 600) {
+    fail("Expected this.foo = 600, but got " + res);
+}
+
+// try variadic function as well
+
+var res = (function() { return arguments[0]*arguments[1] }).bind(null, 20, 30)();
+if (res != 600) {
+    fail("Expected 600, but got " + res);
+}
+
+var obj = new ((function(x, y) { this.foo = arguments[0]*arguments[1] }).bind({}, 20, 30))();
+if (obj.foo != 600) {
+    fail("Expected this.foo = 600, but got " + res);
+}
+
+
--- a/nashorn/test/script/basic/JDK-8020357.js	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/test/script/basic/JDK-8020357.js	Tue Aug 20 17:46:45 2013 -0700
@@ -33,17 +33,6 @@
 
 var limit = Math.pow(2, UNSIGNED_INT_BITS)/BYTES_PER_INT_32
 
-try {
-    // A value at or under the limit should either succeed if we have
-    // enough heap, or throw an OutOfMemoryError if we don't.
-    Int32Array(limit - 1)
-} catch(e) {
-    if(!(e instanceof java.lang.OutOfMemoryError)) {
-        // Only print an unexpected result; OutOfMemoryError is expected
-        print(e)
-    }
-}
-
 // A value over the limit should throw a RangeError.
 try {
     Int32Array(limit)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8022598.js	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8022598: Object.getPrototypeOf should return null for host objects rather than throwing TypeError
+ *
+ * @test
+ * @run
+ */
+
+// the following should not throw TypeError, just return null instead
+
+var proto = Object.getPrototypeOf(new java.lang.Object());
+if (proto !== null) {
+    fail("Expected 'null' __proto__ for host objects");
+}
+
+// on primitive should result in TypeError
+
+function checkTypeError(obj) {
+    try {
+        Object.getPrototypeOf(obj);
+        fail("Expected TypeError for Object.getPrototypeOf on " + obj);
+    } catch (e) {
+        if (! (e instanceof TypeError)) {
+            fail("Expected TypeError, but got " + e);
+        }
+    }
+}
+
+checkTypeError(undefined);
+checkTypeError(null);
+checkTypeError(3.1415);
+checkTypeError("hello");
+checkTypeError(false);
+checkTypeError(true);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8022731.js	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8022731: NativeArguments has wrong implementation of isMapped()
+ *
+ * @test
+ * @run
+ */
+
+Object.defineProperty(Object.prototype, "0", {value: "proto"});
+
+function test0(a, b) {
+    Object.defineProperty(arguments, "1", {get: function() { return "get" }});
+    return arguments[0];
+}
+
+function test1(a, b) {
+    Object.defineProperty(arguments, "0", {get: function() { return "get" }});
+    return a;
+}
+
+function test2(a, b) {
+    Object.defineProperty(arguments, "0", {value: "value"});
+    delete arguments[0];
+    return a;
+}
+
+function test3(a, b) {
+    arguments[1] = "arg1";
+    return b;
+}
+
+function test4(a, b) {
+    b = "b";
+    return arguments[1];
+}
+
+function test5(a, b) {
+    Object.defineProperty(arguments, "0", {value: "value"});
+    arguments[0] = "new";
+    return a;
+}
+
+function test6(a, b) {
+    Object.defineProperty(arguments, "0", {value: "value"});
+    arguments[0] = "new";
+    delete arguments[0];
+    return a;
+}
+
+function test7(a, b) {
+    Object.defineProperty(arguments, "0", {value: "value", writable: false});
+    arguments[0] = "new";
+    return a;
+}
+
+print(test0());
+print(test0("p1", "p2"));
+print(test1());
+print(test1("p1"));
+print(test2());
+print(test2("p1"));
+print(test3());
+print(test3(1, 2));
+print(test4());
+print(test4("p1", "p2"));
+print(test5());
+print(test5("p1"));
+print(test6());
+print(test6("p1"));
+print(test7());
+print(test7("p1"));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8022731.js.EXPECTED	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,16 @@
+proto
+p1
+undefined
+p1
+undefined
+value
+undefined
+arg1
+undefined
+b
+undefined
+new
+undefined
+new
+undefined
+value
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8023026.js	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
+ *
+ * @test
+ * @run
+ */
+
+function checkIterations(obj) {
+    if (typeof obj.getClass == 'function') {
+        print("iterating on an object of " + obj.getClass());
+    } else {
+        print("iterating on " + String(obj));
+    }
+
+    Array.prototype.forEach.call(obj,
+        function(x) { print("forEach " + x); });
+
+    print("left sum " + Array.prototype.reduce.call(obj,
+        function(x, y) { print("reduce", x, y); return x + y; }));
+
+    print("right sum " + Array.prototype.reduceRight.call(obj,
+        function(x, y) { print("reduceRight", x, y); return x + y; }));
+
+    print("squared " + Array.prototype.map.call(obj,
+        function(x) x*x));
+}
+
+var array = new (Java.type("[I"))(4);
+for (var i in array) {
+    array[i] = i;
+}
+
+checkIterations(array);
+
+var list = new java.util.ArrayList();
+list.add(1);
+list.add(3);
+list.add(5);
+list.add(7);
+
+checkIterations(list);
+
+var mirror = loadWithNewGlobal({
+    name: "test",
+    script: "[2, 4, 6, 8]"
+});
+
+checkIterations(mirror);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8023026.js.EXPECTED	Tue Aug 20 17:46:45 2013 -0700
@@ -0,0 +1,42 @@
+iterating on an object of class [I
+forEach 0
+forEach 1
+forEach 2
+forEach 3
+reduce 0 1
+reduce 1 2
+reduce 3 3
+left sum 6
+reduceRight 3 2
+reduceRight 5 1
+reduceRight 6 0
+right sum 6
+squared 0,1,4,9
+iterating on an object of class java.util.ArrayList
+forEach 1
+forEach 3
+forEach 5
+forEach 7
+reduce 1 3
+reduce 4 5
+reduce 9 7
+left sum 16
+reduceRight 7 5
+reduceRight 12 3
+reduceRight 15 1
+right sum 16
+squared 1,9,25,49
+iterating on [object Array]
+forEach 2
+forEach 4
+forEach 6
+forEach 8
+reduce 2 4
+reduce 6 6
+reduce 12 8
+left sum 20
+reduceRight 8 6
+reduceRight 14 4
+reduceRight 18 2
+right sum 20
+squared 4,16,36,64
--- a/nashorn/test/src/jdk/nashorn/api/javaaccess/BooleanAccessTest.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/api/javaaccess/BooleanAccessTest.java	Tue Aug 20 17:46:45 2013 -0700
@@ -33,6 +33,7 @@
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
 import org.testng.TestNG;
+import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
@@ -44,7 +45,7 @@
 public class BooleanAccessTest {
 
     private static ScriptEngine e = null;
-    private static SharedObject o = new SharedObject();
+    private static SharedObject o = null;
 
     public static void main(final String[] args) {
         TestNG.main(args);
@@ -54,10 +55,17 @@
     public static void setUpClass() throws ScriptException {
         final ScriptEngineManager m = new ScriptEngineManager();
         e = m.getEngineByName("nashorn");
+        o = new SharedObject();
         e.put("o", o);
         e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;");
     }
 
+    @AfterClass
+    public static void tearDownClass() {
+        e = null;
+        o = null;
+    }
+
     @Test
     public void accessFieldBoolean() throws ScriptException {
         e.eval("var p_boolean = o.publicBoolean;");
--- a/nashorn/test/src/jdk/nashorn/api/javaaccess/MethodAccessTest.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/api/javaaccess/MethodAccessTest.java	Tue Aug 20 17:46:45 2013 -0700
@@ -36,6 +36,7 @@
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
 import org.testng.TestNG;
+import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
@@ -47,7 +48,7 @@
 public class MethodAccessTest {
 
     private static ScriptEngine e = null;
-    private static SharedObject o = new SharedObject();
+    private static SharedObject o = null;
 
     public static void main(final String[] args) {
         TestNG.main(args);
@@ -57,12 +58,19 @@
     public static void setUpClass() throws ScriptException {
         final ScriptEngineManager m = new ScriptEngineManager();
         e = m.getEngineByName("nashorn");
+        o = new SharedObject();
         o.setEngine(e);
         e.put("o", o);
         e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;");
         e.eval("var Person = Packages.jdk.nashorn.api.javaaccess.Person;");
     }
 
+    @AfterClass
+    public static void tearDownClass() {
+        e = null;
+        o = null;
+    }
+
     @Test
     public void accessMethodthrowsCheckedException() throws ScriptException {
         e.eval("try {" +
--- a/nashorn/test/src/jdk/nashorn/api/javaaccess/NumberAccessTest.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/api/javaaccess/NumberAccessTest.java	Tue Aug 20 17:46:45 2013 -0700
@@ -33,6 +33,7 @@
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
 import org.testng.TestNG;
+import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
@@ -44,7 +45,7 @@
 public class NumberAccessTest {
 
     private static ScriptEngine e = null;
-    private static SharedObject o = new SharedObject();
+    private static SharedObject o = null;
 
     public static void main(final String[] args) {
         TestNG.main(args);
@@ -54,10 +55,17 @@
     public static void setUpClass() throws ScriptException {
         final ScriptEngineManager m = new ScriptEngineManager();
         e = m.getEngineByName("nashorn");
+        o = new SharedObject();
         e.put("o", o);
         e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;");
     }
 
+    @AfterClass
+    public static void tearDownClass() {
+        e = null;
+        o = null;
+    }
+
     // --------------------------------long
     // tests------------------------------------
     @Test
--- a/nashorn/test/src/jdk/nashorn/api/javaaccess/NumberBoxingTest.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/api/javaaccess/NumberBoxingTest.java	Tue Aug 20 17:46:45 2013 -0700
@@ -32,6 +32,7 @@
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
 import org.testng.TestNG;
+import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
@@ -43,7 +44,7 @@
 public class NumberBoxingTest {
 
     private static ScriptEngine e = null;
-    private static SharedObject o = new SharedObject();
+    private static SharedObject o = null;
 
     public static void main(final String[] args) {
         TestNG.main(args);
@@ -53,10 +54,17 @@
     public static void setUpClass() throws ScriptException {
         final ScriptEngineManager m = new ScriptEngineManager();
         e = m.getEngineByName("nashorn");
+        o = new SharedObject();
         e.put("o", o);
         e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;");
     }
 
+    @AfterClass
+    public static void tearDownClass() {
+        e = null;
+        o = null;
+    }
+
     // --------------------------------long
     // tests------------------------------------
     @Test
--- a/nashorn/test/src/jdk/nashorn/api/javaaccess/ObjectAccessTest.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/api/javaaccess/ObjectAccessTest.java	Tue Aug 20 17:46:45 2013 -0700
@@ -32,6 +32,7 @@
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
 import org.testng.TestNG;
+import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
@@ -43,7 +44,7 @@
 public class ObjectAccessTest {
 
     private static ScriptEngine e = null;
-    private static SharedObject o = new SharedObject();
+    private static SharedObject o = null;
 
     public static void main(final String[] args) {
         TestNG.main(args);
@@ -53,11 +54,18 @@
     public static void setUpClass() throws ScriptException {
         final ScriptEngineManager m = new ScriptEngineManager();
         e = m.getEngineByName("nashorn");
+        o = new SharedObject();
         e.put("o", o);
         e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;");
         e.eval("var Person = Packages.jdk.nashorn.api.javaaccess.Person;");
     }
 
+    @AfterClass
+    public static void tearDownClass() {
+        e = null;
+        o = null;
+    }
+
     @Test
     public void accessFieldObject() throws ScriptException {
         e.eval("var p_object = o.publicObject;");
--- a/nashorn/test/src/jdk/nashorn/api/javaaccess/StringAccessTest.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/api/javaaccess/StringAccessTest.java	Tue Aug 20 17:46:45 2013 -0700
@@ -32,6 +32,7 @@
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
 import org.testng.TestNG;
+import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
@@ -43,7 +44,7 @@
 public class StringAccessTest {
 
     private static ScriptEngine e = null;
-    private static SharedObject o = new SharedObject();
+    private static SharedObject o = null;
 
     public static void main(final String[] args) {
         TestNG.main(args);
@@ -53,10 +54,17 @@
     public static void setUpClass() throws ScriptException {
         final ScriptEngineManager m = new ScriptEngineManager();
         e = m.getEngineByName("nashorn");
+        o = new SharedObject();
         e.put("o", o);
         e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;");
     }
 
+    @AfterClass
+    public static void tearDownClass() {
+        e = null;
+        o = null;
+    }
+
     @Test
     public void accessFieldString() throws ScriptException {
         e.eval("var p_string = o.publicString;");
--- a/nashorn/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java	Tue Aug 20 17:46:45 2013 -0700
@@ -1235,7 +1235,8 @@
             fail(t.getMessage());
         }
 
-        assertEquals(sw.toString(), "hello\n");
+        // dos2unix - fix line endings if running on windows
+        assertEquals(sw.toString().replaceAll("\r", ""), "hello\n");
     }
 
     @Test
@@ -1252,6 +1253,7 @@
             fail(t.getMessage());
         }
 
-        assertEquals(sw.toString(), "34 true hello\n");
+        // dos2unix - fix line endings if running on windows
+        assertEquals(sw.toString().replaceAll("\r", ""), "34 true hello\n");
     }
 }
--- a/nashorn/test/src/jdk/nashorn/internal/codegen/CompilerTest.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/internal/codegen/CompilerTest.java	Tue Aug 20 17:46:45 2013 -0700
@@ -35,6 +35,8 @@
 import jdk.nashorn.internal.runtime.Source;
 import jdk.nashorn.internal.runtime.options.Options;
 import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 /**
@@ -58,7 +60,8 @@
     private Context context;
     private ScriptObject global;
 
-    public CompilerTest() {
+    @BeforeClass
+    public void setupTest() {
         final Options options = new Options("nashorn");
         options.set("anon.functions", true);
         options.set("compile.only", true);
@@ -79,6 +82,12 @@
         this.global = context.createGlobal();
     }
 
+    @AfterClass
+    public void tearDownTest() {
+        this.context = null;
+        this.global = null;
+    }
+
     @Test
     public void compileAllTests() {
         if (TEST262) {
--- a/nashorn/test/src/jdk/nashorn/internal/parser/ParserTest.java	Wed Jul 05 19:08:14 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/internal/parser/ParserTest.java	Tue Aug 20 17:46:45 2013 -0700
@@ -28,10 +28,11 @@
 import java.io.File;
 import jdk.nashorn.internal.runtime.Context;
 import jdk.nashorn.internal.runtime.ErrorManager;
-import jdk.nashorn.internal.runtime.ScriptObject;
 import jdk.nashorn.internal.runtime.Source;
 import jdk.nashorn.internal.runtime.options.Options;
 import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 /**
@@ -54,9 +55,9 @@
     }
 
     private Context context;
-    private ScriptObject global;
 
-    public ParserTest() {
+    @BeforeClass
+    public void setupTest() {
         final Options options = new Options("nashorn");
         options.set("anon.functions", true);
         options.set("parse.only", true);
@@ -64,7 +65,11 @@
 
         ErrorManager errors = new ErrorManager();
         this.context = new Context(options, errors, Thread.currentThread().getContextClassLoader());
-        this.global = context.createGlobal();
+    }
+
+    @AfterClass
+    public void tearDownTest() {
+        this.context = null;
     }
 
     @Test
@@ -125,8 +130,6 @@
             log("Begin parsing " + file.getAbsolutePath());
         }
 
-        final ScriptObject oldGlobal = Context.getGlobal();
-        final boolean globalChanged = (oldGlobal != global);
         try {
             final char[] buffer = Source.readFully(file);
             boolean excluded = false;
@@ -150,9 +153,6 @@
                 }
             };
             errors.setLimit(0);
-            if (globalChanged) {
-                Context.setGlobal(global);
-            }
             final Source   source   = new Source(file.getAbsolutePath(), buffer);
             new Parser(context.getEnv(), source, errors).parse();
             if (errors.getNumberOfErrors() > 0) {
@@ -167,10 +167,6 @@
                 exp.printStackTrace(System.out);
             }
             failed++;
-        } finally {
-            if (globalChanged) {
-                Context.setGlobal(oldGlobal);
-            }
         }
 
         if (VERBOSE) {