aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/com
diff options
context:
space:
mode:
authorMichael Bien <[email protected]>2010-02-12 02:22:24 +0100
committerMichael Bien <[email protected]>2010-02-12 02:22:24 +0100
commit0fa706b4eef533ead671310a9a7e063a910198cb (patch)
treeb02b31aa6215c7b2f627ae785d4bdaf9242e0e70 /src/java/com
parentdd7facc1dd5f6a34d2c4542b7435995c4d2d25b1 (diff)
added support for arrays in generated StructAccessors.
Specifying JavaClass or JavaImplClass is no longer a requirement. It is now possible to generate StructAccessors from c headers without emitting any other binding code (force with EmitStruct <name>).
Diffstat (limited to 'src/java/com')
-rw-r--r--src/java/com/sun/gluegen/JavaConfiguration.java6
-rw-r--r--src/java/com/sun/gluegen/JavaEmitter.java127
-rwxr-xr-xsrc/java/com/sun/gluegen/runtime/StructAccessor.java.javame_cdc_fp39
-rwxr-xr-xsrc/java/com/sun/gluegen/runtime/StructAccessor.java.javase65
4 files changed, 198 insertions, 39 deletions
diff --git a/src/java/com/sun/gluegen/JavaConfiguration.java b/src/java/com/sun/gluegen/JavaConfiguration.java
index 0d7b3cc..d5e71e2 100644
--- a/src/java/com/sun/gluegen/JavaConfiguration.java
+++ b/src/java/com/sun/gluegen/JavaConfiguration.java
@@ -214,7 +214,7 @@ public class JavaConfiguration {
}
if (className == null && (emissionStyle() != JavaEmitter.IMPL_ONLY)) {
- throw new RuntimeException("Output class name was not specified in configuration file \"" + filename + "\"");
+// throw new RuntimeException("Output class name was not specified in configuration file \"" + filename + "\"");
}
if (packageName == null && (emissionStyle() != JavaEmitter.IMPL_ONLY)) {
throw new RuntimeException("Output package name was not specified in configuration file \"" + filename + "\"");
@@ -268,6 +268,10 @@ public class JavaConfiguration {
return implClassName;
}
+ public boolean structsOnly() {
+ return className == null && implClassName == null;
+ }
+
/** Returns the Java code output directory parsed from the configuration file. */
public String javaOutputDir() {
return (null != outputRootDir) ? (outputRootDir + "/" + javaOutputDir) : javaOutputDir;
diff --git a/src/java/com/sun/gluegen/JavaEmitter.java b/src/java/com/sun/gluegen/JavaEmitter.java
index 79f2b4d..1371112 100644
--- a/src/java/com/sun/gluegen/JavaEmitter.java
+++ b/src/java/com/sun/gluegen/JavaEmitter.java
@@ -132,36 +132,40 @@ public class JavaEmitter implements GlueEmitter {
}
}
- public void beginEmission(GlueEmitterControls controls) throws IOException {
- try {
- openWriters();
- } catch (Exception e) {
- throw new RuntimeException("Unable to open files for writing", e);
- }
+ public void beginEmission(GlueEmitterControls controls) throws IOException {
- emitAllFileHeaders();
+ // Request emission of any structs requested
+ for (String structs : cfg.forcedStructs()) {
+ controls.forceStructEmission(structs);
+ }
- // Request emission of any structs requested
- for (String structs : cfg.forcedStructs()) {
- controls.forceStructEmission(structs);
- }
+ if (!cfg.structsOnly()) {
+ try {
+ openWriters();
+ } catch (Exception e) {
+ throw new RuntimeException("Unable to open files for writing", e);
+ }
+ emitAllFileHeaders();
- // Handle renaming of constants
- controls.runSymbolFilter(new ConstantRenamer());
- }
+ // Handle renaming of constants
+ controls.runSymbolFilter(new ConstantRenamer());
+ }
+ }
- public void endEmission() {
- emitAllFileFooters();
+ public void endEmission() {
+ if (!cfg.structsOnly()) {
+ emitAllFileFooters();
- try {
- closeWriters();
- } catch (Exception e) {
- throw new RuntimeException("Unable to close open files", e);
+ try {
+ closeWriters();
+ } catch (Exception e) {
+ throw new RuntimeException("Unable to close open files", e);
+ }
+ }
}
- }
public void beginDefines() throws Exception {
- if (cfg.allStatic() || cfg.emitInterface()) {
+ if ((cfg.allStatic() || cfg.emitInterface()) && !cfg.structsOnly()) {
javaWriter().println();
}
}
@@ -362,10 +366,12 @@ public class JavaEmitter implements GlueEmitter {
public void beginFunctions(TypeDictionary typedefDictionary,
TypeDictionary structDictionary,
Map<Type, Type> canonMap) throws Exception {
+
this.typedefDictionary = typedefDictionary;
this.structDictionary = structDictionary;
this.canonMap = canonMap;
- if (cfg.allStatic() || cfg.emitInterface()) {
+
+ if ((cfg.allStatic() || cfg.emitInterface()) && !cfg.structsOnly()) {
javaWriter().println();
}
}
@@ -778,13 +784,14 @@ public class JavaEmitter implements GlueEmitter {
}
- public void endFunctions() throws Exception
- {
- if (cfg.allStatic() || cfg.emitInterface()) {
- emitCustomJavaCode(javaWriter(), cfg.className());
- }
- if (!cfg.allStatic() && cfg.emitImpl()) {
- emitCustomJavaCode(javaImplWriter(), cfg.implClassName());
+ public void endFunctions() throws Exception {
+ if (!cfg.structsOnly()) {
+ if (cfg.allStatic() || cfg.emitInterface()) {
+ emitCustomJavaCode(javaWriter(), cfg.className());
+ }
+ if (!cfg.allStatic() && cfg.emitImpl()) {
+ emitCustomJavaCode(javaImplWriter(), cfg.implClassName());
+ }
}
}
@@ -1069,7 +1076,7 @@ public class JavaEmitter implements GlueEmitter {
}
writer.println();
- writer.print(" public " + (doBaseClass ? "abstract " : "") + fieldType.getName() + " get" + capitalizeString(fieldName) + "()");
+ generateGetterSignature(writer, doBaseClass, fieldType.getName(), capitalizeString(fieldName));
if (doBaseClass) {
writer.println(";");
} else {
@@ -1078,12 +1085,46 @@ public class JavaEmitter implements GlueEmitter {
field.getOffset(intMachDesc) + ", " + fieldType.getSize(intMachDesc) + "));");
writer.println(" }");
}
- // FIXME: add setter by autogenerating "copyTo" for all compound type wrappers
+
} else if (fieldType.isArray()) {
- if (!doBaseClass) {
- System.err.println("WARNING: Array fields (field \"" + field + "\" of type \"" + name +
- "\") not implemented yet");
- }
+
+ Type baseElementType = field.getType().asArray().getBaseElementType();
+
+ if(!baseElementType.isPrimitive())
+ break;
+
+ String paramType = typeToJavaType(baseElementType, false, extMachDesc).getName();
+ String capitalized = capitalizeString(fieldName);
+
+ int slot = -1;
+ if(!doBaseClass) {
+ slot = slot(fieldType, (int) field.getOffset(intMachDesc), intMachDesc);
+ }
+
+ // Setter
+ writer.println();
+ generateSetterSignature(writer, doBaseClass, containingTypeName, capitalized, paramType+"[]");
+ if (doBaseClass) {
+ writer.println(";");
+ } else {
+ writer.println(" {");
+ writer.print (" accessor.set" + capitalizeString(paramType) + "sAt(" + slot + ", ");
+ writer.println("val);");
+ writer.println(" return this;");
+ writer.println(" }");
+ }
+ writer.println();
+ // Getter
+ generateGetterSignature(writer, doBaseClass, paramType+"[]", capitalized);
+ if (doBaseClass) {
+ writer.println(";");
+ } else {
+ writer.println(" {");
+ writer.print (" return ");
+ writer.println("accessor.get" + capitalizeString(paramType) + "sAt(" + slot + ", new " +paramType+"["+fieldType.asArray().getLength()+"]);");
+ writer.println(" }");
+ }
+
} else {
JavaType internalJavaType = null;
JavaType externalJavaType = null;
@@ -1124,7 +1165,7 @@ public class JavaEmitter implements GlueEmitter {
writer.println();
String capitalizedFieldName = capitalizeString(fieldName);
// Setter
- writer.print(" public " + (doBaseClass ? "abstract " : "") + containingTypeName + " set" + capitalizedFieldName + "(" + externalJavaTypeName + " val)");
+ generateSetterSignature(writer, doBaseClass, containingTypeName, capitalizedFieldName, externalJavaTypeName);
if (doBaseClass) {
writer.println(";");
} else {
@@ -1139,7 +1180,7 @@ public class JavaEmitter implements GlueEmitter {
}
writer.println();
// Getter
- writer.print(" public " + (doBaseClass ? "abstract " : "") + externalJavaTypeName + " get" + capitalizedFieldName + "()");
+ generateGetterSignature(writer, doBaseClass, externalJavaTypeName, capitalizedFieldName);
if (doBaseClass) {
writer.println(";");
} else {
@@ -1200,6 +1241,14 @@ public class JavaEmitter implements GlueEmitter {
// Internals only below this point
//
+ private void generateGetterSignature(PrintWriter writer, boolean baseClass, String returnTypeName, String capitalizedFieldName) {
+ writer.print(" public " + (baseClass ? "abstract " : "") + returnTypeName + " get" + capitalizedFieldName + "()");
+ }
+
+ private void generateSetterSignature(PrintWriter writer, boolean baseClass, String returnTypeName, String capitalizedFieldName, String paramTypeName) {
+ writer.print(" public " + (baseClass ? "abstract " : "") + returnTypeName + " set" + capitalizedFieldName + "(" + paramTypeName + " val)");
+ }
+
private JavaType typeToJavaType(Type cType, boolean outgoingArgument, MachineDescription curMachDesc) {
// Recognize JNIEnv* case up front
PointerType opt = cType.asPointer();
@@ -1372,6 +1421,8 @@ public class JavaEmitter implements GlueEmitter {
return byteOffset / 8;
} else if (t.isPointer()) {
return byteOffset / curMachDesc.pointerSizeInBytes();
+ } else if (t.isArray()) {
+ return slot(t.asArray().getBaseElementType(), byteOffset, curMachDesc);
} else {
throw new RuntimeException("Illegal type " + t);
}
diff --git a/src/java/com/sun/gluegen/runtime/StructAccessor.java.javame_cdc_fp b/src/java/com/sun/gluegen/runtime/StructAccessor.java.javame_cdc_fp
index d670bcc..d97d2a0 100755
--- a/src/java/com/sun/gluegen/runtime/StructAccessor.java.javame_cdc_fp
+++ b/src/java/com/sun/gluegen/runtime/StructAccessor.java.javame_cdc_fp
@@ -98,6 +98,45 @@ public class StructAccessor {
intBuffer().put(slot, v);
}
+ public void setBytesAt(int slot, byte[] v) {
+ for (int i = 0; i < v.length; i++) {
+ bb.put(slot++, v[i]);
+ }
+ }
+
+ public byte[] getBytesAt(int slot, byte[] v) {
+ for (int i = 0; i < v.length; i++) {
+ v[i] = bb.get(slot++);
+ }
+ return v;
+ }
+
+ public void setIntsAt(int slot, int[] v) {
+ for (int i = 0; i < v.length; i++) {
+ intBuffer().put(slot++, v[i]);
+ }
+ }
+
+ public int[] getIntsAt(int slot, int[] v) {
+ for (int i = 0; i < v.length; i++) {
+ v[i] = intBuffer().get(slot++);
+ }
+ return v;
+ }
+
+ public void setFloatsAt(int slot, float[] v) {
+ for (int i = 0; i < v.length; i++) {
+ floatBuffer().put(slot++, v[i]);
+ }
+ }
+
+ public float[] getFloatsAt(int slot, float[] v) {
+ for (int i = 0; i < v.length; i++) {
+ v[i] = floatBuffer().get(slot++);
+ }
+ return v;
+ }
+
/** Retrieves the long at the specified slot (8-byte offset). */
public long getLongAt(int slot) {
slot = slot << 1 ; // 8-byte to 4-byte offset
diff --git a/src/java/com/sun/gluegen/runtime/StructAccessor.java.javase b/src/java/com/sun/gluegen/runtime/StructAccessor.java.javase
index 5a0fa5d..9113859 100755
--- a/src/java/com/sun/gluegen/runtime/StructAccessor.java.javase
+++ b/src/java/com/sun/gluegen/runtime/StructAccessor.java.javase
@@ -143,6 +143,71 @@ public class StructAccessor {
shortBuffer().put(slot, v);
}
+ public void setBytesAt(int slot, byte[] v) {
+ for (int i = 0; i < v.length; i++) {
+ bb.put(slot++, v[i]);
+ }
+ }
+
+ public byte[] getBytesAt(int slot, byte[] v) {
+ for (int i = 0; i < v.length; i++) {
+ v[i] = bb.get(slot++);
+ }
+ return v;
+ }
+
+ public void setCharsAt(int slot, char[] v) {
+ for (int i = 0; i < v.length; i++) {
+ charBuffer().put(slot++, v[i]);
+ }
+ }
+
+ public char[] getCharsAt(int slot, char[] v) {
+ for (int i = 0; i < v.length; i++) {
+ v[i] = charBuffer().get(slot++);
+ }
+ return v;
+ }
+
+ public void setIntsAt(int slot, int[] v) {
+ for (int i = 0; i < v.length; i++) {
+ intBuffer().put(slot++, v[i]);
+ }
+ }
+
+ public int[] getIntsAt(int slot, int[] v) {
+ for (int i = 0; i < v.length; i++) {
+ v[i] = intBuffer().get(slot++);
+ }
+ return v;
+ }
+
+ public void setFloatsAt(int slot, float[] v) {
+ for (int i = 0; i < v.length; i++) {
+ floatBuffer().put(slot++, v[i]);
+ }
+ }
+
+ public float[] getFloatsAt(int slot, float[] v) {
+ for (int i = 0; i < v.length; i++) {
+ v[i] = floatBuffer().get(slot++);
+ }
+ return v;
+ }
+
+ public void setDoublesAt(int slot, double[] v) {
+ for (int i = 0; i < v.length; i++) {
+ doubleBuffer().put(slot++, v[i]);
+ }
+ }
+
+ public double[] getDoublesAt(int slot, double[] v) {
+ for (int i = 0; i < v.length; i++) {
+ v[i] = doubleBuffer().get(slot++);
+ }
+ return v;
+ }
+
//----------------------------------------------------------------------
// Internals only below this point
//