From ba09cca1fdf9c0018efa6fc2c72cd1a051d28db1 Mon Sep 17 00:00:00 2001 From: "igor%mir2.org" Date: Sat, 14 Aug 2004 15:12:38 +0000 Subject: [PATCH] Fixing compatibility issue caused by introduction of ContextFactory. See bug 255595 . git-svn-id: svn://10.0.0.236/trunk@160761 18797224-902f-48f8-a5cc-f745e15eee43 --- .../src/org/mozilla/javascript/Context.java | 4 +- .../org/mozilla/javascript/JavaAdapter.java | 70 +++---------------- .../org/mozilla/javascript/ScriptRuntime.java | 13 +++- .../mozilla/javascript/ScriptableObject.java | 13 ++-- 4 files changed, 31 insertions(+), 69 deletions(-) diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/Context.java b/mozilla/js/rhino/src/org/mozilla/javascript/Context.java index 02620be2f9c..830d5002f2f 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/Context.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/Context.java @@ -424,7 +424,7 @@ public class Context * with the thread during call to {@link ContextAction#run(Context)}. *

* It is allowed to use null for factory argument - * in which case ContextFactory.getGlobal().makeContext() will be + * in which case the factory associated with the scope will be * used to create new context instances. * * @see ContextFactory#call(ContextAction) @@ -435,7 +435,7 @@ public class Context throws JavaScriptException { if (factory == null) { - factory = ContextFactory.getGlobal(); + factory = ScriptRuntime.getContextFactory(scope); } Context[] storage = getThreadContextStorage(); Context cx; diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/JavaAdapter.java b/mozilla/js/rhino/src/org/mozilla/javascript/JavaAdapter.java index 3f1d6b4a440..656a7a069ff 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/JavaAdapter.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/JavaAdapter.java @@ -69,7 +69,6 @@ public final class JavaAdapter implements IdFunctionCall copy = null; } copy.function = function; - copy.contextFactory = currentFactory(); return copy; } @@ -85,7 +84,7 @@ public final class JavaAdapter implements IdFunctionCall { Scriptable scope = function.getParentScope(); Scriptable thisObj = scope; - return Context.call(contextFactory, this, scope, thisObj, args); + return Context.call(null, this, scope, thisObj, args); } public Object call(Context cx, Scriptable scope, Scriptable thisObj, @@ -107,7 +106,6 @@ public final class JavaAdapter implements IdFunctionCall private Function function; - private ContextFactory contextFactory; private int[] argsToConvert; } @@ -387,10 +385,6 @@ public final class JavaAdapter implements IdFunctionCall cfw.addField("self", "Lorg/mozilla/javascript/Scriptable;", (short) (ClassFileWriter.ACC_PUBLIC | ClassFileWriter.ACC_FINAL)); - cfw.addField("contextFactory", - "Lorg/mozilla/javascript/ContextFactory;", - (short) (ClassFileWriter.ACC_PRIVATE | - ClassFileWriter.ACC_FINAL)); int interfacesCount = interfaces == null ? 0 : interfaces.length; for (int i=0; i < interfacesCount; i++) { if (interfaces[i] != null) @@ -602,52 +596,37 @@ public final class JavaAdapter implements IdFunctionCall return null; } if (!(x instanceof Function)) - throw ScriptRuntime.typeError1("msg.isnt.function", functionName); + throw ScriptRuntime.notFunctionError(x, functionName); return (Function)x; } - public static ContextFactory currentFactory() - { - ContextFactory factory; - Context cx = Context.getCurrentContext(); - if (cx == null) { - // It can happen during instantiating of classes created - // with the class compiler in script-unaware application. - factory = ContextFactory.getGlobal(); - } else { - factory = cx.getFactory(); - } - return factory; - } - /** * Utility method which dynamically binds a Context to the current thread, * if none already exists. */ - public static Object callMethod(Scriptable scope, final Scriptable thisObj, + public static Object callMethod(final Scriptable thisObj, final Function f, final Object[] args, - final long argsToWrap, - ContextFactory factory) + final long argsToWrap) { if (f == null) { // See comments in getFunction return Undefined.instance; } - scope = ScriptableObject.getTopLevelScope(scope); + final Scriptable scope = f.getParentScope(); if (argsToWrap == 0) { - return Context.call(factory, f, scope, thisObj, args); + return Context.call(null, f, scope, thisObj, args); } Context cx = Context.getCurrentContext(); if (cx != null) { return doCall(cx, scope, thisObj, f, args, argsToWrap); } else { - final Scriptable finalScope = scope; + ContextFactory factory = ScriptRuntime.getContextFactory(scope); return factory.call(new ContextAction() { public Object run(Context cx) { - return doCall(cx, finalScope, thisObj, f, args, argsToWrap); + return doCall(cx, scope, thisObj, f, args, argsToWrap); } }); } @@ -712,8 +691,6 @@ public final class JavaAdapter implements IdFunctionCall cfw.add(ByteCode.PUTFIELD, adapterName, "self", "Lorg/mozilla/javascript/Scriptable;"); - generateContextFactoryInit(cfw, adapterName); - cfw.add(ByteCode.RETURN); cfw.stopMethod((short)3); // 2: this + delegee } @@ -744,8 +721,6 @@ public final class JavaAdapter implements IdFunctionCall cfw.add(ByteCode.PUTFIELD, adapterName, "self", "Lorg/mozilla/javascript/Scriptable;"); - generateContextFactoryInit(cfw, adapterName); - cfw.add(ByteCode.RETURN); cfw.stopMethod((short)20); // TODO: magic number "20" } @@ -793,24 +768,10 @@ public final class JavaAdapter implements IdFunctionCall cfw.add(ByteCode.PUTFIELD, adapterName, "self", "Lorg/mozilla/javascript/Scriptable;"); - generateContextFactoryInit(cfw, adapterName); - cfw.add(ByteCode.RETURN); cfw.stopMethod((short)2); // this + delegee } - private static void generateContextFactoryInit(ClassFileWriter cfw, - String adapterName) - { - cfw.add(ByteCode.ALOAD_0); // this for the following PUTFIELD for self - cfw.addInvoke(ByteCode.INVOKESTATIC, - "org/mozilla/javascript/JavaAdapter", - "currentFactory", - "()Lorg/mozilla/javascript/ContextFactory;"); - cfw.add(ByteCode.PUTFIELD, adapterName, "contextFactory", - "Lorg/mozilla/javascript/ContextFactory;"); - } - /** * Generates code to wrap Java arguments into Object[]. * Non-primitive Java types are left as is pending convertion @@ -986,15 +947,12 @@ public final class JavaAdapter implements IdFunctionCall cfw.startMethod(methodName, methodSignature, ClassFileWriter.ACC_PUBLIC); - int DELEGEE = firstLocal; - int FUNCTION = firstLocal + 1; - int LOCALS_END = firstLocal + 2; + int FUNCTION = firstLocal; + int LOCALS_END = firstLocal + 1; cfw.add(ByteCode.ALOAD_0); cfw.add(ByteCode.GETFIELD, genName, "delegee", "Lorg/mozilla/javascript/Scriptable;"); - cfw.add(ByteCode.DUP); - cfw.addAStore(DELEGEE); cfw.addPush(methodName); cfw.addInvoke(ByteCode.INVOKESTATIC, "org/mozilla/javascript/JavaAdapter", @@ -1006,8 +964,6 @@ public final class JavaAdapter implements IdFunctionCall cfw.addAStore(FUNCTION); // Prepare stack to call calMethod - // push scope - cfw.addALoad(DELEGEE); // push thisObj cfw.add(ByteCode.ALOAD_0); cfw.add(ByteCode.GETFIELD, genName, "self", @@ -1034,21 +990,15 @@ public final class JavaAdapter implements IdFunctionCall } cfw.addPush(convertionMask); - cfw.add(ByteCode.ALOAD_0); - cfw.add(ByteCode.GETFIELD, genName, "contextFactory", - "Lorg/mozilla/javascript/ContextFactory;"); - // go through utility method, which creates a Context to run the // method in. cfw.addInvoke(ByteCode.INVOKESTATIC, "org/mozilla/javascript/JavaAdapter", "callMethod", "(Lorg/mozilla/javascript/Scriptable;" - +"Lorg/mozilla/javascript/Scriptable;" +"Lorg/mozilla/javascript/Function;" +"[Ljava/lang/Object;" +"J" - +"Lorg/mozilla/javascript/ContextFactory;" +")Ljava/lang/Object;"); generateReturnResult(cfw, returnType); diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java b/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java index ce41167ae32..bfe19bfdfb2 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java @@ -124,7 +124,7 @@ public class ScriptRuntime { scope = new NativeObject(); } scope.associateValue(LIBRARY_SCOPE_KEY, scope); - + scope.associateValue(CONTEXT_FACTORY_KEY, cx.getFactory()); (new ClassCache()).associate(scope); BaseFunction.init(cx, scope, sealed); @@ -180,6 +180,17 @@ public class ScriptRuntime { return libScope; } + public static ContextFactory getContextFactory(Scriptable scope) + { + ContextFactory factory; + factory = (ContextFactory)ScriptableObject. + getTopScopeValue(scope, CONTEXT_FACTORY_KEY); + if (factory == null) { + throw new IllegalStateException("Failed to find ContextFactory"); + } + return factory; + } + public static Boolean wrapBoolean(boolean b) { return b ? Boolean.TRUE : Boolean.FALSE; diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/ScriptableObject.java b/mozilla/js/rhino/src/org/mozilla/javascript/ScriptableObject.java index f53ecda4401..2a0bd687bbd 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/ScriptableObject.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/ScriptableObject.java @@ -1483,8 +1483,6 @@ public abstract class ScriptableObject implements Scriptable, Serializable, /** * Call a method of an object. - * The method requires to have active Context associated with - * the current thread. * @param obj the JavaScript object * @param methodName the name of the function property * @param args the arguments for the call @@ -1494,13 +1492,11 @@ public abstract class ScriptableObject implements Scriptable, Serializable, public static Object callMethod(Scriptable obj, String methodName, Object[] args) { - return callMethod(Context.getContext(), obj, methodName, args); + return callMethod(null, obj, methodName, args); } /** * Call a method of an object. - * The method requires to have active Context associated with - * the current thread. * @param cx the Context object associated with the current thread. * @param obj the JavaScript object * @param methodName the name of the function property @@ -1515,7 +1511,12 @@ public abstract class ScriptableObject implements Scriptable, Serializable, throw ScriptRuntime.notFunctionError(obj, methodName); } Function fun = (Function)funObj; - return fun.call(cx, getTopLevelScope(obj), obj, args); + Scriptable scope = fun.getParentScope(); + if (cx != null) { + return fun.call(cx, scope, obj, args); + } else { + return Context.call(null, fun, scope, obj, args); + } } private static Scriptable getBase(Scriptable obj, String name)