From 9843e983a4fc71a64eb3de9cb364a1f4ffa56b3a Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 17 Jun 2014 23:05:42 +0200 Subject: Fix commit f4e753ff1f39be381307ffdb0fb6bb7a2d323eff: 'compound array call-by-value' JavaMethodBindingEmitter, array may still require NIO handling, also consider NIO array length. --- .../jogamp/gluegen/JavaMethodBindingEmitter.java | 102 ++++++++-------- .../gluegen/test/junit/generation/BaseClass.java | 132 +++++++++++++-------- .../jogamp/gluegen/test/junit/generation/test1.c | 32 +++++ .../jogamp/gluegen/test/junit/generation/test1.h | 7 ++ 4 files changed, 176 insertions(+), 97 deletions(-) diff --git a/src/java/com/jogamp/gluegen/JavaMethodBindingEmitter.java b/src/java/com/jogamp/gluegen/JavaMethodBindingEmitter.java index 23a8005..c651114 100644 --- a/src/java/com/jogamp/gluegen/JavaMethodBindingEmitter.java +++ b/src/java/com/jogamp/gluegen/JavaMethodBindingEmitter.java @@ -450,55 +450,63 @@ public class JavaMethodBindingEmitter extends FunctionEmitter { } protected void emitArrayLengthAndNIOBufferChecks(MethodBinding binding, PrintWriter writer) { - - // Check lengths of any incoming arrays if necessary - for (int i = 0; i < binding.getNumArguments(); i++) { - Type type = binding.getCArgumentType(i); - if (type.isArray()) { // FIXME: Compound and Compound-Arrays - ArrayType arrayType = type.asArray(); - writer.println(" if (" + getArgumentName(i) + ".length < " + - arrayType.getLength() + ")"); - writer.println(" throw new " + getRuntimeExceptionType() + - "(\"Length of array \\\"" + getArgumentName(i) + - "\\\" was less than the required " + arrayType.getLength() + "\");"); - } else { - JavaType javaType = binding.getJavaArgumentType(i); - if (javaType.isNIOBuffer()) { - if (useNIODirectOnly) { - writer.println(" if (!Buffers.isDirect(" + getArgumentName(i) + "))"); - writer.println(" throw new " + getRuntimeExceptionType() + "(\"Argument \\\"" + - getArgumentName(i) + "\\\" is not a direct buffer\");"); - } else { - writer.println(" final boolean " + isNIOArgName(i) + " = Buffers.isDirect(" + getArgumentName(i) + ");"); + // Check lengths of any incoming arrays if necessary + for (int i = 0; i < binding.getNumArguments(); i++) { + final Type type = binding.getCArgumentType(i); + final JavaType javaType = binding.getJavaArgumentType(i); + if ( type.isArray() ) { // FIXME: Compound and Compound-Arrays + // Simply add a range check upfront + ArrayType arrayType = type.asArray(); + if (javaType.isNIOBuffer()) { + writer.println(" if ( Buffers.remainingElem("+getArgumentName(i)+") < " + arrayType.getLength() + ")"); + } else { + writer.println(" if ( "+getArgumentName(i)+".length < " + arrayType.getLength() + ")"); + } + writer.print(" throw new " + getRuntimeExceptionType() + + "(\"Array \\\"" + getArgumentName(i) + + "\\\" length (\" + "); + if (javaType.isNIOBuffer()) { + writer.print("Buffers.remainingElem("+getArgumentName(i)+")"); + } else { + writer.print(getArgumentName(i)+".length"); + } + writer.println("+ \") was less than the required (" + arrayType.getLength() + ")\");"); + } + if (javaType.isNIOBuffer()) { + if (useNIODirectOnly) { + writer.println(" if (!Buffers.isDirect(" + getArgumentName(i) + "))"); + writer.println(" throw new " + getRuntimeExceptionType() + "(\"Argument \\\"" + + getArgumentName(i) + "\\\" is not a direct buffer\");"); + } else { + writer.println(" final boolean " + isNIOArgName(i) + " = Buffers.isDirect(" + getArgumentName(i) + ");"); + } + } else if (javaType.isNIOBufferArray()) { + // All buffers passed down in an array of NIO buffers must be direct + String argName = getArgumentName(i); + String arrayName = byteOffsetArrayArgName(i); + writer.println(" final int[] " + arrayName + " = new int[" + argName + ".length];"); + // Check direct buffer properties of all buffers within + writer.println(" if (" + argName + " != null) {"); + writer.println(" for (int _ctr = 0; _ctr < " + argName + ".length; _ctr++) {"); + writer.println(" if (!Buffers.isDirect(" + argName + "[_ctr])) {"); + writer.println(" throw new " + getRuntimeExceptionType() + + "(\"Element \" + _ctr + \" of argument \\\"" + + getArgumentName(i) + "\\\" was not a direct buffer\");"); + writer.println(" }"); + // get the Buffer Array offset values and save them into another array to send down to JNI + writer.print (" " + arrayName + "[_ctr] = Buffers.getDirectBufferByteOffset("); + writer.println(argName + "[_ctr]);"); + writer.println(" }"); + writer.println(" }"); + } else if (javaType.isPrimitiveArray()) { + String argName = getArgumentName(i); + String offsetArg = offsetArgName(i); + writer.println(" if(" + argName + " != null && " + argName + ".length <= " + offsetArg + ")"); + writer.print (" throw new " + getRuntimeExceptionType()); + writer.println("(\"array offset argument \\\"" + offsetArg + "\\\" (\" + " + offsetArg + + " + \") equals or exceeds array length (\" + " + argName + ".length + \")\");"); } - } else if (javaType.isNIOBufferArray()) { - // All buffers passed down in an array of NIO buffers must be direct - String argName = getArgumentName(i); - String arrayName = byteOffsetArrayArgName(i); - writer.println(" final int[] " + arrayName + " = new int[" + argName + ".length];"); - // Check direct buffer properties of all buffers within - writer.println(" if (" + argName + " != null) {"); - writer.println(" for (int _ctr = 0; _ctr < " + argName + ".length; _ctr++) {"); - writer.println(" if (!Buffers.isDirect(" + argName + "[_ctr])) {"); - writer.println(" throw new " + getRuntimeExceptionType() + - "(\"Element \" + _ctr + \" of argument \\\"" + - getArgumentName(i) + "\\\" was not a direct buffer\");"); - writer.println(" }"); - // get the Buffer Array offset values and save them into another array to send down to JNI - writer.print (" " + arrayName + "[_ctr] = Buffers.getDirectBufferByteOffset("); - writer.println(argName + "[_ctr]);"); - writer.println(" }"); - writer.println(" }"); - } else if (javaType.isPrimitiveArray()) { - String argName = getArgumentName(i); - String offsetArg = offsetArgName(i); - writer.println(" if(" + argName + " != null && " + argName + ".length <= " + offsetArg + ")"); - writer.print (" throw new " + getRuntimeExceptionType()); - writer.println("(\"array offset argument \\\"" + offsetArg + "\\\" (\" + " + offsetArg + - " + \") equals or exceeds array length (\" + " + argName + ".length + \")\");"); - } } - } } protected void emitCompoundArrayCopies(MethodBinding binding, PrintWriter writer) { diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java b/src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java index 900d959..cc00373 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java @@ -909,9 +909,30 @@ public class BaseClass extends JunitTracer { } } - private void dumpDim(final String pre, final TK_Dimension dim) { + private static void dumpDim(final String pre, final TK_Dimension dim) { System.err.println(pre+dim.getX()+"/"+dim.getY()+" "+dim.getWidth()+"x"+dim.getHeight()); } + private static void assertDim(final String pre, + final int expX, final int expY, final int expWidth, final int expHeight, + final TK_Dimension hasDim) { + dumpDim(pre, hasDim); + Assert.assertEquals(expX, hasDim.getX()); + Assert.assertEquals(expY, hasDim.getY()); + Assert.assertEquals(expWidth, hasDim.getWidth()); + Assert.assertEquals(expHeight, hasDim.getHeight()); + } + private static void dumpDim(final String pre, final int[] pos, final int size[]) { + System.err.println(pre+pos[0]+"/"+pos[1]+" "+size[0]+"x"+size[1]); + } + private static void assertDim(final String pre, + final int expX, final int expY, final int expWidth, final int expHeight, + final int[] pos, final int size[]) { + dumpDim(pre, pos, size); + Assert.assertEquals(expX, pos[0]); + Assert.assertEquals(expY, pos[1]); + Assert.assertEquals(expWidth, size[0]); + Assert.assertEquals(expHeight, size[1]); + } /** Test compound access call-by-reference */ public void chapter10TestCompoundCallByReference(Bindingtest1 binding) throws Exception { @@ -973,67 +994,78 @@ public class BaseClass extends JunitTracer { { final TK_Surface surface = binding.createSurface(); final TK_Dimension dim0 = surface.getBounds(); - dumpDim("ch11.0: ref-dim ", dim0); - Assert.assertEquals(0x11111111, dim0.getX()); - Assert.assertEquals(0x22222222, dim0.getY()); - Assert.assertEquals(0x33333333, dim0.getWidth()); - Assert.assertEquals(0x44444444, dim0.getHeight()); + assertDim("ch11.0: ref-dim ", 0x11111111, 0x22222222, 0x33333333, 0x44444444, dim0); final TK_Dimension dim1 = binding.getSurfaceBoundsValue(surface); - dumpDim("ch11.0: val-dim ", dim1); - Assert.assertEquals(0x11111111, dim1.getX()); - Assert.assertEquals(0x22222222, dim1.getY()); - Assert.assertEquals(0x33333333, dim1.getWidth()); - Assert.assertEquals(0x44444444, dim1.getHeight()); + assertDim("ch11.0: val-dim ", 0x11111111, 0x22222222, 0x33333333, 0x44444444, dim1); binding.destroySurface(surface); } - final TK_Dimension dim0 = binding.getBoundsValue(11, 22, 33, 44); { - dumpDim("ch11.1: val-dim ", dim0); - Assert.assertEquals(11, dim0.getX()); - Assert.assertEquals(22, dim0.getY()); - Assert.assertEquals(33, dim0.getWidth()); - Assert.assertEquals(44, dim0.getHeight()); + final TK_Dimension dim0 = binding.getBoundsValue(11, 22, 33, 44); + assertDim("ch11.1: val-dim ", 11, 22, 33, 44, dim0); + final TK_Surface surface = binding.getSurfaceValue(dim0); + final TK_Dimension dim1 = binding.getSurfaceBoundsValue(surface); + assertDim("ch11.2: val-dim ", 11, 22, 33, 44, dim1); + final boolean sameInstanceByVal = binding.isSameInstanceByVal(dim0, dim1); + final boolean sameInstanceByRef = binding.isSameInstanceByRef(dim0, dim1); + System.err.println("ch11.3: sameInstanceByVal "+sameInstanceByVal); + System.err.println("ch11.3: sameInstanceByRef "+sameInstanceByRef); + Assert.assertFalse(sameInstanceByVal); + Assert.assertFalse(sameInstanceByRef); + } + { + final TK_Dimension dim1 = binding.getBoundsValue(11, 22, 33, 44); + final TK_Dimension dim2 = binding.getBoundsValue(1, 2, 3, 4); + final TK_Dimension[] sumands = { dim1, dim2 }; + final TK_Dimension dimSum = binding.addDimensions(sumands); + assertDim("ch11.4: sum-dim ", 11+1, 22+2, 33+3, 44+4, dimSum); + binding.zeroDimensions(sumands); + assertDim("ch11.5: zero-dim[0] ", 0, 0, 0, 0, sumands[0]); + assertDim("ch11.5: zero-dim[1] ", 0, 0, 0, 0, sumands[1]); + } + { + final TK_Dimension dim0 = binding.getBoundsValue(0, 0, 0, 0); + final TK_Dimension[] dim0A = { dim0 }; + binding.copyPrimToDimensions(new int[] { 11, 22}, 0, new int[] { 100, 200}, 0, dim0A); + assertDim("ch11.6: copyPrim2Dim ", 11, 22, 100, 200, dim0); + final int[] pos = { 0, 0 }; + final int[] size = { 0, 0 }; + binding.copyDimensionsToPrim(dim0, pos, 0, size, 0); + assertDim("ch11.7: copyDim2Prim ", 11, 22, 100, 200, pos, size); } - final TK_Surface surface = binding.getSurfaceValue(dim0); - final TK_Dimension dim1 = binding.getSurfaceBoundsValue(surface); { - dumpDim("ch11.2: val-dim ", dim0); - Assert.assertEquals(11, dim1.getX()); - Assert.assertEquals(22, dim1.getY()); - Assert.assertEquals(33, dim1.getWidth()); - Assert.assertEquals(44, dim1.getHeight()); + final int expRGBAi = 0x112233aa; + final byte[] expRGBAb = { (byte)0xaa, 0x33, 0x22, 0x11 }; + final int hasRGBAi = binding.rgbaToInt(expRGBAb, 0); + System.err.println("ch11.8: expRGBAb 0x"+ + Integer.toHexString(expRGBAb[3])+", 0x"+ + Integer.toHexString(expRGBAb[2])+", 0x"+ + Integer.toHexString(expRGBAb[1])+", 0x"+ + Integer.toHexString(expRGBAb[0]) ); + System.err.println("ch11.8: hasRGBAi 0x"+Integer.toHexString(hasRGBAi)); + Assert.assertEquals(expRGBAi, hasRGBAi); + + final byte[] hasRGBAb = new byte[] { 0, 0, 0, 0 }; + binding.intToRgba(hasRGBAi, hasRGBAb, 0); + System.err.println("ch11.9: hasRGBAb 0x"+ + Integer.toHexString(hasRGBAb[3])+", 0x"+ + Integer.toHexString(hasRGBAb[2])+", 0x"+ + Integer.toHexString(hasRGBAb[1])+", 0x"+ + Integer.toHexString(hasRGBAb[0]) ); + Assert.assertArrayEquals(expRGBAb, hasRGBAb); } - final boolean sameInstanceByVal = binding.isSameInstanceByVal(dim0, dim1); - final boolean sameInstanceByRef = binding.isSameInstanceByRef(dim0, dim1); - System.err.println("ch11.3: sameInstanceByVal "+sameInstanceByVal); - System.err.println("ch11.3: sameInstanceByRef "+sameInstanceByRef); - Assert.assertFalse(sameInstanceByVal); - Assert.assertFalse(sameInstanceByRef); - - final TK_Dimension dim2 = binding.getBoundsValue(1, 2, 3, 4); - final TK_Dimension[] sumands = { dim1, dim2 }; - final TK_Dimension dimSum = binding.addDimensions(sumands); { - dumpDim("ch11.4: sum-dim ", dimSum); - Assert.assertEquals(11+1, dimSum.getX()); - Assert.assertEquals(22+2, dimSum.getY()); - Assert.assertEquals(33+3, dimSum.getWidth()); - Assert.assertEquals(44+4, dimSum.getHeight()); + final int[] result = { 0 }; + binding.addInt(new int[] { 1, 2}, 0, result, 0); + System.err.println("ch11.10: addInt "+result[0]); + Assert.assertEquals(3, result[0]); } - binding.zeroDimensions(sumands); { - dumpDim("ch11.5: zero-dim[0] ", sumands[0]); - dumpDim("ch11.5: zero-dim[1] ", sumands[1]); - Assert.assertEquals(0, sumands[0].getX()); - Assert.assertEquals(0, sumands[0].getY()); - Assert.assertEquals(0, sumands[0].getWidth()); - Assert.assertEquals(0, sumands[0].getHeight()); - Assert.assertEquals(0, sumands[1].getX()); - Assert.assertEquals(0, sumands[1].getY()); - Assert.assertEquals(0, sumands[1].getWidth()); - Assert.assertEquals(0, sumands[1].getHeight()); + final byte[] result = { 0 }; + binding.addByte(new byte[] { 1, 2}, 0, result, 0); + System.err.println("ch11.11: addByte "+result[0]); + Assert.assertEquals(3, result[0]); } } } diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/test1.c b/src/junit/com/jogamp/gluegen/test/junit/generation/test1.c index ca24444..7581d20 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/test1.c +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test1.c @@ -543,3 +543,35 @@ MYAPI void MYAPIENTRY zeroDimensions(TK_Dimension s[2]) { s[1].width = 0; s[1].height = 0; } + +MYAPI void MYAPIENTRY copyPrimToDimensions(const int pos[2], const int size[2], TK_Dimension dest[1]) { + dest[0].x = pos[0]; + dest[0].y = pos[1]; + dest[0].width = size[0]; + dest[0].height = size[1]; +} +MYAPI void MYAPIENTRY copyDimensionsToPrim(TK_Dimension dim, int dpos[2], int dsize[2]) { + dpos[0] = dim.x; + dpos[1] = dim.y; + dsize[0] = dim.width; + dsize[1] = dim.height; +} +MYAPI int MYAPIENTRY rgbaToInt(const char rgba[4]) { + return ((unsigned int)rgba[3] & 0xffU) << 24 | + ((unsigned int)rgba[2] & 0xffU) << 16 | + ((unsigned int)rgba[1] & 0xffU) << 8 | + ((unsigned int)rgba[0] & 0xffU); +} +MYAPI void MYAPIENTRY intToRgba(int irgba, char rgbaSink[4]) { + rgbaSink[0] = (char) ( (irgba ) & 0xff ); + rgbaSink[1] = (char) ( (irgba >> 8 ) & 0xff ); + rgbaSink[2] = (char) ( (irgba >> 16 ) & 0xff ); + rgbaSink[3] = (char) ( (irgba >> 24 ) & 0xff ); +} +MYAPI void MYAPIENTRY addInt(const int summands[2], int result[1]) { + result[0] = summands[0] + summands[1]; +} +MYAPI void MYAPIENTRY addByte(const char summands[2], char result[1]) { + result[0] = summands[0] + summands[1]; +} + diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/test1.h b/src/junit/com/jogamp/gluegen/test/junit/generation/test1.h index a487f7a..dcf5cfc 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/test1.h +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test1.h @@ -253,3 +253,10 @@ MYAPI Bool MYAPIENTRY isSameInstanceByRef(const TK_Dimension *s1, const TK_Dimen MYAPI TK_Dimension MYAPIENTRY addDimensions(const TK_Dimension s[2]); MYAPI void MYAPIENTRY zeroDimensions(TK_Dimension s[2]); +MYAPI void MYAPIENTRY copyPrimToDimensions(const int pos[2], const int size[2], TK_Dimension dest[1]); +MYAPI void MYAPIENTRY copyDimensionsToPrim(TK_Dimension dim, int dpos[2], int dsize[2]); +MYAPI int MYAPIENTRY rgbaToInt(const char rgba[4]); +MYAPI void MYAPIENTRY intToRgba(int irgba, char rgbaSink[4]); +MYAPI void MYAPIENTRY addInt(const int summands[2], int result[1]); +MYAPI void MYAPIENTRY addByte(const char summands[2], char result[1]); + -- cgit v1.2.3