changeset 43:e62dba3ce52b

8006678: Avoid too many Context.getGlobal() calls Reviewed-by: lagergren, jlaskey
author sundar
date Tue, 22 Jan 2013 22:07:12 +0530
parents e43d1013d871
children 0dbcb7350595
files make/project.properties src/jdk/nashorn/api/scripting/NashornScriptEngine.java src/jdk/nashorn/api/scripting/ScriptObjectMirror.java src/jdk/nashorn/internal/codegen/Lower.java src/jdk/nashorn/internal/ir/debug/JSONWriter.java src/jdk/nashorn/internal/objects/AccessorPropertyDescriptor.java src/jdk/nashorn/internal/objects/Global.java src/jdk/nashorn/internal/objects/NativeArguments.java src/jdk/nashorn/internal/objects/NativeArray.java src/jdk/nashorn/internal/objects/NativeBoolean.java src/jdk/nashorn/internal/objects/NativeDate.java src/jdk/nashorn/internal/objects/NativeFunction.java src/jdk/nashorn/internal/objects/NativeJSAdapter.java src/jdk/nashorn/internal/objects/NativeJSON.java src/jdk/nashorn/internal/objects/NativeJava.java src/jdk/nashorn/internal/objects/NativeNumber.java src/jdk/nashorn/internal/objects/NativeObject.java src/jdk/nashorn/internal/objects/NativeRegExp.java src/jdk/nashorn/internal/objects/NativeString.java src/jdk/nashorn/internal/runtime/Context.java src/jdk/nashorn/internal/runtime/ECMAErrors.java src/jdk/nashorn/internal/runtime/ErrorManager.java src/jdk/nashorn/internal/runtime/JSType.java src/jdk/nashorn/internal/runtime/ParserException.java src/jdk/nashorn/internal/runtime/ScriptFunction.java src/jdk/nashorn/internal/runtime/ScriptObject.java src/jdk/nashorn/internal/runtime/ScriptRuntime.java src/jdk/nashorn/internal/runtime/ScriptingFunctions.java src/jdk/nashorn/internal/runtime/SetMethodCreator.java src/jdk/nashorn/internal/runtime/URIUtils.java src/jdk/nashorn/internal/runtime/Undefined.java src/jdk/nashorn/internal/runtime/arrays/ArrayLikeIterator.java src/jdk/nashorn/internal/runtime/arrays/FrozenArrayFilter.java src/jdk/nashorn/internal/runtime/arrays/IteratorAction.java src/jdk/nashorn/internal/runtime/arrays/SealedArrayFilter.java src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java src/jdk/nashorn/internal/runtime/linker/JavaArgumentConverters.java src/jdk/nashorn/internal/runtime/linker/Lookup.java src/jdk/nashorn/internal/runtime/linker/NashornBottomLinker.java src/jdk/nashorn/internal/runtime/linker/NashornStaticClassLinker.java
diffstat 40 files changed, 379 insertions(+), 188 deletions(-) [+]
line wrap: on
line diff
--- a/make/project.properties	Tue Jan 22 14:36:28 2013 +0100
+++ b/make/project.properties	Tue Jan 22 22:07:12 2013 +0530
@@ -216,7 +216,7 @@
 # add '-Dtest.js.outofprocess' to run each test in a new sub-process
 run.test.jvmargs=-server -Xmx${run.test.xmx} -XX:-TieredCompilation -esa -ea -Dnashorn.debug=true -Dfile.encoding=UTF-8 
 #-XX:+HeapDumpOnOutOfMemoryError -XX:-UseCompressedKlassPointers -XX:+PrintHeapAtGC -XX:ClassMetaspaceSize=300M  
-run.test.jvmargs.octane=-Xms${run.test.xms} -${run.test.jvmargs}
+run.test.jvmargs.octane=-Xms${run.test.xms} ${run.test.jvmargs}
 
 run.test.jvmsecurityargs=-Xverify:all -Djava.security.manager -Djava.security.policy=${basedir}/build/nashorn.policy
 
