From a5a011979944ec230e6ba14e642c9d37f7cbddb9 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 2 Aug 2018 08:45:19 +0200 Subject: [PATCH] Implement the Dictionary#clear script function --- doc/18-library-reference.md | 8 ++++++++ lib/base/dictionary-script.cpp | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/doc/18-library-reference.md b/doc/18-library-reference.md index 5a3209dfe..30277bfdf 100644 --- a/doc/18-library-reference.md +++ b/doc/18-library-reference.md @@ -1428,6 +1428,14 @@ Signature: Removes the item with the specified `key`. Trying to remove an item which does not exist is a no-op. +### Dictionary#clear + +Signature: + + function clear(); + +Removes all items from the dictionary. + ### Dictionary#set Signature: diff --git a/lib/base/dictionary-script.cpp b/lib/base/dictionary-script.cpp index d188119e5..7d805a59b 100644 --- a/lib/base/dictionary-script.cpp +++ b/lib/base/dictionary-script.cpp @@ -57,6 +57,14 @@ static void DictionaryRemove(const String& key) self->Remove(key); } +static void DictionaryClear() +{ + ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); + Dictionary::Ptr self = static_cast(vframe->Self); + REQUIRE_NOT_NULL(self); + self->Clear(); +} + static bool DictionaryContains(const String& key) { ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); @@ -115,6 +123,7 @@ Object::Ptr Dictionary::GetPrototype() { "set", new Function("Dictionary#set", DictionarySet, { "key", "value" }) }, { "get", new Function("Dictionary#get", DictionaryGet, { "key" }) }, { "remove", new Function("Dictionary#remove", DictionaryRemove, { "key" }) }, + { "clear", new Function("Dictionary#clear", DictionaryClear, {}) }, { "contains", new Function("Dictionary#contains", DictionaryContains, { "key" }, true) }, { "shallow_clone", new Function("Dictionary#shallow_clone", DictionaryShallowClone, {}, true) }, { "keys", new Function("Dictionary#keys", DictionaryKeys, {}, true) },