Skip to content

Commit 40ed7ce

Browse files
committed
src: fix regression introduced by #874
Fixes: #1158 Revert part of #874 to avoid breaking existing code. PR-URL: #1159 (review) Reviewed-By: Kevin Eady <[email protected]> Signed-off-by: Michael Dawson <[email protected]>
1 parent 9bea434 commit 40ed7ce

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

napi-inl.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,24 +1299,36 @@ inline Object::Object(napi_env env, napi_value value) : Value(env, value) {
12991299
}
13001300

13011301
inline Object::PropertyLValue<std::string> Object::operator[](
1302-
const char* utf8name) const {
1302+
const char* utf8name) {
13031303
return PropertyLValue<std::string>(*this, utf8name);
13041304
}
13051305

13061306
inline Object::PropertyLValue<std::string> Object::operator[](
1307-
const std::string& utf8name) const {
1307+
const std::string& utf8name) {
13081308
return PropertyLValue<std::string>(*this, utf8name);
13091309
}
13101310

1311-
inline Object::PropertyLValue<uint32_t> Object::operator[](
1312-
uint32_t index) const {
1311+
inline Object::PropertyLValue<uint32_t> Object::operator[](uint32_t index) {
13131312
return PropertyLValue<uint32_t>(*this, index);
13141313
}
13151314

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

1319+
inline MaybeOrValue<Value> Object::operator[](const char* utf8name) const {
1320+
return Get(utf8name);
1321+
}
1322+
1323+
inline MaybeOrValue<Value> Object::operator[](
1324+
const std::string& utf8name) const {
1325+
return Get(utf8name);
1326+
}
1327+
1328+
inline MaybeOrValue<Value> Object::operator[](uint32_t index) const {
1329+
return Get(index);
1330+
}
1331+
13201332
inline MaybeOrValue<bool> Object::Has(napi_value key) const {
13211333
bool result;
13221334
napi_status status = napi_has_property(_env, _value, key, &result);

napi.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,22 +742,36 @@ namespace NAPI_CPP_CUSTOM_NAMESPACE {
742742
/// Gets or sets a named property.
743743
PropertyLValue<std::string> operator[](
744744
const char* utf8name ///< UTF-8 encoded null-terminated property name
745-
) const;
745+
);
746746

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

752752
/// Gets or sets an indexed property or array element.
753753
PropertyLValue<uint32_t> operator[](
754754
uint32_t index /// Property / element index
755-
) const;
755+
);
756756

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

761+
/// Gets a named property.
762+
MaybeOrValue<Value> operator[](
763+
const char* utf8name ///< UTF-8 encoded null-terminated property name
764+
) const;
765+
766+
/// Gets a named property.
767+
MaybeOrValue<Value> operator[](
768+
const std::string& utf8name ///< UTF-8 encoded property name
769+
) const;
770+
771+
/// Gets an indexed property or array element.
772+
MaybeOrValue<Value> operator[](uint32_t index ///< Property / element index
773+
) const;
774+
761775
/// Checks whether a property is present.
762776
MaybeOrValue<bool> Has(napi_value key ///< Property key primitive
763777
) const;

test/object/subscript_operator.cc

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
#include "napi.h"
2+
#include "test_helper.h"
23

34
using namespace Napi;
45

56
Value SubscriptGetWithCStyleString(const CallbackInfo& info) {
6-
Object obj = info[0].As<Object>();
77
String jsKey = info[1].As<String>();
8+
9+
// make sure const case compiles
10+
const Object obj2 = info[0].As<Object>();
11+
MaybeUnwrap(obj2[jsKey.Utf8Value().c_str()]).As<Boolean>();
12+
13+
Object obj = info[0].As<Object>();
814
return obj[jsKey.Utf8Value().c_str()];
915
}
1016

1117
Value SubscriptGetWithCppStyleString(const CallbackInfo& info) {
12-
Object obj = info[0].As<Object>();
1318
String jsKey = info[1].As<String>();
19+
20+
// make sure const case compiles
21+
const Object obj2 = info[0].As<Object>();
22+
MaybeUnwrap(obj2[jsKey.Utf8Value()]).As<Boolean>();
23+
24+
Object obj = info[0].As<Object>();
1425
return obj[jsKey.Utf8Value()];
1526
}
1627

1728
Value SubscriptGetAtIndex(const CallbackInfo& info) {
18-
Object obj = info[0].As<Object>();
1929
uint32_t index = info[1].As<Napi::Number>();
30+
31+
// make sure const case compiles
32+
const Object obj2 = info[0].As<Object>();
33+
MaybeUnwrap(obj2[index]).As<Boolean>();
34+
35+
Object obj = info[0].As<Object>();
2036
return obj[index];
2137
}
2238

0 commit comments

Comments
 (0)