--- a/src/jdk/nashorn/api/scripting/NashornScriptEngine.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/api/scripting/NashornScriptEngine.java	Tue Jan 22 22:07:12 2013 +0530
@@ -216,7 +216,7 @@
             realSelf = self;
         }
         try {
-            final ScriptObject oldGlobal = Context.getGlobal();
+            final ScriptObject oldGlobal = getNashornGlobal();
             try {
                 if(oldGlobal != global) {
                     setNashornGlobal(global);
@@ -343,7 +343,7 @@
     }
 
     private Object invokeImpl(final Object selfObject, final String name, final Object... args) throws ScriptException, NoSuchMethodException {
-        final ScriptObject oldGlobal     = Context.getGlobal();
+        final ScriptObject oldGlobal     = getNashornGlobal();
         final boolean globalChanged = (oldGlobal != global);
 
         Object self = selfObject;
@@ -395,7 +395,7 @@
         if (script == null) {
             return null;
         }
-        final ScriptObject oldGlobal = Context.getGlobal();
+        final ScriptObject oldGlobal = getNashornGlobal();
         final boolean globalChanged = (oldGlobal != global);
         try {
             if (globalChanged) {
@@ -457,7 +457,7 @@
     }
 
     private ScriptFunction compileImpl(final char[] buf, final ScriptContext ctxt) throws ScriptException {
-        final ScriptObject oldGlobal = Context.getGlobal();
+        final ScriptObject oldGlobal = getNashornGlobal();
         final boolean globalChanged = (oldGlobal != global);
         try {
             final Object val = ctxt.getAttribute(ScriptEngine.FILENAME);
@@ -479,13 +479,17 @@
         }
     }
 
-    // don't make this public!!
+    // don't make these public!!
+    static ScriptObject getNashornGlobal() {
+       return Context.getGlobal();
+    }
+
     static void setNashornGlobal(final ScriptObject global) {
         AccessController.doPrivileged(new PrivilegedAction<Void>() {
             @Override
             public Void run() {
-               Context.setGlobal(global);
-               return null;
+                Context.setGlobal(global);
+                return null;
             }
         });
     }
--- a/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java	Tue Jan 22 22:07:12 2013 +0530
@@ -83,7 +83,7 @@
     }
 
     private <V> V inGlobal(final Callable<V> callable) {
-        final ScriptObject oldGlobal = Context.getGlobal();
+        final ScriptObject oldGlobal = NashornScriptEngine.getNashornGlobal();
         final boolean globalChanged = (oldGlobal != global);
         if (globalChanged) {
             NashornScriptEngine.setNashornGlobal(global);
@@ -105,7 +105,7 @@
     @Override
     public Object call(final String methodName, final Object args[]) {
         final Object val = sobj.get(methodName);
-        final ScriptObject oldGlobal = Context.getGlobal();
+        final ScriptObject oldGlobal = NashornScriptEngine.getNashornGlobal();
         final boolean globalChanged = (oldGlobal != global);
 
         if (val instanceof ScriptFunction) {
@@ -180,7 +180,7 @@
 
     @Override
     public void setMember(final String name, final Object value) {
-        put(name, wrap(value, Context.getGlobal()));
+        put(name, wrap(value, NashornScriptEngine.getNashornGlobal()));
     }
 
     @Override
--- a/src/jdk/nashorn/internal/codegen/Lower.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/codegen/Lower.java	Tue Jan 22 22:07:12 2013 +0530
@@ -1080,10 +1080,10 @@
     /*
      * For a script, add scope symbols as defined in the property map
      */
-    private static void initFromPropertyMap(final FunctionNode functionNode) {
+    private static void initFromPropertyMap(final Context context, final FunctionNode functionNode) {
         assert functionNode.isScript();
 
-        final PropertyMap map = Context.getGlobal().getMap();
+        final PropertyMap map = context.getGlobalMap();
 
         for (final Property property : map.getProperties()) {
             final String key = property.getKey();
@@ -1130,7 +1130,7 @@
         }
 
         if (functionNode.isScript()) {
-            initFromPropertyMap(functionNode);
+            initFromPropertyMap(compiler.getContext(), functionNode);
         }
 
         // Add function name as local symbol
--- a/src/jdk/nashorn/internal/ir/debug/JSONWriter.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/ir/debug/JSONWriter.java	Tue Jan 22 22:07:12 2013 +0530
@@ -71,7 +71,6 @@
 import jdk.nashorn.internal.parser.TokenType;
 import jdk.nashorn.internal.runtime.Context;
 import jdk.nashorn.internal.runtime.ParserException;
-import jdk.nashorn.internal.runtime.ScriptObject;
 import jdk.nashorn.internal.runtime.Source;
 
 /**
@@ -87,7 +86,6 @@
      * @return JSON string representation of AST of the supplied code
      */
     public static String parse(final String code, final String name, final boolean includeLoc) {
-        final ScriptObject global     = Context.getGlobal();
         final Context      context    = AccessController.doPrivileged(
                 new PrivilegedAction<Context>() {
                     @Override
@@ -103,7 +101,7 @@
             functionNode.accept(jsonWriter);
             return jsonWriter.getString();
         } catch (final ParserException e) {
-            e.throwAsEcmaException(global);
+            e.throwAsEcmaException();
             return null;
         }
     }
--- a/src/jdk/nashorn/internal/objects/AccessorPropertyDescriptor.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/objects/AccessorPropertyDescriptor.java	Tue Jan 22 22:07:12 2013 +0530
@@ -157,7 +157,7 @@
             if (getter == UNDEFINED || getter instanceof ScriptFunction) {
                 this.get = getter;
             } else {
-                typeError(Global.instance(), "not.a.function", ScriptRuntime.safeToString(getter));
+                typeError("not.a.function", ScriptRuntime.safeToString(getter));
             }
         } else {
             delete(GET, strict);
@@ -168,7 +168,7 @@
             if (setter == UNDEFINED || setter instanceof ScriptFunction) {
                 this.set = setter;
             } else {
-                typeError(Global.instance(), "not.a.function", ScriptRuntime.safeToString(setter));
+                typeError("not.a.function", ScriptRuntime.safeToString(setter));
             }
         } else {
             delete(SET, strict);
--- a/src/jdk/nashorn/internal/objects/Global.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/objects/Global.java	Tue Jan 22 22:07:12 2013 +0530
@@ -1157,7 +1157,7 @@
      */
     public static Object toObject(final Object obj) {
         if (obj == null || obj == UNDEFINED) {
-            typeError(instance(), "not.an.object", ScriptRuntime.safeToString(obj));
+            typeError("not.an.object", ScriptRuntime.safeToString(obj));
         }
 
         if (obj instanceof ScriptObject) {
@@ -1274,7 +1274,7 @@
      */
     public static void checkObject(final Object obj) {
         if (!(obj instanceof ScriptObject)) {
-            typeError(instance(), "not.an.object", ScriptRuntime.safeToString(obj));
+            typeError("not.an.object", ScriptRuntime.safeToString(obj));
         }
     }
 
@@ -1286,7 +1286,7 @@
      */
     public static void checkObjectCoercible(final Object obj) {
         if (obj == null || obj == UNDEFINED) {
-            typeError(instance(), "not.an.object", ScriptRuntime.safeToString(obj));
+            typeError("not.an.object", ScriptRuntime.safeToString(obj));
         }
     }
 
--- a/src/jdk/nashorn/internal/objects/NativeArguments.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/objects/NativeArguments.java	Tue Jan 22 22:07:12 2013 +0530
@@ -485,7 +485,7 @@
             final boolean allowed = super.defineOwnProperty(key, propertyDesc, false);
             if (!allowed) {
                 if (reject) {
-                    typeError(Global.instance(), "cant.redefine.property",  key, ScriptRuntime.safeToString(this));
+                    typeError("cant.redefine.property",  key, ScriptRuntime.safeToString(this));
                 }
                 return false;
             }
--- a/src/jdk/nashorn/internal/objects/NativeArray.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/objects/NativeArray.java	Tue Jan 22 22:07:12 2013 +0530
@@ -47,7 +47,6 @@
 import jdk.nashorn.internal.objects.annotations.Setter;
 import jdk.nashorn.internal.objects.annotations.SpecializedConstructor;
 import jdk.nashorn.internal.objects.annotations.Where;
-import jdk.nashorn.internal.runtime.Context;
 import jdk.nashorn.internal.runtime.JSType;
 import jdk.nashorn.internal.runtime.PropertyDescriptor;
 import jdk.nashorn.internal.runtime.ScriptFunction;
@@ -182,7 +181,7 @@
             // Step 3g
             if (!oldLenDesc.isWritable()) {
                 if (reject) {
-                    typeError(Global.instance(), "property.not.writable", "length", ScriptRuntime.safeToString(this));
+                    typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
                 }
                 return false;
             }
@@ -211,7 +210,7 @@
                     }
                     super.defineOwnProperty("length", newLenDesc, false);
                     if (reject) {
-                        typeError(Global.instance(), "property.not.writable", "length", ScriptRuntime.safeToString(this));
+                        typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
                     }
                     return false;
                 }
@@ -236,7 +235,7 @@
             // setting an element beyond current length, but 'length' is not writable
             if (longIndex >= oldLen && !oldLenDesc.isWritable()) {
                 if (reject) {
-                    typeError(Global.instance(), "property.not.writable", Long.toString(longIndex), ScriptRuntime.safeToString(this));
+                    typeError("property.not.writable", Long.toString(longIndex), ScriptRuntime.safeToString(this));
                 }
                 return false;
             }
@@ -248,7 +247,7 @@
             // Step 4d
             if (!succeeded) {
                 if (reject) {
-                    typeError(Global.instance(), "cant.redefine.property", key, ScriptRuntime.safeToString(this));
+                    typeError("cant.redefine.property", key, ScriptRuntime.safeToString(this));
                 }
                 return false;
             }
@@ -325,7 +324,7 @@
             }
         }
         if (reject) {
-            rangeError(Global.instance(), "inappropriate.array.length", ScriptRuntime.safeToString(length));
+            rangeError("inappropriate.array.length", ScriptRuntime.safeToString(length));
         }
         return -1;
     }
@@ -371,7 +370,7 @@
             final Object obj = iter.next();
 
             if (obj != null && obj != ScriptRuntime.UNDEFINED) {
-                final Object val = JSType.toObject(Global.instance(), obj);
+                final Object val = JSType.toScriptObject(obj);
 
                 try {
                     if (val instanceof ScriptObject) {
@@ -381,7 +380,7 @@
                         if (toLocaleString instanceof ScriptFunction) {
                             sb.append((String)TO_LOCALE_STRING.getInvoker().invokeExact(toLocaleString, sobj));
                         } else {
-                            typeError(Global.instance(), "not.a.function", "toLocaleString");
+                            typeError("not.a.function", "toLocaleString");
                         }
                     }
                 } catch (final Error|RuntimeException t) {
@@ -434,7 +433,7 @@
                  */
                 final double numberLength = ((Number) len).doubleValue();
                 if (length != numberLength) {
-                    rangeError(Global.instance(), "inappropriate.array.length", JSType.toString(numberLength));
+                    rangeError("inappropriate.array.length", JSType.toString(numberLength));
                 }
 
                 return new NativeArray(length);
@@ -624,7 +623,7 @@
 
             return element;
         } catch (final ClassCastException | NullPointerException e) {
-            typeError(Global.instance(), "not.an.object", ScriptRuntime.safeToString(self));
+            typeError("not.an.object", ScriptRuntime.safeToString(self));
             return ScriptRuntime.UNDEFINED;
         }
     }
@@ -660,7 +659,7 @@
 
             return len;
         } catch (final ClassCastException | NullPointerException e) {
-            typeError(Global.instance(), "not.an.object", ScriptRuntime.safeToString(self));
+            typeError("not.an.object", ScriptRuntime.safeToString(self));
             return ScriptRuntime.UNDEFINED;
         }
     }
@@ -699,7 +698,7 @@
             }
             return sobj;
         } catch (final ClassCastException | NullPointerException e) {
-            typeError(Global.instance(), "not.an.object", ScriptRuntime.safeToString(self));
+            typeError("not.an.object", ScriptRuntime.safeToString(self));
             return ScriptRuntime.UNDEFINED;
         }
     }
@@ -805,7 +804,7 @@
         final ScriptFunction cmp = compareFunction(comparefn);
 
         final List<Object> list = Arrays.asList(array);
-        final Object cmpThis = cmp == null || cmp.isStrict() ? ScriptRuntime.UNDEFINED : Context.getGlobal();
+        final Object cmpThis = cmp == null || cmp.isStrict() ? ScriptRuntime.UNDEFINED : Global.instance();
 
         Collections.sort(list, new Comparator<Object>() {
             @Override
@@ -865,7 +864,7 @@
 
             return sobj;
         } catch (final ClassCastException | NullPointerException e) {
-            typeError(Global.instance(), "not.an.object", ScriptRuntime.safeToString(self));
+            typeError("not.an.object", ScriptRuntime.safeToString(self));
             return ScriptRuntime.UNDEFINED;
         }
     }
@@ -1080,7 +1079,7 @@
                 }
             }
         } catch (final ClassCastException | NullPointerException e) {
-            typeError(Global.instance(), "not.an.object", ScriptRuntime.safeToString(self));
+            typeError("not.an.object", ScriptRuntime.safeToString(self));
         }
 
         return -1;
@@ -1202,14 +1201,14 @@
         Object initialValue = initialValuePresent ? args[1] : ScriptRuntime.UNDEFINED;
 
         if (callbackfn == ScriptRuntime.UNDEFINED) {
-            typeError(Global.instance(), "not.a.function", "undefined");
+            typeError("not.a.function", "undefined");
         }
 
         if (!initialValuePresent) {
             if (iter.hasNext()) {
                 initialValue = iter.next();
             } else {
-                typeError(Global.instance(), "array.reduce.invalid.init");
+                typeError("array.reduce.invalid.init");
             }
         }
 
--- a/src/jdk/nashorn/internal/objects/NativeBoolean.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/objects/NativeBoolean.java	Tue Jan 22 22:07:12 2013 +0530
@@ -145,7 +145,7 @@
         } else if (self != null && self == Global.instance().getBooleanPrototype()) {
             return false;
         } else {
-            typeError(Global.instance(), "not.a.boolean", ScriptRuntime.safeToString(self));
+            typeError("not.a.boolean", ScriptRuntime.safeToString(self));
             return false;
         }
     }
--- a/src/jdk/nashorn/internal/objects/NativeDate.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/objects/NativeDate.java	Tue Jan 22 22:07:12 2013 +0530
@@ -867,7 +867,7 @@
             if (func instanceof ScriptFunction) {
                 return TO_ISO_STRING.getInvoker().invokeExact(func, sobj, key);
             }
-            typeError(Global.instance(), "not.a.function", ScriptRuntime.safeToString(func));
+            typeError("not.a.function", ScriptRuntime.safeToString(func));
         } catch (final RuntimeException | Error e) {
             throw e;
         } catch (final Throwable t) {
@@ -1006,7 +1006,7 @@
             return sb.toString();
         }
 
-        rangeError(Global.instance(), "invalid.date");
+        rangeError("invalid.date");
 
         return INVALID_DATE;
     }
@@ -1035,7 +1035,7 @@
             return sb.toString();
         }
 
-        rangeError(Global.instance(), "invalid.date");
+        rangeError("invalid.date");
 
         return INVALID_DATE;
     }
@@ -1268,7 +1268,7 @@
         } else if (self != null && self == Global.instance().getDatePrototype()) {
             return Global.instance().DEFAULT_DATE;
         } else {
-            typeError(Global.instance(), "not.a.date", ScriptRuntime.safeToString(self));
+            typeError("not.a.date", ScriptRuntime.safeToString(self));
             return null;
         }
     }
--- a/src/jdk/nashorn/internal/objects/NativeFunction.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/objects/NativeFunction.java	Tue Jan 22 22:07:12 2013 +0530
@@ -60,7 +60,7 @@
     @Function(attributes = Attribute.NOT_ENUMERABLE)
     public static Object toString(final Object self) {
         if (!(self instanceof ScriptFunction)) {
-            typeError(Global.instance(), "not.a.function", ScriptRuntime.safeToString(self));
+            typeError("not.a.function", ScriptRuntime.safeToString(self));
             return UNDEFINED;
         }
         return ((ScriptFunction)self).toSource();
@@ -71,7 +71,7 @@
             if (thiz == UNDEFINED || thiz == null) {
                 return Global.instance();
             }
-            return JSType.toObject(Global.instance(), thiz);
+            return JSType.toScriptObject(thiz);
         }
 
         return thiz;
