changeset 490:dd79c04ef7df

8022524: Memory leaks in nashorn sources and tests found by jhat analysis Reviewed-by: attila, hannesw
author sundar
date Thu, 08 Aug 2013 16:38:32 +0530
parents 9a3e3bb30db3
children 0d7484bf8597 14ea21d58f83
files make/project.properties src/jdk/nashorn/internal/codegen/CompileUnit.java src/jdk/nashorn/internal/objects/Global.java src/jdk/nashorn/internal/objects/NativeArray.java src/jdk/nashorn/internal/objects/NativeDate.java src/jdk/nashorn/internal/objects/NativeJSON.java src/jdk/nashorn/internal/objects/NativeObject.java src/jdk/nashorn/internal/runtime/GlobalObject.java src/jdk/nashorn/internal/runtime/JSONFunctions.java src/jdk/nashorn/internal/runtime/ListAdapter.java src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java src/jdk/nashorn/internal/runtime/ScriptFunction.java src/jdk/nashorn/internal/runtime/UserAccessorProperty.java src/jdk/nashorn/internal/runtime/linker/JavaAdapterClassLoader.java src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java test/script/basic/JDK-8020357.js test/src/jdk/nashorn/api/javaaccess/BooleanAccessTest.java test/src/jdk/nashorn/api/javaaccess/MethodAccessTest.java test/src/jdk/nashorn/api/javaaccess/NumberAccessTest.java test/src/jdk/nashorn/api/javaaccess/NumberBoxingTest.java test/src/jdk/nashorn/api/javaaccess/ObjectAccessTest.java test/src/jdk/nashorn/api/javaaccess/StringAccessTest.java test/src/jdk/nashorn/internal/codegen/CompilerTest.java test/src/jdk/nashorn/internal/parser/ParserTest.java
diffstat 24 files changed, 473 insertions(+), 133 deletions(-) [+]
line wrap: on
line diff
--- a/make/project.properties	Wed Aug 07 16:38:44 2013 +0200
+++ b/make/project.properties	Thu Aug 08 16:38:32 2013 +0530
@@ -223,9 +223,9 @@
 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.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} -XX:+HeapDumpOnOutOfMemoryError
 
-#-XX:+HeapDumpOnOutOfMemoryError -XX:-UseCompressedKlassPointers -XX:+PrintHeapAtGC -XX:ClassMetaspaceSize=300M  
+#-XX:-UseCompressedKlassPointers -XX:+PrintHeapAtGC -XX:ClassMetaspaceSize=300M  
 run.test.jvmargs.octane.main=-Xms${run.test.xms} ${run.test.jvmargs.main}
 
 run.test.jvmsecurityargs=-Xverify:all -Djava.security.properties=${basedir}/make/java.security.override -Djava.security.manager -Djava.security.policy=${basedir}/build/nashorn.policy
--- a/src/jdk/nashorn/internal/codegen/CompileUnit.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/src/jdk/nashorn/internal/codegen/CompileUnit.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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/src/jdk/nashorn/internal/objects/Global.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/src/jdk/nashorn/internal/objects/Global.java	Thu Aug 08 16:38:32 2013 +0530
@@ -41,6 +41,8 @@
 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 +74,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)
@@ -709,6 +711,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.
      *
--- a/src/jdk/nashorn/internal/objects/NativeArray.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/src/jdk/nashorn/internal/objects/NativeArray.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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");
                         }
@@ -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/src/jdk/nashorn/internal/objects/NativeDate.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/src/jdk/nashorn/internal/objects/NativeDate.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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/src/jdk/nashorn/internal/objects/NativeJSON.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/src/jdk/nashorn/internal/objects/NativeJSON.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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/src/jdk/nashorn/internal/objects/NativeObject.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/src/jdk/nashorn/internal/objects/NativeObject.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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);
 
@@ -402,12 +414,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/src/jdk/nashorn/internal/runtime/GlobalObject.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/src/jdk/nashorn/internal/runtime/GlobalObject.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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/src/jdk/nashorn/internal/runtime/JSONFunctions.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/src/jdk/nashorn/internal/runtime/JSONFunctions.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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/src/jdk/nashorn/internal/runtime/ListAdapter.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/src/jdk/nashorn/internal/runtime/ListAdapter.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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/src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java	Thu Aug 08 16:38:32 2013 +0530
@@ -52,13 +52,19 @@
 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/src/jdk/nashorn/internal/runtime/ScriptFunction.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/src/jdk/nashorn/internal/runtime/ScriptFunction.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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);
--- a/src/jdk/nashorn/internal/runtime/UserAccessorProperty.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/src/jdk/nashorn/internal/runtime/UserAccessorProperty.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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/src/jdk/nashorn/internal/runtime/linker/JavaAdapterClassLoader.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterClassLoader.java	Thu Aug 08 16:38:32 2013 +0530
@@ -48,7 +48,7 @@
     private static final ProtectionDomain GENERATED_PROTECTION_DOMAIN = createGeneratedProtectionDomain();
 
     private final String className;
-    private final byte[] classBytes;
+    private volatile byte[] classBytes;
 
     JavaAdapterClassLoader(String className, byte[] classBytes) {
         this.className = className.replace('/', '.');
@@ -56,6 +56,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
@@ -103,6 +110,7 @@
             @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);
--- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java	Thu Aug 08 16:38:32 2013 +0530
@@ -224,7 +224,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/test/script/basic/JDK-8020357.js	Wed Aug 07 16:38:44 2013 +0200
+++ b/test/script/basic/JDK-8020357.js	Thu Aug 08 16:38:32 2013 +0530
@@ -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)
--- a/test/src/jdk/nashorn/api/javaaccess/BooleanAccessTest.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/test/src/jdk/nashorn/api/javaaccess/BooleanAccessTest.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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/test/src/jdk/nashorn/api/javaaccess/MethodAccessTest.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/test/src/jdk/nashorn/api/javaaccess/MethodAccessTest.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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/test/src/jdk/nashorn/api/javaaccess/NumberAccessTest.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/test/src/jdk/nashorn/api/javaaccess/NumberAccessTest.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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/test/src/jdk/nashorn/api/javaaccess/NumberBoxingTest.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/test/src/jdk/nashorn/api/javaaccess/NumberBoxingTest.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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/test/src/jdk/nashorn/api/javaaccess/ObjectAccessTest.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/test/src/jdk/nashorn/api/javaaccess/ObjectAccessTest.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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/test/src/jdk/nashorn/api/javaaccess/StringAccessTest.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/test/src/jdk/nashorn/api/javaaccess/StringAccessTest.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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/test/src/jdk/nashorn/internal/codegen/CompilerTest.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/test/src/jdk/nashorn/internal/codegen/CompilerTest.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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/test/src/jdk/nashorn/internal/parser/ParserTest.java	Wed Aug 07 16:38:44 2013 +0200
+++ b/test/src/jdk/nashorn/internal/parser/ParserTest.java	Thu Aug 08 16:38:32 2013 +0530
@@ -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) {