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
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);
}