@@ -88,7 +88,7 @@
     @Function(attributes = Attribute.NOT_ENUMERABLE)
     public static Object apply(final Object self, final Object thiz, final Object array) {
         if (!(self instanceof ScriptFunction)) {
-            typeError(Global.instance(), "not.a.function", ScriptRuntime.safeToString(self));
+            typeError("not.a.function", ScriptRuntime.safeToString(self));
             return UNDEFINED;
         }
 
@@ -102,12 +102,12 @@
             final Object       len  = sobj.getLength();
 
             if (len == UNDEFINED || len == null) {
-                typeError(Global.instance(), "function.apply.expects.array");
+                typeError("function.apply.expects.array");
             }
 
             final int n = (int)JSType.toUint32(len);
             if (n != JSType.toNumber(len)) {
-                typeError(Global.instance(), "function.apply.expects.array");
+                typeError("function.apply.expects.array");
             }
 
             args = new Object[(int)JSType.toUint32(len)];
@@ -122,7 +122,7 @@
         } else if (array == null || array == UNDEFINED) {
             args = ScriptRuntime.EMPTY_ARRAY;
         } else {
-            typeError(Global.instance(), "function.apply.expects.array");
+            typeError("function.apply.expects.array");
         }
 
         final ScriptFunction func = (ScriptFunction)self;
@@ -143,7 +143,7 @@
     @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
     public static Object call(final Object self, final Object... args) {
         if (!(self instanceof ScriptFunction)) {
-            typeError(Global.instance(), "not.a.function", ScriptRuntime.safeToString(self));
+            typeError("not.a.function", ScriptRuntime.safeToString(self));
             return UNDEFINED;
         }
 
@@ -178,7 +178,7 @@
     @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
     public static Object bind(final Object self, final Object... args) {
         if (!(self instanceof ScriptFunction)) {
-            typeError(Global.instance(), "not.a.function", ScriptRuntime.safeToString(self));
+            typeError("not.a.function", ScriptRuntime.safeToString(self));
             return UNDEFINED;
         }
 
@@ -209,7 +209,7 @@
     @Function(attributes = Attribute.NOT_ENUMERABLE)
     public static Object toSource(final Object self) {
         if (!(self instanceof ScriptFunction)) {
-            typeError(Global.instance(), "not.a.function", ScriptRuntime.safeToString(self));
+            typeError("not.a.function", ScriptRuntime.safeToString(self));
             return UNDEFINED;
         }
         return ((ScriptFunction)self).toSource();
--- a/src/jdk/nashorn/internal/objects/NativeJSAdapter.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/objects/NativeJSAdapter.java	Tue Jan 22 22:07:12 2013 +0530
@@ -540,7 +540,7 @@
         Object adaptee;
 
         if (args == null || args.length == 0) {
-            typeError(Global.instance(), "not.an.object", "null");
+            typeError("not.an.object", "null");
             return null; //won't reach, but fixed warning
         }
 
@@ -564,7 +564,7 @@
         }
 
         if (!(adaptee instanceof ScriptObject)) {
-            typeError(Global.instance(), "not.an.object", ScriptRuntime.safeToString(adaptee));
+            typeError("not.an.object", ScriptRuntime.safeToString(adaptee));
         }
 
         if (proto != null && !(proto instanceof ScriptObject)) {
@@ -623,7 +623,7 @@
                         func.makeBoundFunction(this, new Object[] { name })), 0, Object.class),
                         adaptee.getMap().getProtoGetSwitchPoint(__call__), testJSAdaptor(adaptee, null, null, null));
             }
-            typeError(Global.instance(), "no.such.function", desc.getNameToken(2), ScriptRuntime.safeToString(this));
+            typeError("no.such.function", desc.getNameToken(2), ScriptRuntime.safeToString(this));
             break;
         default:
             break;
@@ -696,7 +696,7 @@
 
         switch (hook) {
         case __call__:
-            typeError(Global.instance(), "no.such.function", desc.getNameToken(2), ScriptRuntime.safeToString(this));
+            typeError("no.such.function", desc.getNameToken(2), ScriptRuntime.safeToString(this));
             throw new AssertionError("should not reach here");
         default:
             final MethodHandle methodHandle = hook.equals(__put__) ?
--- a/src/jdk/nashorn/internal/objects/NativeJSON.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/objects/NativeJSON.java	Tue Jan 22 22:07:12 2013 +0530
@@ -101,7 +101,7 @@
         try {
             node = parser.parse();
         } catch (final ParserException e) {
-            syntaxError(Global.instance(), e, "invalid.json", e.getMessage());
+            syntaxError(e, "invalid.json", e.getMessage());
             return UNDEFINED;
         }
 
@@ -404,7 +404,7 @@
     // Spec: The abstract operation JO(value) serializes an object.
     private static String JO(final ScriptObject value, final StringifyState state) {
         if (state.stack.containsKey(value)) {
-            typeError(Global.instance(), "JSON.stringify.cyclic");
+            typeError("JSON.stringify.cyclic");
         }
 
         state.stack.put(value, value);
@@ -480,7 +480,7 @@
     // Spec: The abstract operation JA(value) serializes an array.
     private static Object JA(final NativeArray value, final StringifyState state) {
         if (state.stack.containsKey(value)) {
-            typeError(Global.instance(), "JSON.stringify.cyclic");
+            typeError("JSON.stringify.cyclic");
         }
 
         state.stack.put(value, value);
--- a/src/jdk/nashorn/internal/objects/NativeJava.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/objects/NativeJava.java	Tue Jan 22 22:07:12 2013 +0530
@@ -287,7 +287,7 @@
             return new NativeArray(copyArray((boolean[])objArray));
         }
 
-        typeError(Global.instance(), "cant.convert.to.javascript.array", objArray.getClass().getName());
+        typeError("cant.convert.to.javascript.array", objArray.getClass().getName());
 
         throw new AssertionError();
     }
@@ -384,7 +384,7 @@
     @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
     public static Object extend(final Object self, final Object... types) {
         if(types == null || types.length == 0) {
-            typeError(Global.instance(), "extend.expects.at.least.one.argument");
+            typeError("extend.expects.at.least.one.argument");
         }
         final Class<?>[] stypes = new Class<?>[types.length];
         try {
@@ -392,7 +392,7 @@
                 stypes[i] = ((StaticClass)types[i]).getRepresentedClass();
             }
         } catch(final ClassCastException e) {
-            typeError(Global.instance(), "extend.expects.java.types");
+            typeError("extend.expects.java.types");
         }
         return JavaAdapterFactory.getAdapterClassFor(stypes);
     }
--- a/src/jdk/nashorn/internal/objects/NativeNumber.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/objects/NativeNumber.java	Tue Jan 22 22:07:12 2013 +0530
@@ -185,7 +185,7 @@
     public static Object toFixed(final Object self, final Object fractionDigits) {
         final int f = JSType.toInteger(fractionDigits);
         if (f < 0 || f > 20) {
-            rangeError(Global.instance(), "invalid.fraction.digits", "toFixed");
+            rangeError("invalid.fraction.digits", "toFixed");
             return UNDEFINED;
         }
 
@@ -227,7 +227,7 @@
         }
 
         if (fractionDigits != UNDEFINED && (f < 0 || f > 20)) {
-            rangeError(Global.instance(), "invalid.fraction.digits", "toExponential");
+            rangeError("invalid.fraction.digits", "toExponential");
             return UNDEFINED;
         }
 
@@ -258,7 +258,7 @@
         }
 
         if (p < 1 || p > 21) {
-            rangeError(Global.instance(), "invalid.precision");
+            rangeError("invalid.precision");
             return UNDEFINED;
         }
 
@@ -283,7 +283,7 @@
             final int intRadix = JSType.toInteger(radix);
             if (intRadix != 10) {
                 if (intRadix < 2 || intRadix > 36) {
-                    rangeError(Global.instance(), "invalid.radix");
+                    rangeError("invalid.radix");
                 }
                 return JSType.toString(getNumberValue(self), intRadix);
             }
@@ -338,7 +338,7 @@
         } else if (self != null && self == Global.instance().getNumberPrototype()) {
             return 0.0;
         } else {
-            typeError(Global.instance(), "not.a.number", ScriptRuntime.safeToString(self));
+            typeError("not.a.number", ScriptRuntime.safeToString(self));
             return Double.NaN;
         }
     }
--- a/src/jdk/nashorn/internal/objects/NativeObject.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/objects/NativeObject.java	Tue Jan 22 22:07:12 2013 +0530
@@ -316,7 +316,7 @@
      */
     @Function(attributes = Attribute.NOT_ENUMERABLE)
     public static Object toLocaleString(final Object self) {
-        final Object obj = JSType.toObject(Global.instance(), self);
+        final Object obj = JSType.toScriptObject(self);
         if (obj instanceof ScriptObject) {
             final ScriptObject sobj = (ScriptObject)self;
             try {
@@ -331,7 +331,7 @@
                 throw new RuntimeException(t);
             }
 
-            typeError(Global.instance(), "not.a.function", "toString");
+            typeError("not.a.function", "toString");
             throw new AssertionError(); // never reached
         }
 
--- a/src/jdk/nashorn/internal/objects/NativeRegExp.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/objects/NativeRegExp.java	Tue Jan 22 22:07:12 2013 +0530
@@ -86,7 +86,7 @@
             regExp = new RegExp(input, flagString);
         } catch (final ParserException e) {
             // translate it as SyntaxError object and throw it
-            e.throwAsEcmaException(Global.instance());
+            e.throwAsEcmaException();
             throw new AssertionError(); //guard against null warnings below
         }
 
@@ -221,7 +221,7 @@
                 if (!flagsDefined) {
                     return (NativeRegExp)regexp; // 15.10.3.1 - undefined flags and regexp as
                 }
