Skip to content

feat: add sugar method for PropertyLValue (#1651) #1655

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

Merged
merged 2 commits into from
May 9, 2025
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
5 changes: 5 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,11 @@ inline Object::PropertyLValue<Key>& Object::PropertyLValue<Key>::operator=(
return *this;
}

template <typename Key>
inline Value Object::PropertyLValue<Key>::AsValue() const {
return Value(*this);
}

template <typename Key>
inline Object::PropertyLValue<Key>::PropertyLValue(Object object, Key key)
: _env(object.Env()), _object(object), _key(key) {}
Expand Down
3 changes: 3 additions & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,9 @@ class Object : public TypeTaggable {
template <typename ValueType>
PropertyLValue& operator=(ValueType value);

/// Converts an L-value to a value. For convenience.
Value AsValue() const;

private:
PropertyLValue() = delete;
PropertyLValue(Object object, Key key);
Expand Down
6 changes: 6 additions & 0 deletions test/basic_types/value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ static Value ToObject(const CallbackInfo& info) {
return MaybeUnwrap(info[0].ToObject());
}

static Value AccessProp(const CallbackInfo& info) {
Object obj = MaybeUnwrap(info[0].ToObject());
return obj[info[1]].AsValue();
}

Object InitBasicTypesValue(Env env) {
Object exports = Object::New(env);

Expand All @@ -150,6 +155,7 @@ Object InitBasicTypesValue(Env env) {
exports["toNumber"] = Function::New(env, ToNumber);
exports["toString"] = Function::New(env, ToString);
exports["toObject"] = Function::New(env, ToObject);
exports["accessProp"] = Function::New(env, AccessProp);

exports["strictlyEquals"] = Function::New(env, StrictlyEquals);
exports["strictlyEqualsOverload"] = Function::New(env, StrictEqualsOverload);
Expand Down
17 changes: 17 additions & 0 deletions test/basic_types/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ function test (binding) {
assert(value.assertNonEmptyReturnValOnCast());
}

function accessPropTest (value) {
const testObject = { key: '123' };
const testSymbol = Symbol('123');
const testNumber = 123;
const destObj = {
testObject,
testSymbol,
[testNumber]: testNumber
};
assert.strictEqual(value.accessProp(destObj, 'testObject'), testObject);
assert.strictEqual(value.accessProp(destObj, 'testSymbol'), testSymbol);
assert.strictEqual(value.accessProp(destObj, testNumber), testNumber);
assert.strictEqual(value.accessProp(destObj, 'invalidKey'), undefined);
}

const value = binding.basic_types_value;

assertValueStrictlyEqual(value);
Expand Down Expand Up @@ -153,4 +168,6 @@ function test (binding) {
assert.strictEqual(value.toString(null), 'null');

typeConverterTest(value.toObject, Object);

accessPropTest(value);
}