Throw exception if Java array passed in is not the same size as the C array.
Was using C free when array created with C++ new. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4672 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
b6acd9dbd1
commit
c5e9953972
1 changed files with 53 additions and 10 deletions
|
|
@ -1,7 +1,8 @@
|
||||||
/* arrays_java.i
|
/* arrays_java.i
|
||||||
* These typemaps give more natural support for arrays. The typemaps are not efficient
|
* These typemaps give more natural support for arrays. The typemaps are not efficient
|
||||||
* as there is a lot of copying of the array values whenever the array is passed to C/C++
|
* as there is a lot of copying of the array values whenever the array is passed to C/C++
|
||||||
* from Java and visa versa.
|
* from Java and visa versa. The Java array is expected to be the same size as the C array.
|
||||||
|
* An exception is thrown if they are not.
|
||||||
|
|
||||||
Example usage:
|
Example usage:
|
||||||
Wrapping:
|
Wrapping:
|
||||||
|
|
@ -63,8 +64,7 @@ int SWIG_JavaArrayIn##JFUNCNAME (JNIEnv *jenv, JNITYPE **jarr, CTYPE **carr, JNI
|
||||||
|
|
||||||
void SWIG_JavaArrayArgout##JFUNCNAME (JNIEnv *jenv, JNITYPE *jarr, CTYPE *carr, JNITYPE##Array input) {
|
void SWIG_JavaArrayArgout##JFUNCNAME (JNIEnv *jenv, JNITYPE *jarr, CTYPE *carr, JNITYPE##Array input) {
|
||||||
int i;
|
int i;
|
||||||
jsize sz;
|
jsize sz = JCALL1(GetArrayLength, jenv, input);
|
||||||
sz = JCALL1(GetArrayLength, jenv, input);
|
|
||||||
for (i=0; i<sz; i++)
|
for (i=0; i<sz; i++)
|
||||||
jarr[i] = (JNITYPE)carr[i];
|
jarr[i] = (JNITYPE)carr[i];
|
||||||
JCALL3(Release##JAVATYPE##ArrayElements, jenv, input, jarr, 0);
|
JCALL3(Release##JAVATYPE##ArrayElements, jenv, input, jarr, 0);
|
||||||
|
|
@ -185,14 +185,20 @@ JAVA_ARRAYS_IMPL(double, jdouble, Double, Double) /* double[] */
|
||||||
/* Arrays of primitive types use the following macro. The array typemaps use support functions. */
|
/* Arrays of primitive types use the following macro. The array typemaps use support functions. */
|
||||||
%define JAVA_ARRAYS_TYPEMAPS(CTYPE, JNITYPE, JFUNCNAME)
|
%define JAVA_ARRAYS_TYPEMAPS(CTYPE, JNITYPE, JFUNCNAME)
|
||||||
|
|
||||||
|
%typemap(in) CTYPE[] (JNITYPE *jarr)
|
||||||
|
%{ if (!SWIG_JavaArrayIn##JFUNCNAME(jenv, &jarr, &$1, $input)) return $null; %}
|
||||||
%typemap(in) CTYPE[ANY] (JNITYPE *jarr)
|
%typemap(in) CTYPE[ANY] (JNITYPE *jarr)
|
||||||
%{ if (!SWIG_JavaArrayIn##JFUNCNAME(jenv, &jarr, &$1, $input)) return $null; %}
|
%{ if ($input && JCALL1(GetArrayLength, jenv, $input) != $1_size) {
|
||||||
|
SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "incorrect array size");
|
||||||
|
return $null;
|
||||||
|
}
|
||||||
|
if (!SWIG_JavaArrayIn##JFUNCNAME(jenv, &jarr, &$1, $input)) return $null; %}
|
||||||
%typemap(argout) CTYPE[ANY]
|
%typemap(argout) CTYPE[ANY]
|
||||||
%{ SWIG_JavaArrayArgout##JFUNCNAME(jenv, jarr$argnum, $1, $input); %}
|
%{ SWIG_JavaArrayArgout##JFUNCNAME(jenv, jarr$argnum, $1, $input); %}
|
||||||
%typemap(out) CTYPE[ANY]
|
%typemap(out) CTYPE[ANY]
|
||||||
%{$result = SWIG_JavaArrayOut##JFUNCNAME(jenv, $1, $1_dim0); %}
|
%{$result = SWIG_JavaArrayOut##JFUNCNAME(jenv, $1, $1_dim0); %}
|
||||||
%typemap(freearg) CTYPE[ANY]
|
%typemap(freearg) CTYPE[ANY]
|
||||||
#ifdef __cplusplus
|
#if __cplusplus
|
||||||
%{ delete [] $1; %}
|
%{ delete [] $1; %}
|
||||||
#else
|
#else
|
||||||
%{ free($1); %}
|
%{ free($1); %}
|
||||||
|
|
@ -297,7 +303,7 @@ JAVA_ARRAYS_TYPEMAPS(double, jdouble, Double) /* double[ANY] */
|
||||||
return $javaclassname.cArrayWrap($jnicall, $owner);
|
return $javaclassname.cArrayWrap($jnicall, $owner);
|
||||||
}
|
}
|
||||||
|
|
||||||
%typemap(in) ARRAYSOFCLASSES[ANY] (jlong *jarr, jsize sz)
|
%typemap(in) ARRAYSOFCLASSES[] (jlong *jarr, jsize sz)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
if (!$input) {
|
if (!$input) {
|
||||||
|
|
@ -309,7 +315,37 @@ JAVA_ARRAYS_TYPEMAPS(double, jdouble, Double) /* double[ANY] */
|
||||||
if (!jarr) {
|
if (!jarr) {
|
||||||
return $null;
|
return $null;
|
||||||
}
|
}
|
||||||
#ifdef __cplusplus
|
#if __cplusplus
|
||||||
|
$1 = new $*1_ltype[sz];
|
||||||
|
#else
|
||||||
|
$1 = ($1_ltype) calloc(sz, sizeof($*1_ltype));
|
||||||
|
#endif
|
||||||
|
if (!$1) {
|
||||||
|
SWIG_JavaThrowException(jenv, SWIG_JavaOutOfMemoryError, "array memory allocation failed");
|
||||||
|
return $null;
|
||||||
|
}
|
||||||
|
for (i=0; i<sz; i++) {
|
||||||
|
$1[i] = **($&1_ltype)&jarr[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
%typemap(in) ARRAYSOFCLASSES[ANY] (jlong *jarr, jsize sz)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
if (!$input) {
|
||||||
|
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null array");
|
||||||
|
return $null;
|
||||||
|
}
|
||||||
|
sz = JCALL1(GetArrayLength, jenv, $input);
|
||||||
|
if (sz != $1_size) {
|
||||||
|
SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "incorrect array size");
|
||||||
|
return $null;
|
||||||
|
}
|
||||||
|
jarr = JCALL2(GetLongArrayElements, jenv, $input, 0);
|
||||||
|
if (!jarr) {
|
||||||
|
return $null;
|
||||||
|
}
|
||||||
|
#if __cplusplus
|
||||||
$1 = new $*1_ltype[sz];
|
$1 = new $*1_ltype[sz];
|
||||||
#else
|
#else
|
||||||
$1 = ($1_ltype) calloc(sz, sizeof($*1_ltype));
|
$1 = ($1_ltype) calloc(sz, sizeof($*1_ltype));
|
||||||
|
|
@ -353,7 +389,7 @@ JAVA_ARRAYS_TYPEMAPS(double, jdouble, Double) /* double[ANY] */
|
||||||
}
|
}
|
||||||
|
|
||||||
%typemap(freearg) ARRAYSOFCLASSES[ANY]
|
%typemap(freearg) ARRAYSOFCLASSES[ANY]
|
||||||
#ifdef __cplusplus
|
#if __cplusplus
|
||||||
%{ delete [] $1; %}
|
%{ delete [] $1; %}
|
||||||
#else
|
#else
|
||||||
%{ free($1); %}
|
%{ free($1); %}
|
||||||
|
|
@ -392,8 +428,15 @@ JAVA_ARRAYS_TYPEMAPS(double, jdouble, Double) /* double[ANY] */
|
||||||
return $jnicall;
|
return $jnicall;
|
||||||
}
|
}
|
||||||
|
|
||||||
%typemap(in) ARRAYSOFENUMS[ANY] (jint *jarr)
|
%typemap(in) ARRAYSOFENUMS[] (jint *jarr)
|
||||||
%{ if (!SWIG_JavaArrayInInt(jenv, &jarr, (int**)&$1, $input)) return $null; %}
|
%{ if (!SWIG_JavaArrayInInt(jenv, &jarr, (int**)&$1, $input)) return $null; %}
|
||||||
|
%typemap(in) ARRAYSOFENUMS[ANY] (jint *jarr) {
|
||||||
|
if ($input && JCALL1(GetArrayLength, jenv, $input) != $1_size) {
|
||||||
|
SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "incorrect array size");
|
||||||
|
return $null;
|
||||||
|
}
|
||||||
|
if (!SWIG_JavaArrayInInt(jenv, &jarr, (int**)&$1, $input)) return $null;
|
||||||
|
}
|
||||||
%typemap(argout) ARRAYSOFENUMS[ANY]
|
%typemap(argout) ARRAYSOFENUMS[ANY]
|
||||||
%{ SWIG_JavaArrayArgoutInt(jenv, jarr$argnum, (int*)$1, $input); %}
|
%{ SWIG_JavaArrayArgoutInt(jenv, jarr$argnum, (int*)$1, $input); %}
|
||||||
%typemap(out) ARRAYSOFENUMS[ANY]
|
%typemap(out) ARRAYSOFENUMS[ANY]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue