From: <wme...@us...> - 2010-01-30 08:02:25
|
Revision: 6967 http://jython.svn.sourceforge.net/jython/?rev=6967&view=rev Author: wmeissner Date: 2010-01-30 08:02:18 +0000 (Sat, 30 Jan 2010) Log Message: ----------- Add workarounds to make sure jffi.Type does not get loaded by the exposed types processor, so jython still builds, even if there is not libjffi stub lib available for the current arch Modified Paths: -------------- branches/ctypes-jffi/src/org/python/modules/jffi/CType.java branches/ctypes-jffi/src/org/python/modules/jffi/Function.java branches/ctypes-jffi/src/org/python/modules/jffi/StructLayout.java branches/ctypes-jffi/src/org/python/modules/jffi/Util.java Modified: branches/ctypes-jffi/src/org/python/modules/jffi/CType.java =================================================================== --- branches/ctypes-jffi/src/org/python/modules/jffi/CType.java 2010-01-30 07:53:18 UTC (rev 6966) +++ branches/ctypes-jffi/src/org/python/modules/jffi/CType.java 2010-01-30 08:02:18 UTC (rev 6967) @@ -9,11 +9,12 @@ import org.python.core.PyType; import org.python.expose.ExposeAsSuperclass; import org.python.expose.ExposedGet; +import org.python.expose.ExposedMethod; import org.python.expose.ExposedNew; import org.python.expose.ExposedType; @ExposedType(name = "jffi.Type", base = PyObject.class) -public class CType extends PyObject { +public abstract class CType extends PyObject { public static final PyType TYPE = PyType.fromClass(CType.class); static { TYPE.fastGetDict().__setitem__("Array", Array.TYPE); @@ -36,26 +37,13 @@ public static final CType POINTER = primitive(NativeType.POINTER); public static final CType STRING = primitive(NativeType.STRING); - final com.kenai.jffi.Type jffiType; - final NativeType nativeType; - /** Size of this type in bytes */ - @ExposedGet - public final int size; - - /** Minimum alignment of this type in bytes */ - @ExposedGet - public final int alignment; - /** The <tt>MemoryOp</tt> used to read/write items of this type */ private final MemoryOp memoryOp; - CType(NativeType type, com.kenai.jffi.Type jffiType, MemoryOp memoryOp) { + CType(NativeType type, MemoryOp memoryOp) { this.nativeType = type; - this.jffiType = jffiType; - this.size = jffiType.size(); - this.alignment = jffiType.alignment(); this.memoryOp = memoryOp; } @@ -63,35 +51,64 @@ return nativeType; } + abstract com.kenai.jffi.Type jffiType(); + MemoryOp getMemoryOp() { return memoryOp; } - public int alignment() { - return alignment; + public final int alignment() { + return jffiType().alignment(); } - public int size() { - return size; + public final int size() { + return jffiType().size(); } - + + @ExposedMethod(names={"size"}) + public final PyObject pysize() { + return Py.newInteger(size()); + } + + @ExposedMethod(names={"alignment"}) + public final PyObject pyalignment() { + return Py.newInteger(alignment()); + } + static final CType primitive(NativeType type) { - CType t = new Builtin(type, NativeType.jffiType(type)); + CType t = new Builtin(type); CType.TYPE.fastGetDict().__setitem__(type.name(), t); return t; } static final class Builtin extends CType implements ExposeAsSuperclass { - public Builtin(NativeType type, com.kenai.jffi.Type jffiType) { - super(type, jffiType, MemoryOp.getMemoryOp(type)); + public Builtin(NativeType type) { + super(type, MemoryOp.getMemoryOp(type)); } @Override public final String toString() { return "<jffi.Type." + nativeType.name() + ">"; } + + final com.kenai.jffi.Type jffiType() { + return NativeType.jffiType(nativeType); + } } + static abstract class Custom extends CType implements ExposeAsSuperclass { + final com.kenai.jffi.Type jffiType; + + public Custom(NativeType type, com.kenai.jffi.Type jffiType, MemoryOp op) { + super(type, op); + this.jffiType = jffiType; + } + + final com.kenai.jffi.Type jffiType() { + return jffiType; + } + } + static CType typeOf(PyObject obj) { if (obj instanceof CType) { return (CType) obj; @@ -107,17 +124,17 @@ } @ExposedType(name = "jffi.Type.Array", base = CType.class) - static final class Array extends CType { + static final class Array extends CType.Custom { public static final PyType TYPE = PyType.fromClass(Array.class); final CType componentType; final PyType pyComponentType; final MemoryOp componentMemoryOp; - + @ExposedGet public final int length; public Array(PyType pyComponentType, CType componentType, int length) { - super(NativeType.ARRAY, new com.kenai.jffi.Array(componentType.jffiType, length), null); + super(NativeType.ARRAY, new com.kenai.jffi.Array(Util.jffiType(componentType), length), null); this.pyComponentType = pyComponentType; this.componentType = componentType; this.componentMemoryOp = getComponentMemoryOp((PyType) pyComponentType, componentType); @@ -163,7 +180,7 @@ @ExposedType(name = "jffi.Type.Pointer", base = CType.class) - final static class Pointer extends CType { + final static class Pointer extends Custom { public static final PyType TYPE = PyType.fromClass(Pointer.class); private static final ConcurrentMap<PyObject, Pointer> typeCache = new ConcurrentHashMap<PyObject, Pointer>(); Modified: branches/ctypes-jffi/src/org/python/modules/jffi/Function.java =================================================================== --- branches/ctypes-jffi/src/org/python/modules/jffi/Function.java 2010-01-30 07:53:18 UTC (rev 6966) +++ branches/ctypes-jffi/src/org/python/modules/jffi/Function.java 2010-01-30 08:02:18 UTC (rev 6967) @@ -162,10 +162,10 @@ throw Py.NotImplementedError("variadic functions not supported yet; specify a parameter list"); } - com.kenai.jffi.Type jffiReturnType = CType.typeOf(restype).jffiType; + com.kenai.jffi.Type jffiReturnType = Util.jffiType(CType.typeOf(restype)); com.kenai.jffi.Type[] jffiParamTypes = new com.kenai.jffi.Type[argtypes.length]; for (int i = 0; i < jffiParamTypes.length; ++i) { - jffiParamTypes[i] = CType.typeOf(argtypes[i]).jffiType; + jffiParamTypes[i] = Util.jffiType(CType.typeOf(argtypes[i])); } com.kenai.jffi.Function jffiFunction = new com.kenai.jffi.Function(getMemory().getAddress(), jffiReturnType, jffiParamTypes); Modified: branches/ctypes-jffi/src/org/python/modules/jffi/StructLayout.java =================================================================== --- branches/ctypes-jffi/src/org/python/modules/jffi/StructLayout.java 2010-01-30 07:53:18 UTC (rev 6966) +++ branches/ctypes-jffi/src/org/python/modules/jffi/StructLayout.java 2010-01-30 08:02:18 UTC (rev 6967) @@ -18,7 +18,7 @@ import org.python.expose.ExposedType; @ExposedType(name = "jffi.StructLayout", base = CType.class) -public class StructLayout extends CType { +public class StructLayout extends CType.Custom { public static final PyType TYPE = PyType.fromClass(StructLayout.class); static { TYPE.fastGetDict().__setitem__("Field", Field.TYPE); @@ -106,6 +106,28 @@ } } + /** + * We enclose any references to the jffi Type class in a lazily-loaded class + * so the exposed types processor does not crash when jffi can't load the + * native stub lib. + */ + private static final class StructUtil { + + public static final StructLayout newStructLayout(Field[] fields, boolean isUnion) { + com.kenai.jffi.Type[] fieldTypes = new com.kenai.jffi.Type[fields.length]; + + for (int i = 0; i < fields.length; ++i) { + fieldTypes[i] = Util.jffiType(fields[i].ctype); + } + + com.kenai.jffi.Type jffiType = isUnion + ? new com.kenai.jffi.Union(fieldTypes) + : new com.kenai.jffi.Struct(fieldTypes); + + return new StructLayout(fields, jffiType, MemoryOp.INVALID); + } + } + @ExposedNew public static PyObject StructLayout_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) { @@ -117,7 +139,6 @@ } PyList pyFields = (PyList) ap.getPyObject(0); - com.kenai.jffi.Type[] fieldTypes = new com.kenai.jffi.Type[pyFields.size()]; Field[] fields = new Field[pyFields.size()]; for (int i = 0; i < fields.length; ++i) { @@ -125,16 +146,11 @@ if (!(pyField instanceof Field)) { throw Py.TypeError(String.format("element %d of field list is not an instance of jffi.StructLayout.Field", i)); } - Field f = (Field) pyField; - fields[i] = f; - fieldTypes[i] = f.ctype.jffiType; + + fields[i] = (Field) pyField; } - com.kenai.jffi.Type jffiType = ap.getPyObject(1, Py.False).__nonzero__() - ? new com.kenai.jffi.Union(fieldTypes) - : new com.kenai.jffi.Struct(fieldTypes); - - return new StructLayout(fields, jffiType, MemoryOp.INVALID); + return StructUtil.newStructLayout(fields, ap.getPyObject(1, Py.False).__nonzero__()); } @ExposedType(name = "jffi.StructLayout.ScalarField", base = Field.class) Modified: branches/ctypes-jffi/src/org/python/modules/jffi/Util.java =================================================================== --- branches/ctypes-jffi/src/org/python/modules/jffi/Util.java 2010-01-30 07:53:18 UTC (rev 6966) +++ branches/ctypes-jffi/src/org/python/modules/jffi/Util.java 2010-01-30 08:02:18 UTC (rev 6967) @@ -2,6 +2,8 @@ package org.python.modules.jffi; import java.math.BigInteger; +import java.util.HashMap; +import java.util.Map; import org.python.core.Py; import org.python.core.PyInteger; import org.python.core.PyLong; @@ -140,4 +142,8 @@ } throw Py.TypeError("invalid address"); } + + static final com.kenai.jffi.Type jffiType(CType type) { + return (com.kenai.jffi.Type) type.jffiType(); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |