Skip to content

Commit

Permalink
src: implement missing descriptor defs for symbols
Browse files Browse the repository at this point in the history
Implements descriptor definitions with symbols for `StaticMethod`,
`StaticAccessor`, `InstanceAccessor`, `StaticValue`, `InstanceValue`.

Ref: #279

PR-URL: #280
Refs: #279
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
Philipp Renoth authored and mhdawson committed Sep 18, 2018
1 parent 38e01b7 commit 0a00e7c
Show file tree
Hide file tree
Showing 5 changed files with 530 additions and 77 deletions.
133 changes: 133 additions & 0 deletions doc/object_wrap.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,50 @@ One or more of `napi_property_attributes`.
Returns `Napi::PropertyDescriptor` object that represents a static method of a
JavaScript class.

### StaticMethod

Creates property descriptor that represents a static method of a JavaScript class.

```cpp
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name,
StaticVoidMethodCallback method,
napi_property_attributes attributes = napi_default,
void* data = nullptr);
```
- `[in] name`: Napi:Symbol that represents the name of a static
method for the class.
- `[in] method`: The native function that represents a static method of a
JavaScript class.
- `[in] attributes`: The attributes associated with a particular property.
One or more of `napi_property_attributes`.
- `[in] data`: User-provided data passed into method when it is invoked.
Returns `Napi::PropertyDescriptor` object that represents the static method of a
JavaScript class.
### StaticMethod
Creates property descriptor that represents a static method of a JavaScript class.
```cpp
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name,
StaticMethodCallback method,
napi_property_attributes attributes = napi_default,
void* data = nullptr);
```

method for the class.
- `[in] name`: Napi:Symbol that represents the name of a static.
- `[in] method`: The native function that represents a static method of a
JavaScript class.
- `[in] attributes`: The attributes associated with a particular property.
One or more of `napi_property_attributes`.
- `[in] data`: User-provided data passed into method when it is invoked.

Returns `Napi::PropertyDescriptor` object that represents a static method of a
JavaScript class.

### StaticAccessor

Creates property descriptor that represents a static accessor property of a
Expand Down Expand Up @@ -261,6 +305,32 @@ is invoked.
Returns `Napi::PropertyDescriptor` object that represents a static accessor
property of a JavaScript class.
### StaticAccessor
Creates property descriptor that represents a static accessor property of a
JavaScript class.
```cpp
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor(Symbol name,
StaticGetterCallback getter,
StaticSetterCallback setter,
napi_property_attributes attributes = napi_default,
void* data = nullptr);
```

- `[in] name`: Napi:Symbol that represents the name of a static accessor.
- `[in] getter`: The native function to call when a get access to the property of
a JavaScript class is performed.
- `[in] setter`: The native function to call when a set access to the property of
a JavaScript class is performed.
- `[in] attributes`: The attributes associated with a particular property.
One or more of `napi_property_attributes`.
- `[in] data`: User-provided data passed into getter or setter when
is invoked.

Returns `Napi::PropertyDescriptor` object that represents a static accessor
property of a JavaScript class.

### InstanceMethod

Creates property descriptor that represents an instance method of a JavaScript class.
Expand Down Expand Up @@ -375,6 +445,32 @@ One or more of `napi_property_attributes`.
Returns `Napi::PropertyDescriptor` object that represents an instance accessor
property of a JavaScript class.
### InstanceAccessor
Creates property descriptor that represents an instance accessor property of a
JavaScript class.
```cpp
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceAccessor(Symbol name,
InstanceGetterCallback getter,
InstanceSetterCallback setter,
napi_property_attributes attributes = napi_default,
void* data = nullptr);
```

- `[in] name`: The `Napi::Symbol` object whose value is used to identify the
instance accessor.
- `[in] getter`: The native function to call when a get access to the property of
a JavaScript class is performed.
- `[in] setter`: The native function to call when a set access to the property of
a JavaScript class is performed.
- `[in] attributes`: The attributes associated with the particular property.
One or more of `napi_property_attributes`.
- `[in] data`: User-provided data passed into getter or setter when this is invoked.

Returns `Napi::PropertyDescriptor` object that represents an instance accessor
property of a JavaScript class.

### StaticValue

Creates property descriptor that represents an static value property of a
Expand All @@ -394,6 +490,25 @@ to the napi_static attribute. One or more of `napi_property_attributes`.
Returns `Napi::PropertyDescriptor` object that represents an static value
property of a JavaScript class
### StaticValue
Creates property descriptor that represents an static value property of a
JavaScript class.
```cpp
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticValue(Symbol name,
Napi::Value value,
napi_property_attributes attributes = napi_default);
```

- `[in] name`: The `Napi::Symbol` object whose value is used to identify the
name of the static property.
- `[in] value`: The value that's retrieved by a get access of the property.
- `[in] attributes`: The attributes to be associated with the property in addition
to the napi_static attribute. One or more of `napi_property_attributes`.

Returns `Napi::PropertyDescriptor` object that represents an static value
property of a JavaScript class

### InstanceValue

Creates property descriptor that represents an instance value property of a
Expand All @@ -411,3 +526,21 @@ One or more of `napi_property_attributes`.
Returns `Napi::PropertyDescriptor` object that represents an instance value
property of a JavaScript class.
### InstanceValue
Creates property descriptor that represents an instance value property of a
JavaScript class.
```cpp
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceValue(Symbol name,
Napi::Value value,
napi_property_attributes attributes = napi_default);
```

- `[in] name`: The `Napi::Symbol` object whose value is used to identify the
name of the property.
- `[in] value`: The value that's retrieved by a get access of the property.
- `[in] attributes`: The attributes to be associated with the property.
One or more of `napi_property_attributes`.

