Skip to content

Commit 845ba8e

Browse files
authored
fix: add missing const to ObjectReference::Set string parameter (#1713)
1 parent af7d42e commit 845ba8e

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

napi-inl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,8 +3809,8 @@ inline MaybeOrValue<bool> ObjectReference::Set(const std::string& utf8name,
38093809
return Value().Set(utf8name, value);
38103810
}
38113811

3812-
inline MaybeOrValue<bool> ObjectReference::Set(const std::string& utf8name,
3813-
std::string& utf8value) const {
3812+
inline MaybeOrValue<bool> ObjectReference::Set(
3813+
const std::string& utf8name, const std::string& utf8value) const {
38143814
HandleScope scope(_env);
38153815
return Value().Set(utf8name, utf8value);
38163816
}

napi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,7 @@ class ObjectReference : public Reference<Object> {
17331733
MaybeOrValue<bool> Set(const std::string& utf8name, napi_value value) const;
17341734
MaybeOrValue<bool> Set(const std::string& utf8name, Napi::Value value) const;
17351735
MaybeOrValue<bool> Set(const std::string& utf8name,
1736-
std::string& utf8value) const;
1736+
const std::string& utf8value) const;
17371737
MaybeOrValue<bool> Set(const std::string& utf8name, bool boolValue) const;
17381738
MaybeOrValue<bool> Set(const std::string& utf8name, double numberValue) const;
17391739

test/object_reference.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ ObjectReference casted_reference;
2828

2929
enum VAL_TYPES { JS = 0, C_STR, CPP_STR, BOOL, INT, DOUBLE, JS_CAST };
3030

31+
// Test that Set() with std::string key and value accepts temporaries (rvalues).
32+
// This verifies that the parameter is `const std::string&` rather than
33+
// `std::string&`.
34+
void SetWithTempString(const Napi::CallbackInfo& info) {
35+
Env env = info.Env();
36+
HandleScope scope(env);
37+
38+
Napi::ObjectReference ref = Persistent(Object::New(env));
39+
ref.SuppressDestruct();
40+
41+
ref.Set(std::string("tempKey"), std::string("tempValue"));
42+
ref.Set(std::string("anotherKey"), info[0].As<Napi::String>().Utf8Value());
43+
44+
assert(MaybeUnwrap(ref.Get("tempKey")).As<Napi::String>().Utf8Value() ==
45+
"tempValue");
46+
assert(MaybeUnwrap(ref.Get("anotherKey")).As<Napi::String>().Utf8Value() ==
47+
info[0].As<Napi::String>().Utf8Value());
48+
}
49+
3150
void MoveOperatorsTest(const Napi::CallbackInfo& info) {
3251
Napi::ObjectReference existingRef;
3352
Napi::ObjectReference existingRef2;
@@ -412,6 +431,7 @@ Object InitObjectReference(Env env) {
412431
exports["unrefObjects"] = Function::New(env, UnrefObjects);
413432
exports["refObjects"] = Function::New(env, RefObjects);
414433
exports["moveOpTest"] = Function::New(env, MoveOperatorsTest);
434+
exports["setWithTempString"] = Function::New(env, SetWithTempString);
415435

416436
return exports;
417437
}

test/object_reference.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ const configObjects = [
4747

4848
function test (binding) {
4949
binding.objectreference.moveOpTest();
50+
binding.objectreference.setWithTempString('testValue');
51+
5052
function testCastedEqual (testToCompare) {
5153
const compareTest = ['hello', 'world', '!'];
5254
if (testToCompare instanceof Array) {

0 commit comments

Comments
 (0)