Changeset View
Changeset View
Standalone View
Standalone View
src/core/manager.cpp
Show First 20 Lines • Show All 70 Lines • ▼ Show 20 Line(s) | |||||
71 | 71 | | |||
72 | Q_GLOBAL_STATIC(Manager, _self) | 72 | Q_GLOBAL_STATIC(Manager, _self) | ||
73 | 73 | | |||
74 | Manager &Manager::self() | 74 | Manager &Manager::self() | ||
75 | { | 75 | { | ||
76 | return *_self(); | 76 | return *_self(); | ||
77 | } | 77 | } | ||
78 | 78 | | |||
79 | static QFunctionPointer loadLibrary(const char *libname, const char *functionname) | 79 | enum LibraryFunction | ||
80 | { | ||||
81 | LibraryFunctionInterpreter, | ||||
82 | LibraryFunctionModule | ||||
83 | }; | ||||
84 | | ||||
85 | static QFunctionPointer loadLibrary(const char *libname, enum LibraryFunction function) | ||||
80 | { | 86 | { | ||
81 | QLibrary lib; | 87 | QLibrary lib; | ||
82 | QString libAbsoluteFilePath; | 88 | QString libAbsoluteFilePath; | ||
83 | foreach (const QString &path, QCoreApplication::instance()->libraryPaths()) { | 89 | foreach (const QString &path, QCoreApplication::instance()->libraryPaths()) { | ||
84 | const QFileInfo &fileInfo = QFileInfo(path, libname); | 90 | const QFileInfo &fileInfo = QFileInfo(path, libname); | ||
85 | lib.setFileName(fileInfo.filePath()); | 91 | lib.setFileName(fileInfo.filePath()); | ||
86 | lib.setLoadHints(QLibrary::ExportExternalSymbolsHint); | 92 | lib.setLoadHints(QLibrary::ExportExternalSymbolsHint); | ||
87 | if (lib.load()) { | 93 | if (lib.load()) { | ||
88 | libAbsoluteFilePath = fileInfo.absoluteFilePath(); | 94 | libAbsoluteFilePath = fileInfo.absoluteFilePath(); | ||
89 | break; | 95 | break; | ||
90 | } | 96 | } | ||
91 | } | 97 | } | ||
92 | 98 | | |||
93 | if (!lib.isLoaded()) { | 99 | if (!lib.isLoaded()) { | ||
94 | #ifdef KROSS_INTERPRETER_DEBUG | 100 | #ifdef KROSS_INTERPRETER_DEBUG | ||
95 | if (strcmp(functionname, "krossinterpreter") == 0) { | 101 | if (function == LibraryFunctionInterpreter) { | ||
96 | qCDebug(KROSS_LOG) << "Kross Interpreter '" << libname << | 102 | qCDebug(KROSS_LOG) << "Kross Interpreter '" << libname << | ||
97 | "' not available: " << lib.errorString(); | 103 | "' not available: " << lib.errorString(); | ||
98 | } else if (strcmp(functionname, "krossmodule") == 0) { | 104 | } else if (function == LibraryFunctionModule) { | ||
99 | qCDebug(KROSS_LOG) << "Kross Module '" << libname << | 105 | qCDebug(KROSS_LOG) << "Kross Module '" << libname << | ||
100 | "' not available: " << lib.errorString(); | 106 | "' not available: " << lib.errorString(); | ||
101 | } else { | 107 | } else { | ||
102 | qCWarning(KROSS_LOG) << "Failed to load unknown type of '" << | 108 | qCWarning(KROSS_LOG) << "Failed to load unknown type of '" << | ||
103 | libname << "' library: " << lib.errorString(); | 109 | libname << "' library: " << lib.errorString(); | ||
104 | } | 110 | } | ||
105 | #endif | 111 | #endif | ||
106 | return nullptr; | 112 | return nullptr; | ||
107 | } | 113 | } | ||
108 | 114 | | |||
109 | QFunctionPointer funcPtr = lib.resolve(functionname); | 115 | const char* functionName = function == LibraryFunctionInterpreter ? "krossinterpreter" : "krossmodule"; | ||
116 | QFunctionPointer funcPtr = lib.resolve(functionName); | ||||
110 | if (!funcPtr) { | 117 | if (!funcPtr) { | ||
111 | qCWarning(KROSS_LOG) << QStringLiteral("Failed to resolve %1 in %2%3") | 118 | qCWarning(KROSS_LOG) << QStringLiteral("Failed to resolve %1 in %2%3") | ||
112 | .arg(functionname) | 119 | .arg(functionName) | ||
113 | .arg(lib.fileName()) | 120 | .arg(lib.fileName()) | ||
114 | .arg(libAbsoluteFilePath.isEmpty() ? "" : QString(" (%1)").arg(libAbsoluteFilePath)); | 121 | .arg(libAbsoluteFilePath.isEmpty() ? "" : QString(" (%1)").arg(libAbsoluteFilePath)); | ||
115 | } | 122 | } | ||
116 | return funcPtr; | 123 | return funcPtr; | ||
117 | } | 124 | } | ||
118 | 125 | | |||
119 | Manager::Manager() | 126 | Manager::Manager() | ||
120 | : QObject() | 127 | : QObject() | ||
121 | , QScriptable() | 128 | , QScriptable() | ||
122 | , ChildrenInterface() | 129 | , ChildrenInterface() | ||
123 | , d(new Private()) | 130 | , d(new Private()) | ||
124 | { | 131 | { | ||
125 | d->strictTypesEnabled = true; | 132 | d->strictTypesEnabled = true; | ||
126 | setObjectName("Kross"); | 133 | setObjectName("Kross"); | ||
127 | d->collection = new ActionCollection("main"); | 134 | d->collection = new ActionCollection("main"); | ||
128 | 135 | | |||
129 | #ifdef KROSS_PYTHON_LIBRARY | 136 | #ifdef KROSS_PYTHON_LIBRARY | ||
130 | if (QFunctionPointer funcPtr = loadLibrary(KROSS_PYTHON_LIBRARY, "krossinterpreter")) { | 137 | if (QFunctionPointer funcPtr = loadLibrary(KROSS_PYTHON_LIBRARY, LibraryFunctionInterpreter)) { | ||
131 | d->interpreterinfos.insert("python", | 138 | d->interpreterinfos.insert("python", | ||
132 | new InterpreterInfo("python", | 139 | new InterpreterInfo("python", | ||
133 | funcPtr, // library | 140 | funcPtr, // library | ||
134 | "*.py", // file filter-wildcard | 141 | "*.py", // file filter-wildcard | ||
135 | QStringList() << "text/x-python" // mimetypes | 142 | QStringList() << "text/x-python" // mimetypes | ||
136 | ) | 143 | ) | ||
137 | ); | 144 | ); | ||
138 | } | 145 | } | ||
139 | #endif | 146 | #endif | ||
140 | 147 | | |||
141 | #ifdef KROSS_RUBY_LIBRARY | 148 | #ifdef KROSS_RUBY_LIBRARY | ||
142 | if (QFunctionPointer funcPtr = loadLibrary(KROSS_RUBY_LIBRARY, "krossinterpreter")) { | 149 | if (QFunctionPointer funcPtr = loadLibrary(KROSS_RUBY_LIBRARY, LibraryFunctionInterpreter)) { | ||
143 | InterpreterInfo::Option::Map options; | 150 | InterpreterInfo::Option::Map options; | ||
144 | options.insert("safelevel", new InterpreterInfo::Option( | 151 | options.insert("safelevel", new InterpreterInfo::Option( | ||
145 | i18n("Level of safety of the Ruby interpreter"), | 152 | i18n("Level of safety of the Ruby interpreter"), | ||
146 | QVariant(0))); // 0 -> unsafe, 4 -> very safe | 153 | QVariant(0))); // 0 -> unsafe, 4 -> very safe | ||
147 | d->interpreterinfos.insert("ruby", | 154 | d->interpreterinfos.insert("ruby", | ||
148 | new InterpreterInfo("ruby", | 155 | new InterpreterInfo("ruby", | ||
149 | funcPtr, // library | 156 | funcPtr, // library | ||
150 | "*.rb", // file filter-wildcard | 157 | "*.rb", // file filter-wildcard | ||
151 | QStringList() << /* "text/x-ruby" << */ "application/x-ruby", // mimetypes | 158 | QStringList() << /* "text/x-ruby" << */ "application/x-ruby", // mimetypes | ||
152 | options // options | 159 | options // options | ||
153 | ) | 160 | ) | ||
154 | ); | 161 | ); | ||
155 | } | 162 | } | ||
156 | #endif | 163 | #endif | ||
157 | 164 | | |||
158 | #ifdef KROSS_JAVA_LIBRARY | 165 | #ifdef KROSS_JAVA_LIBRARY | ||
159 | if (QFunctionPointer funcPtr = loadLibrary(KROSS_JAVA_LIBRARY, "krossinterpreter")) { | 166 | if (QFunctionPointer funcPtr = loadLibrary(KROSS_JAVA_LIBRARY, LibraryFunctionInterpreter)) { | ||
160 | d->interpreterinfos.insert("java", | 167 | d->interpreterinfos.insert("java", | ||
161 | new InterpreterInfo("java", | 168 | new InterpreterInfo("java", | ||
162 | funcPtr, // library | 169 | funcPtr, // library | ||
163 | "*.java *.class *.jar", // file filter-wildcard | 170 | "*.java *.class *.jar", // file filter-wildcard | ||
164 | QStringList() << "application/java" // mimetypes | 171 | QStringList() << "application/java" // mimetypes | ||
165 | ) | 172 | ) | ||
166 | ); | 173 | ); | ||
167 | } | 174 | } | ||
168 | #endif | 175 | #endif | ||
169 | 176 | | |||
170 | #ifdef KROSS_FALCON_LIBRARY | 177 | #ifdef KROSS_FALCON_LIBRARY | ||
171 | if (QFunctionPointer funcPtr = loadLibrary(KROSS_FALCON_LIBRARY, "krossinterpreter")) { | 178 | if (QFunctionPointer funcPtr = loadLibrary(KROSS_FALCON_LIBRARY, LibraryFunctionInterpreter)) { | ||
172 | d->interpreterinfos.insert("falcon", | 179 | d->interpreterinfos.insert("falcon", | ||
173 | new InterpreterInfo("falcon", | 180 | new InterpreterInfo("falcon", | ||
174 | funcPtr, // library | 181 | funcPtr, // library | ||
175 | "*.fal", // file filter-wildcard | 182 | "*.fal", // file filter-wildcard | ||
176 | QStringList() << "application/x-falcon" // mimetypes | 183 | QStringList() << "application/x-falcon" // mimetypes | ||
177 | ) | 184 | ) | ||
178 | ); | 185 | ); | ||
179 | } | 186 | } | ||
180 | #endif | 187 | #endif | ||
181 | 188 | | |||
182 | #ifdef KROSS_QTSCRIPT_LIBRARY | 189 | #ifdef KROSS_QTSCRIPT_LIBRARY | ||
183 | if (QFunctionPointer funcPtr = loadLibrary(KROSS_QTSCRIPT_LIBRARY, "krossinterpreter")) { | 190 | if (QFunctionPointer funcPtr = loadLibrary(KROSS_QTSCRIPT_LIBRARY, LibraryFunctionInterpreter)) { | ||
184 | d->interpreterinfos.insert("qtscript", | 191 | d->interpreterinfos.insert("qtscript", | ||
185 | new InterpreterInfo("qtscript", | 192 | new InterpreterInfo("qtscript", | ||
186 | funcPtr, // library | 193 | funcPtr, // library | ||
187 | "*.es", // file filter-wildcard | 194 | "*.es", // file filter-wildcard | ||
188 | QStringList() << "application/ecmascript" // mimetypes | 195 | QStringList() << "application/ecmascript" // mimetypes | ||
189 | ) | 196 | ) | ||
190 | ); | 197 | ); | ||
191 | } | 198 | } | ||
192 | #endif | 199 | #endif | ||
193 | 200 | | |||
194 | #ifdef KROSS_LUA_LIBRARY | 201 | #ifdef KROSS_LUA_LIBRARY | ||
195 | if (QFunctionPointer funcPtr = loadLibrary(KROSS_LUA_LIBRARY, "krossinterpreter")) { | 202 | if (QFunctionPointer funcPtr = loadLibrary(KROSS_LUA_LIBRARY, LibraryFunctionInterpreter)) { | ||
196 | d->interpreterinfos.insert("lua", | 203 | d->interpreterinfos.insert("lua", | ||
197 | new InterpreterInfo("lua", | 204 | new InterpreterInfo("lua", | ||
198 | funcPtr, // library | 205 | funcPtr, // library | ||
199 | "*.lua *.luac", // file filter-wildcard | 206 | "*.lua *.luac", // file filter-wildcard | ||
200 | QStringList() << "application/x-lua" // mimetypes | 207 | QStringList() << "application/x-lua" // mimetypes | ||
201 | ) | 208 | ) | ||
202 | ); | 209 | ); | ||
203 | } | 210 | } | ||
▲ Show 20 Lines • Show All 100 Lines • ▼ Show 20 Line(s) | 304 | { | |||
304 | 311 | | |||
305 | if (modulename.isEmpty() || modulename.contains(QRegExp("[^a-zA-Z0-9]"))) { | 312 | if (modulename.isEmpty() || modulename.contains(QRegExp("[^a-zA-Z0-9]"))) { | ||
306 | qCWarning(KROSS_LOG) << "Invalid module name " << modulename; | 313 | qCWarning(KROSS_LOG) << "Invalid module name " << modulename; | ||
307 | return nullptr; | 314 | return nullptr; | ||
308 | } | 315 | } | ||
309 | 316 | | |||
310 | QByteArray libraryname = QString("krossmodule%1").arg(modulename).toLower().toLatin1(); | 317 | QByteArray libraryname = QString("krossmodule%1").arg(modulename).toLower().toLatin1(); | ||
311 | 318 | | |||
312 | if (QFunctionPointer funcPtr = loadLibrary(libraryname.constData(), "krossmodule")) { | 319 | if (QFunctionPointer funcPtr = loadLibrary(libraryname.constData(), LibraryFunctionModule)) { | ||
313 | def_module_func func = (def_module_func) funcPtr; | 320 | def_module_func func = (def_module_func) funcPtr; | ||
314 | Q_ASSERT(func); | 321 | Q_ASSERT(func); | ||
315 | QObject *module = (QObject *)(func)(); // call the function | 322 | QObject *module = (QObject *)(func)(); // call the function | ||
316 | Q_ASSERT(module); | 323 | Q_ASSERT(module); | ||
317 | //krossdebug( QString("Manager::module Module successfully loaded: modulename=%1 module.objectName=%2 module.className=%3").arg(modulename).arg(module->objectName()).arg(module->metaObject()->className()) ); | 324 | //krossdebug( QString("Manager::module Module successfully loaded: modulename=%1 module.objectName=%2 module.className=%3").arg(modulename).arg(module->objectName()).arg(module->metaObject()->className()) ); | ||
318 | d->modules.insert(modulename, module); | 325 | d->modules.insert(modulename, module); | ||
319 | return module; | 326 | return module; | ||
320 | } else { | 327 | } else { | ||
▲ Show 20 Lines • Show All 71 Lines • Show Last 20 Lines |