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
17 changes: 17 additions & 0 deletions doc/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@ void Nan::SetPrototypeMethod(v8::Local<v8::FunctionTemplate> recv,

Sets getters and setters for a property with a given name on an `ObjectTemplate` or a plain `Object`. Accepts getters with the `Nan::GetterCallback` signature (see <a href="#api_nan_getter">Getter declaration</a>) and setters with the `Nan::SetterCallback` signature (see <a href="#api_nan_setter">Setter declaration</a>).

**Note** `v8::AccessControl` was deprectaed in v8 14.2 and subsequently removed. To maintain backward compatibility new enum `enum AccessControl {DEFAULT = 0};` was added. Nan will determine v8 version used and switch to a correct signature.

Signature:

```c++
Expand All @@ -531,6 +533,21 @@ bool SetAccessor(v8::Local<v8::Object> obj,
v8::Local<v8::Value> data = v8::Local<v8::Value>(),
v8::AccessControl settings = v8::DEFAULT,
v8::PropertyAttribute attribute = v8::None)
// Starting from v8 14.2 new enum is used
void SetAccessor(v8::Local<v8::ObjectTemplate> tpl,
v8::Local<v8::String> name,
Nan::GetterCallback getter,
Nan::SetterCallback setter = 0,
v8::Local<v8::Value> data = v8::Local<v8::Value>(),
enum Nan::AccessControl settings = DEFAULT,
v8::PropertyAttribute attribute = v8::None);
bool SetAccessor(v8::Local<v8::Object> obj,
v8::Local<v8::String> name,
Nan::GetterCallback getter,
Nan::SetterCallback setter = 0,
v8::Local<v8::Value> data = v8::Local<v8::Value>(),
enum Nan::AccessControl settings = DEFAULT,
v8::PropertyAttribute attribute = v8::None)
```

See the V8 [`ObjectTemplate#SetAccessor()`](https://v8docs.nodesource.com/node-8.16/db/d5f/classv8_1_1_object_template.html#aca0ed196f8a9adb1f68b1aadb6c9cd77) and [`Object#SetAccessor()`](https://v8docs.nodesource.com/node-8.16/db/d85/classv8_1_1_object.html#ae91b3b56b357f285288c89fbddc46d1b) for further information about how to use `Nan::SetAccessor()`.
Expand Down
92 changes: 91 additions & 1 deletion nan.h
Original file line number Diff line number Diff line change
Expand Up @@ -2619,6 +2619,96 @@ inline void SetPrototypeMethod(
}

//=== Accessors and Such =======================================================
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 14 \
|| (V8_MAJOR_VERSION == 14 && defined(V8_MINOR_VERSION) \
&& V8_MINOR_VERSION >= 2))

enum AccessControl {DEFAULT = 0};

inline void SetAccessor(
v8::Local<v8::ObjectTemplate> tpl
, v8::Local<v8::String> name
, GetterCallback getter
, SetterCallback setter = 0
, v8::Local<v8::Value> data = v8::Local<v8::Value>()
, enum AccessControl settings = DEFAULT
, v8::PropertyAttribute attribute = v8::None) {
HandleScope scope;

imp::NativeGetter getter_ =
imp::GetterCallbackWrapper;
imp::NativeSetter setter_ =
setter ? imp::SetterCallbackWrapper : 0;

v8::Local<v8::ObjectTemplate> otpl = New<v8::ObjectTemplate>();
otpl->SetInternalFieldCount(imp::kAccessorFieldCount);
v8::Local<v8::Object> obj = NewInstance(otpl).ToLocalChecked();

obj->SetInternalField(
imp::kGetterIndex
, New<v8::External>(reinterpret_cast<void *>(getter)));

if (setter != 0) {
obj->SetInternalField(
imp::kSetterIndex
, New<v8::External>(reinterpret_cast<void *>(setter)));
}

if (!data.IsEmpty()) {
obj->SetInternalField(imp::kDataIndex, data);
}

tpl->SetNativeDataProperty(
name
, getter_
, setter_
, obj
, attribute
);
}

inline bool SetAccessor(
v8::Local<v8::Object> obj
, v8::Local<v8::String> name
, GetterCallback getter
, SetterCallback setter = 0
, v8::Local<v8::Value> data = v8::Local<v8::Value>()
, enum AccessControl settings = DEFAULT
, v8::PropertyAttribute attribute = v8::None) {
HandleScope scope;

imp::NativeGetter getter_ =
imp::GetterCallbackWrapper;
imp::NativeSetter setter_ =
setter ? imp::SetterCallbackWrapper : 0;

v8::Local<v8::ObjectTemplate> otpl = New<v8::ObjectTemplate>();
otpl->SetInternalFieldCount(imp::kAccessorFieldCount);
v8::Local<v8::Object> dataobj = NewInstance(otpl).ToLocalChecked();

dataobj->SetInternalField(
imp::kGetterIndex
, New<v8::External>(reinterpret_cast<void *>(getter)));

if (!data.IsEmpty()) {
dataobj->SetInternalField(imp::kDataIndex, data);
}

if (setter) {
dataobj->SetInternalField(
imp::kSetterIndex
, New<v8::External>(reinterpret_cast<void *>(setter)));
}
return obj->SetNativeDataProperty(
GetCurrentContext()
, name
, getter_
, setter_
, dataobj
, attribute).FromMaybe(false);
}

#else

NAN_DEPRECATED inline void SetAccessor(
v8::Local<v8::ObjectTemplate> tpl
Expand Down Expand Up @@ -2795,7 +2885,7 @@ inline bool SetAccessor(
, attribute);
#endif
}

#endif
inline void SetNamedPropertyHandler(
v8::Local<v8::ObjectTemplate> tpl
, PropertyGetterCallback getter
Expand Down