Skip to content

Commit

Permalink
Scripting|libcore: Accessing native/imported modules
Browse files Browse the repository at this point in the history
A more convenient API.
  • Loading branch information
skyjake committed Nov 18, 2018
1 parent 0976a77 commit adff2f6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions doomsday/sdk/libcore/include/de/scriptsys/scriptsystem.h
Expand Up @@ -72,6 +72,15 @@ class DENG2_PUBLIC ScriptSystem : public System

Record &nativeModule(String const &name);

/**
* Returns a native or an imported module.
*
* @param name Name of the module.
*
* @return Module namespace.
*/
Record &operator[](const String &nativeModuleName);

/**
* Returns a list of the names of all the existing native modules.
*
Expand Down
20 changes: 19 additions & 1 deletion doomsday/sdk/libcore/src/scriptsys/scriptsystem.cpp
Expand Up @@ -91,7 +91,7 @@ DENG2_PIMPL(ScriptSystem)

~Impl()
{
qDeleteAll(modules.values());
qDeleteAll(modules);
}

static Value *Function_ImportPath(Context &, Function::ArgumentValues const &)
Expand Down Expand Up @@ -216,6 +216,24 @@ Record &ScriptSystem::nativeModule(String const &name)
return *foundNative.value();
}

Record &ScriptSystem::operator[](const String &name)
{
if (nativeModuleExists(name))
{
return nativeModule(name);
}
// Imported modules.
{
const auto &mods = d->modules;
auto found = mods.find(name);
if (found != mods.end())
{
return found.value()->names();
}
}
throw NotFoundError("ScriptSystem::operator[]", "Module not found: " + name);
}

bool ScriptSystem::nativeModuleExists(const String &name) const
{
DENG2_GUARD_FOR(d->nativeModules, G);
Expand Down

0 comments on commit adff2f6

Please sign in to comment.