Skip to content

Commit

Permalink
libdeng2|App: Defining native modules
Browse files Browse the repository at this point in the history
The application can have any number of built-in native modules.
This way namespaces that have been created by native code can be
imported from scripts.

The "Config" module is now defined as one of the native modules.
  • Loading branch information
skyjake committed Dec 8, 2012
1 parent cadccce commit 4a9e091
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
11 changes: 10 additions & 1 deletion doomsday/libdeng2/include/de/core/app.h
Expand Up @@ -77,6 +77,8 @@ class DENG2_PUBLIC App : public QApplication

~App();

bool notify(QObject *receiver, QEvent *event);

/**
* Finishes App construction by initializing all the application's
* subsystems. This includes Config and FS. Has to be called manually in
Expand All @@ -87,7 +89,14 @@ class DENG2_PUBLIC App : public QApplication
*/
void initSubsystems(SubsystemInitFlags flags = DefaultSubsystems);

bool notify(QObject *receiver, QEvent *event);
/**
* Adds a native module to the set of modules that can be imported in
* scripts.
*
* @param name Name of the module.
* @param module Module namespace.
*/
void addNativeModule(String const &name, Record &module);

static App &app();

Expand Down
40 changes: 36 additions & 4 deletions doomsday/libdeng2/src/core/app.cpp
Expand Up @@ -24,6 +24,7 @@
#include "de/Log"
#include "de/LogBuffer"
#include "de/Module"
#include "de/Record"
#include "de/Version"
#include "de/Block"
#include "de/ZipArchive"
Expand All @@ -36,7 +37,7 @@

namespace de {

struct App::Instance
struct App::Instance : DENG2_OBSERVES(Record, Deletion)
{
App &app;

Expand All @@ -63,6 +64,11 @@ struct App::Instance
/// The configuration.
Config *config;

/// Built-in special modules. These are constructed by native code and thus not
/// parsed from any script.
typedef QMap<String, Record *> NativeModules;
NativeModules nativeModules;

/// Resident modules.
typedef std::map<String, Module *> Modules;
Modules modules;
Expand All @@ -72,9 +78,26 @@ struct App::Instance

~Instance()
{
DENG2_FOR_EACH(NativeModules, i, nativeModules)
{
i.value()->audienceForDeletion -= this;
}
delete config;
}

void recordBeingDeleted(Record &record)
{
QMutableMapIterator<String, Record *> iter(nativeModules);
while(iter.hasNext())
{
iter.next();
if(iter.value() == &record)
{
iter.remove();
}
}
}

void initFileSystem(bool allowPlugins)
{
Folder &binFolder = fs.makeFolder("/bin");
Expand Down Expand Up @@ -280,6 +303,8 @@ void App::initSubsystems(SubsystemInitFlags flags)

// The configuration.
d->config = new Config("/modules/Config.de");
addNativeModule("Config", d->config->names());

d->config->read();

LogBuffer &logBuf = LogBuffer::appBuffer();
Expand Down Expand Up @@ -311,6 +336,12 @@ void App::initSubsystems(SubsystemInitFlags flags)
LOG_VERBOSE("libdeng2::App %s subsystems initialized.") << Version().asText();
}

void App::addNativeModule(String const &name, Record &module)
{
d->nativeModules.insert(name, &module);
module.audienceForDeletion += d;
}

bool App::notify(QObject *receiver, QEvent *event)
{
try
Expand Down Expand Up @@ -387,10 +418,11 @@ Record &App::importModule(String const &name, String const &fromPath)

App &self = app();

// There are some special modules.
if(name == "Config")
// There are some special native modules.
Instance::NativeModules::const_iterator foundNative = self.d->nativeModules.constFind(name);
if(foundNative != self.d->nativeModules.constEnd())
{
return self.config().names();
return *foundNative.value();
}

// Maybe we already have this module?
Expand Down
3 changes: 1 addition & 2 deletions doomsday/libdeng2/src/scriptsys/process.cpp
Expand Up @@ -299,8 +299,7 @@ void Process::call(Function const &function, ArrayValue const &arguments)
// that namespace on the stack first.
if(function.globals() && function.globals() != &globals())
{
_stack.push_back(new Context(Context::GLOBAL_NAMESPACE, this,
function.globals()));
_stack.push_back(new Context(Context::GLOBAL_NAMESPACE, this, function.globals()));
}

// Create a new context.
Expand Down

0 comments on commit 4a9e091

Please sign in to comment.