Skip to content

Commit

Permalink
src: fix regression introduced by #874
Browse files Browse the repository at this point in the history
Fixes: nodejs/node-addon-api#1158

Revert part of nodejs/node-addon-api#874
to avoid breaking existing code.

PR-URL: nodejs/node-addon-api#1159 (review)
Reviewed-By: Kevin Eady <kevin.c.eady@gmail.com>

Signed-off-by: Michael Dawson <mdawson@devrus.com>
  • Loading branch information
wroy7860 committed Apr 13, 2022
1 parent 08e44eb commit b069c27
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
20 changes: 16 additions & 4 deletions napi-inl.h
Expand Up @@ -1299,24 +1299,36 @@ inline Object::Object(napi_env env, napi_value value) : Value(env, value) {
}

inline Object::PropertyLValue<std::string> Object::operator[](
const char* utf8name) const {
const char* utf8name) {
return PropertyLValue<std::string>(*this, utf8name);
}

inline Object::PropertyLValue<std::string> Object::operator[](
const std::string& utf8name) const {
const std::string& utf8name) {
return PropertyLValue<std::string>(*this, utf8name);
}

inline Object::PropertyLValue<uint32_t> Object::operator[](
uint32_t index) const {
inline Object::PropertyLValue<uint32_t> Object::operator[](uint32_t index) {
return PropertyLValue<uint32_t>(*this, index);
}

inline Object::PropertyLValue<Value> Object::operator[](Value index) const {
return PropertyLValue<Value>(*this, index);
}

inline MaybeOrValue<Value> Object::operator[](const char* utf8name) const {
return Get(utf8name);
}

inline MaybeOrValue<Value> Object::operator[](
const std::string& utf8name) const {
return Get(utf8name);
}

inline MaybeOrValue<Value> Object::operator[](uint32_t index) const {
return Get(index);
}

inline MaybeOrValue<bool> Object::Has(napi_value key) const {
bool result;
napi_status status = napi_has_property(_env, _value, key, &result);
Expand Down
20 changes: 17 additions & 3 deletions napi.h
Expand Up @@ -742,22 +742,36 @@ namespace NAPI_CPP_CUSTOM_NAMESPACE {
/// Gets or sets a named property.
PropertyLValue<std::string> operator[](
const char* utf8name ///< UTF-8 encoded null-terminated property name
) const;
);

/// Gets or sets a named property.
PropertyLValue<std::string> operator[](
const std::string& utf8name ///< UTF-8 encoded property name
) const;
);

/// Gets or sets an indexed property or array element.
PropertyLValue<uint32_t> operator[](
uint32_t index /// Property / element index
) const;
);

/// Gets or sets an indexed property or array element.
PropertyLValue<Value> operator[](Value index /// Property / element index
) const;

/// Gets a named property.
MaybeOrValue<Value> operator[](
const char* utf8name ///< UTF-8 encoded null-terminated property name
) const;

/// Gets a named property.
MaybeOrValue<Value> operator[](
const std::string& utf8name ///< UTF-8 encoded property name
) const;

/// Gets an indexed property or array element.
MaybeOrValue<Value> operator[](uint32_t index ///< Property / element index
) const;

/// Checks whether a property is present.
MaybeOrValue<bool> Has(napi_value key ///< Property key primitive
) const;
Expand Down
22 changes: 19 additions & 3 deletions 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<Object>();
String jsKey = info[1].As<String>();

// make sure const case compiles
const Object obj2 = info[0].As<Object>();
MaybeUnwrap(obj2[jsKey.Utf8Value().c_str()]).As<Boolean>();

Object obj = info[0].As<Object>();
return obj[jsKey.Utf8Value().c_str()];
}

Value SubscriptGetWithCppStyleString(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String jsKey = info[1].As<String>();

// make sure const case compiles
const Object obj2 = info[0].As<Object>();
MaybeUnwrap(obj2[jsKey.Utf8Value()]).As<Boolean>();

Object obj = info[0].As<Object>();
return obj[jsKey.Utf8Value()];
}

Value SubscriptGetAtIndex(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
uint32_t index = info[1].As<Napi::Number>();

// make sure const case compiles
const Object obj2 = info[0].As<Object>();
MaybeUnwrap(obj2[index]).As<Boolean>();

Object obj = info[0].As<Object>();
return obj[index];
}

Expand Down

0 comments on commit b069c27

Please sign in to comment.