# HG changeset patch # User David Demelier # Date 1445580963 -7200 # Node ID 738751252248d61ff7ff47cae88c1b9ce85022ce # Parent aaf975293996d297e2af1cd86a1b4a89dc9efc65 Js: add optionalProperty diff -r aaf975293996 -r 738751252248 C++/modules/Js/Js.h --- a/C++/modules/Js/Js.h Fri Oct 23 08:15:48 2015 +0200 +++ b/C++/modules/Js/Js.h Fri Oct 23 08:16:03 2015 +0200 @@ -818,6 +818,44 @@ } /** + * Get an optional property `name` from the object at the specified index. + * + * @param index the object index + * @param name the property name + * @param def the default value + * @return the value or def + * @note The stack is unchanged + */ + template + inline auto optionalProperty(int index, const std::string &name, DefaultValue &&def) -> decltype(optional(0, std::forward(def))) + { + duk_get_prop_string(m_handle.get(), index, name.c_str()); + decltype(optional(0, std::forward(def))) value = optional(-1, std::forward(def)); + duk_pop(m_handle.get()); + + return value; + } + + /** + * Get an optional property by index, for arrays + * + * @param index the object index + * @param position the position int the object + * @param def the default value + * @return the value or def + * @note The stack is unchanged + */ + template + inline auto optionalProperty(int index, int position, DefaultValue &&def) -> decltype(optional(0, std::forward(def))) + { + duk_get_prop_index(m_handle.get(), index, position); + decltype(optional(0, std::forward(def))) value = optional(-1, std::forward(def)); + duk_pop(m_handle.get()); + + return value; + } + + /** * Set a property to the object at the specified index. * * @param index the object index