2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 #ifndef __SCILABPYTHONENVIRONMENTWRAPPER_HXX__
14 #define __SCILABPYTHONENVIRONMENTWRAPPER_HXX__
16 #include "ExternalObjects.h"
17 #include "JavaOptionsHelper.hxx"
19 #include "ScilabAbstractEnvironmentWrapper.hxx"
20 #include "ScilabJavaException.hxx"
22 #include "ScilabJavaObject.hxx"
24 #include "GiwsException.hxx"
27 #include "getScilabJavaVM.h"
30 #define SCILABJAVAOBJECT "org/scilab/modules/external_objects_java/ScilabJavaObject"
32 using namespace org_modules_external_objects;
34 namespace org_scilab_modules_external_objects_java
43 class ScilabJavaEnvironmentWrapper : public ScilabAbstractEnvironmentWrapper
46 // JavaVariablesScope & scope;
47 JavaOptionsHelper & helper;
48 jclass ScilabJavaObjectClass_;
50 jmethodID unwrapStringID_;
51 jmethodID unwrapRowStringID_;
52 jmethodID unwrapMatStringID_;
54 jmethodID unwrapBooleanID_;
55 jmethodID unwrapRowBooleanID_;
56 jmethodID unwrapMatBooleanID_;
58 jmethodID unwrapByteID_;
59 jmethodID unwrapRowByteID_;
60 jmethodID unwrapMatByteID_;
62 jmethodID unwrapShortID_;
63 jmethodID unwrapRowShortID_;
64 jmethodID unwrapMatShortID_;
66 jmethodID unwrapIntID_;
67 jmethodID unwrapRowIntID_;
68 jmethodID unwrapMatIntID_;
70 jmethodID unwrapLongID_;
71 jmethodID unwrapRowLongID_;
72 jmethodID unwrapMatLongID_;
74 jmethodID unwrapDoubleID_;
75 jmethodID unwrapRowDoubleID_;
76 jmethodID unwrapMatDoubleID_;
78 jmethodID unwrapFloatID_;
79 jmethodID unwrapRowFloatID_;
80 jmethodID unwrapMatFloatID_;
82 jmethodID wrapAsDirectByteBufferID_;
83 jmethodID wrapAsDirectDoubleBufferID_;
84 jmethodID wrapAsDirectIntBufferID_;
85 jmethodID wrapAsDirectCharBufferID_;
86 jmethodID wrapAsDirectFloatBufferID_;
87 jmethodID wrapAsDirectLongBufferID_;
88 jmethodID wrapAsDirectShortBufferID_;
92 ScilabJavaEnvironmentWrapper(JavaOptionsHelper & _helper) : helper(_helper)
94 JavaVM * vm = getScilabJavaVM();
95 JNIEnv * curEnv = NULL;
97 vm->AttachCurrentThread(reinterpret_cast<void **>(&curEnv), NULL);
98 ScilabJavaObjectClass_ = curEnv->FindClass(SCILABJAVAOBJECT);
100 unwrapStringID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapString", "(I)Ljava/lang/String;");
101 unwrapRowStringID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowString", "(I)[Ljava/lang/String;");
102 unwrapMatStringID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatString", "(I)[[Ljava/lang/String;");
104 unwrapBooleanID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapBoolean", "(I)Z");
105 unwrapRowBooleanID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowBoolean", "(I)[Z");
106 unwrapMatBooleanID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatBoolean", "(I)[[Z");
108 unwrapByteID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapByte", "(I)B");
109 unwrapRowByteID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowByte", "(I)Ljava/lang/Object;");
110 unwrapMatByteID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatByte", "(I)[[B");
112 unwrapShortID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapShort", "(I)S");
113 unwrapRowShortID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowShort", "(I)Ljava/lang/Object;");
114 unwrapMatShortID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatShort", "(I)[[S");
116 unwrapIntID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapInt", "(I)I");
117 unwrapRowIntID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowInt", "(I)Ljava/lang/Object;");
118 unwrapMatIntID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatInt", "(I)[[I");
120 unwrapLongID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapLong", "(I)J");
121 unwrapRowLongID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowLong", "(I)Ljava/lang/Object;");
122 unwrapMatLongID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatLong", "(I)[[J");
124 unwrapDoubleID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapDouble", "(I)D");
125 unwrapRowDoubleID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowDouble", "(I)Ljava/lang/Object;");
126 unwrapMatDoubleID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatDouble", "(I)[[D");
128 unwrapFloatID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapFloat", "(I)F");
129 unwrapRowFloatID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowFloat", "(I)Ljava/lang/Object;");
130 unwrapMatFloatID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatFloat", "(I)[[F");
132 wrapAsDirectByteBufferID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "wrapAsDirectByteBuffer", "(Ljava/nio/ByteBuffer;)I");
133 wrapAsDirectDoubleBufferID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "wrapAsDirectDoubleBuffer", "(Ljava/nio/ByteBuffer;)I");
134 wrapAsDirectIntBufferID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "wrapAsDirectIntBuffer", "(Ljava/nio/ByteBuffer;)I");
135 wrapAsDirectCharBufferID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "wrapAsDirectCharBuffer", "(Ljava/nio/ByteBuffer;)I");
136 wrapAsDirectFloatBufferID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "wrapAsDirectFloatBuffer", "(Ljava/nio/ByteBuffer;)I");
137 wrapAsDirectLongBufferID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "wrapAsDirectLongBuffer", "(Ljava/nio/ByteBuffer;)I");
138 wrapAsDirectShortBufferID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "wrapAsDirectShortBuffer", "(Ljava/nio/ByteBuffer;)I");
141 ~ScilabJavaEnvironmentWrapper() { }
143 VariableType isunwrappable(int id) const;
145 int wrap(double * x, const bool isRef) const;
147 int wrap(double * x, int xSize, const bool isRef) const;
149 int wrap(double * x, int xSize, int xSizeCol, const bool isRef) const;
151 int wrap(double * re, double * im, const bool isRef) const;
153 int wrap(double * re, double * im, int xSize, const bool isRef) const;
155 int wrap(double * re, double * im, int xSize, int xSizeCol, const bool isRef) const;
157 int wrap(char * x, const bool isRef) const;
159 int wrap(char * x, int xSize, const bool isRef) const;
161 int wrap(char * x, int xSize, int xSizeCol, const bool isRef) const;
164 int wrap(unsigned char * x, const bool isRef) const;
166 int wrap(unsigned char * x, int xSize, const bool isRef) const;
168 int wrap(unsigned char * x, int xSize, int xSizeCol, const bool isRef) const;
171 int wrap(short * x, const bool isRef) const;
173 int wrap(short * x, int xSize, const bool isRef) const;
175 int wrap(short * x, int xSize, int xSizeCol, const bool isRef) const;
178 int wrap(unsigned short * x, const bool isRef) const;
180 int wrap(unsigned short * x, int xSize, const bool isRef) const;
182 int wrap(unsigned short * x, int xSize, int xSizeCol, const bool isRef) const;
185 int wrap(int * x, const bool isRef) const;
187 int wrap(int * x, int xSize, const bool isRef) const;
189 int wrap(int * x, int xSize, int xSizeCol, const bool isRef) const;
192 int wrap(unsigned int * x, const bool isRef) const;
194 int wrap(unsigned int * x, int xSize, const bool isRef) const;
196 int wrap(unsigned int * x, int xSize, int xSizeCol, const bool isRef) const;
198 #ifdef __SCILAB_INT64__
200 int wrap(long long * x, const bool isRef) const;
202 int wrap(long long * x, int xSize, const bool isRef) const;
204 int wrap(long long * x, int xSize, int xSizeCol, const bool isRef) const;
207 int wrap(unsigned long long * x, const bool isRef) const;
209 int wrap(unsigned long long * x, int xSize, const bool isRef) const;
211 int wrap(unsigned long long * x, int xSize, int xSizeCol, const bool isRef) const;
215 int wrap(char ** x, const bool isRef) const;
217 int wrap(char ** x, int xSize, const bool isRef) const;
219 int wrap(char ** x, int xSize, int xSizeCol, const bool isRef) const;
222 int wrapFloat(double * x, const bool isRef) const;
224 int wrapFloat(double * x, int xSize, const bool isRef) const;
226 int wrapFloat(double * x, int xSize, int xSizeCol, const bool isRef) const;
229 int wrapBool(int * x, const bool isRef) const;
231 int wrapBool(int * x, int xSize, const bool isRef) const;
233 int wrapBool(int * x, int xSize, int xSizeCol, const bool isRef) const;
236 * Unwrap a the external object with the given id into a double
237 * @param id the value to wrap
238 * @param pos the position in the Scilab stack where to put the result
240 void unwrapdouble(int id, const ScilabDoubleStackAllocator & allocator) const;
242 void unwraprowdouble(int id, const ScilabDoubleStackAllocator & allocator) const;
244 void unwrapmatdouble(int id, const ScilabDoubleStackAllocator & allocator) const;
246 void unwrapcomplex(int id, const ScilabComplexStackAllocator & allocator) const;
248 void unwraprowcomplex(int id, const ScilabComplexStackAllocator & allocator) const;
250 void unwrapmatcomplex(int id, const ScilabComplexStackAllocator & allocator) const;
252 void unwrapchar(int id, const ScilabCharStackAllocator & allocator) const;
254 void unwraprowchar(int id, const ScilabCharStackAllocator & allocator) const;
256 void unwrapmatchar(int id, const ScilabCharStackAllocator & allocator) const;
258 void unwrapuchar(int id, const ScilabUCharStackAllocator & allocator) const;
260 void unwraprowuchar(int id, const ScilabUCharStackAllocator & allocator) const;
262 void unwrapmatuchar(int id, const ScilabUCharStackAllocator & allocator) const ;
264 void unwrapshort(int id, const ScilabShortStackAllocator & allocator) const;
266 void unwraprowshort(int id, const ScilabShortStackAllocator & allocator) const;
268 void unwrapmatshort(int id, const ScilabShortStackAllocator & allocator) const;
270 void unwrapushort(int id, const ScilabUShortStackAllocator & allocator) const;
272 void unwraprowushort(int id, const ScilabUShortStackAllocator & allocator) const;
274 void unwrapmatushort(int id, const ScilabUShortStackAllocator & allocator) const;
276 void unwrapint(int id, const ScilabIntStackAllocator & allocator) const;
278 void unwraprowint(int id, const ScilabIntStackAllocator & allocator) const;
280 void unwrapmatint(int id, const ScilabIntStackAllocator & allocator) const;
282 void unwrapuint(int id, const ScilabUIntStackAllocator & allocator) const;
284 void unwraprowuint(int id, const ScilabUIntStackAllocator & allocator) const;
286 void unwrapmatuint(int id, const ScilabUIntStackAllocator & allocator) const;
288 void unwraplong(int id, const ScilabLongStackAllocator & allocator) const;
290 void unwraprowlong(int id, const ScilabLongStackAllocator & allocator) const;
292 void unwrapmatlong(int id, const ScilabLongStackAllocator & allocator) const;
294 void unwrapulong(int id, const ScilabULongStackAllocator & allocator) const;
296 void unwraprowulong(int id, const ScilabULongStackAllocator & allocator) const;
298 void unwrapmatulong(int id, const ScilabULongStackAllocator & allocator) const;
300 void unwrapboolean(int id, const ScilabBooleanStackAllocator & allocator) const;
302 void unwraprowboolean(int id, const ScilabBooleanStackAllocator & allocator) const;
304 void unwrapmatboolean(int id, const ScilabBooleanStackAllocator & allocator) const;
306 void unwrapstring(int id, const ScilabStringStackAllocator & allocator) const;
308 void unwraprowstring(int id, const ScilabStringStackAllocator & allocator) const;
310 void unwrapmatstring(int id, const ScilabStringStackAllocator & allocator) const;
312 void unwrapfloat(int id, const ScilabFloatStackAllocator & allocator) const;
314 void unwraprowfloat(int id, const ScilabFloatStackAllocator & allocator) const;
316 void unwrapmatfloat(int id, const ScilabFloatStackAllocator & allocator) const;
321 inline int wrap(JavaVM * vm, T * x) const
323 return ScilabJavaObject::wrap(vm, *x);
326 template<typename T, typename U>
327 inline int wrap(JavaVM * vm, T * x) const
329 return ScilabJavaObject::wrap(vm, (U) * x);
333 inline int wrap(JavaVM * vm, T * x, const int len) const
335 return ScilabJavaObject::wrap(vm, x, len);
338 template<typename T, typename U>
339 inline int wrap(JavaVM * vm, T * x, const int len) const
343 for (i = 0; i < len; i++)
348 i = ScilabJavaObject::wrap(vm, xx, len);
355 inline int wrap(JavaVM * vm, T * x, const int r, const int c) const
357 if (helper.getMethodOfConv())
361 for (i = 0; i < r; i++)
364 for (j = 0; j < c; j++)
366 xx[i][j] = x[j * r + i];
369 j = ScilabJavaObject::wrap(vm, xx, r, c);
370 for (i = 0; i < r; delete[] xx[i++]);
379 for (i = 1; i < c; xx[i] = xx[i++ - 1] + r);
380 i = ScilabJavaObject::wrap(vm, xx, c, r);
386 template<typename T, typename U>
387 inline int wrap(JavaVM * vm, T * x, const int r, const int c) const
389 if (helper.getMethodOfConv())
393 for (i = 0; i < r; i++)
396 for (j = 0; j < c; j++)
398 xx[i][j] = (U)(x[j * r + i]);
401 j = ScilabJavaObject::wrap(vm, xx, r, c);
402 for (i = 0; i < r; delete[] xx[i++]);
410 for (i = 0; i < c; i++)
413 for (j = 0; j < r; j++)
415 xx[i][j] = (U)(x[i * r + j]);
418 j = ScilabJavaObject::wrap(vm, xx, c, r);
419 for (i = 0; i < c; delete[] xx[i++]);
425 inline const jmethodID getSingleMethod(const char * x) const
427 return unwrapByteID_;
429 inline const jmethodID getSingleMethod(const short * x) const
431 return unwrapShortID_;
433 inline const jmethodID getSingleMethod(const unsigned short * x) const
435 return unwrapShortID_;
437 inline const jmethodID getSingleMethod(const int * x) const
441 inline const jmethodID getSingleMethod(const long long * x) const
443 return unwrapLongID_;
445 inline const jmethodID getSingleMethod(const double * x) const
447 return unwrapDoubleID_;
449 inline const jmethodID getSingleMethod(const bool * x) const
451 return unwrapBooleanID_;
453 inline const jmethodID getSingleMethod(const float * x) const
455 return unwrapFloatID_;
458 inline const jmethodID getRowMethod(const char * x) const
460 return unwrapRowByteID_;
462 inline const jmethodID getRowMethod(const short * x) const
464 return unwrapRowShortID_;
466 inline const jmethodID getRowMethod(const unsigned short * x) const
468 return unwrapRowShortID_;
470 inline const jmethodID getRowMethod(const int * x) const
472 return unwrapRowIntID_;
474 inline const jmethodID getRowMethod(const long long * x) const
476 return unwrapRowLongID_;
478 inline const jmethodID getRowMethod(const double * x) const
480 return unwrapRowDoubleID_;
482 inline const jmethodID getRowMethod(const bool * x) const
484 return unwrapRowBooleanID_;
486 inline const jmethodID getRowMethod(const float * x) const
488 return unwrapRowFloatID_;
491 inline const jmethodID getMatMethod(const char * x) const
493 return unwrapMatByteID_;
495 inline const jmethodID getMatMethod(const short * x) const
497 return unwrapMatShortID_;
499 inline const jmethodID getMatMethod(const unsigned short * x) const
501 return unwrapMatShortID_;
503 inline const jmethodID getMatMethod(const int * x) const
505 return unwrapMatIntID_;
507 inline const jmethodID getMatMethod(const long long * x) const
509 return unwrapMatLongID_;
511 inline const jmethodID getMatMethod(const double * x) const
513 return unwrapMatDoubleID_;
515 inline const jmethodID getMatMethod(const bool * x) const
517 return unwrapMatBooleanID_;
519 inline const jmethodID getMatMethod(const float * x) const
521 return unwrapMatFloatID_;
524 inline const jmethodID getDBMethod(const char * x) const
526 return wrapAsDirectByteBufferID_;
528 inline const jmethodID getDBMethod(const double * x) const
530 return wrapAsDirectDoubleBufferID_;
532 inline const jmethodID getDBMethod(const int * x) const
534 return wrapAsDirectIntBufferID_;
536 inline const jmethodID getDBMethod(const float * x) const
538 return wrapAsDirectFloatBufferID_;
540 inline const jmethodID getDBMethod(const long long * x) const
542 return wrapAsDirectLongBufferID_;
544 inline const jmethodID getDBMethod(const short * x) const
546 return wrapAsDirectShortBufferID_;
549 template <typename T>
550 inline T CallStatic(JNIEnv * env_, const char * x, int javaID) const
552 return static_cast<T>(env_->CallStaticByteMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
555 template <typename T>
556 inline T CallStatic(JNIEnv * env_, const short * x, int javaID) const
558 return static_cast<T>(env_->CallStaticShortMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
561 template <typename T>
562 inline T CallStatic(JNIEnv * env_, const unsigned short * x, int javaID) const
564 return static_cast<T>(env_->CallStaticShortMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
567 template <typename T>
568 inline T CallStatic(JNIEnv * env_, const int * x, int javaID) const
570 return static_cast<T>(env_->CallStaticIntMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
573 template <typename T>
574 inline T CallStatic(JNIEnv * env_, const long long * x, int javaID) const
576 return static_cast<T>(env_->CallStaticLongMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
579 template <typename T>
580 inline T CallStatic(JNIEnv * env_, const double * x, int javaID) const
582 return static_cast<T>(env_->CallStaticDoubleMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
585 template <typename T>
586 inline T CallStatic(JNIEnv * env_, const bool * x, int javaID) const
588 return static_cast<T>(env_->CallStaticBooleanMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
591 template <typename T>
592 inline T CallStatic(JNIEnv * env_, const float * x, int javaID) const
594 return static_cast<T>(env_->CallStaticFloatMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
597 template <typename T, typename U, class V>
598 inline void unwrapSingle(JavaVM * jvm_, const int javaID, const V & allocator) const
600 unwrapSingle<T, U, U>(jvm_, javaID, allocator);
603 template <typename T, typename U, typename V, class W>
604 inline void unwrapSingle(JavaVM * jvm_, const int javaID, const W & allocator, const bool mustAlloc = false) const
606 JNIEnv * curEnv = NULL;
610 jvm_->AttachCurrentThread(reinterpret_cast<void **>(&curEnv), NULL);
612 T res = CallStatic<T>(curEnv, (V *)addr, javaID);
613 if (curEnv->ExceptionCheck())
615 throw GiwsException::JniCallMethodException(curEnv);
620 addr = reinterpret_cast<U *>(allocator.allocate(1, 1, 0));
621 *addr = static_cast<U>(res);
625 allocator.allocate(1, 1, reinterpret_cast<V *>(&res));
629 template <typename T, typename U, class V>
630 inline void unwrapRow(JavaVM * jvm_, const int javaID, const V & allocator) const
632 unwrapRow<T, U, U>(jvm_, javaID, allocator);
635 template <typename T, typename U, typename V, class W>
636 inline void unwrapRow(JavaVM * jvm_, const int javaID, const W & allocator, const bool mustAlloc = false) const
639 jboolean isCopy = JNI_FALSE;
640 JNIEnv * curEnv = NULL;
643 jobjectArray array = 0;
646 jvm_->AttachCurrentThread(reinterpret_cast<void **>(&curEnv), NULL);
648 res = curEnv->CallStaticObjectMethod(ScilabJavaObjectClass_, getRowMethod((V *)addr), javaID);
649 if (curEnv->ExceptionCheck())
651 curEnv->DeleteLocalRef(res);
652 throw GiwsException::JniCallMethodException(curEnv);
655 buffer = curEnv->GetDirectBufferAddress(res);
658 const jlong len = curEnv->GetDirectBufferCapacity(res);
659 V * resultsArray = (V *)buffer;
664 addr = reinterpret_cast<U* >(allocator.allocate(1, len, 0));
665 for (jlong i = 0; i < len; i++)
667 addr[i] = static_cast<U>(resultsArray[i]);
670 catch (const ScilabAbstractEnvironmentException & e)
672 curEnv->DeleteLocalRef(res);
680 allocator.allocate(1, len, reinterpret_cast<V *>(resultsArray));
682 catch (const ScilabAbstractEnvironmentException & e)
684 curEnv->DeleteLocalRef(res);
691 array = static_cast<jobjectArray>(res);
692 lenRow = curEnv->GetArrayLength(array);
698 addr = reinterpret_cast<U *>(allocator.allocate(1, lenRow, 0));
700 catch (const ScilabAbstractEnvironmentException & e)
702 curEnv->DeleteLocalRef(array);
707 T * resultsArray = static_cast<T *>(curEnv->GetPrimitiveArrayCritical(array, &isCopy));
711 for (int i = 0; i < lenRow; i++)
713 addr[i] = static_cast<U>(resultsArray[i]);
720 allocator.allocate(1, lenRow, reinterpret_cast<V *>(resultsArray));
722 catch (const ScilabAbstractEnvironmentException & e)
724 curEnv->ReleasePrimitiveArrayCritical(array, resultsArray, JNI_ABORT);
725 curEnv->DeleteLocalRef(array);
730 curEnv->ReleasePrimitiveArrayCritical(array, resultsArray, JNI_ABORT);
731 curEnv->DeleteLocalRef(array);
733 if (curEnv->ExceptionCheck())
735 throw GiwsException::JniCallMethodException(curEnv);
739 template <typename T, typename U, class V>
740 inline void unwrapMat(JavaVM * jvm_, const int javaID, const V & allocator) const
742 unwrapMat<T, U, U>(jvm_, javaID, allocator);
745 template <typename T, typename U, typename V, class W>
746 inline void unwrapMat(JavaVM * jvm_, const int javaID, const W & allocator) const
749 jboolean isCopy = JNI_FALSE;
751 JNIEnv * curEnv = NULL;
754 jvm_->AttachCurrentThread(reinterpret_cast<void **>(&curEnv), NULL);
756 jobjectArray res = static_cast<jobjectArray>(curEnv->CallStaticObjectMethod(ScilabJavaObjectClass_, getMatMethod((V *)addr), javaID));
757 if (curEnv->ExceptionCheck())
759 throw GiwsException::JniCallMethodException(curEnv);
762 lenRow = curEnv->GetArrayLength(res);
763 oneDim = reinterpret_cast<jarray>(curEnv->GetObjectArrayElement(res, 0));
764 lenCol = curEnv->GetArrayLength(oneDim);
765 curEnv->DeleteLocalRef(oneDim);
769 if (helper.getMethodOfConv())
771 addr = (U *)allocator.allocate(lenRow, lenCol, 0);
775 addr = (U *)allocator.allocate(lenCol, lenRow, 0);
778 catch (const ScilabAbstractEnvironmentException & e)
780 curEnv->DeleteLocalRef(res);
785 for (int i = 0; i < lenRow; i++)
787 oneDim = reinterpret_cast<jarray>(curEnv->GetObjectArrayElement(res, i));
788 resultsArray = static_cast<T *>(curEnv->GetPrimitiveArrayCritical(oneDim, &isCopy));
789 if (helper.getMethodOfConv())
791 for (int j = 0; j < lenCol; j++)
793 addr[j * lenRow + i] = static_cast<U>(resultsArray[j]);
798 for (int j = 0; j < lenCol; j++)
800 addr[i * lenCol + j] = static_cast<U>(resultsArray[j]);
803 curEnv->ReleasePrimitiveArrayCritical(oneDim, resultsArray, JNI_ABORT);
804 curEnv->DeleteLocalRef(oneDim);
807 curEnv->DeleteLocalRef(res);
808 if (curEnv->ExceptionCheck())
810 throw GiwsException::JniCallMethodException(curEnv);
815 inline int wrapAsDirectBuffer(JavaVM * jvm_, T * data, const int size) const
817 JNIEnv * curEnv = NULL;
818 jvm_->AttachCurrentThread(reinterpret_cast<void **>(&curEnv), NULL);
820 jobject dbuffer = curEnv->NewDirectByteBuffer((void*)data, (jlong)(size * sizeof(T)));
821 int id = static_cast<jint>(curEnv->CallStaticIntMethod(ScilabJavaObjectClass_, getDBMethod(data), dbuffer));
823 curEnv->DeleteLocalRef(dbuffer);
824 if (curEnv->ExceptionCheck())
826 throw GiwsException::JniCallMethodException(curEnv);
834 #endif // __SCILABJAVAENVIRONMENTWRAPPER_HXX__