Continue the work on the wrap 17/11917/4
Sylvestre Ledru [Wed, 3 Jul 2013 11:49:28 +0000 (13:49 +0200)]
Change-Id: I3415a423dab5260e4e5d666d962f01a3974d6fbd

scilab/modules/external_objects_java/TODO
scilab/modules/external_objects_java/src/cpp/ScilabJavaEnvironment.cpp
scilab/modules/external_objects_java/tests/unit_tests/jarray.tst
scilab/modules/external_objects_java/tests/unit_tests/jwrap.dia.ref
scilab/modules/external_objects_java/tests/unit_tests/jwrap.tst

index a87682b..4b0055b 100644 (file)
@@ -1,5 +1,5 @@
 jallowClassReloading
-jarray
+jarray                   - Tests are failing
 jautoUnwrap
 jcast
 jconvMatrixMethod
@@ -11,5 +11,5 @@ junwrap
 jvoid
 jwrapinchar
 jwrapinfloat
-jwrap (a faire pour les autres types)
+jwrap                    - Other types should be managed
 new
index 8d1838e..921ae8a 100644 (file)
@@ -677,84 +677,10 @@ int ScilabJavaEnvironment::getarrayelement(int id, int * index, int length)
 
         writeLog("getarrayelement", "Get element from array with id %d and with index: %s.", id, os.str().c_str());
     }
-    /*
-        PyObject * obj = scope.getObject(id);
-        if (!obj)
-        {
-            throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid object with id %d"), id);
-        }
-
-        if (!PyList_Check(obj) && !PyArray_Check(obj))
-        {
-            throw ScilabJavaException(__LINE__, __FILE__, gettext("Not a list or an array"));
-        }
-
-        if (length == 0)
-        {
-            return 0;
-        }
-
-        if (PyList_Check(obj))
-        {
-            for (int i = 0; i < length; i++)
-            {
-                if (index[i] < 0 || index[i] >= PyList_Size(obj))
-                {
-                    throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid index"));
-                }
-                obj = PyList_GetItem(obj, index[i]);
-                if (i != length - 1 && !PyList_Check(obj))
-                {
-                    throw ScilabJavaException(__LINE__, __FILE__, gettext("Not a list"));
-                }
-            }
-       Py_INCREF(obj);
-        }
-        else if (PyArray_Check(obj))
-        {
-            PyArrayObject * arr = reinterpret_cast<PyArrayObject *>(obj);
-            npy_intp * ind = reinterpret_cast<npy_intp *>(index);
-
-            if (length != PyArray_NDIM(arr))
-            {
-                throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid index dimension"));
-            }
-
-            npy_intp * dims = PyArray_DIMS(arr);
-
-            if (sizeof(int) != sizeof(npy_intp))
-            {
-                ind = new npy_intp[length];
-                for (int i = 0; i < length; i++)
-                {
-                    if (index[i] < 0 || index[i] >= dims[i])
-                    {
-                        delete[] ind;
-                        throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid index at position %d"), i + 1);
-                    }
-
-                    ind[i] = static_cast<npy_intp>(index[i]);
-                }
-            }
 
-            obj = PyArray_GETITEM(arr, PyArray_GetPtr(arr, ind));
-
-            if (sizeof(int) != sizeof(npy_intp))
-            {
-                delete[] ind;
-            }
-        }
-        else
-        {
-            throw ScilabJavaException(__LINE__, __FILE__, gettext("Must be a list or a Numpy array"));
-        }
-
-        int ret = scope.addObject(obj);
-        writeLog("getarrayelement", "returned id %d.", ret);
+    JavaVM * vm = getScilabJavaVM();
 
-        return ret;
-    */
-    return 0;
+    return ScilabJavaObject::getArrayElement(vm, id, index, length);
 }
 
 void ScilabJavaEnvironment::setarrayelement(int id, int * index, int length, int idArg)
@@ -771,82 +697,11 @@ void ScilabJavaEnvironment::setarrayelement(int id, int * index, int length, int
 
         writeLog("setarrayelement", "Set element with id %d in array with id %d and with index: %s.", idArg, id, os.str().c_str());
     }
-    /*
-        PyObject * obj = scope.getObject(id);
-        if (!obj)
-        {
-            throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid object with id %d"), id);
-        }
-
-        PyObject * value = scope.getObject(idArg);
-        if (!value)
-        {
-            throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid object with id %d"), id);
-        }
-
-        if (PyList_Check(obj))
-        {
-            if (length == 0)
-            {
-                return;
-            }
-
-            for (int i = 0; i < length - 1; i++)
-            {
-                if (index[i] < 0 || index[i] >= PyList_Size(obj))
-                {
-                    throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid index at position %d"), i + 1);
-                }
-                obj = PyList_GetItem(obj, index[i]);
-                if (!PyList_Check(obj))
-                {
-                    throw ScilabJavaException(__LINE__, __FILE__, gettext("Not a list at position %d"), index[i]);
-                }
-            }
-
-            Py_INCREF(value);
-            PyList_SetItem(obj, index[length - 1], value);
-        }
-        else if (PyArray_Check(obj))
-        {
-            PyArrayObject * arr = reinterpret_cast<PyArrayObject *>(obj);
-            npy_intp * ind = reinterpret_cast<npy_intp *>(index);
-
-            if (length != PyArray_NDIM(arr))
-            {
-                throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid index dimension"));
-            }
-
-            npy_intp * dims = PyArray_DIMS(arr);
-
-            if (sizeof(int) != sizeof(npy_intp))
-            {
-                ind = new npy_intp[length];
-                for (int i = 0; i < length; i++)
-                {
-                    if (index[i] < 0 || index[i] >= dims[i])
-                    {
-                        delete[] ind;
-                        throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid index at position %d"), i);
-                    }
 
-                    ind[i] = static_cast<npy_intp>(index[i]);
-                }
-            }
-
-            int ret = PyArray_SETITEM(arr, PyArray_GetPtr(arr, ind), value);
+    JavaVM * vm = getScilabJavaVM();
 
-            if (sizeof(int) != sizeof(npy_intp))
-            {
-                delete[] ind;
-            }
+    ScilabJavaObject::setArrayElement(vm, id, index, length, idArg);
 
-            if (ret == -1)
-            {
-                throw ScilabJavaException(__LINE__, __FILE__, gettext("Cannot set the value in the array"));
-            }
-        }
-    */
     writeLog("setarrayelement", "Successfully set");
 }
 
index 8cd95aa..750e8b4 100644 (file)
@@ -6,6 +6,10 @@
 // =============================================================================
 
 a = jarray("java.lang.String", 2, 2, 3);
+str = "Hi Jims again !!"
 // array insertion
 a(0, 0, 2) = "Hi Jims !";
-a(1, 0, 2) = "Hi Jims again !!"
+a(1, 0, 2) = str;
+
+assert_checkequal(length(str), length(a(1, 0, 2)));
+assert_checkequal(str, a(1, 0, 2));
index 78c930c..ace3570 100644 (file)
@@ -11,3 +11,139 @@ assert_checkequal(typeof(a),"_EObj");
 assert_checkequal(jgetclassname(a),"double");
 b=junwrap(a);
 assert_checkequal(typeof(b),"constant");
+c = jcompile("Test", ["public class Test {";
+    "public int[] returnArrayInt() {";
+    "    return new int[]{1,2,3,4};";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(int32([1,2,3,4]), t.returnArrayInt());
+c = jcompile("Test", ["public class Test {";
+    "public int returnInt() {";
+    "    return 32;";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(int32(32), t.returnInt());
+//////////////////////////////////////////////
+c = jcompile("Test", ["public class Test {";
+    "public double[] returnDouble() {";
+    "    return new double[]{1,2,3,4};";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal([1,2,3,4], t.returnDouble());
+c = jcompile("Test", ["public class Test {";
+    "public double returnDouble() {";
+    "    return 32;";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(32, t.returnDouble());
+//////////////////////////////////////////////
+c = jcompile("Test", ["public class Test {";
+    "public byte[] returnByte() {";
+    "    return new byte[]{1,2,3,4};";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(int8([1,2,3,4]), t.returnByte());
+c = jcompile("Test", ["public class Test {";
+    "public byte returnByte() {";
+    "    return 32;";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(int8(32), t.returnByte());
+//////////////////////////////////////////////
+c = jcompile("Test", ["public class Test {";
+    "public short[] returnShort() {";
+    "    return new short[]{1,2,3,4};";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(int16([1,2,3,4]), t.returnShort());
+c = jcompile("Test", ["public class Test {";
+    "public short returnShort() {";
+    "    return 32;";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(int16(32), t.returnShort());
+//////////////////////////////////////////////
+// c = jcompile("Test", ["public class Test {";
+//     "public long[] returnLong() {";
+//     "    return new long[]{1L,2L,3L,4L};";
+//     "}";
+//     "}";]);
+// t = c.new();
+// assert_checkequal(jgetclassname(c),"Test");
+// assert_checkequal(int16([1,2,3,4]), t.returnLong());
+// c = jcompile("Test", ["public class Test {";
+//     "public long returnLong() {";
+//     "    return 32L;";
+//     "}";
+//     "}";]);
+// t = c.new();
+// assert_checkequal(jgetclassname(c),"Test");
+// assert_checkequal(int16(32), t.returnLong());
+//////////////////////////////////////////////
+// c = jcompile("Test", ["public class Test {";
+//     "public float[] returnFloat() {";
+//     "    return new float[]{1f,2f,3f,4f};";
+//     "}";
+//     "}";]);
+// t = c.new();
+// assert_checkequal(jgetclassname(c),"Test");
+// assert_checkequal(int16([1,2,3,4]), t.returnFloat());
+// c = jcompile("Test", ["public class Test {";
+//     "public float returnFloat() {";
+//     "    return 32f;";
+//     "}";
+//     "}";]);
+// t = c.new();
+// assert_checkequal(jgetclassname(c),"Test");
+// assert_checkequal(int16(32), t.returnFloat());
+//////////////////////////////////////////////
+// c = jcompile("Test", ["public class Test {";
+//     "public char[] returnChar() {";
+//     "    return new char[]{''a'',''b'',''c'',''d''};";
+//     "}";
+//     "}";]);
+// t = c.new();
+// assert_checkequal(jgetclassname(c),"Test");
+// assert_checkequal(['a','b','c','d'], t.returnChar());
+// c = jcompile("Test", ["public class Test {";
+//     "public char returnChar() {";
+//     "    return ''a'';";
+//     "}";
+//     "}";]);
+// t = c.new();
+// assert_checkequal(jgetclassname(c),"Test");
+// assert_checkequal(int16(32), t.returnChar());
+//
+//////////////////////////////////////////////
+c = jcompile("Test", ["public class Test {";
+    "public boolean[] returnBoolean() {";
+    "    return new boolean[]{true,true,false,false};";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal([%t,%t,%f,%f], t.returnBoolean());
+c = jcompile("Test", ["public class Test {";
+    "public boolean returnBoolean() {";
+    "    return true;";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(%t, t.returnBoolean());
index 545b5f1..ae41b0e 100644 (file)
@@ -14,3 +14,167 @@ assert_checkequal(typeof(a),"_EObj");
 assert_checkequal(jgetclassname(a),"double");
 b=junwrap(a);
 assert_checkequal(typeof(b),"constant");
+
+
+c = jcompile("Test", ["public class Test {";
+    "public int[] returnArrayInt() {";
+    "    return new int[]{1,2,3,4};";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(int32([1,2,3,4]), t.returnArrayInt());
+
+c = jcompile("Test", ["public class Test {";
+    "public int returnInt() {";
+    "    return 32;";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(int32(32), t.returnInt());
+
+//////////////////////////////////////////////
+
+c = jcompile("Test", ["public class Test {";
+    "public double[] returnDouble() {";
+    "    return new double[]{1,2,3,4};";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal([1,2,3,4], t.returnDouble());
+
+c = jcompile("Test", ["public class Test {";
+    "public double returnDouble() {";
+    "    return 32;";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(32, t.returnDouble());
+
+//////////////////////////////////////////////
+
+c = jcompile("Test", ["public class Test {";
+    "public byte[] returnByte() {";
+    "    return new byte[]{1,2,3,4};";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(int8([1,2,3,4]), t.returnByte());
+
+c = jcompile("Test", ["public class Test {";
+    "public byte returnByte() {";
+    "    return 32;";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(int8(32), t.returnByte());
+
+//////////////////////////////////////////////
+
+c = jcompile("Test", ["public class Test {";
+    "public short[] returnShort() {";
+    "    return new short[]{1,2,3,4};";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(int16([1,2,3,4]), t.returnShort());
+
+c = jcompile("Test", ["public class Test {";
+    "public short returnShort() {";
+    "    return 32;";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(int16(32), t.returnShort());
+
+
+//////////////////////////////////////////////
+
+// c = jcompile("Test", ["public class Test {";
+//     "public long[] returnLong() {";
+//     "    return new long[]{1L,2L,3L,4L};";
+//     "}";
+//     "}";]);
+// t = c.new();
+// assert_checkequal(jgetclassname(c),"Test");
+// assert_checkequal(int16([1,2,3,4]), t.returnLong());
+
+// c = jcompile("Test", ["public class Test {";
+//     "public long returnLong() {";
+//     "    return 32L;";
+//     "}";
+//     "}";]);
+// t = c.new();
+// assert_checkequal(jgetclassname(c),"Test");
+// assert_checkequal(int16(32), t.returnLong());
+
+//////////////////////////////////////////////
+
+// c = jcompile("Test", ["public class Test {";
+//     "public float[] returnFloat() {";
+//     "    return new float[]{1f,2f,3f,4f};";
+//     "}";
+//     "}";]);
+// t = c.new();
+// assert_checkequal(jgetclassname(c),"Test");
+// assert_checkequal(int16([1,2,3,4]), t.returnFloat());
+
+// c = jcompile("Test", ["public class Test {";
+//     "public float returnFloat() {";
+//     "    return 32f;";
+//     "}";
+//     "}";]);
+// t = c.new();
+// assert_checkequal(jgetclassname(c),"Test");
+// assert_checkequal(int16(32), t.returnFloat());
+
+
+//////////////////////////////////////////////
+
+// c = jcompile("Test", ["public class Test {";
+//     "public char[] returnChar() {";
+//     "    return new char[]{''a'',''b'',''c'',''d''};";
+//     "}";
+//     "}";]);
+// t = c.new();
+// assert_checkequal(jgetclassname(c),"Test");
+// assert_checkequal(['a','b','c','d'], t.returnChar());
+
+// c = jcompile("Test", ["public class Test {";
+//     "public char returnChar() {";
+//     "    return ''a'';";
+//     "}";
+//     "}";]);
+// t = c.new();
+// assert_checkequal(jgetclassname(c),"Test");
+// assert_checkequal(int16(32), t.returnChar());
+//
+
+
+//////////////////////////////////////////////
+
+c = jcompile("Test", ["public class Test {";
+    "public boolean[] returnBoolean() {";
+    "    return new boolean[]{true,true,false,false};";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal([%t,%t,%f,%f], t.returnBoolean());
+
+c = jcompile("Test", ["public class Test {";
+    "public boolean returnBoolean() {";
+    "    return true;";
+    "}";
+    "}";]);
+t = c.new();
+assert_checkequal(jgetclassname(c),"Test");
+assert_checkequal(%t, t.returnBoolean());
+