Skip to content

Commit ce91e14

Browse files
dmitryashGabriel Schulhof
authored andcommitted
objectwrap: add template methods
ObjectWrap<T> was enhanced to support template methods for defining properties and methods of JS class. Now C++ methods and functions may be passed as template parameters for ObjectWrap<T>::InstanceMethod, ObjectWrap<T>::StaticAccessor, etc. There are several benefits: - no need to allocate extra memory for passing C++ function napi callback and use add_finalizer() to free memory; - a compiler can see whole chain of calls up to napi callback that may allow better optimisation. Some examples: ```cpp // Method InstanceMethod<&MyClass::method>("method"); // Read-write property InstanceAccessor<&MyClass::get, &MyClass::set>("rw_prop"); // Read-only property InstanceAccessor<&MyClass::get>("ro_prop"); ``` Fixes: #602 PR-URL: #604 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent cfa71b6 commit ce91e14

File tree

7 files changed

+694
-11
lines changed

7 files changed

+694
-11
lines changed

doc/class_property_descriptor.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class Example : public Napi::ObjectWrap<Example> {
2626
Napi::Object Example::Init(Napi::Env env, Napi::Object exports) {
2727
Napi::Function func = DefineClass(env, "Example", {
2828
// Register a class instance accessor with getter and setter functions.
29-
InstanceAccessor("value", &Example::GetValue, &Example::SetValue),
30-
// We can also register a readonly accessor by passing nullptr as the setter.
31-
InstanceAccessor("readOnlyProp", &Example::GetValue, nullptr)
29+
InstanceAccessor<&Example::GetValue, &Example::SetValue>("value"),
30+
// We can also register a readonly accessor by omitting the setter.
31+
InstanceAccessor<&Example::GetValue>("readOnlyProp")
3232
});
3333

3434
constructor = Napi::Persistent(func);

doc/object_wrap.md

Lines changed: 278 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class Example : public Napi::ObjectWrap<Example> {
3434
Napi::Object Example::Init(Napi::Env env, Napi::Object exports) {
3535
// This method is used to hook the accessor and method callbacks
3636
Napi::Function func = DefineClass(env, "Example", {
37-
InstanceMethod("GetValue", &Example::GetValue),
38-
InstanceMethod("SetValue", &Example::SetValue)
37+
InstanceMethod<&Example::GetValue>("GetValue"),
38+
InstanceMethod<&Example::SetValue>("SetValue")
3939
});
4040

4141
// Create a peristent reference to the class constructor. This will allow
@@ -289,6 +289,93 @@ One or more of `napi_property_attributes`.
289289
Returns `Napi::PropertyDescriptor` object that represents a static method of a
290290
JavaScript class.
291291
292+
### StaticMethod
293+
294+
Creates property descriptor that represents a static method of a JavaScript class.
295+
296+
```cpp
297+
template <StaticVoidMethodCallback method>
298+
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(const char* utf8name,
299+
napi_property_attributes attributes = napi_default,
300+
void* data = nullptr);
301+
```
302+
303+
- `[in] method`: The native function that represents a static method of a
304+
JavaScript class. This function returns nothing.
305+
- `[in] utf8name`: Null-terminated string that represents the name of a static
306+
method for the class.
307+
- `[in] attributes`: The attributes associated with a particular property.
308+
One or more of `napi_property_attributes`.
309+
- `[in] data`: User-provided data passed into method when it is invoked.
310+
311+
Returns `Napi::PropertyDescriptor` object that represents the static method of a
312+
JavaScript class.
313+
314+
### StaticMethod
315+
316+
Creates property descriptor that represents a static method of a JavaScript class.
317+
318+
```cpp
319+
template <StaticMethodCallback method>
320+
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(const char* utf8name,
321+
napi_property_attributes attributes = napi_default,
322+
void* data = nullptr);
323+
```
324+
325+
- `[in] method`: The native function that represents a static method of a
326+
JavaScript class.
327+
- `[in] utf8name`: Null-terminated string that represents the name of a static
328+
method for the class.
329+
- `[in] attributes`: The attributes associated with a particular property.
330+
One or more of `napi_property_attributes`.
331+
- `[in] data`: User-provided data passed into method when it is invoked.
332+
333+
Returns `Napi::PropertyDescriptor` object that represents a static method of a
334+
JavaScript class.
335+
336+
### StaticMethod
337+
338+
Creates property descriptor that represents a static method of a JavaScript class.
339+
340+
```cpp
341+
template <StaticVoidMethodCallback method>
342+
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name,
343+
napi_property_attributes attributes = napi_default,
344+
void* data = nullptr);
345+
```
346+
347+
- `[in] method`: The native function that represents a static method of a
348+
JavaScript class.
349+
- `[in] name`: Napi:Symbol that represents the name of a static
350+
method for the class.
351+
- `[in] attributes`: The attributes associated with a particular property.
352+
One or more of `napi_property_attributes`.
353+
- `[in] data`: User-provided data passed into method when it is invoked.
354+
355+
Returns `Napi::PropertyDescriptor` object that represents the static method of a
356+
JavaScript class.
357+
358+
### StaticMethod
359+
360+
Creates property descriptor that represents a static method of a JavaScript class.
361+
362+
```cpp
363+
template <StaticMethodCallback method>
364+
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name,
365+
napi_property_attributes attributes = napi_default,
366+
void* data = nullptr);
367+
```
368+
369+
- `[in] method`: The native function that represents a static method of a
370+
JavaScript class.
371+
- `[in] name`: Napi:Symbol that represents the name of a static.
372+
- `[in] attributes`: The attributes associated with a particular property.
373+
One or more of `napi_property_attributes`.
374+
- `[in] data`: User-provided data passed into method when it is invoked.
375+
376+
Returns `Napi::PropertyDescriptor` object that represents a static method of a
377+
JavaScript class.
378+
292379
### StaticAccessor
293380
294381
Creates property descriptor that represents a static accessor property of a
@@ -342,6 +429,57 @@ is invoked.
342429
Returns `Napi::PropertyDescriptor` object that represents a static accessor
343430
property of a JavaScript class.
344431
432+
### StaticAccessor
433+
434+
Creates property descriptor that represents a static accessor property of a
435+
JavaScript class.
436+
437+
```cpp
438+
template <StaticGetterCallback getter, StaticSetterCallback setter=nullptr>
439+
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor(const char* utf8name,
440+
napi_property_attributes attributes = napi_default,
441+
void* data = nullptr);
442+
```
443+
444+
- `[in] getter`: The native function to call when a get access to the property of
445+
a JavaScript class is performed.
446+
- `[in] setter`: The native function to call when a set access to the property of
447+
a JavaScript class is performed.
448+
- `[in] utf8name`: Null-terminated string that represents the name of a static
449+
accessor property for the class.
450+
- `[in] attributes`: The attributes associated with a particular property.
451+
One or more of `napi_property_attributes`.
452+
- `[in] data`: User-provided data passed into getter or setter when
453+
is invoked.
454+
455+
Returns `Napi::PropertyDescriptor` object that represents a static accessor
456+
property of a JavaScript class.
457+
458+
### StaticAccessor
459+
460+
Creates property descriptor that represents a static accessor property of a
461+
JavaScript class.
462+
463+
```cpp
464+
template <StaticGetterCallback getter, StaticSetterCallback setter=nullptr>
465+
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor(Symbol name,
466+
napi_property_attributes attributes = napi_default,
467+
void* data = nullptr);
468+
```
469+
470+
- `[in] getter`: The native function to call when a get access to the property of
471+
a JavaScript class is performed.
472+
- `[in] setter`: The native function to call when a set access to the property of
473+
a JavaScript class is performed.
474+
- `[in] name`: Napi:Symbol that represents the name of a static accessor.
475+
- `[in] attributes`: The attributes associated with a particular property.
476+
One or more of `napi_property_attributes`.
477+
- `[in] data`: User-provided data passed into getter or setter when
478+
is invoked.
479+
480+
Returns `Napi::PropertyDescriptor` object that represents a static accessor
481+
property of a JavaScript class.
482+
345483
### InstanceMethod
346484
347485
Creates property descriptor that represents an instance method of a JavaScript class.
@@ -430,6 +568,94 @@ One or more of `napi_property_attributes`.
430568
Returns `Napi::PropertyDescriptor` object that represents an instance method of a
431569
JavaScript class.
432570
571+
### InstanceMethod
572+
573+
Creates property descriptor that represents an instance method of a JavaScript class.
574+
575+
```cpp
576+
template <InstanceVoidMethodCallback method>
577+
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceMethod(const char* utf8name,
578+
napi_property_attributes attributes = napi_default,
579+
void* data = nullptr);
580+
```
581+
582+
- `[in] method`: The native function that represents an instance method of a
583+
JavaScript class.
584+
- `[in] utf8name`: Null-terminated string that represents the name of an instance
585+
method for the class.
586+
- `[in] attributes`: The attributes associated with a particular property.
587+
One or more of `napi_property_attributes`.
588+
- `[in] data`: User-provided data passed into method when it is invoked.
589+
590+
Returns `Napi::PropertyDescriptor` object that represents an instance method of a
591+
JavaScript class.
592+
593+
### InstanceMethod
594+
595+
Creates property descriptor that represents an instance method of a JavaScript class.
596+
597+
```cpp
598+
template <InstanceMethodCallback method>
599+
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceMethod(const char* utf8name,
600+
napi_property_attributes attributes = napi_default,
601+
void* data = nullptr);
602+
```
603+
604+
- `[in] method`: The native function that represents an instance method of a
605+
JavaScript class.
606+
- `[in] utf8name`: Null-terminated string that represents the name of an instance
607+
method for the class.
608+
- `[in] attributes`: The attributes associated with a particular property.
609+
One or more of `napi_property_attributes`.
610+
- `[in] data`: User-provided data passed into method when it is invoked.
611+
612+
Returns `Napi::PropertyDescriptor` object that represents an instance method of a
613+
JavaScript class.
614+
615+
### InstanceMethod
616+
617+
Creates property descriptor that represents an instance method of a JavaScript class.
618+
619+
```cpp
620+
template <InstanceVoidMethodCallback method>
621+
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceMethod(Napi::Symbol name,
622+
napi_property_attributes attributes = napi_default,
623+
void* data = nullptr);
624+
```
625+
626+
- `[in] method`: The native function that represents an instance method of a
627+
JavaScript class.
628+
- `[in] name`: The `Napi::Symbol` object whose value is used to identify the
629+
instance method for the class.
630+
- `[in] attributes`: The attributes associated with a particular property.
631+
One or more of `napi_property_attributes`.
632+
- `[in] data`: User-provided data passed into method when it is invoked.
633+
634+
Returns `Napi::PropertyDescriptor` object that represents an instance method of a
635+
JavaScript class.
636+
637+
### InstanceMethod
638+
639+
Creates property descriptor that represents an instance method of a JavaScript class.
640+
641+
```cpp
642+
template <InstanceMethodCallback method>
643+
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceMethod(Napi::Symbol name,
644+
napi_property_attributes attributes = napi_default,
645+
void* data = nullptr);
646+
```
647+
648+
- `[in] method`: The native function that represents an instance method of a
649+
JavaScript class.
650+
- `[in] name`: The `Napi::Symbol` object whose value is used to identify the
651+
instance method for the class.
652+
- `[in] attributes`: The attributes associated with a particular property.
653+
One or more of `napi_property_attributes`.
654+
- `[in] data`: User-provided data passed into method when it is invoked.
655+
656+
Returns `Napi::PropertyDescriptor` object that represents an instance method of a
657+
JavaScript class.
658+
433659
### InstanceAccessor
434660
435661
Creates property descriptor that represents an instance accessor property of a
@@ -482,6 +708,56 @@ One or more of `napi_property_attributes`.
482708
Returns `Napi::PropertyDescriptor` object that represents an instance accessor
483709
property of a JavaScript class.
484710
711+
### InstanceAccessor
712+
713+
Creates property descriptor that represents an instance accessor property of a
714+
JavaScript class.
715+
716+
```cpp
717+
template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
718+
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceAccessor(const char* utf8name,
719+
napi_property_attributes attributes = napi_default,
720+
void* data = nullptr);
721+
```
722+
723+
- `[in] getter`: The native function to call when a get access to the property of
724+
a JavaScript class is performed.
725+
- `[in] setter`: The native function to call when a set access to the property of
726+
a JavaScript class is performed.
727+
- `[in] utf8name`: Null-terminated string that represents the name of an instance
728+
accessor property for the class.
729+
- `[in] attributes`: The attributes associated with the particular property.
730+
One or more of `napi_property_attributes`.
731+
- `[in] data`: User-provided data passed into getter or setter when this is invoked.
732+
733+
Returns `Napi::PropertyDescriptor` object that represents an instance accessor
734+
property of a JavaScript class.
735+
736+
### InstanceAccessor
737+
738+
Creates property descriptor that represents an instance accessor property of a
739+
JavaScript class.
740+
741+
```cpp
742+
template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
743+
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceAccessor(Symbol name,
744+
napi_property_attributes attributes = napi_default,
745+
void* data = nullptr);
746+
```
747+
748+
- `[in] getter`: The native function to call when a get access to the property of
749+
a JavaScript class is performed.
750+
- `[in] setter`: The native function to call when a set access to the property of
751+
a JavaScript class is performed.
752+
- `[in] name`: The `Napi::Symbol` object whose value is used to identify the
753+
instance accessor.
754+
- `[in] attributes`: The attributes associated with the particular property.
755+
One or more of `napi_property_attributes`.
756+
- `[in] data`: User-provided data passed into getter or setter when this is invoked.
757+
758+
Returns `Napi::PropertyDescriptor` object that represents an instance accessor
759+
property of a JavaScript class.
760+
485761
### StaticValue
486762
487763
Creates property descriptor that represents an static value property of a

0 commit comments

Comments
 (0)