Skip to content
Merged
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
4 changes: 2 additions & 2 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3809,8 +3809,8 @@ inline MaybeOrValue<bool> ObjectReference::Set(const std::string& utf8name,
return Value().Set(utf8name, value);
}

inline MaybeOrValue<bool> ObjectReference::Set(const std::string& utf8name,
std::string& utf8value) const {
inline MaybeOrValue<bool> ObjectReference::Set(
const std::string& utf8name, const std::string& utf8value) const {
HandleScope scope(_env);
return Value().Set(utf8name, utf8value);
}
Expand Down
2 changes: 1 addition & 1 deletion napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1733,7 +1733,7 @@ class ObjectReference : public Reference<Object> {
MaybeOrValue<bool> Set(const std::string& utf8name, napi_value value) const;
MaybeOrValue<bool> Set(const std::string& utf8name, Napi::Value value) const;
MaybeOrValue<bool> Set(const std::string& utf8name,
std::string& utf8value) const;
const std::string& utf8value) const;
MaybeOrValue<bool> Set(const std::string& utf8name, bool boolValue) const;
MaybeOrValue<bool> Set(const std::string& utf8name, double numberValue) const;

Expand Down
20 changes: 20 additions & 0 deletions test/object_reference.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Napi::String>().Utf8Value());

assert(MaybeUnwrap(ref.Get("tempKey")).As<Napi::String>().Utf8Value() ==
"tempValue");
assert(MaybeUnwrap(ref.Get("anotherKey")).As<Napi::String>().Utf8Value() ==
info[0].As<Napi::String>().Utf8Value());
}

void MoveOperatorsTest(const Napi::CallbackInfo& info) {
Napi::ObjectReference existingRef;
Napi::ObjectReference existingRef2;
Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions test/object_reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down