From bde381a460186c06931d8bd6b9c57226a5f7d0b1 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Thu, 31 Mar 2022 16:33:24 -0400 Subject: [PATCH] src: fix regression introduced by #874 Fixes: https://github.com/nodejs/node-addon-api/issues/1158 Revert part of https://github.com/nodejs/node-addon-api/pull/874 to avoid breaking existing code. Signed-off-by: Michael Dawson --- napi-inl.h | 20 ++++++++++++++++---- napi.h | 20 +++++++++++++++++--- test/object/subscript_operator.cc | 22 +++++++++++++++++++--- 3 files changed, 52 insertions(+), 10 deletions(-) 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]; }