-                typeError(Global.instance(), "regex.cant.supply.flags");
+                typeError("regex.cant.supply.flags");
             }
             patternString = JSType.toString(regexp);
         }
@@ -716,7 +716,7 @@
         } else if (self != null && self == Global.instance().getRegExpPrototype()) {
             return Global.instance().DEFAULT_REGEXP;
         } else {
-            typeError(Global.instance(), "not.a.regexp", ScriptRuntime.safeToString(self));
+            typeError("not.a.regexp", ScriptRuntime.safeToString(self));
             return null;
         }
     }
--- a/src/jdk/nashorn/internal/objects/NativeString.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/objects/NativeString.java	Tue Jan 22 22:07:12 2013 +0530
@@ -288,7 +288,7 @@
     private boolean checkDeleteIndex(final int index, final boolean strict) {
         if (isValid(index)) {
             if (strict) {
-                typeError(Global.instance(), "cant.delete.property", Integer.toString(index), ScriptRuntime.safeToString(this));
+                typeError("cant.delete.property", Integer.toString(index), ScriptRuntime.safeToString(this));
             }
             return true;
         }
@@ -904,7 +904,7 @@
         } else if (self != null && self == Global.instance().getStringPrototype()) {
             return "";
         } else {
-            typeError(Global.instance(), "not.a.string", ScriptRuntime.safeToString(self));
+            typeError("not.a.string", ScriptRuntime.safeToString(self));
             return null;
         }
     }
@@ -919,7 +919,7 @@
         } else if (self != null && self == Global.instance().getStringPrototype()) {
             return "";
         } else {
-            typeError(Global.instance(), "not.a.string", ScriptRuntime.safeToString(self));
+            typeError( "not.a.string", ScriptRuntime.safeToString(self));
             return null;
         }
     }
--- a/src/jdk/nashorn/internal/runtime/Context.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/Context.java	Tue Jan 22 22:07:12 2013 +0530
@@ -50,10 +50,12 @@
 import jdk.nashorn.internal.codegen.Compiler;
 import jdk.nashorn.internal.codegen.Namespace;
 import jdk.nashorn.internal.codegen.objects.ObjectClassGenerator;
+import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory;
 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
 import jdk.nashorn.internal.runtime.options.KeyValueOption;
 import jdk.nashorn.internal.runtime.options.Option;
 import jdk.nashorn.internal.runtime.options.Options;
+import sun.reflect.Reflection;
 
 /**
  * This class manages the global state of execution. Context is immutable.
@@ -72,11 +74,27 @@
         };
 
     /**
-     * Return the current global scope
-     * @return current global scope
+     * Get the current global scope
+     * @return the current global scope
      */
     public static ScriptObject getGlobal() {
-        return currentGlobal.get();
+        final SecurityManager sm = System.getSecurityManager();
+        if (sm != null) {
+            // skip getCallerClass and getGlobal and get to the real caller
+            Class<?> caller = Reflection.getCallerClass(2);
+            ClassLoader callerLoader = caller.getClassLoader();
+
+            // Allow this method only for nashorn's own classes, script
+            // generated classes and Java adapter classes. Rest should
+            // have the necessary security permission.
+            if (callerLoader != myLoader &&
+                !(callerLoader instanceof NashornLoader) &&
+                !(JavaAdapterFactory.isAdapterClass(caller))) {
+                sm.checkPermission(new RuntimePermission("getNashornGlobal"));
+            }
+        }
+
+        return getGlobalTrusted();
     }
 
     /**
@@ -93,7 +111,7 @@
             throw new IllegalArgumentException("global does not implement GlobalObject!");
         }
 
-        currentGlobal.set(global);
+        setGlobalTrusted(global);
     }
 
     /**
@@ -114,7 +132,7 @@
      * @return error writer of the current context
      */
     public static PrintWriter getCurrentErr() {
-        final ScriptObject global = getGlobal();
+        final ScriptObject global = getGlobalTrusted();
         return (global != null)? global.getContext().getErr() : new PrintWriter(System.err);
     }
 
@@ -456,6 +474,14 @@
         return _timezone;
     }
 
+    /*
+     * Get the PropertyMap of the current global scope
+     * @return the property map of the current global scope
+     */
+    public PropertyMap getGlobalMap() {
+        return Context.getGlobalTrusted().getMap();
+    }
+
     /**
      * Compile a top level script.
      *
@@ -501,7 +527,7 @@
         final String  file       = (location == UNDEFINED || location == null) ? "<eval>" : location.toString();
         final Source  source     = new Source(file, string);
         final boolean directEval = location != UNDEFINED; // is this direct 'eval' call or indirectly invoked eval?
-        final ScriptObject global = Context.getGlobal();
+        final ScriptObject global = Context.getGlobalTrusted();
 
         ScriptObject scope = initialScope;
 
@@ -624,7 +650,7 @@
             }
         }
 
-        typeError(Context.getGlobal(), "cant.load.script", ScriptRuntime.safeToString(source));
+        typeError("cant.load.script", ScriptRuntime.safeToString(source));
 
         return UNDEFINED;
     }
@@ -726,13 +752,13 @@
         final ScriptObject global = newGlobal();
         // Need only minimal global object, if we are just compiling.
         if (!_compile_only) {
-            final ScriptObject oldGlobal = Context.getGlobal();
+            final ScriptObject oldGlobal = Context.getGlobalTrusted();
             try {
-                Context.setGlobal(global);
+                Context.setGlobalTrusted(global);
                 // initialize global scope with builtin global objects
                 ((GlobalObject)global).initBuiltinObjects();
             } finally {
-                Context.setGlobal(oldGlobal);
+                Context.setGlobalTrusted(oldGlobal);
             }
         }
 
@@ -740,10 +766,30 @@
     }
 
     /**
-     * Trusted variant package-private
+     * Trusted variants - package-private
+     */
+
+    /**
+     * Return the current global scope
+     * @return current global scope
+     */
+    static ScriptObject getGlobalTrusted() {
+        return currentGlobal.get();
+    }
+
+    /**
+     * Set the current global scope
+     */
+    static void setGlobalTrusted(ScriptObject global) {
+         currentGlobal.set(global);
+    }
+
+    /**
+     * Return the current global's context
+     * @return current global's context
      */
     static Context getContextTrusted() {
-        return Context.getGlobal().getContext();
+        return Context.getGlobalTrusted().getContext();
     }
 
     /**
@@ -770,7 +816,7 @@
         try {
             script = compileScript(name, url, scope, new Context.ThrowErrorManager(), _strict);
         } catch (final ParserException e) {
-            e.throwAsEcmaException(Context.getGlobal());
+            e.throwAsEcmaException();
         }
 
         return ScriptRuntime.apply(script, thiz);
@@ -782,7 +828,7 @@
         try {
             script = compileScript(source, scope, new Context.ThrowErrorManager(), _strict);
         } catch (final ParserException e) {
-            e.throwAsEcmaException(Context.getGlobal());
+            e.throwAsEcmaException();
         }
 
         return ScriptRuntime.apply(script, thiz);
@@ -813,7 +859,7 @@
         }
 
         // Package as a JavaScript function and pass function back to shell.
-        return ((GlobalObject)Context.getGlobal()).newScriptFunction(RUN_SCRIPT.tag(), runMethodHandle, scope, strict);
+        return ((GlobalObject)Context.getGlobalTrusted()).newScriptFunction(RUN_SCRIPT.tag(), runMethodHandle, scope, strict);
     }
 
     private ScriptFunction compileScript(final String name, final URL url, final ScriptObject scope, final ErrorManager errMan, final boolean strict) throws IOException {
@@ -836,7 +882,7 @@
         Class<?> script;
 
         if (_class_cache_size > 0) {
-            global = (GlobalObject)Context.getGlobal();
+            global = (GlobalObject)Context.getGlobalTrusted();
             script = global.findCachedClass(source);
             if (script != null) {
                 return script;
--- a/src/jdk/nashorn/internal/runtime/ECMAErrors.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/ECMAErrors.java	Tue Jan 22 22:07:12 2013 +0530
@@ -56,6 +56,16 @@
         throw new ECMAException(thrown, cause);
     }
 
+     /**
+     * Error dispatch mechanism.
+     * Throw a {@link ParserException} as the correct JavaScript error
+     *
+     * @param e {@code ParserException} for error dispatcher
+     */
+    public static void throwAsEcmaException(final ParserException e) {
+        throwAsEcmaException(Context.getGlobalTrusted(), e);
+    }
+
     /**
      * Error dispatch mechanism.
      * Throw a {@link ParserException} as the correct JavaScript error
@@ -103,6 +113,15 @@
         // should not happen - perhaps unknown error type?
         throw e;
     }
+    /**
+     * Throw a syntax error (ECMA 15.11.6.4)
+     *
+     * @param msgId   resource tag for error message
+     * @param args    arguments to resource
+     */
+    public static void syntaxError(final String msgId, final String... args) {
+        syntaxError(Context.getGlobalTrusted(), msgId, args);
+    }
 
     /**
      * Throw a syntax error (ECMA 15.11.6.4)
@@ -118,6 +137,17 @@
     /**
      * Throw a syntax error (ECMA 15.11.6.4)
      *
+     * @param cause   native Java {@code Throwable} that is the cause of error
+     * @param msgId   resource tag for error message
+     * @param args    arguments to resource
+     */
+    public static void syntaxError(final Throwable cause, final String msgId, final String... args) {
+        syntaxError(Context.getGlobalTrusted(), cause, msgId, args);
+    }
+
+    /**
+     * Throw a syntax error (ECMA 15.11.6.4)
+     *
      * @param global  global scope object
      * @param cause   native Java {@code Throwable} that is the cause of error
      * @param msgId   resource tag for error message
@@ -131,6 +161,16 @@
     /**
      * Throw a type error (ECMA 15.11.6.5)
      *
+     * @param msgId   resource tag for error message
+     * @param args    arguments to resource
+     */
+    public static void typeError(final String msgId, final String... args) {
+        typeError(Context.getGlobalTrusted(), msgId, args);
+    }
+
+    /**
+     * Throw a type error (ECMA 15.11.6.5)
+     *
      * @param global  global scope object
      * @param msgId   resource tag for error message
      * @param args    arguments to resource
@@ -142,6 +182,17 @@
     /**
      * Throw a type error (ECMA 15.11.6.5)
      *
+     * @param cause   native Java {@code Throwable} that is the cause of error
+     * @param msgId   resource tag for error message
+     * @param args    arguments to resource
+     */
+    public static void typeError(final Throwable cause, final String msgId, final String... args) {
+        typeError(Context.getGlobalTrusted(), cause, msgId, args);
+    }
+
+    /**
+     * Throw a type error (ECMA 15.11.6.5)
+     *
      * @param global  global scope object
      * @param cause   native Java {@code Throwable} that is the cause of error
      * @param msgId   resource tag for error message
@@ -155,6 +206,16 @@
     /**
      * Throw a range error (ECMA 15.11.6.2)
      *
+     * @param msgId   resource tag for error message
+     * @param args    arguments to resource
+     */
+    public static void rangeError(final String msgId, final String... args) {
+        rangeError(Context.getGlobalTrusted(), msgId, args);
+    }
+
+    /**
+     * Throw a range error (ECMA 15.11.6.2)
+     *
      * @param global  global scope object
      * @param msgId   resource tag for error message
      * @param args    arguments to resource
@@ -166,6 +227,17 @@
     /**
      * Throw a range error (ECMA 15.11.6.2)
      *
+     * @param cause   native Java {@code Throwable} that is the cause of error
+     * @param msgId   resource tag for error message
+     * @param args    arguments to resource
+     */
+    public static void rangeError(final Throwable cause, final String msgId, final String... args) {
+        rangeError(Context.getGlobalTrusted(), cause, msgId, args);
+    }
+
+    /**
+     * Throw a range error (ECMA 15.11.6.2)
+     *
      * @param global  global scope object
      * @param cause   native Java {@code Throwable} that is the cause of error
      * @param msgId   resource tag for error message
@@ -179,6 +251,16 @@
     /**
      * Throw a reference error (ECMA 15.11.6.3)
      *
+     * @param msgId   resource tag for error message
+     * @param args    arguments to resource
+     */
+    public static void referenceError(final String msgId, final String... args) {
+        referenceError(Context.getGlobalTrusted(), msgId, args);
+    }
+
+    /**
+     * Throw a reference error (ECMA 15.11.6.3)
+     *
      * @param global  global scope object
      * @param msgId   resource tag for error message
      * @param args    arguments to resource
@@ -190,6 +272,17 @@
     /**
      * Throw a reference error (ECMA 15.11.6.3)
      *
+     * @param cause   native Java {@code Throwable} that is the cause of error
+     * @param msgId   resource tag for error message
+     * @param args    arguments to resource
+     */
+    public static void referenceError(final Throwable cause, final String msgId, final String... args) {
+        referenceError(Context.getGlobalTrusted(), cause, msgId, args);
+    }
+
+    /**
+     * Throw a reference error (ECMA 15.11.6.3)
+     *
      * @param global  global scope object
      * @param cause   native Java {@code Throwable} that is the cause of error
      * @param msgId   resource tag for error message
@@ -203,6 +296,16 @@
     /**
      * Throw a URI error (ECMA 15.11.6.6)
      *
+     * @param msgId   resource tag for error message
+     * @param args    arguments to resource
+     */
+    public static void uriError(final String msgId, final String... args) {
+        uriError(Context.getGlobalTrusted(), msgId, args);
+    }
+
+    /**
+     * Throw a URI error (ECMA 15.11.6.6)
+     *
      * @param global  global scope object
      * @param msgId   resource tag for error message
      * @param args    arguments to resource
@@ -214,6 +317,17 @@
     /**
      * Throw a URI error (ECMA 15.11.6.6)
      *
+     * @param cause   native Java {@code Throwable} that is the cause of error
+     * @param msgId   resource tag for error message
+     * @param args    arguments to resource
+     */
+    public static void uriError(final Throwable cause, final String msgId, final String... args) {
+        uriError(Context.getGlobalTrusted(), cause, msgId, args);
+    }
+
+    /**
+     * Throw a URI error (ECMA 15.11.6.6)
+     *
      * @param global  global scope object
      * @param cause   native Java {@code Throwable} that is the cause of error
      * @param msgId   resource tag for error message
--- a/src/jdk/nashorn/internal/runtime/ErrorManager.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/ErrorManager.java	Tue Jan 22 22:07:12 2013 +0530
@@ -79,7 +79,7 @@
         }
 
         if (limit != 0 && count > limit) {
-            rangeError(Context.getGlobal(), "too.many.errors", Integer.toString(limit));
+            rangeError("too.many.errors", Integer.toString(limit));
         }
     }
 
--- a/src/jdk/nashorn/internal/runtime/JSType.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/JSType.java	Tue Jan 22 22:07:12 2013 +0530
@@ -248,7 +248,7 @@
         final Object       result = sobj.getDefaultValue(hint);
 
         if (!isPrimitive(result)) {
-            typeError(Context.getGlobal(), "bad.default.value", result.toString());
+            typeError("bad.default.value", result.toString());
         }
 
         return result;
@@ -803,12 +803,25 @@
      * NativeObject type
      * See ECMA 9.9 ToObject
      *
+     * @param obj     the object to convert
+     *
+     * @return the wrapped object
+     */
+    public static Object toScriptObject(final Object obj) {
+        return toScriptObject(Context.getGlobalTrusted(), obj);
+    }
+
+    /**
+     * Object conversion. This is used to convert objects and numbers to their corresponding
+     * NativeObject type
+     * See ECMA 9.9 ToObject
+     *
      * @param global  the global object
      * @param obj     the object to convert
      *
      * @return the wrapped object
      */
