diff --git a/napi-inl.h b/napi-inl.h index c3f712c9b..14e75aa10 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -1299,17 +1299,16 @@ inline Object::Object(napi_env env, napi_value value) : Value(env, value) { } inline Object::PropertyLValue Object::operator[]( - const char* utf8name) const { + const char* utf8name) { return PropertyLValue(*this, utf8name); } inline Object::PropertyLValue Object::operator[]( - const std::string& utf8name) const { + const std::string& utf8name) { return PropertyLValue(*this, utf8name); } -inline Object::PropertyLValue Object::operator[]( - uint32_t index) const { +inline Object::PropertyLValue Object::operator[](uint32_t index) { return PropertyLValue(*this, index); } @@ -1317,6 +1316,19 @@ inline Object::PropertyLValue Object::operator[](Value index) const { return PropertyLValue(*this, index); } +inline MaybeOrValue Object::operator[](const char* utf8name) const { + return Get(utf8name); +} + +inline MaybeOrValue Object::operator[]( + const std::string& utf8name) const { + return Get(utf8name); +} + +inline MaybeOrValue Object::operator[](uint32_t index) const { + return Get(index); +} + inline MaybeOrValue Object::Has(napi_value key) const { bool result; napi_status status = napi_has_property(_env, _value, key, &result); diff --git a/napi.h b/napi.h index 0684543f7..c88ca3d45 100644 --- a/napi.h +++ b/napi.h @@ -742,22 +742,36 @@ namespace NAPI_CPP_CUSTOM_NAMESPACE { /// Gets or sets a named property. PropertyLValue operator[]( const char* utf8name ///< UTF-8 encoded null-terminated property name - ) const; + ); /// Gets or sets a named property. PropertyLValue operator[]( const std::string& utf8name ///< UTF-8 encoded property name - ) const; + ); /// Gets or sets an indexed property or array element. PropertyLValue operator[]( uint32_t index /// Property / element index - ) const; + ); /// Gets or sets an indexed property or array element. PropertyLValue operator[](Value index /// Property / element index ) const; + /// Gets a named property. + MaybeOrValue operator[]( + const char* utf8name ///< UTF-8 encoded null-terminated property name + ) const; + + /// Gets a named property. + MaybeOrValue operator[]( + const std::string& utf8name ///< UTF-8 encoded property name + ) const; + + /// Gets an indexed property or array element. + MaybeOrValue operator[](uint32_t index ///< Property / element index + ) const; + /// Checks whether a property is present. MaybeOrValue Has(napi_value key ///< Property key primitive ) const; diff --git a/test/object/subscript_operator.cc b/test/object/subscript_operator.cc index 3af39fc54..7f94b2dd3 100644 --- a/test/object/subscript_operator.cc +++ b/test/object/subscript_operator.cc @@ -1,22 +1,38 @@ #include "napi.h" +#include "test_helper.h" using namespace Napi; Value SubscriptGetWithCStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); String jsKey = info[1].As(); + + // make sure const case compiles + const Object obj2 = info[0].As(); + MaybeUnwrap(obj2[jsKey.Utf8Value().c_str()]).As(); + + Object obj = info[0].As(); return obj[jsKey.Utf8Value().c_str()]; } Value SubscriptGetWithCppStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); String jsKey = info[1].As(); + + // make sure const case compiles + const Object obj2 = info[0].As(); + MaybeUnwrap(obj2[jsKey.Utf8Value()]).As(); + + Object obj = info[0].As(); return obj[jsKey.Utf8Value()]; } Value SubscriptGetAtIndex(const CallbackInfo& info) { - Object obj = info[0].As(); uint32_t index = info[1].As(); + + // make sure const case compiles + const Object obj2 = info[0].As(); + MaybeUnwrap(obj2[index]).As(); + + Object obj = info[0].As(); return obj[index]; }