diff --git a/src/binding.cpp b/src/binding.cpp index 847e92a..1bd5716 100644 --- a/src/binding.cpp +++ b/src/binding.cpp @@ -76,6 +76,10 @@ struct CallbackScopeFor { struct JsServer { Isolate* isolate; Global context; +#if NODE_MAJOR_VERSION >= 27 + // Taken once in serve(), so node::MakeCallback does not look it up on every call. + node::Environment* env = nullptr; +#endif Global onRequest; Global onRequestBatch; // Depth of cbOnWsRead: while > 0 the context and Node callback scope of @@ -707,6 +711,19 @@ static void printThrown(Isolate* iso, Local value, const char* where) { // delivered from inside upgradeToWebSocket(), an onAborted delivered from // inside close()) drain nothing themselves; the outermost one does. Cost per // call: an async-context push/pop and, with an empty queue, one cheap check. +// node::MakeCallback with the server's resource object, and its Environment +// where Node takes one (nodejs/node#66510). +static inline MaybeLocal makeCallback(JsServer* js, Isolate* iso, Local f, + int argc, Local* argv) { +#if NODE_MAJOR_VERSION >= 27 + return node::MakeCallback(js->env, js->asyncResource.Get(iso), f, argc, argv, + node::async_context{0, 0}); +#else + return node::MakeCallback(iso, js->asyncResource.Get(iso), f, argc, argv, + node::async_context{0, 0}); +#endif +} + inline CallbackScopeFor::CallbackScopeFor(JsServer* js, Isolate* iso) : scope(iso, js->asyncResource.IsEmpty() ? Object::New(iso) : js->asyncResource.Get(iso), @@ -819,8 +836,7 @@ static void invokeJs(JsServer* js, Global& fn, uint32_t reqId, argv[2] = pathStringFor(js, iso, path); argc = 3; } - (void)node::MakeCallback(iso, js->asyncResource.Get(iso), fn.Get(iso), argc, argv, - node::async_context{0, 0}); + (void)makeCallback(js, iso, fn.Get(iso), argc, argv); } static void cbOnRequest(void* user, Connection* c) { @@ -956,8 +972,7 @@ static void cbOnRequestBatch(void* user, Connection* c, uint32_t count) { ThreadState& t = threadState(); JsServer* const outer = t.batchJs; t.batchJs = js; - (void)node::MakeCallback(iso, js->asyncResource.Get(iso), js->onRequestBatch.Get(iso), 1, argv, - node::async_context{0, 0}); + (void)makeCallback(js, iso, js->onRequestBatch.Get(iso), 1, argv); t.batchJs = outer; js->server->batchLoopReturned(); } @@ -1292,6 +1307,9 @@ static void Serve(const FunctionCallbackInfo& args) { JsServer* js = new JsServer(); js->isolate = iso; js->context.Reset(iso, ctx); +#if NODE_MAJOR_VERSION >= 27 + js->env = node::GetCurrentEnvironment(ctx); +#endif js->asyncResource.Reset(iso, Object::New(iso)); js->viaTrampoline = args.Length() >= 3 && args[2]->IsTrue(); {