-    public static Object toObject(final ScriptObject global, final Object obj) {
+    public static Object toScriptObject(final ScriptObject global, final Object obj) {
         if (nullOrUndefined(obj)) {
             typeError(global, "not.an.object", ScriptRuntime.safeToString(obj));
         }
@@ -851,7 +864,7 @@
         if (obj instanceof ScriptObject) {
             if (safe) {
                 final ScriptObject sobj = (ScriptObject)obj;
-                final GlobalObject gobj = (GlobalObject)Context.getGlobal();
+                final GlobalObject gobj = (GlobalObject)Context.getGlobalTrusted();
                 return gobj.isError(sobj) ?
                     ECMAException.safeToString(sobj) :
                     sobj.safeToString();
--- a/src/jdk/nashorn/internal/runtime/ParserException.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/ParserException.java	Tue Jan 22 22:07:12 2013 +0530
@@ -129,6 +129,13 @@
 
     /**
      * Throw this {@code ParserException} as one of the 7 native JavaScript errors
+     */
+    public void throwAsEcmaException() {
+        ECMAErrors.throwAsEcmaException(this);
+    }
+
+    /**
+     * Throw this {@code ParserException} as one of the 7 native JavaScript errors
      * @param global global scope object
      */
     public void throwAsEcmaException(final ScriptObject global) {
--- a/src/jdk/nashorn/internal/runtime/ScriptFunction.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/ScriptFunction.java	Tue Jan 22 22:07:12 2013 +0530
@@ -289,7 +289,7 @@
     @Override
     public boolean isInstance(final ScriptObject instance) {
         if (!(prototype instanceof ScriptObject)) {
-            typeError(Context.getGlobal(), "prototype.not.an.object", ScriptRuntime.safeToString(this), ScriptRuntime.safeToString(prototype));
+            typeError("prototype.not.an.object", ScriptRuntime.safeToString(this), ScriptRuntime.safeToString(prototype));
         }
 
         for (ScriptObject proto = instance.getProto(); proto != null; proto = proto.getProto()) {
@@ -401,7 +401,7 @@
      */
     public Object construct(final Object self, final Object... args) throws Throwable {
         if (constructHandle == null) {
-            typeError(Context.getGlobal(), "not.a.constructor", ScriptRuntime.safeToString(this));
+            typeError("not.a.constructor", ScriptRuntime.safeToString(this));
         }
 
         if (isVarArg(constructHandle)) {
@@ -480,7 +480,7 @@
         }
 
         if (getConstructHandle() == null) {
-            typeError(Context.getGlobal(), "not.a.constructor", ScriptRuntime.safeToString(this));
+            typeError("not.a.constructor", ScriptRuntime.safeToString(this));
         }
 
         ScriptObject object = null;
@@ -863,7 +863,7 @@
         MethodHandle constructor = getConstructHandle(type);
 
         if (constructor == null) {
-            typeError(Context.getGlobal(), "not.a.constructor", ScriptRuntime.safeToString(this));
+            typeError("not.a.constructor", ScriptRuntime.safeToString(this));
             return null;
         }
 
@@ -925,7 +925,7 @@
 
             if(NashornCallSiteDescriptor.isScope(desc)) {
                 // (this, callee, args...) => (callee, args...) => (callee, [this], args...)
-                boundHandle = MH.bindTo(callHandle, isNonStrictFunction() ? Context.getGlobal(): ScriptRuntime.UNDEFINED);
+                boundHandle = MH.bindTo(callHandle, isNonStrictFunction() ? Context.getGlobalTrusted() : ScriptRuntime.UNDEFINED);
                 boundHandle = MH.dropArguments(boundHandle, 1, Object.class);
             } else {
                 // (this, callee, args...) permute => (callee, this, args...) which is what we get in
@@ -943,7 +943,7 @@
             final MethodHandle callHandle = getBestSpecializedInvokeHandle(type.dropParameterTypes(0, 1));
 
             if(NashornCallSiteDescriptor.isScope(desc)) {
-                boundHandle = MH.bindTo(callHandle, isNonStrictFunction()? Context.getGlobal() : ScriptRuntime.UNDEFINED);
+                boundHandle = MH.bindTo(callHandle, isNonStrictFunction()? Context.getGlobalTrusted() : ScriptRuntime.UNDEFINED);
                 boundHandle = MH.dropArguments(boundHandle, 0, Object.class, Object.class);
             } else {
                 boundHandle = MH.dropArguments(callHandle, 0, Object.class);
@@ -952,7 +952,7 @@
 
         boundHandle = pairArguments(boundHandle, type);
         return new NashornGuardedInvocation(boundHandle, null, NashornGuards.getFunctionGuard(this), isNonStrictFunction());
-    }
+   }
 
     /**
      * Used for noSuchMethod/noSuchProperty and JSAdapter hooks.
--- a/src/jdk/nashorn/internal/runtime/ScriptObject.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/ScriptObject.java	Tue Jan 22 22:07:12 2013 +0530
@@ -274,7 +274,7 @@
       * @return property descriptor
       */
     public final PropertyDescriptor toPropertyDescriptor() {
-        final GlobalObject global = (GlobalObject) Context.getGlobal();
+        final GlobalObject global = (GlobalObject) Context.getGlobalTrusted();
 
         final PropertyDescriptor desc;
         if (isDataDescriptor()) {
@@ -324,7 +324,7 @@
     public Object getOwnPropertyDescriptor(final String key) {
         final Property property = getMap().findProperty(key);
 
-        final GlobalObject global = (GlobalObject)Context.getGlobal();
+        final GlobalObject global = (GlobalObject)Context.getGlobalTrusted();
 
         if (property != null) {
             final ScriptFunction get   = property.getGetterFunction(this);
@@ -389,7 +389,7 @@
      * @return true if property was successfully defined
      */
     public boolean defineOwnProperty(final String key, final Object propertyDesc, final boolean reject) {
-        final ScriptObject       global  = Context.getGlobal();
+        final ScriptObject       global  = Context.getGlobalTrusted();
         final PropertyDescriptor desc    = toPropertyDescriptor(global, propertyDesc);
         final Object             current = getOwnPropertyDescriptor(key);
         final String             name    = JSType.toString(key);
@@ -594,7 +594,7 @@
         final int propFlags = Property.toFlags(pdesc);
 
         if (pdesc.type() == PropertyDescriptor.GENERIC) {
-            final GlobalObject global = (GlobalObject) Context.getGlobal();
+            final GlobalObject global = (GlobalObject) Context.getGlobalTrusted();
             final PropertyDescriptor dDesc = global.newDataDescriptor(UNDEFINED, false, false, false);
 
             dDesc.fillFrom((ScriptObject)pdesc);
@@ -1142,8 +1142,8 @@
         if (newProto == null || newProto instanceof ScriptObject) {
             setProto((ScriptObject)newProto);
         } else {
-            final ScriptObject global = Context.getGlobal();
-            final Object  newProtoObject = JSType.toObject(global, newProto);
+            final ScriptObject global = Context.getGlobalTrusted();
+            final Object  newProtoObject = JSType.toScriptObject(global, newProto);
 
             if (newProtoObject instanceof ScriptObject) {
                 setProto((ScriptObject)newProtoObject);
@@ -1246,7 +1246,7 @@
         // "valueOf" methods, and in order to avoid those call sites from becoming megamorphic when multiple contexts
         // are being executed in a long-running program, we move the code and their associated dynamic call sites
         // (Global.TO_STRING and Global.VALUE_OF) into per-context code.
-        return ((GlobalObject)Context.getGlobal()).getDefaultValue(this, typeHint);
+        return ((GlobalObject)Context.getGlobalTrusted()).getDefaultValue(this, typeHint);
     }
 
     /**
@@ -1575,7 +1575,7 @@
     }
 
     private GuardedInvocation notAFunction() {
-        typeError(Context.getGlobal(), "not.a.function", ScriptRuntime.safeToString(this));
+        typeError("not.a.function", ScriptRuntime.safeToString(this));
         return null;
     }
 
@@ -1754,7 +1754,7 @@
     private GuardedInvocation createEmptySetMethod(final CallSiteDescriptor desc, String strictErrorMessage, boolean canBeFastScope) {
         final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
         if (NashornCallSiteDescriptor.isStrict(desc)) {
-               typeError(Context.getGlobal(), strictErrorMessage, name, ScriptRuntime.safeToString((this)));
+               typeError(strictErrorMessage, name, ScriptRuntime.safeToString((this)));
            }
            assert canBeFastScope || !NashornCallSiteDescriptor.isFastScope(desc);
            final PropertyMap myMap = getMap();
@@ -1781,7 +1781,7 @@
     private boolean trySetEmbedOrSpill(final CallSiteDescriptor desc, final PropertyMap oldMap, final PropertyMap newMap, final Object value) {
         final boolean isStrict = NashornCallSiteDescriptor.isStrict(desc);
         if (!isExtensible() && isStrict) {
-            typeError(Context.getGlobal(), "object.non.extensible", desc.getNameToken(2), ScriptRuntime.safeToString(this));
+            typeError("object.non.extensible", desc.getNameToken(2), ScriptRuntime.safeToString(this));
             throw new AssertionError(); // never reached
         } else if (compareAndSetMap(oldMap, newMap)) {
             return true;
@@ -1798,7 +1798,7 @@
 
         if (!obj.isExtensible()) {
             if (isStrict) {
-                typeError(Context.getGlobal(), "object.non.extensible", desc.getNameToken(2), ScriptRuntime.safeToString(obj));
+                typeError("object.non.extensible", desc.getNameToken(2), ScriptRuntime.safeToString(obj));
             }
         } else if (obj.compareAndSetMap(oldMap, newMap)) {
             obj.spill = new Object[SPILL_RATE];
@@ -1815,7 +1815,7 @@
 
         if (!obj.isExtensible()) {
             if (isStrict) {
-                typeError(Context.getGlobal(), "object.non.extensible", desc.getNameToken(2), ScriptRuntime.safeToString(obj));
+                typeError("object.non.extensible", desc.getNameToken(2), ScriptRuntime.safeToString(obj));
             }
         } else if (obj.compareAndSetMap(oldMap, newMap)) {
             final int oldLength = obj.spill.length;
@@ -1870,7 +1870,7 @@
 
         if (find == null) {
             if (scopeCall) {
-                ECMAErrors.referenceError(Context.getGlobal(), "not.defined", name);
+                ECMAErrors.referenceError("not.defined", name);
                 throw new AssertionError(); // never reached
             }
             return createEmptyGetter(desc, name);
@@ -1909,7 +1909,7 @@
         }
 
         if (scopeAccess) {
-            referenceError(Context.getGlobal(), "not.defined", name);
+            referenceError("not.defined", name);
         }
 
         return createEmptyGetter(desc, name);
@@ -2510,7 +2510,7 @@
         if (longIndex >= oldLength) {
             if (!isExtensible()) {
                 if (strict) {
-                    typeError(Context.getGlobal(), "object.non.extensible", JSType.toString(index), ScriptRuntime.safeToString(this));
+                    typeError("object.non.extensible", JSType.toString(index), ScriptRuntime.safeToString(this));
                 }
                 return;
             }
@@ -2559,7 +2559,7 @@
         if (f != null) {
             if (!f.isWritable()) {
                 if (strict) {
-                    typeError(Context.getGlobal(), "property.not.writable", key, ScriptRuntime.safeToString(this));
+                    typeError("property.not.writable", key, ScriptRuntime.safeToString(this));
                 }
 
                 return;
@@ -2575,7 +2575,7 @@
             }
         } else if (!isExtensible()) {
             if (strict) {
-                typeError(Context.getGlobal(), "object.non.extensible", key, ScriptRuntime.safeToString(this));
+                typeError("object.non.extensible", key, ScriptRuntime.safeToString(this));
             }
         } else {
             spill(key, value);
@@ -3062,7 +3062,7 @@
 
         if (!find.isConfigurable()) {
             if (strict) {
-                typeError(Context.getGlobal(), "cant.delete.property", propName, ScriptRuntime.safeToString(this));
+                typeError("cant.delete.property", propName, ScriptRuntime.safeToString(this));
             }
             return false;
         }
@@ -3232,7 +3232,7 @@
                 throw new RuntimeException(t);
             }
         }  else if (name != null) {
-            typeError(Context.getGlobal(), "property.has.no.setter", name, ScriptRuntime.safeToString(self));
+            typeError("property.has.no.setter", name, ScriptRuntime.safeToString(self));
         }
     }
 
--- a/src/jdk/nashorn/internal/runtime/ScriptRuntime.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/ScriptRuntime.java	Tue Jan 22 22:07:12 2013 +0530
@@ -385,14 +385,14 @@
      * @return {@link WithObject} that is the new scope
      */
     public static ScriptObject openWith(final ScriptObject scope, final Object expression) {
-        final ScriptObject global = Context.getGlobal();
+        final ScriptObject global = Context.getGlobalTrusted();
         if (expression == UNDEFINED) {
             typeError(global, "cant.apply.with.to.undefined");
         } else if (expression == null) {
             typeError(global, "cant.apply.with.to.null");
         }
 
-        final ScriptObject withObject = new WithObject(scope, JSType.toObject(global, expression));
+        final ScriptObject withObject = new WithObject(scope, JSType.toScriptObject(global, expression));
 
         return withObject;
     }
@@ -488,9 +488,9 @@
             } else if (object instanceof Undefined) {
                 obj = ((Undefined)obj).get(property);
             } else if (object == null) {
-                typeError(Context.getGlobal(), "cant.get.property", safeToString(property), "null");
+                typeError("cant.get.property", safeToString(property), "null");
             } else if (JSType.isPrimitive(obj)) {
-                obj = ((ScriptObject)JSType.toObject(Context.getGlobal(), obj)).get(property);
+                obj = ((ScriptObject)JSType.toScriptObject(obj)).get(property);
             } else {
                 obj = UNDEFINED;
             }
@@ -526,7 +526,7 @@
      * @return undefined
      */
     public static Object REFERENCE_ERROR(final Object lhs, final Object rhs, final Object msg) {
-        referenceError(Context.getGlobal(), "cant.be.used.as.lhs", Objects.toString(msg));
+        referenceError("cant.be.used.as.lhs", Objects.toString(msg));
         return UNDEFINED;
     }
 
@@ -549,11 +549,11 @@
         }
 
         if (obj == null) {
-            typeError(Context.getGlobal(), "cant.delete.property", safeToString(property), "null");
+            typeError("cant.delete.property", safeToString(property), "null");
         }
 
         if (JSType.isPrimitive(obj)) {
-            return ((ScriptObject) JSType.toObject(Context.getGlobal(), obj)).delete(property, Boolean.TRUE.equals(strict));
+            return ((ScriptObject) JSType.toScriptObject(obj)).delete(property, Boolean.TRUE.equals(strict));
         }
 
         // if object is not reference type, vacuously delete is successful.
@@ -574,7 +574,7 @@
      */
     public static boolean FAIL_DELETE(final Object obj, final Object property, final Object strict) {
         if (Boolean.TRUE.equals(strict)) {
-            syntaxError(Context.getGlobal(), "strict.cant.delete", safeToString(property));
+            syntaxError("strict.cant.delete", safeToString(property));
         }
         return false;
     }
@@ -751,7 +751,7 @@
             return false;
         }
 
-        typeError(Context.getGlobal(), "in.with.non.object", rvalType.toString().toLowerCase());
+        typeError("in.with.non.object", rvalType.toString().toLowerCase());
 
         return false;
     }
@@ -776,7 +776,7 @@
             return ((StaticClass)clazz).getRepresentedClass().isInstance(obj);
         }
 
-        typeError(Context.getGlobal(), "instanceof.on.non.object");
+        typeError("instanceof.on.non.object");
 
         return false;
     }
--- a/src/jdk/nashorn/internal/runtime/ScriptingFunctions.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/ScriptingFunctions.java	Tue Jan 22 22:07:12 2013 +0530
@@ -88,7 +88,7 @@
         }
 
         if (f == null || !f.isFile()) {
-            typeError(Context.getGlobal(), "not.a.file", ScriptRuntime.safeToString(file));
+            typeError("not.a.file", ScriptRuntime.safeToString(file));
             return UNDEFINED;
         }
 
--- a/src/jdk/nashorn/internal/runtime/SetMethodCreator.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/SetMethodCreator.java	Tue Jan 22 22:07:12 2013 +0530
@@ -144,7 +144,7 @@
         // In strict mode, assignment can not create a new variable.
         // See also ECMA Annex C item 4. ReferenceError is thrown.
         if (NashornCallSiteDescriptor.isScope(desc) && NashornCallSiteDescriptor.isStrict(desc)) {
-            referenceError(Context.getGlobal(), "not.defined", getName());
+            referenceError("not.defined", getName());
         }
     }
 
@@ -171,7 +171,7 @@
     }
 
     private SetMethod createGlobalPropertySetter() {
-        final ScriptObject global = Context.getGlobal();
+        final ScriptObject global = Context.getGlobalTrusted();
         return new SetMethod(ScriptObject.bindTo(global.addSpill(getName()), global), null, false);
     }
 
--- a/src/jdk/nashorn/internal/runtime/URIUtils.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/URIUtils.java	Tue Jan 22 22:07:12 2013 +0530
@@ -71,7 +71,7 @@
             }
 
             if (C >= 0xDC00 && C <= 0xDFFF) {
-                return error(Context.getGlobal(), string, k);
+                return error(string, k);
             }
 
             int V;
@@ -80,12 +80,12 @@
             } else {
                 k++;
                 if (k == len) {
-                    return error(Context.getGlobal(), string, k);
+                    return error(string, k);
                 }
 
                 final char kChar = string.charAt(k);
                 if (kChar < 0xDC00 || kChar > 0xDFFF) {
-                    return error(Context.getGlobal(), string, k);
+                    return error(string, k);
                 }
                 V = ((C - 0xD800) * 0x400 + (kChar - 0xDC00) + 0x10000);
             }
@@ -93,7 +93,7 @@
             try {
                 sb.append(toHexEscape(V));
             } catch (final Exception e) {
-                uriError(Context.getGlobal(), e, "bad.uri", string, Integer.toString(k));
+                uriError( e, "bad.uri", string, Integer.toString(k));
                 return null;
             }
         }
@@ -118,12 +118,12 @@
             }
             final int start = k;
             if (k + 2 >= len) {
-                return error(Context.getGlobal(), string, k);
+                return error(string, k);
             }
 
             int B = toHexByte(string.charAt(k + 1), string.charAt(k + 2));
             if (B < 0) {
-                return error(Context.getGlobal(), string, k + 1);
+                return error(string, k + 1);
             }
 
             k += 2;
@@ -146,11 +146,11 @@
                 }
 
                 if (n == 1 || n > 4) {
-                    return error(Context.getGlobal(), string, k);
+                    return error(string, k);
                 }
 
                 if ((k + (3 * (n - 1))) >= len) {
-                    return error(Context.getGlobal(), string, k);
+                    return error(string, k);
                 }
 
                 final byte[] bbuf = new byte[n];
@@ -159,16 +159,16 @@
                 for (int j = 1; j < n; j++) {
                     k++;
                     if (string.charAt(k) != '%') {
-                        return error(Context.getGlobal(), string, k);
+                        return error(string, k);
                     }
 
                     if (k + 2 == len) {
-                        return error(Context.getGlobal(), string, k);
+                        return error(string, k);
                     }
 
                     B = toHexByte(string.charAt(k + 1), string.charAt(k + 2));
                     if (B < 0 || (B & 0xC0) != 0x80) {
-                        return error(Context.getGlobal(), string, k + 1);
+                        return error(string, k + 1);
                     }
 
                     k += 2;
@@ -179,7 +179,7 @@
                 try {
                     V = ucs4Char(bbuf);
                 } catch (final Exception e) {
-                    uriError(Context.getGlobal(), e, "bad.uri", string, Integer.toString(k));
+                    uriError(e, "bad.uri", string, Integer.toString(k));
                     return null;
                 }
                 if (V < 0x10000) {
@@ -193,7 +193,7 @@
                     }
                 } else { // V >= 0x10000
                     if (V > 0x10FFFF) {
-                        return error(Context.getGlobal(), string, k);
+                        return error(string, k);
                     }
                     final int L = ((V - 0x10000) & 0x3FF) + 0xDC00;
                     final int H = (((V - 0x10000) >> 10) & 0x3FF) + 0xD800;
@@ -268,8 +268,8 @@
         return sb.toString();
     }
 
-    private static String error(final ScriptObject global, final String string, final int index) {
-        uriError(global, "bad.uri", string, Integer.toString(index));
+    private static String error(final String string, final int index) {
+        uriError("bad.uri", string, Integer.toString(index));
         return null;
     }
 
--- a/src/jdk/nashorn/internal/runtime/Undefined.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/Undefined.java	Tue Jan 22 22:07:12 2013 +0530
@@ -129,7 +129,7 @@
     }
 
     private static void lookupTypeError(final String msg, final CallSiteDescriptor desc) {
-        typeError(Context.getGlobal(), msg, desc.getNameTokenCount() > 2 ? desc.getNameToken(2) : null);
+        typeError(msg, desc.getNameTokenCount() > 2 ? desc.getNameToken(2) : null);
     }
 
     /**
@@ -174,18 +174,18 @@
 
     @Override
     public Object get(final Object key) {
-        typeError(Context.getGlobal(), "cant.read.property.of.undefined", ScriptRuntime.safeToString(key));
+        typeError("cant.read.property.of.undefined", ScriptRuntime.safeToString(key));
         return ScriptRuntime.UNDEFINED;
     }
 
     @Override
     public void set(final Object key, final Object value, final boolean strict) {
-        typeError(Context.getGlobal(), "cant.set.property.of.undefined", ScriptRuntime.safeToString(key));
+        typeError("cant.set.property.of.undefined", ScriptRuntime.safeToString(key));
     }
 
     @Override
     public boolean delete(final Object key, final boolean strict) {
-        typeError(Context.getGlobal(), "cant.delete.property.of.undefined", ScriptRuntime.safeToString(key));
+        typeError("cant.delete.property.of.undefined", ScriptRuntime.safeToString(key));
         return false;
     }
 
--- a/src/jdk/nashorn/internal/runtime/arrays/ArrayLikeIterator.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/arrays/ArrayLikeIterator.java	Tue Jan 22 22:07:12 2013 +0530
@@ -122,7 +122,7 @@
             return new ArrayIterator((ScriptObject) obj, includeUndefined);
         }
 
-        obj = JSType.toObject(Context.getGlobal(), obj);
+        obj = JSType.toScriptObject(obj);
         if (obj instanceof ScriptObject) {
             return new MapIterator((ScriptObject)obj, includeUndefined);
         }
@@ -143,7 +143,7 @@
             return new ReverseArrayIterator((ScriptObject) obj, includeUndefined);
         }
 
-        obj = JSType.toObject(Context.getGlobal(), obj);
+        obj = JSType.toScriptObject(obj);
         if (obj instanceof ScriptObject) {
             return new ReverseMapIterator((ScriptObject)obj, includeUndefined);
         }
--- a/src/jdk/nashorn/internal/runtime/arrays/FrozenArrayFilter.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/arrays/FrozenArrayFilter.java	Tue Jan 22 22:07:12 2013 +0530
@@ -47,7 +47,7 @@
     @Override
     public ArrayData set(final int index, final int value, final boolean strict) {
         if (strict) {
-            typeError(Context.getGlobal(), "cant.set.property", Integer.toString(index), "frozen array");
+            typeError("cant.set.property", Integer.toString(index), "frozen array");
         }
         return this;
     }
@@ -55,7 +55,7 @@
     @Override
     public ArrayData set(final int index, final long value, final boolean strict) {
         if (strict) {
-            typeError(Context.getGlobal(), "cant.set.property", Integer.toString(index), "frozen array");
+            typeError("cant.set.property", Integer.toString(index), "frozen array");
         }
         return this;
     }
@@ -63,7 +63,7 @@
     @Override
     public ArrayData set(final int index, final double value, final boolean strict) {
         if (strict) {
-            typeError(Context.getGlobal(), "cant.set.property", Integer.toString(index), "frozen array");
+            typeError("cant.set.property", Integer.toString(index), "frozen array");
         }
         return this;
     }
@@ -71,7 +71,7 @@
     @Override
     public ArrayData set(final int index, final Object value, final boolean strict) {
         if (strict) {
-            typeError(Context.getGlobal(), "cant.set.property", Integer.toString(index), "frozen array");
+            typeError("cant.set.property", Integer.toString(index), "frozen array");
         }
         return this;
     }
--- a/src/jdk/nashorn/internal/runtime/arrays/IteratorAction.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/arrays/IteratorAction.java	Tue Jan 22 22:07:12 2013 +0530
@@ -97,7 +97,7 @@
      */
     public final T apply() {
         if (!(callbackfn instanceof ScriptFunction)) {
-            typeError(Context.getGlobal(), "not.a.function", ScriptRuntime.safeToString(callbackfn));
+            typeError("not.a.function", ScriptRuntime.safeToString(callbackfn));
             return result;
         }
         final ScriptFunction func = ((ScriptFunction)callbackfn);
@@ -136,4 +136,5 @@
      * @throws Throwable if invocation throws an exception/error
      */
     protected abstract boolean forEach(final Object val, final int i) throws Throwable;
+
 }
--- a/src/jdk/nashorn/internal/runtime/arrays/SealedArrayFilter.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/arrays/SealedArrayFilter.java	Tue Jan 22 22:07:12 2013 +0530
@@ -47,7 +47,7 @@
     @Override
     public boolean canDelete(final int index, final boolean strict) {
         if (strict) {
-            typeError(Context.getGlobal(), "cant.delete.property", Integer.toString(index), "sealed array");
+            typeError("cant.delete.property", Integer.toString(index), "sealed array");
         }
         return false;
     }
--- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java	Tue Jan 22 22:07:12 2013 +0530
@@ -430,6 +430,21 @@
         }
     }
 
