@@ -76,46 +76,56 @@ type FunctionDecl = {
76
76
returnType : string ;
77
77
argumentTypes : string [ ] ;
78
78
libraryPath : string ;
79
+ fallbackReturnStatement : string ;
79
80
} ;
80
81
81
- export function generateNodeApiFunction ( {
82
+ export function generateNodeApiFunctionStubBody ( {
82
83
name,
83
84
returnType,
84
85
argumentTypes,
85
86
libraryPath,
87
+ fallbackReturnStatement,
86
88
} : FunctionDecl ) {
87
- const stubbedReturnStatement =
88
- returnType === "void "
89
+ const returnStatement =
90
+ name === "napi_fatal_error "
89
91
? "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`;
91
97
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
98
98
static ${ name } _t real_func = NULL;
99
99
100
100
if (!real_func) {
101
101
void* handle = dlopen("${ libraryPath } ", RTLD_LAZY | RTLD_GLOBAL);
102
102
if (!handle) {
103
103
fprintf(stderr, "Failed to load ${ libraryPath } : %s\\n", dlerror());
104
- ${ stubbedReturnStatement }
104
+ ${ fallbackReturnStatement }
105
105
}
106
106
107
107
real_func = (${ name } _t)dlsym(handle, "${ name } ");
108
108
if (!real_func) {
109
109
fprintf(stderr, "Failed to find symbol: %s\\n", dlerror());
110
- ${ stubbedReturnStatement }
110
+ ${ fallbackReturnStatement }
111
111
}
112
112
}
113
113
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 ) }
117
127
#else
118
- ${ stubbedReturnStatement }
128
+ ${ fallbackReturnStatement }
119
129
#endif
120
130
}` ;
121
131
}
@@ -179,6 +189,10 @@ export function generateFakeNodeApiSource(version: NodeApiVersion) {
179
189
libraryPath : engineSymbols . has ( name )
180
190
? "libhermes.so"
181
191
: "libnode-api-host.so" ,
192
+ fallbackReturnStatement :
193
+ returnType === "void"
194
+ ? "abort();"
195
+ : "return napi_status::napi_generic_failure;" ,
182
196
} )
183
197
) ;
184
198
}
@@ -201,21 +215,6 @@ async function run() {
201
215
sourceCode ,
202
216
"utf-8"
203
217
) ;
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
- ) ;
219
218
}
220
219
221
220
run ( ) . catch ( ( err ) => {
0 commit comments