From 1314cba61b987fa991e763d56fae83903250176f Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 30 Jul 2015 20:58:52 +0200 Subject: [PATCH] Implement Dictionary#get and Array#get fixes #9796 --- doc/20-library-reference.md | 17 +++++++++++++++++ lib/base/array-script.cpp | 8 ++++++++ lib/base/dictionary-script.cpp | 8 ++++++++ 3 files changed, 33 insertions(+) diff --git a/doc/20-library-reference.md b/doc/20-library-reference.md index 6b78192d2..b3ed5b8de 100644 --- a/doc/20-library-reference.md +++ b/doc/20-library-reference.md @@ -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. +### Array#get + +Signature: + + function get(index); + +Retrieves the element at the specified zero-based index. + ### Array#sort Signature: @@ -616,6 +624,15 @@ Signature: Creates or updates an item with the specified `key` and `value`. +### Dictionary#get + +Signature: + + function get(key); + +Retrieves the value for the specified `key`. Returns `null` if they `key` does not exist +in the dictionary. + ## Function type ### Function#call diff --git a/lib/base/array-script.cpp b/lib/base/array-script.cpp index 5ab103608..2efde98b6 100644 --- a/lib/base/array-script.cpp +++ b/lib/base/array-script.cpp @@ -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(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)); diff --git a/lib/base/dictionary-script.cpp b/lib/base/dictionary-script.cpp index 1eb1d53d2..4e2c9c310 100644 --- a/lib/base/dictionary-script.cpp +++ b/lib/base/dictionary-script.cpp @@ -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(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));