Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: fix regression introduced by #874 #1159

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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