Changing NativeJavaClass.construct and NativeJavaMethod.call not to modify passed argument array with unwrapped values. Instead a new array is allocated if any unwrapped value would be different from the original JS argument.

This commit is contained in:
igor%mir2.org 2003-04-24 17:13:57 +00:00
Родитель 6e1d160a48
Коммит f87766bec4
2 изменённых файлов: 38 добавлений и 16 удалений

Просмотреть файл

@ -173,6 +173,7 @@ public class NativeJavaClass extends NativeJavaObject implements Function {
Modifier.isAbstract(modifiers))) Modifier.isAbstract(modifiers)))
{ {
Constructor[] ctors = members.getConstructors(); Constructor[] ctors = members.getConstructors();
args = NativeJavaMethod.unwrapArgs(args);
Member member = NativeJavaMethod.findFunction(ctors, args); Member member = NativeJavaMethod.findFunction(ctors, args);
Constructor ctor = (Constructor) member; Constructor ctor = (Constructor) member;
if (ctor == null) { if (ctor == null) {

Просмотреть файл

@ -189,7 +189,8 @@ public class NativeJavaMethod extends NativeFunction implements Function {
if (methods.length == 0) { if (methods.length == 0) {
throw new RuntimeException("No methods defined for call"); throw new RuntimeException("No methods defined for call");
} }
Object[] origArgs = args;
args = unwrapArgs(args);
Method meth = (Method) findFunction(methods, args); Method meth = (Method) findFunction(methods, args);
if (meth == null) { if (meth == null) {
Class c = methods[0].getDeclaringClass(); Class c = methods[0].getDeclaringClass();
@ -204,7 +205,15 @@ public class NativeJavaMethod extends NativeFunction implements Function {
// First, we marshall the args. // First, we marshall the args.
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
args[i] = NativeJavaObject.coerceType(paramTypes[i], args[i], true); Object arg = args[i];
Object coerced = NativeJavaObject.coerceType(paramTypes[i], arg,
true);
if (coerced != arg) {
if (origArgs == args) {
args = (Object[])args.clone();
}
args[i] = coerced;
}
} }
Object javaObject; Object javaObject;
if (Modifier.isStatic(meth.getModifiers())) { if (Modifier.isStatic(meth.getModifiers())) {
@ -311,6 +320,32 @@ public class NativeJavaMethod extends NativeFunction implements Function {
throw illegalAccess; throw illegalAccess;
} }
/**
* If args arrray contains instances of {@link Wrapper}, return a new array
* with all such objects unwrapped, otherwise return the original array.
*/
static Object[] unwrapArgs(Object[] args)
{
Object[] result = args;
for (int i = 0, N = args.length; i != N; ++i) {
Object arg = args[i];
if (arg instanceof Wrapper) {
Object unwrapped = ((Wrapper)arg).unwrap();
if (!(unwrapped 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.
if (result == args) {
result = (Object[])args.clone();
}
result[i] = unwrapped;
}
}
}
return result;
}
/** /**
* Find the correct function to call given the set of methods * Find the correct function to call given the set of methods
* or constructors and the arguments. * or constructors and the arguments.
@ -320,20 +355,6 @@ public class NativeJavaMethod extends NativeFunction implements Function {
if (methodsOrCtors.length == 0) if (methodsOrCtors.length == 0)
return null; return null;
boolean hasMethods = methodsOrCtors[0] instanceof Method; boolean hasMethods = methodsOrCtors[0] instanceof Method;
// 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;
}
}
}
Member bestFit = null; Member bestFit = null;
Class[] bestFitTypes = null; Class[] bestFitTypes = null;