Skip to content

Commit 6d9052e

Browse files
committed
feat: add macro to export a function with a method
1 parent 11e3050 commit 6d9052e

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

lib/node_modules/@stdlib/napi/export/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,37 @@ The macro expects the following arguments:
129129
130130
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
131131
132+
#### STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( fcn_name, prop_name, method_name )
133+
134+
Macro for registering a Node-API module which exports a function having a method.
135+
136+
```c
137+
#include <node_api.h>
138+
139+
// ...
140+
141+
static napi_value addon( napi_env env, napi_callback_info info ) {
142+
// ...
143+
}
144+
145+
static napi_value method( napi_env env, napi_callback_info info ) {
146+
// ...
147+
}
148+
149+
// ...
150+
151+
// Register a Node-API module:
152+
STDLIB_NAPI_MODULE_EXPORT_FCN( addon, "foo", method )
153+
```
154+
155+
The macro expects the following arguments:
156+
157+
- **fcn_name**: name of the C function to export.
158+
- **prop_name**: property name.
159+
- **method_name**: name of the C function to export as a method.
160+
161+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
162+
132163
</section>
133164

134165
<!-- /.usage -->

lib/node_modules/@stdlib/napi/export/include/stdlib/napi/export.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,50 @@
6060
}; \
6161
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_napi_module_export_fcn_init )
6262

63+
/**
64+
* Macro for registering a Node-API module which exports a function having a method.
65+
*
66+
* @param fcn_name exported function name
67+
* @param prop_name property name
68+
* @param method_name exported function name for the method
69+
*
70+
* @example
71+
* #include <node_api.h>
72+
*
73+
* // ...
74+
*
75+
* static napi_value addon( napi_env env, napi_callback_info info ) {
76+
* // ...
77+
* }
78+
*
79+
* static napi_value method( napi_env env, napi_callback_info info ) {
80+
* // ...
81+
* }
82+
*
83+
* // ...
84+
*
85+
* // Register a Node-API module:
86+
* STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "foo", method )
87+
*/
88+
#define STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( fcn_name, prop_name, method_name ) \
89+
static napi_value stdlib_napi_module_export_fcn_with_method_init( \
90+
napi_env env, \
91+
napi_value exports \
92+
) { \
93+
napi_value fcn; \
94+
napi_status status = napi_create_function( \
95+
env, \
96+
"exports", \
97+
NAPI_AUTO_LENGTH, \
98+
fcn_name, \
99+
NULL, \
100+
&fcn \
101+
); \
102+
assert( status == napi_ok ); \
103+
status = napi_set_named_property( env, fcn, prop_name, method_name ) \
104+
assert( status == napi_ok ); \
105+
return fcn; \
106+
}; \
107+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_napi_module_export_fcn_with_method_init )
108+
63109
#endif // !STDLIB_NAPI_EXPORT_H

0 commit comments

Comments
 (0)