[armedbear-cvs] r11691 - trunk/abcl/src/org/armedbear/lisp
Erik Huelsmann
ehuelsmann at common-lisp.net
Sun Mar 1 20:11:45 UTC 2009
Author: ehuelsmann
Date: Sun Mar 1 20:11:40 2009
New Revision: 11691
Log:
Don't box values which have well-defined meanings within the Lisp system (ie LispObject-s)
when returning from JCALL/JCALL-RAW.
Essentially, this moves Java.java::makeLispObject() to JavaObject.java::getInstance();
partially because I discovered makeLispObject() very late in the game. This change
however, makes for more efficient boxing/unboxing in a change-to-come.
Modified:
trunk/abcl/src/org/armedbear/lisp/Java.java
trunk/abcl/src/org/armedbear/lisp/JavaObject.java
Modified: trunk/abcl/src/org/armedbear/lisp/Java.java
==============================================================================
--- trunk/abcl/src/org/armedbear/lisp/Java.java (original)
+++ trunk/abcl/src/org/armedbear/lisp/Java.java Sun Mar 1 20:11:40 2009
@@ -112,7 +112,7 @@
@Override
public LispObject execute(LispObject arg) throws ConditionThrowable
{
- return new JavaObject(javaClass(arg));
+ return JavaObject.getInstance(javaClass(arg));
}
};
@@ -151,7 +151,7 @@
@Override
public LispObject execute(LispObject[] args) throws ConditionThrowable
{
- return makeLispObject((JFIELD_RAW.execute(args)).javaInstance());
+ return JavaObject.getInstance((JFIELD_RAW.execute(args)).javaInstance(), true);
}
};
@@ -214,7 +214,7 @@
f.set(instance,args[3].javaInstance(fieldType));
return args[3];
}
- return new JavaObject(f.get(instance));
+ return JavaObject.getInstance(f.get(instance));
}
catch (NoSuchFieldException e) {
error(new LispError("no such field"));
@@ -256,14 +256,14 @@
for (int i = 1; i < args.length; i++) {
parameterTypes[i-1] = javaClass(args[i]);
}
- return new JavaObject(c.getConstructor(parameterTypes));
+ return JavaObject.getInstance(c.getConstructor(parameterTypes));
}
// Parameter types not explicitly specified.
Constructor[] constructors = c.getConstructors();
for (int i = 0; i < constructors.length; i++) {
Constructor constructor = constructors[i];
if (constructor.getParameterTypes().length == argCount)
- return new JavaObject(constructor);
+ return JavaObject.getInstance(constructor);
}
throw new NoSuchMethodException();
}
@@ -301,8 +301,8 @@
Class[] parameterTypes = new Class[args.length-2];
for (int i = 2; i < args.length; i++)
parameterTypes[i-2] = javaClass(args[i]);
- return new JavaObject(c.getMethod(methodName,
- parameterTypes));
+ return JavaObject.getInstance(c.getMethod(methodName,
+ parameterTypes));
}
// Parameter types were not explicitly specified.
Method[] methods = c.getMethods();
@@ -310,7 +310,7 @@
Method method = methods[i];
if (method.getName().equals(methodName) &&
method.getParameterTypes().length == argCount)
- return new JavaObject(method);
+ return JavaObject.getInstance(method);
}
throw new NoSuchMethodException();
}
@@ -346,7 +346,7 @@
@Override
public LispObject execute(LispObject[] args) throws ConditionThrowable
{
- return makeLispObject((JSTATIC_RAW.execute(args)).javaInstance());
+ return JavaObject.getInstance((JSTATIC_RAW.execute(args)).javaInstance(), true);
}
};
@@ -398,7 +398,7 @@
methodArgs[i-2] = arg.javaInstance(argTypes[i-2]);
}
Object result = m.invoke(null, methodArgs);
- return new JavaObject(result);
+ return JavaObject.getInstance(result);
}
catch (Throwable t) {
if (t instanceof InvocationTargetException)
@@ -410,7 +410,7 @@
Symbol.SIGNAL.execute(
condition,
Keyword.CAUSE,
- new JavaObject(t),
+ JavaObject.getInstance(t),
Keyword.FORMAT_CONTROL,
new SimpleString(getMessage(t)));
}
@@ -441,7 +441,7 @@
initargs[i-1] = arg.javaInstance(argTypes[i-1]);
}
}
- return new JavaObject(constructor.newInstance(initargs));
+ return JavaObject.getInstance(constructor.newInstance(initargs));
}
catch (Throwable t) {
if (t instanceof InvocationTargetException)
@@ -453,7 +453,7 @@
Symbol.SIGNAL.execute(
condition,
Keyword.CAUSE,
- new JavaObject(t),
+ JavaObject.getInstance(t),
Keyword.FORMAT_CONTROL,
new SimpleString(getMessage(t)));
}
@@ -477,7 +477,7 @@
int[] dimensions = new int[args.length - 1];
for (int i = 1; i < args.length; i++)
dimensions[i-1] = ((Integer)args[i].javaInstance()).intValue();
- return new JavaObject(Array.newInstance(c, dimensions));
+ return JavaObject.getInstance(Array.newInstance(c, dimensions));
}
catch (Throwable t) {
error(new JavaException(t));
@@ -495,7 +495,7 @@
@Override
public LispObject execute(LispObject[] args) throws ConditionThrowable
{
- return makeLispObject((JARRAY_REF_RAW.execute(args)).javaInstance());
+ return JavaObject.getInstance((JARRAY_REF_RAW.execute(args)).javaInstance(), true);
}
};
@@ -513,7 +513,7 @@
Object a = args[0].javaInstance();
for (int i = 1; i<args.length - 1; i++)
a = Array.get(a, ((Integer)args[i].javaInstance()).intValue());
- return new JavaObject(Array.get(a, ((Integer)args[args.length - 1].javaInstance()).intValue()));
+ return JavaObject.getInstance(Array.get(a, ((Integer)args[args.length - 1].javaInstance()).intValue()));
}
catch (Throwable t) {
Symbol condition = getCondition(t.getClass());
@@ -523,7 +523,7 @@
Symbol.SIGNAL.execute(
condition,
Keyword.CAUSE,
- new JavaObject(t),
+ JavaObject.getInstance(t),
Keyword.FORMAT_CONTROL,
new SimpleString(getMessage(t)));
}
@@ -558,7 +558,7 @@
Symbol.SIGNAL.execute(
condition,
Keyword.CAUSE,
- new JavaObject(t),
+ JavaObject.getInstance(t),
Keyword.FORMAT_CONTROL,
new SimpleString(getMessage(t)));
}
@@ -577,7 +577,7 @@
{
if (args.length < 2)
error(new WrongNumberOfArgumentsException(this));
- return makeLispObject(jcall(args));
+ return JavaObject.getInstance(jcall(args), true);
}
};
@@ -592,7 +592,7 @@
{
if (args.length < 2)
error(new WrongNumberOfArgumentsException(this));
- return new JavaObject(jcall(args));
+ return JavaObject.getInstance(jcall(args));
}
};
@@ -645,7 +645,7 @@
Symbol.SIGNAL.execute(
condition,
Keyword.CAUSE,
- new JavaObject(t),
+ JavaObject.getInstance(t),
Keyword.FORMAT_CONTROL,
new SimpleString(getMessage(t)));
}
@@ -683,19 +683,19 @@
LispObject type = args[1];
if (type == Keyword.BOOLEAN) {
if (object == NIL)
- return new JavaObject(Boolean.FALSE);
+ return JavaObject.getInstance(Boolean.FALSE);
else
- return new JavaObject(Boolean.TRUE);
+ return JavaObject.getInstance(Boolean.TRUE);
}
if (type == Keyword.REF) {
if (object == NIL)
- return new JavaObject(null);
+ return JavaObject.getInstance(null);
else
throw new Error();
}
// other special cases come here
}
- return new JavaObject(object.javaInstance());
+ return JavaObject.getInstance(object.javaInstance());
}
catch (Throwable t) {
error(new LispError("MAKE-IMMEDIATE-OBJECT: not implemented"));
@@ -723,7 +723,7 @@
@Override
public LispObject execute(LispObject arg) throws ConditionThrowable
{
- return makeLispObject(arg.javaInstance());
+ return JavaObject.getInstance(arg.javaInstance(), true);
}
};
@@ -739,7 +739,7 @@
if(value instanceof LispObject) {
return (LispObject) value;
} else if(value != null) {
- return new JavaObject(value);
+ return JavaObject.getInstance(value);
} else {
return NIL;
}
@@ -853,41 +853,6 @@
}
}
- private static final LispObject makeLispObject(Object obj)
- throws ConditionThrowable
- {
- if (obj == null)
- return NIL;
- if (obj instanceof Boolean)
- return ((Boolean)obj).booleanValue() ? T : NIL;
- if (obj instanceof Byte)
- return new Fixnum(((Byte)obj).intValue());
- if (obj instanceof Integer)
- return new Fixnum(((Integer)obj).intValue());
- if (obj instanceof Short)
- return new Fixnum(((Short)obj).shortValue());
- if (obj instanceof Long)
- return new Bignum(((Long)obj).longValue());
- if (obj instanceof Float)
- return new SingleFloat(((Float)obj).floatValue());
- if (obj instanceof Double)
- return new DoubleFloat(((Double)obj).doubleValue());
- if (obj instanceof String)
- return new SimpleString((String)obj);
- if (obj instanceof Character)
- return LispCharacter.getInstance(((Character)obj).charValue());
- if (obj instanceof Object[]) {
- Object[] array = (Object[]) obj;
- SimpleVector v = new SimpleVector(array.length);
- for (int i = array.length; i-- > 0;)
- v.aset(i, new JavaObject(array[i]));
- return v;
- }
- if (obj instanceof LispObject)
- return (LispObject) obj;
- return new JavaObject(obj);
- }
-
private static final String getMessage(Throwable t)
{
String message = t.getMessage();
Modified: trunk/abcl/src/org/armedbear/lisp/JavaObject.java
==============================================================================
--- trunk/abcl/src/org/armedbear/lisp/JavaObject.java (original)
+++ trunk/abcl/src/org/armedbear/lisp/JavaObject.java Sun Mar 1 20:11:40 2009
@@ -33,6 +33,8 @@
package org.armedbear.lisp;
+import java.math.BigInteger;
+
public final class JavaObject extends LispObject
{
private final Object obj;
@@ -76,6 +78,92 @@
return obj;
}
+ /** Encapsulates obj, if required.
+ * If obj is a {@link LispObject}, it's returned as-is.
+ *
+ * @param obj Any java object
+ * @return obj or a new JavaObject encapsulating obj
+ */
+ public final static LispObject getInstance(Object obj) {
+ if (obj == null)
+ return new JavaObject(null);
+
+ if (obj instanceof LispObject)
+ return (LispObject)obj;
+
+ return new JavaObject(obj);
+ }
+
+ /** Encapsulates obj, if required.
+ * If obj is a {@link LispObject}, it's returned as-is.
+ * If obj is of a type which can be mapped to a lisp type,
+ * an object of the mapped type is returned, if translated is true.
+ *
+ * @param obj
+ * @param translated
+ * @return a LispObject representing or encapsulating obj
+ */
+ public final static LispObject getInstance(Object obj, boolean translated)
+ throws ConditionThrowable
+ {
+ if (! translated)
+ return getInstance(obj);
+
+ if (obj == null) return NIL;
+
+ if (obj instanceof LispObject)
+ return (LispObject)obj;
+
+ if (obj instanceof String)
+ return new SimpleString((String)obj);
+
+ if (obj instanceof Number) {
+ // Number types ordered according to decreasing
+ // estimated chances of occurrance
+
+ if (obj instanceof Integer)
+ return Fixnum.getInstance(((Integer)obj).intValue());
+
+ if (obj instanceof Float)
+ return new SingleFloat((Float)obj);
+
+ if (obj instanceof Double)
+ return new DoubleFloat((Double)obj);
+
+ if (obj instanceof Long)
+ return LispInteger.getInstance(((Long)obj).longValue());
+
+ if (obj instanceof BigInteger)
+ return new Bignum((BigInteger)obj);
+
+ if (obj instanceof Short)
+ return Fixnum.getInstance(((Short)obj).shortValue());
+
+ if (obj instanceof Byte)
+ return Fixnum.getInstance(((Byte)obj).byteValue());
+ // We don't handle BigDecimal: it doesn't map to a Lisp type
+ }
+
+ if (obj instanceof Boolean)
+ return ((Boolean)obj).booleanValue() ? T : NIL;
+
+ if (obj instanceof Character)
+ return new LispCharacter((Character)obj);
+
+ if (obj instanceof Object[]) {
+ Object[] array = (Object[]) obj;
+ SimpleVector v = new SimpleVector(array.length);
+ for (int i = array.length; i-- > 0;)
+ v.aset(i, JavaObject.getInstance(array[i]));
+ return v;
+ }
+ // TODO
+ // We might want to handle:
+ // - streams
+ // - others?
+ return new JavaObject(obj);
+ }
+
@Override
public Object javaInstance()
{
More information about the armedbear-cvs
mailing list