@@ -76,46 +76,56 @@ type FunctionDecl = {
7676 returnType : string ;
7777 argumentTypes : string [ ] ;
7878 libraryPath : string ;
79+ fallbackReturnStatement : string ;
7980} ;
8081
81- export function generateNodeApiFunction ( {
82+ export function generateNodeApiFunctionStubBody ( {
8283 name,
8384 returnType,
8485 argumentTypes,
8586 libraryPath,
87+ fallbackReturnStatement,
8688} : FunctionDecl ) {
87- const stubbedReturnStatement =
88- returnType === "void "
89+ const returnStatement =
90+ name === "napi_fatal_error "
8991 ? "abort();"
90- : "return napi_status::napi_generic_failure;" ;
92+ : returnType === "void"
93+ ? ""
94+ : `return real_func(${ argumentTypes
95+ . map ( ( t , index ) => `arg${ index } ` )
96+ . join ( ", " ) } ); // Call the real function`;
9197 return `
92- typedef ${ returnType } (*${ name } _t)(${ argumentTypes . join ( ", " ) } );
93-
94- ${ returnType } ${ name } (${ argumentTypes
95- . map ( ( type , index ) => `${ type } arg${ index } ` )
96- . join ( ", " ) } ) {
97- #ifdef NODE_API_REEXPORT
9898 static ${ name } _t real_func = NULL;
9999
100100 if (!real_func) {
101101 void* handle = dlopen("${ libraryPath } ", RTLD_LAZY | RTLD_GLOBAL);
102102 if (!handle) {
103103 fprintf(stderr, "Failed to load ${ libraryPath } : %s\\n", dlerror());
104- ${ stubbedReturnStatement }
104+ ${ fallbackReturnStatement }
105105 }
106106
107107 real_func = (${ name } _t)dlsym(handle, "${ name } ");
108108 if (!real_func) {
109109 fprintf(stderr, "Failed to find symbol: %s\\n", dlerror());
110- ${ stubbedReturnStatement }
110+ ${ fallbackReturnStatement }
111111 }
112112 }
113113
114- ${ returnType === "void" ? "" : "return " } real_func(${ argumentTypes
115- . map ( ( t , index ) => `arg${ index } ` )
116- . join ( ", " ) } ); // Call the real function
114+ ${ returnStatement }
115+ ` ;
116+ }
117+
118+ export function generateNodeApiFunction ( decl : FunctionDecl ) {
119+ const { name, returnType, argumentTypes, fallbackReturnStatement } = decl ;
120+ return `
121+ typedef ${ returnType } (*${ name } _t)(${ argumentTypes . join ( ", " ) } );
122+ ${ returnType } ${ name } (${ argumentTypes
123+ . map ( ( type , index ) => `${ type } arg${ index } ` )
124+ . join ( ", " ) } ) {
125+ #ifdef NODE_API_REEXPORT
126+ ${ generateNodeApiFunctionStubBody ( decl ) }
117127 #else
118- ${ stubbedReturnStatement }
128+ ${ fallbackReturnStatement }
119129 #endif
120130}` ;
121131}
@@ -179,6 +189,10 @@ export function generateFakeNodeApiSource(version: NodeApiVersion) {
179189 libraryPath : engineSymbols . has ( name )
180190 ? "libhermes.so"
181191 : "libnode-api-host.so" ,
192+ fallbackReturnStatement :
193+ returnType === "void"
194+ ? "abort();"
195+ : "return napi_status::napi_generic_failure;" ,
182196 } )
183197 ) ;
184198 }
@@ -201,21 +215,6 @@ async function run() {
201215 sourceCode ,
202216 "utf-8"
203217 ) ;
204- // Build for all supported platforms
205- cp . execFileSync (
206- "react-native-node-api-cmake" ,
207- [
208- "--android" ,
209- "--apple" ,
210- "--no-auto-link" ,
211- "--no-weak-node-api-linkage" ,
212- // TODO: Add support for passing variables through to CMake
213- // "-D NODE_API_REEXPORT=1",
214- "--source" ,
215- WEAK_NODE_API_PATH ,
216- ] ,
217- { stdio : "inherit" }
218- ) ;
219218}
220219
221220run ( ) . catch ( ( err ) => {
0 commit comments