Implement the Dictionary#keys method

fixes #9882
This commit is contained in:
Gunnar Beutner 2015-08-11 13:56:42 +02:00
parent 2e43c57d6b
commit d30d1d4dda
2 changed files with 23 additions and 0 deletions

View File

@ -633,6 +633,14 @@ Signature:
Retrieves the value for the specified `key`. Returns `null` if they `key` does not exist
in the dictionary.
### <a id="dictionary-keys"></a> Dictionary#keys
Signature:
function keys();
Returns a list of keys for all items that are currently in the dictionary.
## <a id="scriptfunction-type"></a> Function type
### <a id="scriptfunction-call"></a> Function#call

View File

@ -21,6 +21,8 @@
#include "base/function.hpp"
#include "base/functionwrapper.hpp"
#include "base/scriptframe.hpp"
#include "base/array.hpp"
#include <boost/foreach.hpp>
using namespace icinga;
@ -66,6 +68,18 @@ static Dictionary::Ptr DictionaryClone(void)
return self->ShallowClone();
}
static Array::Ptr DictionaryKeys(void)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
Array::Ptr keys = new Array();
ObjectLock olock(self);
BOOST_FOREACH(const Dictionary::Pair& kv, self) {
keys->Add(kv.first);
}
return keys;
}
Object::Ptr Dictionary::GetPrototype(void)
{
static Dictionary::Ptr prototype;
@ -78,6 +92,7 @@ Object::Ptr Dictionary::GetPrototype(void)
prototype->Set("remove", new Function(WrapFunction(DictionaryRemove)));
prototype->Set("contains", new Function(WrapFunction(DictionaryContains), true));
prototype->Set("clone", new Function(WrapFunction(DictionaryClone), true));
prototype->Set("keys", new Function(WrapFunction(DictionaryKeys), true));
}
return prototype;