Skip to content

Commit

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

Revert part of nodejs#874
to avoid breaking existing code.

Signed-off-by: Michael Dawson <[email protected]>
  • Loading branch information
mhdawson committed Mar 31, 2022
1 parent 0127496 commit bde381a
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 bde381a

Please sign in to comment.