+    /**
+     * Tells if the given Class is an adapter or support class
+     * @param clazz Class object
+     * @return true if the Class given is adapter or support class
+     */
+    public static boolean isAdapterClass(Class<?> clazz) {
+        return clazz.getClassLoader() instanceof AdapterLoader;
+    }
+
+    private static class AdapterLoader extends SecureClassLoader {
+        AdapterLoader(ClassLoader parent) {
+            super(parent);
+        }
+    }
+
     // Creation of class loader is in a separate static method so that it doesn't retain a reference to the factory
     // instance. Note that the adapter class is created in the protection domain of the class/interface being
     // extended/implemented, and only the privileged global setter action class is generated in the protection domain
@@ -440,7 +455,7 @@
     // security tradeoff...
     private static ClassLoader createClassLoader(final ClassLoader parentLoader, final String className,
             final byte[] classBytes, final String privilegedActionClassName) {
-        return new SecureClassLoader(parentLoader) {
+        return new AdapterLoader(parentLoader) {
             @Override
             protected Class<?> findClass(final String name) throws ClassNotFoundException {
                 if(name.equals(className)) {
@@ -688,7 +703,7 @@
      */
     public static MethodHandle getHandle(final Object obj, final String name, final MethodType type, final boolean varArg) {
         if (! (obj instanceof ScriptObject)) {
-            typeError(Context.getGlobal(), "not.an.object", ScriptRuntime.safeToString(obj));
+            typeError("not.an.object", ScriptRuntime.safeToString(obj));
             throw new AssertionError();
         }
 
@@ -704,7 +719,7 @@
         } else if(fnObj == null || fnObj instanceof Undefined) {
             return null;
         } else {
-            typeError(Context.getGlobal(), "not.a.function", name);
+            typeError("not.a.function", name);
             throw new AssertionError();
         }
     }
@@ -1076,7 +1091,7 @@
 
         void typeError() {
             assert adaptationOutcome != AdaptationOutcome.SUCCESS;
-            ECMAErrors.typeError(Context.getGlobal(), "extend." + adaptationOutcome, classList);
+            ECMAErrors.typeError("extend." + adaptationOutcome, classList);
         }
     }
 
@@ -1219,7 +1234,7 @@
         while(it.hasNext()) {
             b.append(", ").append(it.next().clazz.getCanonicalName());
         }
-        typeError(Context.getGlobal(), "extend.ambiguous.defining.class", b.toString());
+        typeError("extend.ambiguous.defining.class", b.toString());
         throw new AssertionError(); // never reached
     }
 
--- a/src/jdk/nashorn/internal/runtime/linker/JavaArgumentConverters.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/linker/JavaArgumentConverters.java	Tue Jan 22 22:07:12 2013 +0530
@@ -34,7 +34,6 @@
 import java.util.HashMap;
 import java.util.Map;
 import jdk.nashorn.internal.runtime.ConsString;
-import jdk.nashorn.internal.runtime.Context;
 import jdk.nashorn.internal.runtime.JSType;
 import jdk.nashorn.internal.runtime.ScriptObject;
 import org.dynalang.dynalink.support.TypeUtilities;
@@ -110,7 +109,7 @@
                 return Character.valueOf((char) ival);
             }
 
-            typeError(Context.getGlobal(), "cant.convert.number.to.char");
+            typeError("cant.convert.number.to.char");
         }
 
         final String s = toString(o);
