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 __SCILABPYTHONENVIRNOMENT_HXX__
14 #define __SCILABPYTHONENVIRNOMENT_HXX__
17 #define PATHSEPARATOR ";"
19 #define PATHSEPARATOR ":"
24 #include "dynlib_external_objects_java_scilab.h"
28 #include "strdup_windows.h"
32 //#include "PythonVariablesScope.hxx"
33 #include "ScilabAbstractEnvironment.hxx"
34 #include "ScilabEnvironments.hxx"
35 #include "ScilabJavaException.hxx"
36 #include "ScilabJavaEnvironmentWrapper.hxx"
37 // #include "ScilabPythonInvokers.hxx"
38 #include "JavaOptionsHelper.hxx"
39 #include "ScilabStream.hxx"
40 //#include "ScilabPythonOStream.hxx"
50 // #if defined(PIMS_EXPORTS)
51 // #pragma message("defined(PIMS_EXPORTS)")
53 // #pragma message("Houston !")
55 #define LOG_BUFFER_SIZE 4096
57 using namespace org_modules_external_objects;
59 namespace org_scilab_modules_external_objects_java
62 class EXTERNAL_OBJECTS_JAVA_SCILAB_IMPEXP ScilabJavaEnvironment : public ScilabAbstractEnvironment
64 static const std::string environmentName;
66 static ScilabJavaEnvironment * instance;
69 // PythonVariablesScope & scope;
72 ScilabStream & scilabStream;
73 JavaOptionsHelper & helper;
74 ScilabGatewayOptions & gwOptions;
75 ScilabJavaEnvironmentWrapper & wrapper;
79 ScilabJavaEnvironment();
81 ~ScilabJavaEnvironment();
83 ScilabJavaTupleInvoker getTupleInvoker()
85 return ScilabJavaTupleInvoker(scope);
88 ScilabJavaDictionaryInvoker getDictionaryInvoker()
90 return ScilabJavaDictionaryInvoker(scope);
93 ScilabJavaListInvoker getListInvoker()
95 return ScilabJavaListInvoker(scope);
98 ScilabJavaSetInvoker getSetInvoker()
100 return ScilabJavaSetInvoker(scope);
103 ScilabJavaGetAttrInvoker getGetAttrInvoker()
105 return ScilabJavaGetAttrInvoker(scope);
108 ScilabJavaInvokeInvoker getInvokeInvoker()
110 return ScilabJavaInvokeInvoker(scope);
113 ScilabJavaBuiltinInvoker getBuiltinInvoker(const std::string & name)
115 return ScilabJavaBuiltinInvoker(scope, name);
118 ScilabJavaModuleInvoker getModuleInvoker(const std::string & name)
120 return ScilabJavaModuleInvoker(scope, name);
129 static void finish();
131 static ScilabJavaEnvironment* getInstance()
136 JavaOptionsHelper & getOptionsHelper();
138 ScilabGatewayOptions & getGatewayOptions();
140 ScilabAbstractEnvironmentWrapper & getWrapper();
142 const std::string & getEnvironmentName();
144 int extract(int id, int * args, int argsSize);
146 void insert(int id, int * args, int argsSize);
148 void addNamedVariable(int id, const char * varName);
150 int getNamedVariable(const char * varName);
152 void evalString(const char ** code, int nbLines, ScilabStringStackAllocator * allocator);
154 void getEnvironmentInfos(const ScilabStringStackAllocator & allocator);
156 void garbagecollect();
158 void addtoclasspath(const char * path);
160 void getclasspath(const ScilabStringStackAllocator & allocator);
162 int createarray(char * className, int * dims, int len);
164 int loadclass(char * className, char * currentSciPath, bool isNamedVarCreated, bool allowReload);
166 void getrepresentation(int id, const ScilabStringStackAllocator & allocator);
168 std::string getrepresentation(int id);
170 bool isvalidobject(int id);
172 int newinstance(int id, int * args, int argsSize);
174 int operation(int idA, int idB, const OperatorsType type);
176 int * invoke(int id, const char * methodName, int * args, int argsSize);
178 void setfield(int id, const char * fieldName, int idarg);
180 int getfield(int id, const char * fieldName);
182 int getfieldtype(int id, const char * fieldName);
184 int getarrayelement(int id, int * index, int length);
186 void setarrayelement(int id, int * index, int length, int idArg);
188 int cast(int id, char * className);
190 int castwithid(int id, int classId);
192 void removeobject(int id);
194 void removeobject(const int * id, const int length);
196 void autoremoveobject(int id);
198 void getaccessiblemethods(int id, const ScilabStringStackAllocator & allocator);
200 void getaccessiblefields(int id, const ScilabStringStackAllocator & allocator);
202 std::vector<std::string> getCompletion(int id, char ** fieldPath, const int fieldPathLen);
204 std::string getclassname(int id);
206 VariableType isunwrappable(int id);
208 int compilecode(char * className, char ** code, int size);
210 void enabletrace(const char * filename);
214 void writeLog(const std::string & fun, const std::string str, ...) const;
216 inline int getEnvId() const
221 inline bool isTraceEnabled() const
226 /* static inline std::wstring getWString(PyObject * obj)
228 if (PyString_Check(obj))
230 int len = PyString_GET_SIZE(obj);
231 char * str = PyString_AsString(obj);
233 return std::wstring(str, str + len);
235 else if (PyUnicode_Check(obj))
237 return std::wstring(reinterpret_cast<const wchar_t *>(PyUnicode_AS_DATA(obj)));
240 throw ScilabJavaException(__LINE__, __FILE__, gettext("Cannot convert in a wide string"));
245 // static void initNumpy();
247 void getMethodResult(JavaVM * jvm_, const char * const methodName, int id, const ScilabStringStackAllocator & allocator);
249 // template <typename T, typename U, class V> void unwrapMat(JavaVM * jvm_, const bool methodOfConv, const int javaID, const ScilabStringStackAllocator & allocator);
250 // template <typename T, typename U, class V> void unwrapRow(JavaVM * jvm_, const bool methodOfConv, const int javaID, const ScilabStringStackAllocator & allocator);
251 // template <typename T, typename U, class V> void unwrapSingle(JavaVM * jvm_, const bool methodOfConv, const int javaID, const ScilabStringStackAllocator & allocator);
254 inline void getAccessibleFields(int id, const ScilabStringStackAllocator & allocator, const bool isField)
258 PyObject * obj = scope.getObject(id);
261 throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid object with id %d"), id);
264 PyObject * dir = PyObject_Dir(obj);
265 if (!dir || PyList_Size(dir) == 0)
267 allocator.allocate(0, 0, static_cast<char **>(0));
271 int size = PyList_Size(dir);
273 char ** arr = new char*[size];
275 for (int i = 0; i < size; i++)
277 PyObject * fieldName = PyList_GetItem(dir, i);
278 char * _field = PyString_AsString(fieldName);
279 if (helper.getShowPrivate() || _field[0] != '_')
281 PyObject * field = PyObject_GetAttr(obj, fieldName);
282 if (isField && !PyCallable_Check(field))
286 else if (!isField && PyCallable_Check(field))
293 allocator.allocate(j, 1, arr);
300 static inline PyObject * createMultiList(const int * dims, const int len)
304 return PyList_New(0);
309 return PyList_New(dims[0]);
312 PyObject * list = PyList_New(dims[0]);
313 for (int i = 0; i < dims[0]; i++)
315 PyList_SetItem(list, i, createMultiList(dims + 1, len - 1));
322 static inline std::vector<char *> breakInLines(const std::string & str)
324 std::vector<char *> buf;
326 std::size_t pos = str.find_first_of("\n");
327 while (pos != std::string::npos)
329 buf.push_back(strdup(str.substr(prev, pos - prev).c_str()));
331 pos = str.find_first_of("\n", prev);
333 buf.push_back(strdup(str.substr(prev).c_str()));
335 std::vector<char *>::iterator last = buf.end();
336 for (std::vector<char *>::iterator i = buf.end() - 1; i >= buf.begin(); i--)
348 buf.erase(last, buf.end());
353 static inline const char * getOpNameFromType(const OperatorsType type)
358 throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid operator: \'"));
372 throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid operator: .*"));
374 throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid operator: ./"));
376 throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid operator: .\\"));
378 throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid operator: .*."));
380 throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid operator: ./."));
381 case DotBackslashDot :
382 throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid operator: .\\."));
392 throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid operator: .^"));
396 throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid operator: .\'"));
413 #endif // __SCILABJAVAENVIRONMENT_HXX__