From 1a0311a49ff3d508d7735d6708f0d0c4b5f9c27f Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 7 Aug 2018 13:55:41 +0200 Subject: [PATCH] Implement namespace support for the keys() function --- lib/base/scriptutils.cpp | 22 +++++++++++++++++----- lib/base/scriptutils.hpp | 2 +- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/base/scriptutils.cpp b/lib/base/scriptutils.cpp index befccde5f..4aa0b3bcd 100644 --- a/lib/base/scriptutils.cpp +++ b/lib/base/scriptutils.cpp @@ -30,6 +30,7 @@ #include "base/application.hpp" #include "base/dependencygraph.hpp" #include "base/initialize.hpp" +#include "base/namespace.hpp" #include #include #include @@ -83,11 +84,11 @@ enum MatchType void ScriptUtils::StaticInitialize() { - ScriptGlobal::Set("MatchAll", MatchAll, true); - ScriptGlobal::Set("MatchAny", MatchAny, true); + ScriptGlobal::Set("System.MatchAll", MatchAll, true); + ScriptGlobal::Set("System.MatchAny", MatchAny, true); - ScriptGlobal::Set("GlobFile", GlobFile, true); - ScriptGlobal::Set("GlobDirectory", GlobDirectory, true); + ScriptGlobal::Set("System.GlobFile", GlobFile, true); + ScriptGlobal::Set("System.GlobDirectory", GlobDirectory, true); } String ScriptUtils::CastString(const Value& value) @@ -397,10 +398,12 @@ Type::Ptr ScriptUtils::TypeOf(const Value& value) return value.GetReflectionType(); } -Array::Ptr ScriptUtils::Keys(const Dictionary::Ptr& obj) +Array::Ptr ScriptUtils::Keys(const Object::Ptr& obj) { ArrayData result; + Dictionary::Ptr dict = dynamic_pointer_cast(obj); + if (dict) { ObjectLock olock(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(obj); + + if (ns) { + ObjectLock olock(ns); + for (const Namespace::Pair& kv : ns) { + result.push_back(kv.first); + } + } + return new Array(std::move(result)); } diff --git a/lib/base/scriptutils.hpp b/lib/base/scriptutils.hpp index 9515b381e..fcb80f6e4 100644 --- a/lib/base/scriptutils.hpp +++ b/lib/base/scriptutils.hpp @@ -49,7 +49,7 @@ public: static void Log(const std::vector& arguments); static Array::Ptr Range(const std::vector& arguments); 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 Array::Ptr GetObjects(const Type::Ptr& type); static void Assert(const Value& arg);