@@ -76,18 +76,31 @@ AddonRegistry::NodeAddon& AddonRegistry::loadAddon(std::string packageName,
76
76
}
77
77
78
78
bool AddonRegistry::tryLoadAddonAsDynamicLib (NodeAddon &addon, const std::string &path) {
79
+ {
80
+ // There can be only a SINGLE pending module (the same limitation
81
+ // has Node.js since Jan 28, 2014 commit 76b9846, see link below).
82
+ // We MUST clear it before attempting to load next addon.
83
+ // https://github.com/nodejs/node/blob/76b98462e589a69d9fd48ccb9fb5f6e96b539715/src/node.cc#L1949)
84
+ assert (nullptr == pendingRegistration_);
85
+ }
86
+
79
87
// Load addon as dynamic library
80
88
typename LoaderPolicy::Module library = LoaderPolicy::loadLibrary (path.c_str ());
81
89
if (nullptr != library) {
82
- // pending addon remains empty, we should look for the symbols...
83
- typename LoaderPolicy::Symbol initFn = LoaderPolicy::getSymbol (library, " napi_register_module_v1" );
84
- if (nullptr != initFn) {
85
- addon.initFun_ = (napi_addon_register_func)initFn;
86
- addon.moduleApiVersion_ = NODE_API_DEFAULT_MODULE_API_VERSION;
87
- // This solves https://github.com/callstackincubator/react-native-node-api-modules/issues/4
88
- typename LoaderPolicy::Symbol getVersionFn = LoaderPolicy::getSymbol (library, " node_api_module_get_api_version_v1" );
89
- if (nullptr != getVersionFn) {
90
- addon.moduleApiVersion_ = ((node_api_addon_get_api_version_func)getVersionFn)();
90
+ if (nullptr != pendingRegistration_) {
91
+ // there is a pending addon that used the deprecated `napi_register_module()`
92
+ addon.initFun_ = pendingRegistration_;
93
+ } else {
94
+ // pending addon remains empty, we should look for the symbols...
95
+ typename LoaderPolicy::Symbol initFn = LoaderPolicy::getSymbol (library, " napi_register_module_v1" );
96
+ if (nullptr != initFn) {
97
+ addon.initFun_ = (napi_addon_register_func)initFn;
98
+ addon.moduleApiVersion_ = NODE_API_DEFAULT_MODULE_API_VERSION;
99
+ // This solves https://github.com/callstackincubator/react-native-node-api-modules/issues/4
100
+ typename LoaderPolicy::Symbol getVersionFn = LoaderPolicy::getSymbol (library, " node_api_module_get_api_version_v1" );
101
+ if (nullptr != getVersionFn) {
102
+ addon.moduleApiVersion_ = ((node_api_addon_get_api_version_func)getVersionFn)();
103
+ }
91
104
}
92
105
}
93
106
@@ -96,6 +109,11 @@ bool AddonRegistry::tryLoadAddonAsDynamicLib(NodeAddon &addon, const std::string
96
109
addon.loadedFilePath_ = path;
97
110
}
98
111
}
112
+
113
+ // We MUST clear the `pendingAddon_`, even when the module failed to load!
114
+ // See: https://github.com/nodejs/node/commit/a60056df3cad2867d337fc1d7adeebe66f89031a
115
+ pendingRegistration_ = nullptr ;
116
+ return addon.isLoaded ();
99
117
}
100
118
101
119
jsi::Value AddonRegistry::instantiateAddonInRuntime (jsi::Runtime &rt, NodeAddon &addon) {
@@ -135,6 +153,12 @@ jsi::Value AddonRegistry::instantiateAddonInRuntime(jsi::Runtime &rt, NodeAddon
135
153
return lookupAddonByFullPath (rt, fqap);
136
154
}
137
155
156
+ bool AddonRegistry::handleOldNapiModuleRegister (napi_addon_register_func addonInitFunc) {
157
+ assert (nullptr == pendingRegistration_);
158
+ pendingRegistration_ = addonInitFunc;
159
+ return true ;
160
+ }
161
+
138
162
napi_status AddonRegistry::createAddonDescriptor (napi_env env, napi_value exports, napi_value *outDescriptor) {
139
163
// Create the descriptor object
140
164
assert (nullptr != outDescriptor);
0 commit comments