@@ -11,22 +11,26 @@ export function generateHeader() {
1111
1212 #include "NodeApiHost.hpp"
1313
14+ typedef NodeApiHost* (*NodeApiHostGetter)(node_api_basic_env env);
15+ extern "C" void inject_weak_node_api_host_getter(NodeApiHostGetter host_getter);
16+
1417 typedef void(*InjectHostFunction)(const NodeApiHost&);
1518 extern "C" void inject_weak_node_api_host(const NodeApiHost& host);
1619 ` ;
1720}
1821
1922function generateFunctionImpl ( fn : FunctionDecl ) {
20- const { name, returnType, argumentTypes } = fn ;
23+ const { name, returnType, argumentTypes, needEnv } = fn ;
2124 return generateFunction ( {
2225 ...fn ,
2326 extern : true ,
2427 body : `
25- if (g_host.${ name } == nullptr) {
28+ NodeApiHost* host = g_host_getter(${ needEnv ? "arg0" : "nullptr" } );
29+ if (host->${ name } == nullptr) {
2630 fprintf(stderr, "Node-API function '${ name } ' called before it was injected!\\n");
2731 abort();
2832 }
29- ${ returnType === "void" ? "" : "return " } g_host. ${ name } (
33+ ${ returnType === "void" ? "" : "return " } host-> ${ name } (
3034 ${ argumentTypes . map ( ( _ , index ) => `arg${ index } ` ) . join ( ", " ) }
3135 );
3236 ` ,
@@ -44,6 +48,11 @@ export function generateSource(functions: FunctionDecl[]) {
4448 * It is set via inject_weak_node_api_host() before any Node-API function is dispatched.
4549 * All Node-API calls are routed through this host.
4650 */
51+
52+ NodeApiHostGetter g_host_getter;
53+
54+ void inject_weak_node_api_host_getter(NodeApiHostGetter host_getter) { g_host_getter = host_getter; };
55+
4756 NodeApiHost g_host;
4857 void inject_weak_node_api_host(const NodeApiHost& host) {
4958 g_host = host;
0 commit comments