From b2bcf040c09a28c38c5a4516dcf6966982f8123b Mon Sep 17 00:00:00 2001 From: Andrey Starodubtsev Date: Wed, 8 Jun 2016 15:04:47 +0300 Subject: java directorargout + java/typemaps.i fixes - directorargout didn't work - it used string "jresult" instead of argument name - java/typemaps.i didn't work with directors - test for using java/typemaps.i in directors added --- Examples/test-suite/java/Makefile.in | 1 + .../java/java_director_typemaps_runme.java | 115 +++++++++ Examples/test-suite/java_director_typemaps.i | 265 +++++++++++++++++++++ Lib/java/typemaps.i | 104 +++++--- Source/Modules/java.cxx | 2 +- 5 files changed, 459 insertions(+), 28 deletions(-) create mode 100644 Examples/test-suite/java/java_director_typemaps_runme.java create mode 100644 Examples/test-suite/java_director_typemaps.i diff --git a/Examples/test-suite/java/Makefile.in b/Examples/test-suite/java/Makefile.in index e69964352..d63a25879 100644 --- a/Examples/test-suite/java/Makefile.in +++ b/Examples/test-suite/java/Makefile.in @@ -28,6 +28,7 @@ CPP_TEST_CASES = \ java_director_exception_feature \ java_director_exception_feature_nspace \ java_director_ptrclass \ + java_director_typemaps \ java_enums \ java_jnitypes \ java_lib_arrays_dimensionless \ diff --git a/Examples/test-suite/java/java_director_typemaps_runme.java b/Examples/test-suite/java/java_director_typemaps_runme.java new file mode 100644 index 000000000..612ccb31a --- /dev/null +++ b/Examples/test-suite/java/java_director_typemaps_runme.java @@ -0,0 +1,115 @@ +// tests for java/typemaps.i used for directors + +import java_director_typemaps.*; +import java.math.BigInteger; + +public class java_director_typemaps_runme { + + static { + try { + System.loadLibrary("java_director_typemaps"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); + System.exit(1); + } + } + + + public static void main(String argv[]) { + Quux quux = new java_director_MyQuux(); + quux.etest(); + } +} + +class java_director_MyQuux extends Quux { + public java_director_MyQuux() { + super(); + } + + public void director_method_bool_output( + boolean[] bool_arg, + + byte[] signed_char_arg, + short[] unsigned_char_arg, + + short[] short_arg, + int[] unsigned_short_arg, + + int[] int_arg, + long[] unsigned_int_arg, + + int[] long_arg, + long[] unsigned_long_arg, + + long[] long_long_arg, + // BigInteger[] unsigned_long_long_arg, + + float[] float_arg, + double[] double_arg) + { + bool_arg[0] = true; + signed_char_arg[0] = 1; + unsigned_char_arg[0] = 2; + short_arg[0] = 3; + unsigned_short_arg[0] = 4; + int_arg[0] = 5; + unsigned_int_arg[0] = 6; + long_arg[0] = 7; + unsigned_long_arg[0] = 8; + long_long_arg[0] = 9; + // unsigned_long_long_arg[0] = 10; + float_arg[0] = 11; + double_arg[0] = 12; + } + + public void director_method_bool_inout( + boolean[] bool_arg, + + byte[] signed_char_arg, + short[] unsigned_char_arg, + + short[] short_arg, + int[] unsigned_short_arg, + + int[] int_arg, + long[] unsigned_int_arg, + + int[] long_arg, + long[] unsigned_long_arg, + + long[] long_long_arg, + // BigInteger[] unsigned_long_long_arg, + + float[] float_arg, + double[] double_arg) + { + if (bool_arg[0]) throw new RuntimeException("unexpected value for bool_arg"); + + if (signed_char_arg[0] != 101) throw new RuntimeException("unexpected value for signed_char_arg"); + if (unsigned_char_arg[0] != 101) throw new RuntimeException("unexpected value for unsigned_char_arg"); + if (short_arg[0] != 101) throw new RuntimeException("unexpected value for short_arg"); + if (unsigned_short_arg[0] != 101) throw new RuntimeException("unexpected value for unsigned_short_arg"); + if (int_arg[0] != 101) throw new RuntimeException("unexpected value for int_arg"); + if (unsigned_int_arg[0] != 101) throw new RuntimeException("unexpected value for unsigned_int_arg"); + if (long_arg[0] != 101) throw new RuntimeException("unexpected value for long_arg"); + if (unsigned_long_arg[0] != 101) throw new RuntimeException("unexpected value for unsigned_long_arg"); + if (long_long_arg[0] != 101) throw new RuntimeException("unexpected value for long_long_arg"); + // if (unsigned_long_long_arg[0] != 101) throw new RuntimeException("unexpected value for unsigned_long_long_arg"); + if (float_arg[0] != 101) throw new RuntimeException("unexpected value for float_arg"); + if (double_arg[0] != 101) throw new RuntimeException("unexpected value for double_arg"); + + bool_arg[0] = false; + signed_char_arg[0] = 11; + unsigned_char_arg[0] = 12; + short_arg[0] = 13; + unsigned_short_arg[0] = 14; + int_arg[0] = 15; + unsigned_int_arg[0] = 16; + long_arg[0] = 17; + unsigned_long_arg[0] = 18; + long_long_arg[0] = 19; + // unsigned_long_long_arg[0] = 110; + float_arg[0] = 111; + double_arg[0] = 112; + } +} diff --git a/Examples/test-suite/java_director_typemaps.i b/Examples/test-suite/java_director_typemaps.i new file mode 100644 index 000000000..8c7bf9ba5 --- /dev/null +++ b/Examples/test-suite/java_director_typemaps.i @@ -0,0 +1,265 @@ +/* + * Test Lib/java/typemaps.i + */ + +%module(directors="1") java_director + +%feature("director", assumeoverride=1) Quux; + +%include + +%apply bool& OUTPUT {bool& boolarg_output}; + +%apply signed char& OUTPUT {signed char& signed_chararg_output}; +%apply unsigned char& OUTPUT {unsigned char& unsigned_chararg_output}; + +%apply short& OUTPUT {short& shortarg_output}; +%apply unsigned short& OUTPUT {unsigned short& unsigned_shortarg_output}; + +%apply int& OUTPUT {int& intarg_output}; +%apply unsigned int& OUTPUT {unsigned int& unsigned_intarg_output}; + +%apply long& OUTPUT {long& longarg_output}; +%apply unsigned long& OUTPUT {unsigned long& unsigned_longarg_output}; + +%apply long long& OUTPUT {long long& long_longarg_output}; +// %apply unsigned long long& OUTPUT {unsigned long long& unsigned_long_longarg_output}; + +%apply float& OUTPUT {float& floatarg_output}; +%apply double& OUTPUT {double& doublearg_output}; + +%apply bool& INOUT {bool& boolarg_inout}; + +%apply signed char& INOUT {signed char& signed_chararg_inout}; +%apply unsigned char& INOUT {unsigned char& unsigned_chararg_inout}; + +%apply short& INOUT {short& shortarg_inout}; +%apply unsigned short& INOUT {unsigned short& unsigned_shortarg_inout}; + +%apply int& INOUT {int& intarg_inout}; +%apply unsigned int& INOUT {unsigned int& unsigned_intarg_inout}; + +%apply long& INOUT {long& longarg_inout}; +%apply unsigned long& INOUT {unsigned long& unsigned_longarg_inout}; + +%apply long long& INOUT {long long& long_longarg_inout}; +// %apply unsigned long long& INOUT {unsigned long long& unsigned_long_longarg_inout}; + +%apply float& INOUT {float& floatarg_inout}; +%apply double& INOUT {double& doublearg_inout}; + +%inline %{ +#include + +class Quux { +public: + Quux() {} + virtual ~Quux() {} + + virtual void director_method_bool_output( + bool& boolarg_output, + + signed char& signed_chararg_output, + unsigned char& unsigned_chararg_output, + + short& shortarg_output, + unsigned short& unsigned_shortarg_output, + + int& intarg_output, + unsigned int& unsigned_intarg_output, + + long& longarg_output, + unsigned long& unsigned_longarg_output, + + long long& long_longarg_output, + // unsigned long long& unsigned_long_longarg_output, + + float& floatarg_output, + double& doublearg_output) + { + boolarg_output = false; + + signed_chararg_output = 50; + unsigned_chararg_output = 50; + + shortarg_output = 50; + unsigned_shortarg_output = 50; + + intarg_output = 50; + unsigned_intarg_output = 50; + + longarg_output = 50; + unsigned_longarg_output = 50; + + long_longarg_output = 50; + // unsigned_long_longarg_output = 50; + + floatarg_output = 50; + doublearg_output = 50; + } + + virtual void director_method_bool_inout( + bool& boolarg_inout, + + signed char& signed_chararg_inout, + unsigned char& unsigned_chararg_inout, + + short& shortarg_inout, + unsigned short& unsigned_shortarg_inout, + + int& intarg_inout, + unsigned int& unsigned_intarg_inout, + + long& longarg_inout, + unsigned long& unsigned_longarg_inout, + + long long& long_longarg_inout, + // unsigned long long& unsigned_long_longarg_inout, + + float& floatarg_inout, + double& doublearg_inout) + { + boolarg_inout = false; + + signed_chararg_inout = 50; + unsigned_chararg_inout = 50; + + shortarg_inout = 50; + unsigned_shortarg_inout = 50; + + intarg_inout = 50; + unsigned_intarg_inout = 50; + + longarg_inout = 50; + unsigned_longarg_inout = 50; + + long_longarg_inout = 50; + // unsigned_long_longarg_inout = 50; + + floatarg_inout = 50; + doublearg_inout = 50; + } + + void etest() { + bool boolarg_inout = false; + + signed char signed_chararg_inout = 150; + unsigned char unsigned_chararg_inout = 150; + + short shortarg_inout = 150; + unsigned short unsigned_shortarg_inout = 150; + + int intarg_inout = 150; + unsigned int unsigned_intarg_inout = 150; + + long longarg_inout = 150; + unsigned long unsigned_longarg_inout = 150; + + long long long_longarg_inout = 150; + // unsigned long long unsigned_long_longarg_inout = 150; + + float floatarg_inout = 150; + double doublearg_inout = 150; + + director_method_bool_output( + boolarg_inout, + + signed_chararg_inout, + unsigned_chararg_inout, + + shortarg_inout, + unsigned_shortarg_inout, + + intarg_inout, + unsigned_intarg_inout, + + longarg_inout, + unsigned_longarg_inout, + + long_longarg_inout, + // unsigned_long_longarg_inout, + + floatarg_inout, + doublearg_inout); + + assert(boolarg_inout == true); + assert(signed_chararg_inout == 1); + assert(unsigned_chararg_inout == 2); + + assert(shortarg_inout == 3); + assert(unsigned_shortarg_inout == 4); + + assert(intarg_inout == 5); + assert(unsigned_intarg_inout == 6); + + assert(longarg_inout == 7); + assert(unsigned_longarg_inout == 8); + + assert(long_longarg_inout == 9); + // assert(unsigned_long_longarg_inout == 10); + + assert(floatarg_inout == 11); + assert(doublearg_inout == 12); + + boolarg_inout = false; + + signed_chararg_inout = 101; + unsigned_chararg_inout = 101; + + shortarg_inout = 101; + unsigned_shortarg_inout = 101; + + intarg_inout = 101; + unsigned_intarg_inout = 101; + + longarg_inout = 101; + unsigned_longarg_inout = 101; + + long_longarg_inout = 101; + // unsigned_long_longarg_inout = 101; + + floatarg_inout = 101; + doublearg_inout = 101; + + director_method_bool_inout( + boolarg_inout, + + signed_chararg_inout, + unsigned_chararg_inout, + + shortarg_inout, + unsigned_shortarg_inout, + + intarg_inout, + unsigned_intarg_inout, + + longarg_inout, + unsigned_longarg_inout, + + long_longarg_inout, + // unsigned_long_longarg_inout, + + floatarg_inout, + doublearg_inout); + + assert(boolarg_inout == false); + assert(signed_chararg_inout == 11); + assert(unsigned_chararg_inout == 12); + + assert(shortarg_inout == 13); + assert(unsigned_shortarg_inout == 14); + + assert(intarg_inout == 15); + assert(unsigned_intarg_inout == 16); + + assert(longarg_inout == 17); + assert(unsigned_longarg_inout == 18); + + assert(long_longarg_inout == 19); + // assert(unsigned_long_longarg_inout == 110); + + assert(floatarg_inout == 111); + assert(doublearg_inout == 112); + } +}; +%} diff --git a/Lib/java/typemaps.i b/Lib/java/typemaps.i index 041cb24e0..b3b08867b 100644 --- a/Lib/java/typemaps.i +++ b/Lib/java/typemaps.i @@ -187,6 +187,8 @@ There are no char *OUTPUT typemaps, however you can apply the signed char * type %typemap(jtype) TYPE *OUTPUT, TYPE &OUTPUT "JTYPE[]" %typemap(jstype) TYPE *OUTPUT, TYPE &OUTPUT "JTYPE[]" %typemap(javain) TYPE *OUTPUT, TYPE &OUTPUT "$javainput" +%typemap(javadirectorin) TYPE *OUTPUT, TYPE &OUTPUT "$jniinput" +%typemap(javadirectorout) TYPE *OUTPUT, TYPE &OUTPUT "$javacall" %typemap(in) TYPE *OUTPUT($*1_ltype temp), TYPE &OUTPUT($*1_ltype temp) { @@ -204,29 +206,48 @@ There are no char *OUTPUT typemaps, however you can apply the signed char * type %typemap(freearg) TYPE *OUTPUT, TYPE &OUTPUT "" -%typemap(argout) TYPE *OUTPUT, TYPE &OUTPUT +%typemap(argout) TYPE *OUTPUT, TYPE &OUTPUT { JNITYPE jvalue = (JNITYPE)temp$argnum; JCALL4(Set##JAVATYPE##ArrayRegion, jenv, $input, 0, 1, &jvalue); } +%typemap(directorin,descriptor=JNIDESC) TYPE &OUTPUT, TYPE *OUTPUT +{ + $input = JCALL1(New##JAVATYPE##Array, jenv, 1); +} + +%typemap(directorargout) TYPE &OUTPUT +{ + JNITYPE jvalue; + JCALL4(Get##JAVATYPE##ArrayRegion, jenv, $input, 0, 1, &jvalue); + $result = jvalue; +} + +%typemap(directorargout) TYPE *OUTPUT +{ + JNITYPE jvalue; + JCALL4(Get##JAVATYPE##ArrayRegion, jenv, $input, 0, 1, &jvalue); + *$result = jvalue; +} + %typemap(typecheck) TYPE *OUTPUT = TYPECHECKTYPE; %typemap(typecheck) TYPE &OUTPUT = TYPECHECKTYPE; %enddef -OUTPUT_TYPEMAP(bool, jboolean, boolean, Boolean, "[Ljava/lang/Boolean;", jbooleanArray); -OUTPUT_TYPEMAP(signed char, jbyte, byte, Byte, "[Ljava/lang/Byte;", jbyteArray); -OUTPUT_TYPEMAP(unsigned char, jshort, short, Short, "[Ljava/lang/Short;", jshortArray); -OUTPUT_TYPEMAP(short, jshort, short, Short, "[Ljava/lang/Short;", jshortArray); -OUTPUT_TYPEMAP(unsigned short, jint, int, Int, "[Ljava/lang/Integer;", jintArray); -OUTPUT_TYPEMAP(int, jint, int, Int, "[Ljava/lang/Integer;", jintArray); -OUTPUT_TYPEMAP(unsigned int, jlong, long, Long, "[Ljava/lang/Long;", jlongArray); -OUTPUT_TYPEMAP(long, jint, int, Int, "[Ljava/lang/Integer;", jintArray); -OUTPUT_TYPEMAP(unsigned long, jlong, long, Long, "[Ljava/lang/Long;", jlongArray); -OUTPUT_TYPEMAP(long long, jlong, long, Long, "[Ljava/lang/Long;", jlongArray); -OUTPUT_TYPEMAP(unsigned long long, jobject, java.math.BigInteger, NOTUSED, "[Ljava/lang/BigInteger;", SWIGBIGINTEGERARRAY); -OUTPUT_TYPEMAP(float, jfloat, float, Float, "[Ljava/lang/Float;", jfloatArray); -OUTPUT_TYPEMAP(double, jdouble, double, Double, "[Ljava/lang/Double;", jdoubleArray); +OUTPUT_TYPEMAP(bool, jboolean, boolean, Boolean, "[Z", jbooleanArray); +OUTPUT_TYPEMAP(signed char, jbyte, byte, Byte, "[B", jbyteArray); +OUTPUT_TYPEMAP(unsigned char, jshort, short, Short, "[S", jshortArray); +OUTPUT_TYPEMAP(short, jshort, short, Short, "[S", jshortArray); +OUTPUT_TYPEMAP(unsigned short, jint, int, Int, "[I", jintArray); +OUTPUT_TYPEMAP(int, jint, int, Int, "[I", jintArray); +OUTPUT_TYPEMAP(unsigned int, jlong, long, Long, "[J", jlongArray); +OUTPUT_TYPEMAP(long, jint, int, Int, "[I", jintArray); +OUTPUT_TYPEMAP(unsigned long, jlong, long, Long, "[J", jlongArray); +OUTPUT_TYPEMAP(long long, jlong, long, Long, "[J", jlongArray); +OUTPUT_TYPEMAP(unsigned long long, jobject, java.math.BigInteger, Object, "[Ljava/math/BigInteger;", jobjectArray); +OUTPUT_TYPEMAP(float, jfloat, float, Float, "[F", jfloatArray); +OUTPUT_TYPEMAP(double, jdouble, double, Double, "[D", jdoubleArray); #undef OUTPUT_TYPEMAP @@ -330,6 +351,8 @@ There are no char *INOUT typemaps, however you can apply the signed char * typem %typemap(jtype) TYPE *INOUT, TYPE &INOUT "JTYPE[]" %typemap(jstype) TYPE *INOUT, TYPE &INOUT "JTYPE[]" %typemap(javain) TYPE *INOUT, TYPE &INOUT "$javainput" +%typemap(javadirectorin) TYPE *INOUT, TYPE &INOUT "$jniinput" +%typemap(javadirectorout) TYPE *INOUT, TYPE &INOUT "$javacall" %typemap(in) TYPE *INOUT, TYPE &INOUT { if (!$input) { @@ -348,24 +371,51 @@ There are no char *INOUT typemaps, however you can apply the signed char * typem %typemap(argout) TYPE *INOUT, TYPE &INOUT { JCALL3(Release##JAVATYPE##ArrayElements, jenv, $input, (JNITYPE *)$1, 0); } +%typemap(directorin,descriptor=JNIDESC) TYPE &INOUT +{ + $input = JCALL1(New##JAVATYPE##Array, jenv, 1); + const JNITYPE jvalue = (JNITYPE)$1; + JCALL4(Set##JAVATYPE##ArrayRegion, jenv, $input, 0, 1, &jvalue); +} + +%typemap(directorin,descriptor=JNIDESC) TYPE *INOUT +{ + $input = JCALL1(New##JAVATYPE##Array, jenv, 1); + const JNITYPE jvalue = (JNITYPE)*$1; + JCALL4(Set##JAVATYPE##ArrayRegion, jenv, $input, 0, 1, &jvalue); +} + +%typemap(directorargout) TYPE &INOUT +{ + JNITYPE jvalue; + JCALL4(Get##JAVATYPE##ArrayRegion, jenv, $input, 0, 1, &jvalue); + $result = jvalue; +} + +%typemap(directorargout) TYPE *INOUT +{ + JNITYPE jvalue; + JCALL4(Get##JAVATYPE##ArrayRegion, jenv, $input, 0, 1, &jvalue); + *$result = jvalue; +} %typemap(typecheck) TYPE *INOUT = TYPECHECKTYPE; %typemap(typecheck) TYPE &INOUT = TYPECHECKTYPE; %enddef -INOUT_TYPEMAP(bool, jboolean, boolean, Boolean, "[Ljava/lang/Boolean;", jbooleanArray); -INOUT_TYPEMAP(signed char, jbyte, byte, Byte, "[Ljava/lang/Byte;", jbyteArray); -INOUT_TYPEMAP(unsigned char, jshort, short, Short, "[Ljava/lang/Short;", jshortArray); -INOUT_TYPEMAP(short, jshort, short, Short, "[Ljava/lang/Short;", jshortArray); -INOUT_TYPEMAP(unsigned short, jint, int, Int, "[Ljava/lang/Integer;", jintArray); -INOUT_TYPEMAP(int, jint, int, Int, "[Ljava/lang/Integer;", jintArray); -INOUT_TYPEMAP(unsigned int, jlong, long, Long, "[Ljava/lang/Long;", jlongArray); -INOUT_TYPEMAP(long, jint, int, Int, "[Ljava/lang/Integer;", jintArray); -INOUT_TYPEMAP(unsigned long, jlong, long, Long, "[Ljava/lang/Long;", jlongArray); -INOUT_TYPEMAP(long long, jlong, long, Long, "[Ljava/lang/Long;", jlongArray); -INOUT_TYPEMAP(unsigned long long, jobject, java.math.BigInteger, NOTUSED, "[Ljava.math.BigInteger;", SWIGBIGINTEGERARRAY); -INOUT_TYPEMAP(float, jfloat, float, Float, "[Ljava/lang/Float;", jfloatArray); -INOUT_TYPEMAP(double, jdouble, double, Double, "[Ljava/lang/Double;", jdoubleArray); +INOUT_TYPEMAP(bool, jboolean, boolean, Boolean, "[Z", jbooleanArray); +INOUT_TYPEMAP(signed char, jbyte, byte, Byte, "[B", jbyteArray); +INOUT_TYPEMAP(unsigned char, jshort, short, Short, "[S", jshortArray); +INOUT_TYPEMAP(short, jshort, short, Short, "[S", jshortArray); +INOUT_TYPEMAP(unsigned short, jint, int, Int, "[I", jintArray); +INOUT_TYPEMAP(int, jint, int, Int, "[I", jintArray); +INOUT_TYPEMAP(unsigned int, jlong, long, Long, "[J", jlongArray); +INOUT_TYPEMAP(long, jint, int, Int, "[I", jintArray); +INOUT_TYPEMAP(unsigned long, jlong, long, Long, "[J", jlongArray); +INOUT_TYPEMAP(long long, jlong, long, Long, "[J", jlongArray); +INOUT_TYPEMAP(unsigned long long, jobject, java.math.BigInteger, Object, "[java/math/BigInteger;", jobjectArray); +INOUT_TYPEMAP(float, jfloat, float, Float, "[F", jfloatArray); +INOUT_TYPEMAP(double, jdouble, double, Double, "[D", jdoubleArray); #undef INOUT_TYPEMAP diff --git a/Source/Modules/java.cxx b/Source/Modules/java.cxx index b7a5ebbfe..bb37013fb 100644 --- a/Source/Modules/java.cxx +++ b/Source/Modules/java.cxx @@ -4383,7 +4383,7 @@ public: for (p = l; p;) { if ((tm = Getattr(p, "tmap:directorargout"))) { addThrows(n, "tmap:directorargout", p); - Replaceall(tm, "$result", "jresult"); + Replaceall(tm, "$result", Getattr(p, "name")); Replaceall(tm, "$input", Getattr(p, "emit:directorinput")); Printv(w->code, tm, "\n", NIL); p = Getattr(p, "tmap:directorargout:next"); -- cgit v1.2.1