Implement namespace support for the keys() function

This commit is contained in:
Gunnar Beutner 2018-08-07 13:55:41 +02:00
parent 8fda8d72ac
commit 1a0311a49f
2 changed files with 18 additions and 6 deletions

View File

@ -30,6 +30,7 @@
#include "base/application.hpp" #include "base/application.hpp"
#include "base/dependencygraph.hpp" #include "base/dependencygraph.hpp"
#include "base/initialize.hpp" #include "base/initialize.hpp"
#include "base/namespace.hpp"
#include <boost/regex.hpp> #include <boost/regex.hpp>
#include <algorithm> #include <algorithm>
#include <set> #include <set>
@ -83,11 +84,11 @@ enum MatchType
void ScriptUtils::StaticInitialize() void ScriptUtils::StaticInitialize()
{ {
ScriptGlobal::Set("MatchAll", MatchAll, true); ScriptGlobal::Set("System.MatchAll", MatchAll, true);
ScriptGlobal::Set("MatchAny", MatchAny, true); ScriptGlobal::Set("System.MatchAny", MatchAny, true);
ScriptGlobal::Set("GlobFile", GlobFile, true); ScriptGlobal::Set("System.GlobFile", GlobFile, true);
ScriptGlobal::Set("GlobDirectory", GlobDirectory, true); ScriptGlobal::Set("System.GlobDirectory", GlobDirectory, true);
} }
String ScriptUtils::CastString(const Value& value) String ScriptUtils::CastString(const Value& value)
@ -397,10 +398,12 @@ Type::Ptr ScriptUtils::TypeOf(const Value& value)
return value.GetReflectionType(); return value.GetReflectionType();
} }
Array::Ptr ScriptUtils::Keys(const Dictionary::Ptr& obj) Array::Ptr ScriptUtils::Keys(const Object::Ptr& obj)
{ {
ArrayData result; ArrayData result;
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(obj);
if (dict) { if (dict) {
ObjectLock olock(dict); ObjectLock olock(dict);
for (const Dictionary::Pair& kv : dict) { for (const Dictionary::Pair& kv : dict) {
@ -408,6 +411,15 @@ Array::Ptr ScriptUtils::Keys(const Dictionary::Ptr& obj)
} }
} }
Namespace::Ptr ns = dynamic_pointer_cast<Namespace>(obj);
if (ns) {
ObjectLock olock(ns);
for (const Namespace::Pair& kv : ns) {
result.push_back(kv.first);
}
}
return new Array(std::move(result)); return new Array(std::move(result));
} }

View File

@ -49,7 +49,7 @@ public:
static void Log(const std::vector<Value>& arguments); static void Log(const std::vector<Value>& arguments);
static Array::Ptr Range(const std::vector<Value>& arguments); static Array::Ptr Range(const std::vector<Value>& arguments);
static Type::Ptr TypeOf(const Value& value); static Type::Ptr TypeOf(const Value& value);
static Array::Ptr Keys(const Dictionary::Ptr& dict); static Array::Ptr Keys(const Object::Ptr& obj);
static ConfigObject::Ptr GetObject(const Value& type, const String& name); static ConfigObject::Ptr GetObject(const Value& type, const String& name);
static Array::Ptr GetObjects(const Type::Ptr& type); static Array::Ptr GetObjects(const Type::Ptr& type);
static void Assert(const Value& arg); static void Assert(const Value& arg);