@@ -873,7 +873,7 @@ JavaScript Object associated with the `napi_ref`. Otherise, result
873
873
will be NULL.
874
874
875
875
## Module registration
876
- N-API modules are registered in the same manner as other modules
876
+ N-API modules are registered in a manner similar to other modules
877
877
except that instead of using the `NODE_MODULE` macro the following
878
878
is used:
879
879
@@ -885,32 +885,39 @@ The next difference is the signature for the `Init` method. For a N-API
885
885
module it is as follows:
886
886
887
887
```C
888
- void Init(napi_env env, napi_value exports, napi_value module, void* priv );
888
+ napi_value Init(napi_env env, napi_value exports);
889
889
```
890
890
891
- As with any other module, functions are exported by either adding them to
892
- the `exports` or `module` objects passed to the `Init` method.
891
+ The return value from `Init` is treated as the `exports` object for the module.
892
+ The `Init` method is passed an empty object via the `exports` parameter as a
893
+ convenience. If `Init` returns NULL, the parameter passed as `exports` is
894
+ exported by the module. N-API modules cannot modify the `module` object but can
895
+ specify anything as the `exports` property of the module.
893
896
894
897
For example, to add the method `hello` as a function so that it can be called
895
898
as a method provided by the addon:
896
899
897
900
```C
898
- void Init(napi_env env, napi_value exports, napi_value module, void* priv ) {
901
+ napi_value Init(napi_env env, napi_value exports) {
899
902
napi_status status;
900
903
napi_property_descriptor desc =
901
904
{"hello", Method, 0, 0, 0, napi_default, 0};
905
+ if (status != napi_ok) return nullptr;
902
906
status = napi_define_properties(env, exports, 1, &desc);
907
+ if (status != napi_ok) return nullptr;
908
+ return exports;
903
909
}
904
910
```
905
911
906
912
For example, to set a function to be returned by the `require()` for the addon:
907
913
908
914
```C
909
- void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
915
+ napi_value Init(napi_env env, napi_value exports) {
916
+ napi_value method;
910
917
napi_status status;
911
- napi_property_descriptor desc =
912
- {"exports", Method, 0, 0, 0, napi_default, 0} ;
913
- status = napi_define_properties(env, module, 1, &desc) ;
918
+ status = napi_create_function(env, "exports", Method, NULL, &method));
919
+ if (status != napi_ok) return nullptr ;
920
+ return method ;
914
921
}
915
922
```
916
923
@@ -919,28 +926,30 @@ For example, to define a class so that new instances can be created
919
926
920
927
```C
921
928
// NOTE: partial example, not all referenced code is included
922
-
923
- napi_status status;
924
- napi_property_descriptor properties[] = {
929
+ napi_value Init(napi_env env, napi_value exports) {
930
+ napi_status status;
931
+ napi_property_descriptor properties[] = {
925
932
{ "value", nullptr, GetValue, SetValue, 0, napi_default, 0 },
926
933
DECLARE_NAPI_METHOD("plusOne", PlusOne),
927
934
DECLARE_NAPI_METHOD("multiply", Multiply),
928
- };
935
+ };
929
936
930
- napi_value cons;
931
- status =
932
- napi_define_class(env, "MyObject", New, nullptr, 3, properties, &cons);
933
- if (status != napi_ok) return;
937
+ napi_value cons;
938
+ status =
939
+ napi_define_class(env, "MyObject", New, nullptr, 3, properties, &cons);
940
+ if (status != napi_ok) return nullptr ;
934
941
935
- status = napi_create_reference(env, cons, 1, &constructor);
936
- if (status != napi_ok) return;
942
+ status = napi_create_reference(env, cons, 1, &constructor);
943
+ if (status != napi_ok) return nullptr ;
937
944
938
- status = napi_set_named_property(env, exports, "MyObject", cons);
939
- if (status != napi_ok) return;
945
+ status = napi_set_named_property(env, exports, "MyObject", cons);
946
+ if (status != napi_ok) return nullptr;
947
+
948
+ return exports;
949
+ }
940
950
```
941
951
942
- For more details on setting properties on either the `exports` or `module`
943
- objects, see the section on
952
+ For more details on setting properties on objects, see the section on
944
953
[Working with JavaScript Properties][].
945
954
946
955
For more details on building addon modules in general, refer to the existing API
0 commit comments