@@ -119,7 +118,7 @@
         }
 
         if (s.length() != 1) {
-            typeError(Context.getGlobal(), "cant.convert.string.to.char");
+            typeError("cant.convert.string.to.char");
         }
 
         return s.charAt(0);
--- a/src/jdk/nashorn/internal/runtime/linker/Lookup.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/linker/Lookup.java	Tue Jan 22 22:07:12 2013 +0530
@@ -31,7 +31,6 @@
 import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
-import jdk.nashorn.internal.runtime.Context;
 import jdk.nashorn.internal.runtime.JSType;
 import jdk.nashorn.internal.runtime.Property;
 import jdk.nashorn.internal.runtime.PropertyMap;
@@ -112,7 +111,7 @@
      * @return undefined (but throws error before return point)
      */
     public static Object typeErrorThrowerGetter(final Object self) {
-        typeError(Context.getGlobal(), "strict.getter.setter.poison", ScriptRuntime.safeToString(self));
+        typeError("strict.getter.setter.poison", ScriptRuntime.safeToString(self));
         return UNDEFINED;
     }
 
@@ -123,7 +122,7 @@
      * @param value (ignored)
      */
     public static void typeErrorThrowerSetter(final Object self, final Object value) {
-        typeError(Context.getGlobal(), "strict.getter.setter.poison", ScriptRuntime.safeToString(self));
+        typeError("strict.getter.setter.poison", ScriptRuntime.safeToString(self));
     }
 
     /**
--- a/src/jdk/nashorn/internal/runtime/linker/NashornBottomLinker.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/linker/NashornBottomLinker.java	Tue Jan 22 22:07:12 2013 +0530
@@ -30,8 +30,6 @@
 import static jdk.nashorn.internal.runtime.linker.Lookup.MH;
 
 import java.lang.invoke.MethodHandle;
-import jdk.nashorn.internal.runtime.Context;
-import jdk.nashorn.internal.runtime.ScriptObject;
 import jdk.nashorn.internal.runtime.ScriptRuntime;
 import org.dynalang.dynalink.CallSiteDescriptor;
 import org.dynalang.dynalink.linker.GuardedInvocation;
@@ -81,21 +79,21 @@
         switch (operator) {
         case "new":
             if(isJavaDynamicMethod(self)) {
-                typeError(Context.getGlobal(), "method.not.constructor", ScriptRuntime.safeToString(self));
+                typeError("method.not.constructor", ScriptRuntime.safeToString(self));
             } else {
-                typeError(Context.getGlobal(), "not.a.function", ScriptRuntime.safeToString(self));
+                typeError("not.a.function", ScriptRuntime.safeToString(self));
             }
             break;
         case "call":
             if(isJavaDynamicMethod(self)) {
-                typeError(Context.getGlobal(), "no.method.matches.args", ScriptRuntime.safeToString(self));
+                typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
             } else {
-                typeError(Context.getGlobal(), "not.a.function", ScriptRuntime.safeToString(self));
+                typeError("not.a.function", ScriptRuntime.safeToString(self));
             }
             break;
         case "callMethod":
         case "getMethod":
-            typeError(Context.getGlobal(), "no.such.function", getArgument(linkRequest), ScriptRuntime.safeToString(self));
+            typeError("no.such.function", getArgument(linkRequest), ScriptRuntime.safeToString(self));
             break;
         case "getProp":
         case "getElem":
@@ -136,25 +134,24 @@
     }
 
     private static GuardedInvocation linkNull(final LinkRequest linkRequest) {
-        final ScriptObject global = Context.getGlobal();
         final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor();
         final String operator = desc.getFirstOperator();
         switch (operator) {
         case "new":
         case "call":
-            typeError(global, "not.a.function", "null");
+            typeError("not.a.function", "null");
             break;
         case "callMethod":
         case "getMethod":
-            typeError(global, "no.such.function", getArgument(linkRequest), "null");
+            typeError("no.such.function", getArgument(linkRequest), "null");
             break;
         case "getProp":
         case "getElem":
-            typeError(global, "cant.get.property", getArgument(linkRequest), "null");
+            typeError("cant.get.property", getArgument(linkRequest), "null");
             break;
         case "setProp":
         case "setElem":
-            typeError(global, "cant.set.property", getArgument(linkRequest), "null");
+            typeError("cant.set.property", getArgument(linkRequest), "null");
             break;
         default:
             break;
--- a/src/jdk/nashorn/internal/runtime/linker/NashornStaticClassLinker.java	Tue Jan 22 14:36:28 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/linker/NashornStaticClassLinker.java	Tue Jan 22 22:07:12 2013 +0530
@@ -25,7 +25,6 @@
 
 package jdk.nashorn.internal.runtime.linker;
 
-import jdk.nashorn.internal.runtime.Context;
 import jdk.nashorn.internal.runtime.ECMAErrors;
 import org.dynalang.dynalink.CallSiteDescriptor;
 import org.dynalang.dynalink.beans.BeansLinker;
@@ -94,7 +93,7 @@
 
     private static GuardedInvocation checkNullConstructor(final GuardedInvocation ctorInvocation, final Class<?> receiverClass) {
         if(ctorInvocation == null) {
-            ECMAErrors.typeError(Context.getGlobal(), "no.constructor.matches.args", receiverClass.getName());
+            ECMAErrors.typeError("no.constructor.matches.args", receiverClass.getName());
         }
         return ctorInvocation;
     }