diff --git a/js/rhino/src/org/mozilla/javascript/Context.java b/js/rhino/src/org/mozilla/javascript/Context.java index 6d73b4ba01e..9f85db28054 100644 --- a/js/rhino/src/org/mozilla/javascript/Context.java +++ b/js/rhino/src/org/mozilla/javascript/Context.java @@ -1646,25 +1646,82 @@ public class Context { FunctionObject.setCachingEnabled(cachingEnabled); } + // Proxy to allow to use deprecated WrapHandler in place of WrapFactory + private static class WrapHandlerProxy extends WrapFactory { + WrapHandler _handler; + + WrapHandlerProxy(WrapHandler handler) { + _handler = handler; + } + + public Object wrap(Context cx, Scriptable scope, + Object obj, Class staticType) + { + if (obj == null) { return obj; } + Object result = _handler.wrap(scope, obj, staticType); + if (result == null) { + result = super.wrap(cx, scope, obj, staticType); + } + return result; + } + + public Scriptable wrapNewObject(Context cx, Scriptable scope, + Object obj) + { + Object wrap = _handler.wrap(scope, obj, obj.getClass()); + if (wrap instanceof Scriptable) { + return (Scriptable)wrap; + } + if (wrap == null) { + return super.wrapNewObject(cx, scope, obj); + } + throw new RuntimeException + ("Please upgrade from WrapHandler to WrapFactory"); + } + } + /** - * Set a WrapHandler for this Context. - *
- * The WrapHandler allows custom object wrapping behavior for - * Java object manipulated with JavaScript. - * @see org.mozilla.javascript.WrapHandler - * @since 1.5 Release 2 + * @deprecated As of Rhino 1.5 Release 4, use + * {@link WrapFactory} and {@link #setWrapHandler(WrapFactory)} */ public void setWrapHandler(WrapHandler wrapHandler) { - this.wrapHandler = wrapHandler; + WrapFactory proxy = (wrapHandler == null) ? null + : new WrapHandlerProxy(wrapHandler); + setWrapFactory(proxy); + } + + /** + * @deprecated As of Rhino 1.5 Release 4, use + * {@link WrapFactory} and {@link #getWrapHandler(WrapFactory)} + */ + public WrapHandler getWrapHandler() { + WrapHandlerProxy proxy = (WrapHandlerProxy)getWrapFactory(); + return (proxy == null) ? null : proxy._handler; + } + + /** + * Set a WrapFactory for this Context. + *
+ * The WrapFactory allows custom object wrapping behavior for + * Java object manipulated with JavaScript. + * @see org.mozilla.javascript.WrapFactory + * @since 1.5 Release 4 + */ + public void setWrapFactory(WrapFactory wrapFactory) { + if (wrapFactory == null) throw new IllegalArgumentException(); + this.wrapFactory = wrapFactory; } /** * Return the current WrapHandler, or null if none is defined. * @see org.mozilla.javascript.WrapHandler - * @since 1.5 Release 2 + * @since 1.5 Release 4 */ - public WrapHandler getWrapHandler() { - return wrapHandler; + public WrapFactory getWrapFactory() { + if (wrapFactory == null) { + wrapFactory = new WrapFactory(); + } + return wrapFactory; } /** @@ -2209,7 +2266,7 @@ public class Context { private boolean generatingSource=true; private boolean compileFunctionsWithDynamicScopeFlag; private int optimizationLevel; - WrapHandler wrapHandler; + private WrapFactory wrapFactory; Debugger debugger; private Object debuggerData; private int enterCount; diff --git a/js/rhino/src/org/mozilla/javascript/JavaMembers.java b/js/rhino/src/org/mozilla/javascript/JavaMembers.java index f66b89befab..7e8883b6267 100644 --- a/js/rhino/src/org/mozilla/javascript/JavaMembers.java +++ b/js/rhino/src/org/mozilla/javascript/JavaMembers.java @@ -217,7 +217,7 @@ class JavaMembers { if (method == null) throw reportMemberNotFound(name); Class[] types = method.getParameterTypes(); - Object[] args = { NativeJavaObject.coerceType(types[0], value, + Object[] args = { NativeJavaObject.coerceType(types[0], value, true) }; method.invoke(javaObject, args); } catch (IllegalAccessException accessEx) { diff --git a/js/rhino/src/org/mozilla/javascript/NativeJavaClass.java b/js/rhino/src/org/mozilla/javascript/NativeJavaClass.java index 32624710bd0..33feb4035a5 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeJavaClass.java +++ b/js/rhino/src/org/mozilla/javascript/NativeJavaClass.java @@ -220,14 +220,9 @@ public class NativeJavaClass extends NativeJavaObject implements Function { for (int i = 0; i < args.length; i++) { args[i] = NativeJavaObject.coerceType(paramTypes[i], args[i], true); } + Object instance; try { - // we need to force this to be wrapped, because construct _has_ - // to return a scriptable - return - (Scriptable) NativeJavaObject.wrap(topLevel, - ctor.newInstance(args), - classObject); - + instance = ctor.newInstance(args); } catch (InstantiationException instEx) { throw Context.reportRuntimeError2( "msg.cant.instantiate", @@ -243,6 +238,9 @@ public class NativeJavaClass extends NativeJavaObject implements Function { throw Context.reportRuntimeError1( "msg.java.internal.private", accessEx.getMessage()); } + // we need to force this to be wrapped, because construct _has_ + // to return a scriptable + return cx.getWrapFactory().wrapNewObject(cx, topLevel, instance); } public String toString() { diff --git a/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java b/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java index 2591782407d..5d5a9976450 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java +++ b/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java @@ -241,7 +241,8 @@ public class NativeJavaMethod extends NativeFunction implements Function { " expect = " + staticType); } - Object wrapped = NativeJavaObject.wrap(scope, retval, staticType); + Object wrapped = cx.getWrapFactory().wrap(cx, scope, + retval, staticType); if (debug) { Class actualType = (wrapped == null) ? null @@ -342,8 +343,8 @@ public class NativeJavaMethod extends NativeFunction implements Function { 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 + // 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 8be7192887d..2f59a825675 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java +++ b/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java @@ -161,16 +161,17 @@ public class NativeJavaObject implements Scriptable, Wrapper, Externalizable { return members.getIds(false); } - public static Object wrap(Scriptable scope, Object obj, Class staticType) + public static Object wrap(Scriptable scope, Object obj, Class staticType) { + + Context cx = Context.getContext(); + return cx.getWrapFactory().wrap(cx, scope, obj, staticType); + } + + static Object defaultWrap(Context cx, Scriptable scope, + Object obj, Class staticType) { if (obj == null) return obj; - Context cx = Context.getCurrentContext(); - if (cx != null && cx.wrapHandler != null) { - Object result = cx.wrapHandler.wrap(scope, obj, staticType); - if (result != null) - return result; - } Class cls = obj.getClass(); if (staticType != null && staticType.isPrimitive()) { if (staticType == Void.TYPE) diff --git a/js/rhino/src/org/mozilla/javascript/WrapFactory.java b/js/rhino/src/org/mozilla/javascript/WrapFactory.java new file mode 100644 index 00000000000..1d8cbd7ecc6 --- /dev/null +++ b/js/rhino/src/org/mozilla/javascript/WrapFactory.java @@ -0,0 +1,84 @@ +/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * The contents of this file are subject to the Netscape Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Rhino code, released + * May 6, 1999. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1997-2000 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Norris Boyd + * + * Alternatively, the contents of this file may be used under the + * terms of the GNU Public License (the "GPL"), in which case the + * provisions of the GPL are applicable instead of those above. + * If you wish to allow use of your version of this file only + * under the terms of the GPL and not to allow others to use your + * version of this file under the NPL, indicate your decision by + * deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete + * the provisions above, a recipient may use your version of this + * file under either the NPL or the GPL. + */ + +// API class + +package org.mozilla.javascript; + +/** + * Embeddings that wish to provide their own custom wrappings for Java + * objects may extend this call and call Context.setWrapFactory. + * XXX + * @see org.mozilla.javascript.Context#setWrapFactory(WrapFactory) + * @since 1.5 Release 4 + */ +public class WrapFactory { + + /** + * Wrap the object. + *
+ * The value returned must be one of + *
- * The value returned must be one of - *
- * If null is returned, the value obj will be wrapped as if - * no WrapHandler had been called. - *