Atlas-Launcher/source/javascript/module/AtlasJsModule.cpp

49 lines
No EOL
1.6 KiB
C++

#include "AtlasJsModule.hpp"
#include "debug/DebugJsModule.hpp"
#include "file_system/FsJsModule.hpp"
#include "image/ImageJsModule.hpp"
AtlasJsModule::AtlasJsModule(AtlasJsEngine* engine, QObject* parent) : QObject(parent) {
// set the engine
this->engine = engine;
// load the submodules
this->submodules[QStringLiteral("debug")] = std::make_shared<DebugJsModule>(this->engine);
this->submodules[QStringLiteral("fs")] = std::make_shared<FsJsModule>(this->engine);
// this->submodules[QStringLiteral("image")] = std::make_shared<ImageJsModule>(this->engine);
// TODO(Faraphel): check if the filesystem is correctly restricted
}
QJSValue AtlasJsModule::require(const QString& name) const {
std::shared_ptr<BaseJsModule> submodule;
try {
// get the submodule from its name
submodule = this->submodules.at(name);
} catch (const std::out_of_range&) {
// if not found, throw a Javascript error and return null
this->engine->throwError(
QJSValue::ReferenceError,
QStringLiteral("the module \"") + name + QStringLiteral("\" does not exist.")
);
return QJSValue(QJSPrimitiveNull());
}
// return the module as a new javascript object
return this->engine->newQObject(submodule.get());
}
QJSValueList AtlasJsModule::getVersion() {
// TODO(Faraphel): should be stored somewhere else.
// TODO(Faraphel): The type should implement an easy comparison system, if possible
QJSValueList version;
version.append(1);
version.append(0);
version.append(0);
return version;
}