Returns `Napi::PropertyDescriptor` object that represents an instance value
96 changes: 96 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2823,6 +2823,40 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
return desc;
}

template <typename T>
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
Symbol name,
StaticVoidMethodCallback method,
napi_property_attributes attributes,
void* data) {
// TODO: Delete when the class is destroyed
StaticVoidMethodCallbackData* callbackData = new StaticVoidMethodCallbackData({ method, data });

napi_property_descriptor desc = napi_property_descriptor();
desc.name = name;
desc.method = T::StaticVoidMethodCallbackWrapper;
desc.data = callbackData;
desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
return desc;
}

template <typename T>
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
Symbol name,
StaticMethodCallback method,
napi_property_attributes attributes,
void* data) {
// TODO: Delete when the class is destroyed
StaticMethodCallbackData* callbackData = new StaticMethodCallbackData({ method, data });

napi_property_descriptor desc = napi_property_descriptor();
desc.name = name;
desc.method = T::StaticMethodCallbackWrapper;
desc.data = callbackData;
desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
return desc;
}

template <typename T>
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticAccessor(
const char* utf8name,
Expand All @@ -2843,6 +2877,26 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticAccessor(
return desc;
}

template <typename T>
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticAccessor(
Symbol name,
StaticGetterCallback getter,
StaticSetterCallback setter,
napi_property_attributes attributes,
void* data) {
// TODO: Delete when the class is destroyed
StaticAccessorCallbackData* callbackData =
new StaticAccessorCallbackData({ getter, setter, data });

napi_property_descriptor desc = napi_property_descriptor();
desc.name = name;
desc.getter = getter != nullptr ? T::StaticGetterCallbackWrapper : nullptr;
desc.setter = setter != nullptr ? T::StaticSetterCallbackWrapper : nullptr;
desc.data = callbackData;
desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
return desc;
}

template <typename T>
inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceMethod(
const char* utf8name,
Expand Down Expand Up @@ -2933,6 +2987,26 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceAccessor(
return desc;
}

template <typename T>
inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceAccessor(
Symbol name,
InstanceGetterCallback getter,
InstanceSetterCallback setter,
napi_property_attributes attributes,
void* data) {
// TODO: Delete when the class is destroyed
InstanceAccessorCallbackData* callbackData =
new InstanceAccessorCallbackData({ getter, setter, data });

napi_property_descriptor desc = napi_property_descriptor();
desc.name = name;
desc.getter = getter != nullptr ? T::InstanceGetterCallbackWrapper : nullptr;
desc.setter = setter != nullptr ? T::InstanceSetterCallbackWrapper : nullptr;
desc.data = callbackData;
desc.attributes = attributes;
return desc;
}

template <typename T>
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticValue(const char* utf8name,
Napi::Value value, napi_property_attributes attributes) {
Expand All @@ -2943,6 +3017,16 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticValue(const char* utf8nam
return desc;
}

template <typename T>
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticValue(Symbol name,
Napi::Value value, napi_property_attributes attributes) {
napi_property_descriptor desc = napi_property_descriptor();
desc.name = name;
desc.value = value;
desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
return desc;
}

template <typename T>
inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceValue(
const char* utf8name,
Expand All @@ -2955,6 +3039,18 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceValue(
return desc;
}

template <typename T>
inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceValue(
Symbol name,
Napi::Value value,
napi_property_attributes attributes) {
napi_property_descriptor desc = napi_property_descriptor();
desc.name = name;
desc.value = value;
desc.attributes = attributes;
return desc;
}

template <typename T>
inline napi_value ObjectWrap<T>::ConstructorCallbackWrapper(
napi_env env,
Expand Down
24 changes: 24 additions & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1453,11 +1453,24 @@ namespace Napi {
StaticMethodCallback method,
napi_property_attributes attributes = napi_default,
void* data = nullptr);
static PropertyDescriptor StaticMethod(Symbol name,
StaticVoidMethodCallback method,
napi_property_attributes attributes = napi_default,
void* data = nullptr);
static PropertyDescriptor StaticMethod(Symbol name,
StaticMethodCallback method,
napi_property_attributes attributes = napi_default,
void* data = nullptr);
static PropertyDescriptor StaticAccessor(const char* utf8name,
StaticGetterCallback getter,
StaticSetterCallback setter,
napi_property_attributes attributes = napi_default,
void* data = nullptr);
static PropertyDescriptor StaticAccessor(Symbol name,
StaticGetterCallback getter,
StaticSetterCallback setter,
napi_property_attributes attributes = napi_default,
void* data = nullptr);
static PropertyDescriptor InstanceMethod(const char* utf8name,
InstanceVoidMethodCallback method,
napi_property_attributes attributes = napi_default,
Expand All @@ -1479,12 +1492,23 @@ namespace Napi {
InstanceSetterCallback setter,
napi_property_attributes attributes = napi_default,
void* data = nullptr);
static PropertyDescriptor InstanceAccessor(Symbol name,
InstanceGetterCallback getter,
InstanceSetterCallback setter,
napi_property_attributes attributes = napi_default,
void* data = nullptr);
static PropertyDescriptor StaticValue(const char* utf8name,
Napi::Value value,
napi_property_attributes attributes = napi_default);
static PropertyDescriptor StaticValue(Symbol name,
Napi::Value value,
napi_property_attributes attributes = napi_default);
static PropertyDescriptor InstanceValue(const char* utf8name,
Napi::Value value,
napi_property_attributes attributes = napi_default);
static PropertyDescriptor InstanceValue(Symbol name,
Napi::Value value,
napi_property_attributes attributes = napi_default);

private:
static napi_value ConstructorCallbackWrapper(napi_env env, napi_callback_info info);
Expand Down
Loading

0 comments on commit 0a00e7c

Please sign in to comment.