diff --git a/napi-inl.h b/napi-inl.h index 0f1717ecd..b49bbf07d 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -3809,8 +3809,8 @@ inline MaybeOrValue ObjectReference::Set(const std::string& utf8name, return Value().Set(utf8name, value); } -inline MaybeOrValue ObjectReference::Set(const std::string& utf8name, - std::string& utf8value) const { +inline MaybeOrValue ObjectReference::Set( + const std::string& utf8name, const std::string& utf8value) const { HandleScope scope(_env); return Value().Set(utf8name, utf8value); } diff --git a/napi.h b/napi.h index 013a9114d..6f4f8e5ea 100644 --- a/napi.h +++ b/napi.h @@ -1733,7 +1733,7 @@ class ObjectReference : public Reference { MaybeOrValue Set(const std::string& utf8name, napi_value value) const; MaybeOrValue Set(const std::string& utf8name, Napi::Value value) const; MaybeOrValue Set(const std::string& utf8name, - std::string& utf8value) const; + const std::string& utf8value) const; MaybeOrValue Set(const std::string& utf8name, bool boolValue) const; MaybeOrValue Set(const std::string& utf8name, double numberValue) const; diff --git a/test/object_reference.cc b/test/object_reference.cc index e74b14b2c..691b5f63d 100644 --- a/test/object_reference.cc +++ b/test/object_reference.cc @@ -28,6 +28,25 @@ ObjectReference casted_reference; enum VAL_TYPES { JS = 0, C_STR, CPP_STR, BOOL, INT, DOUBLE, JS_CAST }; +// Test that Set() with std::string key and value accepts temporaries (rvalues). +// This verifies that the parameter is `const std::string&` rather than +// `std::string&`. +void SetWithTempString(const Napi::CallbackInfo& info) { + Env env = info.Env(); + HandleScope scope(env); + + Napi::ObjectReference ref = Persistent(Object::New(env)); + ref.SuppressDestruct(); + + ref.Set(std::string("tempKey"), std::string("tempValue")); + ref.Set(std::string("anotherKey"), info[0].As().Utf8Value()); + + assert(MaybeUnwrap(ref.Get("tempKey")).As().Utf8Value() == + "tempValue"); + assert(MaybeUnwrap(ref.Get("anotherKey")).As().Utf8Value() == + info[0].As().Utf8Value()); +} + void MoveOperatorsTest(const Napi::CallbackInfo& info) { Napi::ObjectReference existingRef; Napi::ObjectReference existingRef2; @@ -412,6 +431,7 @@ Object InitObjectReference(Env env) { exports["unrefObjects"] = Function::New(env, UnrefObjects); exports["refObjects"] = Function::New(env, RefObjects); exports["moveOpTest"] = Function::New(env, MoveOperatorsTest); + exports["setWithTempString"] = Function::New(env, SetWithTempString); return exports; } diff --git a/test/object_reference.js b/test/object_reference.js index 8039b3741..5b713dce8 100644 --- a/test/object_reference.js +++ b/test/object_reference.js @@ -47,6 +47,8 @@ const configObjects = [ function test (binding) { binding.objectreference.moveOpTest(); + binding.objectreference.setWithTempString('testValue'); + function testCastedEqual (testToCompare) { const compareTest = ['hello', 'world', '!']; if (testToCompare instanceof Array) {