From 558b670d2995df5a5afad303b7909d232fa11b59 Mon Sep 17 00:00:00 2001 From: "igor%mir2.org" Date: Wed, 12 Jun 2002 05:32:35 +0000 Subject: [PATCH] Removal of unused code for explicit JSObject support: > Norris Boyd wrote: > > Igor Bukaniv wrote: > > > > I am curios, why there is a need to have a special JSObject support in Rhino? Was it used for anything? The implementation in the ICEbrowser does not use it as in rare cases where conversion from JSObject to/from JS type may be needed (like calling JSObject.getWindow from a script), it seems that WrapHandler (or similar modifications to pre Rhino 1.5R2 sources) and Wrapper are enough to cover all the cases. > Yes, we should probably just remove the JSObject code. We added it early on when Rhino was first written and we thought we might need JSObject compatibility with the JS + Java implementation in Navigator 4.x. That's not important now, so we should just remove this code (which likely doesn't work at this point anyway). --- .../src/org/mozilla/javascript/Context.java | 2 - .../mozilla/javascript/NativeJavaMethod.java | 40 ++----- .../mozilla/javascript/NativeJavaObject.java | 102 +++--------------- .../mozilla/javascript/NativeJavaPackage.java | 3 - 4 files changed, 24 insertions(+), 123 deletions(-) diff --git a/js/rhino/src/org/mozilla/javascript/Context.java b/js/rhino/src/org/mozilla/javascript/Context.java index 9f62b6f62ccc..68431c17dce1 100644 --- a/js/rhino/src/org/mozilla/javascript/Context.java +++ b/js/rhino/src/org/mozilla/javascript/Context.java @@ -2239,8 +2239,6 @@ public class Context { static final boolean check = true; - static final boolean useJSObject = false; - static boolean isCachingEnabled = true; private static Hashtable threadContexts = new Hashtable(11); diff --git a/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java b/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java index 90fcc36d63e1..728cb86acc46 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java +++ b/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java @@ -320,35 +320,17 @@ public class NativeJavaMethod extends NativeFunction implements Function { if (methodsOrCtors.length == 0) return null; boolean hasMethods = methodsOrCtors[0] instanceof Method; - if (Context.useJSObject && - NativeJavaObject.jsObjectClass != null) - { - try { - for (int i = 0; i < args.length; i++) { - if (NativeJavaObject.jsObjectClass.isInstance(args[i])) - args[i] = NativeJavaObject.jsObjectGetScriptable.invoke( - args[i], ScriptRuntime.emptyArgs); - } - } - catch (InvocationTargetException e) { - // Just abandon conversion from JSObject - } - catch (IllegalAccessException e) { - // Just abandon conversion from JSObject - } - } else { - // Wrapper support - for (int i=0; i < args.length; i++) { - Object arg = args[i]; - if (arg instanceof Wrapper) { - arg = ((Wrapper)arg).unwrap(); - if (!(arg instanceof Number)) { - // Since numbers are internally represented as - // java.lang.Double, etc. then java.lang.Doubles are - // distinquished by being wrapped. Thus don't unwrap - // here or we'll get overloading wrong. - args[i] = arg; - } + // Wrapper support + for (int i=0; i < args.length; i++) { + Object arg = args[i]; + if (arg instanceof Wrapper) { + arg = ((Wrapper)arg).unwrap(); + if (!(arg instanceof Number)) { + // Since numbers are internally represented as + // java.lang.Double, etc. then java.lang.Doubles are + // distinquished by being wrapped. Thus don't unwrap + // here or we'll get overloading wrong. + args[i] = arg; } } } diff --git a/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java b/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java index cf20b42db0f3..0d14a243940e 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java +++ b/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java @@ -188,20 +188,6 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)} Class cls = obj.getClass(); if (cls.isArray()) return NativeJavaArray.wrap(scope, obj); - if (Context.useJSObject && jsObjectClass != null && - staticType != jsObjectClass && jsObjectClass.isInstance(obj)) - { - try { - return jsObjectGetScriptable. - invoke(obj, ScriptRuntime.emptyArgs); - } - catch (InvocationTargetException e) { - // Just abandon conversion from JSObject - } - catch (IllegalAccessException e) { - // Just abandon conversion from JSObject - } - } return new NativeJavaObject(scope, obj, staticType); } @@ -374,10 +360,6 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)} if (to == ScriptRuntime.ClassClass) { result = 1; } - else if (Context.useJSObject && jsObjectClass != null && - jsObjectClass.isAssignableFrom(to)) { - result = 2; - } else if (to == ScriptRuntime.ObjectClass) { result = 3; } @@ -410,11 +392,7 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)} case JSTYPE_OBJECT: // Other objects takes #1-#3 spots - if (Context.useJSObject && jsObjectClass != null && - jsObjectClass.isAssignableFrom(to)) { - result = 1; - } - else if (fromObj instanceof NativeArray && to.isArray()) { + if (fromObj instanceof NativeArray && to.isArray()) { // This is a native array conversion to a java array // Array conversions are all equal, and preferable to object // and string conversion, per LC3. @@ -612,26 +590,19 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)} break; case JSTYPE_JAVA_CLASS: - if (Context.useJSObject && jsObjectClass != null && - (type == ScriptRuntime.ObjectClass || - jsObjectClass.isAssignableFrom(type))) { - return coerceToJSObject(type, (Scriptable)value); + if (value instanceof Wrapper) { + value = ((Wrapper)value).unwrap(); + } + + if (type == ScriptRuntime.ClassClass || + type == ScriptRuntime.ObjectClass) { + return value; + } + else if (type == ScriptRuntime.StringClass) { + return value.toString(); } else { - if (value instanceof Wrapper) { - value = ((Wrapper)value).unwrap(); - } - - if (type == ScriptRuntime.ClassClass || - type == ScriptRuntime.ObjectClass) { - return value; - } - else if (type == ScriptRuntime.StringClass) { - return value.toString(); - } - else { - reportConversionError(value, type, !useErrorHandler); - } + reportConversionError(value, type, !useErrorHandler); } break; @@ -662,12 +633,7 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)} break; case JSTYPE_OBJECT: - if (Context.useJSObject && jsObjectClass != null && - (type == ScriptRuntime.ObjectClass || - jsObjectClass.isAssignableFrom(type))) { - return coerceToJSObject(type, (Scriptable)value); - } - else if (type == ScriptRuntime.StringClass) { + if (type == ScriptRuntime.StringClass) { return ScriptRuntime.toString(value); } else if (type.isPrimitive()) { @@ -714,28 +680,6 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)} return value; } - static Object coerceToJSObject(Class type, Scriptable value) { - // If JSObject compatibility is enabled, and the method wants it, - // wrap the Scriptable value in a JSObject. - - if (ScriptRuntime.ScriptableClass.isAssignableFrom(type)) - return value; - - try { - Object ctorArgs[] = { value }; - return jsObjectCtor.newInstance(ctorArgs); - } catch (InstantiationException instEx) { - throw new EvaluatorException("error generating JSObject wrapper for " + - value); - } catch (IllegalArgumentException argEx) { - throw new EvaluatorException("JSObject constructor doesn't want [Scriptable]!"); - } catch (InvocationTargetException e) { - throw WrappedException.wrapException(e.getTargetException()); - } catch (IllegalAccessException accessEx) { - throw new EvaluatorException("JSObject constructor is protected/private!"); - } - } - static Object coerceToNumber(Class type, Object value, boolean useErrorHandler) { Class valueClass = value.getClass(); @@ -934,23 +878,6 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)} value.toString(), NativeJavaMethod.javaSignature(type)); } - public static void initJSObject() { - // if netscape.javascript.JSObject is in the CLASSPATH, enable JSObject - // compatability wrappers - jsObjectClass = null; - try { - jsObjectClass = Class.forName("netscape.javascript.JSObject"); - Class ctorParms[] = { ScriptRuntime.ScriptableClass }; - jsObjectCtor = jsObjectClass.getConstructor(ctorParms); - jsObjectGetScriptable = jsObjectClass.getMethod("getScriptable", - new Class[0]); - } catch (ClassNotFoundException classEx) { - // jsObjectClass already null - } catch (NoSuchMethodException methEx) { - // jsObjectClass already null - } - } - /** * The prototype of this object. */ @@ -965,9 +892,6 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)} protected JavaMembers members; protected Class staticType; private Hashtable fieldAndMethods; - static Class jsObjectClass; - static Constructor jsObjectCtor; - static Method jsObjectGetScriptable; public void writeExternal(ObjectOutput out) throws IOException diff --git a/js/rhino/src/org/mozilla/javascript/NativeJavaPackage.java b/js/rhino/src/org/mozilla/javascript/NativeJavaPackage.java index ee85ca4c6b1a..470503386dcd 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeJavaPackage.java +++ b/js/rhino/src/org/mozilla/javascript/NativeJavaPackage.java @@ -131,9 +131,6 @@ public class NativeJavaPackage extends ScriptableObject { for (int i = 0; i < commonPackages.length; i++) packages.forcePackage(commonPackages[i]); - if (Context.useJSObject) - NativeJavaObject.initJSObject(); - Method[] m = FunctionObject.findMethods(NativeJavaPackage.class, "jsFunction_getClass"); FunctionObject f = new FunctionObject("getClass", m[0], global);