Skip to content
Open
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
22 changes: 22 additions & 0 deletions doc/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,28 @@ from being added to it and marking all existing properties as non-configurable.
Values of present properties can still be changed as long as they are
writable.

### GetPrototype()

```cpp
Napi::Object Napi::Object::GetPrototype() const;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prototype of the method is MaybeOrValue<Object> Object::GetPrototype() const.

Maybe it could be good to explain if the object has no prototype.

```

The `Napi::Object::GetPrototype()` method returns the prototype of the object.

### SetPrototype()

```cpp
bool Napi::Object::SetPrototype(const Napi::Object& value) const;
```

- `[in] value`: The prototype value.

The `Napi::Object::SetPrototype()` method sets the prototype of the object.

**NOTE**: The support for `Napi::Object::SetPrototype` is only available when
using `NAPI_EXPERIMENTAL` and building against Node.js headers that support this
feature.

### operator\[\]()

```cpp
Expand Down
13 changes: 13 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1966,6 +1966,19 @@ inline MaybeOrValue<bool> Object::Seal() const {
}
#endif // NAPI_VERSION >= 8

inline MaybeOrValue<Object> Object::GetPrototype() const {
napi_value result;
napi_status status = napi_get_prototype(_env, _value, &result);
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, Object(_env, result), Object);
}

#ifdef NODE_API_EXPERIMENTAL_HAS_SET_PROTOTYPE
inline MaybeOrValue<bool> Object::SetPrototype(const Object& value) const {
napi_status status = node_api_set_prototype(_env, _value, value);
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool);
}
#endif

////////////////////////////////////////////////////////////////////////////////
// External class
////////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,12 @@ class Object : public TypeTaggable {
/// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof
MaybeOrValue<bool> Seal() const;
#endif // NAPI_VERSION >= 8

MaybeOrValue<Object> GetPrototype() const;

#ifdef NODE_API_EXPERIMENTAL_HAS_SET_PROTOTYPE
MaybeOrValue<bool> SetPrototype(const Object& value) const;
#endif
};

template <typename T>
Expand Down
18 changes: 18 additions & 0 deletions test/object/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,19 @@ Value InstanceOf(const CallbackInfo& info) {
return Boolean::New(info.Env(), MaybeUnwrap(obj.InstanceOf(constructor)));
}

Value GetPrototype(const CallbackInfo& info) {
Object obj = info[0].UnsafeAs<Object>();
return MaybeUnwrap(obj.GetPrototype());
}

#ifdef NODE_API_EXPERIMENTAL_HAS_SET_PROTOTYPE
Value SetPrototype(const CallbackInfo& info) {
Object obj = info[0].UnsafeAs<Object>();
Object prototype = info[1].UnsafeAs<Object>();
return Boolean::New(info.Env(), MaybeUnwrap(obj.SetPrototype(prototype)));
}
#endif // NODE_API_EXPERIMENTAL_HAS_SET_PROTOTYPE

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

Expand Down Expand Up @@ -426,5 +439,10 @@ Object InitObject(Env env) {
Function::New(env, SubscriptSetWithCppStyleString);
exports["subscriptSetAtIndex"] = Function::New(env, SubscriptSetAtIndex);

exports["getPrototype"] = Function::New(env, GetPrototype);
#ifdef NODE_API_EXPERIMENTAL_HAS_SET_PROTOTYPE
exports["setPrototype"] = Function::New(env, SetPrototype);
#endif // NODE_API_EXPERIMENTAL_HAS_SET_PROTOTYPE

return exports;
}
13 changes: 13 additions & 0 deletions test/object/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,17 @@ function test (binding) {
c: 3
});
}

{
const prototype = {};
const obj = Object.create(prototype);
assert.strictEqual(binding.object.getPrototype(obj), prototype);
}
Copy link
Member

@NickNaso NickNaso Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be useful to test when the oblect has no prototype const obj = Object.create(null); ?


if ('setPrototype' in binding.object) {
const prototype = {};
const obj = Object.create(null);
assert.strictEqual(binding.object.setPrototype(obj, prototype), true);
assert.strictEqual(Object.getPrototypeOf(obj), prototype);
}
}