@@ -10,25 +10,41 @@ export const CPP_SOURCE_PATH = path.join(__dirname, "../cpp");
1010 * Generates source code which injects the Node API functions from the host.
1111 */
1212export function generateSource ( functions : FunctionDecl [ ] ) {
13- return [
14- "// This file is generated by react-native-node-api-modules" ,
15- `#include <Logger.hpp>` ,
16- `#include <weak_node_api.hpp>` ,
17- "namespace callstack::nodeapihost {" ,
18- "using node_api::internal::inject_host;" ,
19- "using node_api::internal::NodeApiHost;" ,
20- "void injectIntoWeakNodeApi() {" ,
21- ` log_debug("Injecting WeakNodeApiHost");` ,
22- " inject_host(NodeApiHost {" ,
23- ...functions
24- . filter ( ( { kind } ) => kind === "engine" )
25- . flatMap ( ( { name } ) => {
26- return [ `.${ name } = ${ name } ,` ] ;
27- } ) ,
28- " });" ,
29- "}" ,
30- "} // namespace callstack::nodeapihost" ,
31- ] . join ( "\n" ) ;
13+ return `
14+ // This file is generated by react-native-node-api-modules
15+ #include <Logger.hpp>
16+ #include <dlfcn.h>
17+ #include <weak_node_api.hpp>
18+
19+ namespace callstack::nodeapihost {
20+
21+ using node_api::internal::inject_host_t;
22+ using node_api::internal::NodeApiHost;
23+
24+ void injectIntoWeakNodeApi() {
25+ void *module = dlopen("libweak-node-api.so", RTLD_NOW | RTLD_GLOBAL);
26+ if (NULL == module) {
27+ log_debug("NapiHost: Failed to load weak-node-api: %s", dlerror());
28+ abort();
29+ }
30+
31+ auto inject_host = (inject_host_t)dlsym(
32+ module, "_ZN8node_api8internal11inject_hostERKNS0_11NodeApiHostE");
33+ if (NULL == inject_host) {
34+ log_debug("NapiHost: Failed to find 'inject_host' function: %s", dlerror());
35+ abort();
36+ }
37+
38+ log_debug("Injecting WeakNodeApiHost");
39+ inject_host(NodeApiHost {
40+ ${ functions
41+ . filter ( ( { kind } ) => kind === "engine" )
42+ . flatMap ( ( { name } ) => `.${ name } = ${ name } ,` )
43+ . join ( "\n" ) }
44+ });
45+ }
46+ } // namespace callstack::nodeapihost
47+ ` ;
3248}
3349
3450async function run ( ) {
0 commit comments