]> granicus.if.org Git - icinga2/commitdiff
Implement Dictionary#get and Array#get
authorGunnar Beutner <gunnar@beutner.name>
Thu, 30 Jul 2015 18:58:52 +0000 (20:58 +0200)
committerGunnar Beutner <gunnar@beutner.name>
Thu, 30 Jul 2015 18:58:52 +0000 (20:58 +0200)
fixes #9796

doc/20-library-reference.md
lib/base/array-script.cpp
lib/base/dictionary-script.cpp

index 6b78192d2f4e2156cfe20f044943bb449544ee9d..b3ed5b8ded3a485facdc751e04b98e541d068e4b 100644 (file)
@@ -554,6 +554,14 @@ Signature:
 Sets the element at the zero-based index to the specified value. The `index` must refer to an element
 which already exists in the array.
 
+### <a id="array-get"></a> Array#get
+
+Signature:
+
+    function get(index);
+
+Retrieves the element at the specified zero-based index.
+
 ### <a id="array-sort"></a> Array#sort
 
 Signature:
@@ -616,6 +624,15 @@ Signature:
 
 Creates or updates an item with the specified `key` and `value`.
 
+### <a id="dictionary-get"></a> Dictionary#get
+
+Signature:
+
+    function get(key);
+
+Retrieves the value for the specified `key`. Returns `null` if they `key` does not exist
+in the dictionary.
+
 ## <a id="scriptfunction-type"></a> Function type
 
 ### <a id="scriptfunction-call"></a> Function#call
index 5ab10360866a0beacaeb888ddf6076331ffb6957..2efde98b6a0b0897806709b03fdc30be01db69e4 100644 (file)
@@ -40,6 +40,13 @@ static void ArraySet(int index, const Value& value)
        self->Set(index, value);
 }
 
+static Value ArrayGet(int index)
+{
+       ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
+       Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
+       return self->Get(index);
+}
+
 static void ArrayAdd(const Value& value)
 {
        ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
@@ -131,6 +138,7 @@ Object::Ptr Array::GetPrototype(void)
                prototype = new Dictionary();
                prototype->Set("len", new Function(WrapFunction(ArrayLen), true));
                prototype->Set("set", new Function(WrapFunction(ArraySet)));
+               prototype->Set("get", new Function(WrapFunction(ArrayGet)));
                prototype->Set("add", new Function(WrapFunction(ArrayAdd)));
                prototype->Set("remove", new Function(WrapFunction(ArrayRemove)));
                prototype->Set("contains", new Function(WrapFunction(ArrayContains), true));
index 1eb1d53d20edb25341d2e8c8b95a186f4b1a47de..4e2c9c31082f169a251129f048e532936d991102 100644 (file)
@@ -38,6 +38,13 @@ static void DictionarySet(const String& key, const Value& value)
        self->Set(key, value);
 }
 
+static Value DictionaryGet(const String& key)
+{
+       ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
+       Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
+       return self->Get(key);
+}
+
 static void DictionaryRemove(const String& key)
 {
        ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
@@ -67,6 +74,7 @@ Object::Ptr Dictionary::GetPrototype(void)
                prototype = new Dictionary();
                prototype->Set("len", new Function(WrapFunction(DictionaryLen), true));
                prototype->Set("set", new Function(WrapFunction(DictionarySet)));
+               prototype->Set("get", new Function(WrapFunction(DictionaryGet)));
                prototype->Set("remove", new Function(WrapFunction(DictionaryRemove)));
                prototype->Set("contains", new Function(WrapFunction(DictionaryContains), true));
                prototype->Set("clone", new Function(WrapFunction(DictionaryClone), true));