From f934c228fe483ef5ffaa0bc8bc4f9287bf62932c Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 22 Mar 2019 10:23:51 +0100 Subject: [PATCH] src: move AsyncResource impl out of public header Implementing the methods out-of-line (i.e., not inline) means we can fix bugs and have already compiled add-ons pick up the fixes automatically, something that doesn't work when the methods are inline because then they get compiled into the add-on instead of the node binary. --- node.gyp | 1 + src/api/async_resource.cc | 70 +++++++++++++++++++++++++++++++++++++++ src/node.h | 56 ++++++++----------------------- 3 files changed, 85 insertions(+), 42 deletions(-) create mode 100644 src/api/async_resource.cc diff --git a/node.gyp b/node.gyp index 81be566ddf8e28..87c30fcd4119b5 100644 --- a/node.gyp +++ b/node.gyp @@ -405,6 +405,7 @@ 'dependencies': [ 'deps/histogram/histogram.gyp:histogram' ], 'sources': [ + 'src/api/async_resource.cc', 'src/api/callback.cc', 'src/api/encoding.cc', 'src/api/environment.cc', diff --git a/src/api/async_resource.cc b/src/api/async_resource.cc new file mode 100644 index 00000000000000..1d8224114e9511 --- /dev/null +++ b/src/api/async_resource.cc @@ -0,0 +1,70 @@ +#include "node.h" + +namespace node { + +using v8::Function; +using v8::HandleScope; +using v8::Isolate; +using v8::Local; +using v8::MaybeLocal; +using v8::Object; +using v8::String; +using v8::Value; + +AsyncResource::AsyncResource(Isolate* isolate, + Local resource, + const char* name, + async_id trigger_async_id) + : isolate_(isolate), + resource_(isolate, resource) { + async_context_ = EmitAsyncInit(isolate, resource, name, + trigger_async_id); +} + +AsyncResource::~AsyncResource() { + EmitAsyncDestroy(isolate_, async_context_); + resource_.Reset(); +} + +MaybeLocal AsyncResource::MakeCallback(Local callback, + int argc, + Local* argv) { + return node::MakeCallback(isolate_, get_resource(), + callback, argc, argv, + async_context_); +} + +MaybeLocal AsyncResource::MakeCallback(const char* method, + int argc, + Local* argv) { + return node::MakeCallback(isolate_, get_resource(), + method, argc, argv, + async_context_); +} + +MaybeLocal AsyncResource::MakeCallback(Local symbol, + int argc, + Local* argv) { + return node::MakeCallback(isolate_, get_resource(), + symbol, argc, argv, + async_context_); +} + +Local AsyncResource::get_resource() { + return resource_.Get(isolate_); +} + +async_id AsyncResource::get_async_id() const { + return async_context_.async_id; +} + +async_id AsyncResource::get_trigger_async_id() const { + return async_context_.trigger_async_id; +} + +AsyncResource::CallbackScope::CallbackScope(AsyncResource* res) + : node::CallbackScope(res->isolate_, + res->resource_.Get(res->isolate_), + res->async_context_) {} + +} // namespace node diff --git a/src/node.h b/src/node.h index 32fd4b288e75f3..fe985699ae99e4 100644 --- a/src/node.h +++ b/src/node.h @@ -756,69 +756,41 @@ v8::MaybeLocal MakeCallback(v8::Isolate* isolate, /* Helper class users can optionally inherit from. If * `AsyncResource::MakeCallback()` is used, then all four callbacks will be * called automatically. */ -class AsyncResource { +class NODE_EXTERN AsyncResource { public: AsyncResource(v8::Isolate* isolate, v8::Local resource, const char* name, - async_id trigger_async_id = -1) - : isolate_(isolate), - resource_(isolate, resource) { - async_context_ = EmitAsyncInit(isolate, resource, name, - trigger_async_id); - } + async_id trigger_async_id = -1); - virtual ~AsyncResource() { - EmitAsyncDestroy(isolate_, async_context_); - resource_.Reset(); - } + virtual ~AsyncResource(); + + AsyncResource(const AsyncResource&) = delete; + void operator=(const AsyncResource&) = delete; v8::MaybeLocal MakeCallback( v8::Local callback, int argc, - v8::Local* argv) { - return node::MakeCallback(isolate_, get_resource(), - callback, argc, argv, - async_context_); - } + v8::Local* argv); v8::MaybeLocal MakeCallback( const char* method, int argc, - v8::Local* argv) { - return node::MakeCallback(isolate_, get_resource(), - method, argc, argv, - async_context_); - } + v8::Local* argv); v8::MaybeLocal MakeCallback( v8::Local symbol, int argc, - v8::Local* argv) { - return node::MakeCallback(isolate_, get_resource(), - symbol, argc, argv, - async_context_); - } + v8::Local* argv); - v8::Local get_resource() { - return resource_.Get(isolate_); - } - - async_id get_async_id() const { - return async_context_.async_id; - } - - async_id get_trigger_async_id() const { - return async_context_.trigger_async_id; - } + v8::Local get_resource(); + async_id get_async_id() const; + async_id get_trigger_async_id() const; protected: - class CallbackScope : public node::CallbackScope { + class NODE_EXTERN CallbackScope : public node::CallbackScope { public: - explicit CallbackScope(AsyncResource* res) - : node::CallbackScope(res->isolate_, - res->resource_.Get(res->isolate_), - res->async_context_) {} + explicit CallbackScope(AsyncResource* res); }; private: