JIMS: plug extraction
[scilab.git] / scilab / modules / external_objects_java / src / cpp / ScilabJavaEnvironment.cpp
index 9d0d42e..aba4a48 100644 (file)
@@ -181,41 +181,8 @@ int ScilabJavaEnvironment::extract(int id, int * args, int argsSize)
         writeLog("extract", "Extraction on object %d with arguments: %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 (PyDict_Check(obj))
-        {
-            if (argsSize != 1)
-            {
-                throw ScilabJavaException(__LINE__, __FILE__, gettext("Cannot extract more than one element from a dictionary"));
-            }
-
-            PyObject * key = scope.getObject(*args);
-            if (!obj)
-            {
-                throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid key object"));
-            }
-
-            if (!PyDict_Contains(obj, key))
-            {
-                throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid key"));
-            }
-
-            PyObject * value = PyDict_GetItem(obj, key);
-       Py_INCREF(value);
-
-            int ret = scope.addObject(value);
-            writeLog("extract", "returned id: %d.", ret);
-
-            return ret;
-            }
-    */
-
-    throw ScilabJavaException(__LINE__, __FILE__, gettext("Cannot extract from Java object"));
+    JavaVM * vm = getScilabJavaVM();
+    return ScilabJavaObject::extract(vm, id, args, argsSize);
 }
 
 void ScilabJavaEnvironment::insert(int id, int * args, int argsSize)
@@ -590,44 +557,16 @@ void ScilabJavaEnvironment::setfield(int id, const char * fieldName, int idarg)
         throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid field name"));
     }
 
-    if (!helper.getShowPrivate() && *fieldName == '_')
+    try
     {
-        throw ScilabJavaException(__LINE__, __FILE__, gettext("Private field: %s"), fieldName);
+        JavaVM * vm = getScilabJavaVM();
+        ScilabJavaObject::setField(vm, id, fieldName, idarg);
+    }
+    catch (const GiwsException::JniCallMethodException & e)
+    {
+        throw ScilabJavaException(__LINE__, __FILE__, gettext("Cannot set the field: %s"), fieldName);
     }
 
-    /*
-        PyObject * obj = scope.getObject(id);
-        if (!obj)
-        {
-            throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid object with id %d"), id);
-        }
-
-        if (!PyObject_HasAttrString(obj, fieldName))
-        {
-            throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid field name: %s"), fieldName);
-        }
-
-        PyObject * value = scope.getObject(idarg);
-        if (!value)
-        {
-            throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid object with id %d"), id);
-        }
-
-        int ret = PyObject_SetAttrString(obj, fieldName, value);
-        if (ret == -1)
-        {
-            if (PyErr_Occurred())
-            {
-                PyObject * type, * value, * traceback;
-                PyErr_Fetch(&type, &value, &traceback);
-                PyErr_NormalizeException(&type, &value, &traceback);
-                PyErr_Clear();
-
-                throw ScilabJavaException(__LINE__, __FILE__, type, value, traceback, gettext("Unable to set the field: %s"), fieldName);
-            }
-            throw ScilabJavaException(__LINE__, __FILE__, gettext("Unable to set the field: %s"), fieldName);
-        }
-    */
     writeLog("setfield", "Value successfully set.");
 }
 
@@ -635,101 +574,24 @@ int ScilabJavaEnvironment::getfield(int id, const char * fieldName)
 {
     writeLog("getfield", "Get the field named %s on object with id %d.", fieldName, id);
 
-    JavaVM *vm = getScilabJavaVM();
-    int ret = ScilabJavaObject::getField(vm, id, fieldName);
-    return ret;
-    //    allocator.allocate(1, 1, &str);
-
-
-
-    /*
     if (*fieldName == '\0')
     {
         throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid field name"));
     }
 
-    if (!helper.getShowPrivate() && *fieldName == '_')
-    {
-        throw ScilabJavaException(__LINE__, __FILE__, gettext("Private field: %s"), fieldName);
-    }
-
-        PyObject * obj = scope.getObject(id);
-        if (!obj)
-        {
-            throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid object with id %d"), id);
-        }
-
-        if (!PyObject_HasAttrString(obj, fieldName))
-        {
-            throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid field name: %s"), fieldName);
-        }
-
-        PyObject * field = PyObject_GetAttrString(obj, fieldName);
-        if (!field)
-        {
-            if (PyErr_Occurred())
-            {
-                PyObject * type, * value, * traceback;
-                PyErr_Fetch(&type, &value, &traceback);
-                PyErr_NormalizeException(&type, &value, &traceback);
-                PyErr_Clear();
-
-                throw ScilabJavaException(__LINE__, __FILE__, type, value, traceback, gettext("Unable to get the field value: %s"), fieldName);
-            }
-            throw ScilabJavaException(__LINE__, __FILE__, gettext("Unable to get the field value: %s"), fieldName);
-        }
-
-        int ret = scope.addObject(field);
-        writeLog("getfield", "returned id %d.", ret);
+    JavaVM * vm = getScilabJavaVM();
+    int ret = ScilabJavaObject::getField(vm, id, fieldName);
 
-        return ret;
-    */
-    return 0;
+    return ret;
 }
 
 int ScilabJavaEnvironment::getfieldtype(int id, const char * fieldName)
 {
     writeLog("getfieldtype", "Get the type of the field %s on object with id %d.", fieldName, id);
 
-    if ((!helper.getShowPrivate() && *fieldName == '_') || *fieldName == '\0')
-    {
-        writeLog("getfieldtype", "Return NONE.");
-        return -1;
-    }
-    /*
-        PyObject * obj = scope.getObject(id);
-        if (!obj)
-        {
-            throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid object with id %d"), id);
-        }
-
-        if (!PyObject_HasAttrString(obj, fieldName))
-        {
-            writeLog("getfieldtype", "Return NONE.");
-            return -1;
-        }
+    JavaVM * vm = getScilabJavaVM();
 
-        PyObject * field = PyObject_GetAttrString(obj, fieldName);
-        if (!field)
-        {
-            writeLog("getfieldtype", "Return NONE.");
-            return -1;
-        }
-
-        if (PyCallable_Check(field))
-        {
-            Py_DECREF(field);
-            writeLog("getfieldtype", "Return METHOD.");
-            return 0;
-        }
-        else
-        {
-            Py_DECREF(field);
-            writeLog("getfieldtype", "Return FIELD.");
-            return 1;
-        }
-    */
-    return 0;
+    return ScilabJavaObject::getFieldType(vm, id, fieldName);
 }
 
 int ScilabJavaEnvironment::getarrayelement(int id, int * index, int length)
@@ -746,84 +608,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));
+    JavaVM * vm = getScilabJavaVM();
 
-            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);
-
-        return ret;
-    */
-    return 0;
+    return ScilabJavaObject::getArrayElement(vm, id, index, length);
 }
 
 void ScilabJavaEnvironment::setarrayelement(int id, int * index, int length, int idArg)
@@ -840,82 +628,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);
-        }
+    JavaVM * vm = getScilabJavaVM();
 
-        if (PyList_Check(obj))
-        {
-            if (length == 0)
-            {
-                return;
-            }
+    ScilabJavaObject::setArrayElement(vm, id, index, length, idArg);
 
-            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);
-
-            if (sizeof(int) != sizeof(npy_intp))
-            {
-                delete[] ind;
-            }
-
-            if (ret == -1)
-            {
-                throw ScilabJavaException(__LINE__, __FILE__, gettext("Cannot set the value in the array"));
-            }
-        }
-    */
     writeLog("setarrayelement", "Successfully set");
 }