diff --git a/js/rhino/src/org/mozilla/javascript/FunctionObject.java b/js/rhino/src/org/mozilla/javascript/FunctionObject.java index 20c1549f872..2fc45464b50 100644 --- a/js/rhino/src/org/mozilla/javascript/FunctionObject.java +++ b/js/rhino/src/org/mozilla/javascript/FunctionObject.java @@ -186,9 +186,21 @@ public class FunctionObject extends BaseFunction { hasVoidReturn = method != null && method.getReturnType() == Void.TYPE; ScriptRuntime.setFunctionProtoAndParent(scope, this); - Context cx = Context.getCurrentContext(); - useDynamicScope = cx != null && - cx.hasCompileFunctionsWithDynamicScope(); + Context cx = Context.getContext(); + useDynamicScope = cx.hasCompileFunctionsWithDynamicScope(); + + if (method != null) { + Invoker master = invokerMaster; + if (master != null) { + try { + invoker = master.createInvoker(cx, method, types); + } catch (SecurityException ex) { + // Ignore invoker optimization in case of SecurityException + // which can be the case if class loader creation is + // disabled. + } + } + } } /** @@ -489,11 +501,7 @@ public class FunctionObject extends BaseFunction { private final Object doInvoke(Context cx, Object thisObj, Object[] args) throws IllegalAccessException, InvocationTargetException { - Invoker master = invokerMaster; - if (master != null) { - if (invoker == null) { - invoker = master.createInvoker(cx, method, types); - } + if (invoker != null) { try { return invoker.invoke(thisObj, args); } catch (Exception e) { diff --git a/js/rhino/src/org/mozilla/javascript/optimizer/InvokerImpl.java b/js/rhino/src/org/mozilla/javascript/optimizer/InvokerImpl.java index 59829df2123..83dc3c3d117 100644 --- a/js/rhino/src/org/mozilla/javascript/optimizer/InvokerImpl.java +++ b/js/rhino/src/org/mozilla/javascript/optimizer/InvokerImpl.java @@ -57,9 +57,13 @@ public class InvokerImpl extends Invoker { int classNum; synchronized (this) { if (invokersCache == null) { - invokersCache = new Hashtable(); ClassLoader parentLoader = cx.getClass().getClassLoader(); classLoader = cx.createClassLoader(parentLoader); + // Initialize invokersCache after creation of classloader + // since it can throw SecurityException. It prevents + // NullPointerException when accessing classLoader on + // the second Invoker invocation. + invokersCache = new Hashtable(); } else { result = (Invoker)invokersCache.get(method); if (result != null) { return result; }