Skip to content

Commit eb2dccb

Browse files
bnoordhuisBridgeAR
authored andcommitted
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. PR-URL: #26348 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent e575ba6 commit eb2dccb

File tree

3 files changed

+85
-42
lines changed

3 files changed

+85
-42
lines changed

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@
405405
'dependencies': [ 'deps/histogram/histogram.gyp:histogram' ],
406406

407407
'sources': [
408+
'src/api/async_resource.cc',
408409
'src/api/callback.cc',
409410
'src/api/encoding.cc',
410411
'src/api/environment.cc',

src/api/async_resource.cc

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "node.h"
2+
3+
namespace node {
4+
5+
using v8::Function;
6+
using v8::HandleScope;
7+
using v8::Isolate;
8+
using v8::Local;
9+
using v8::MaybeLocal;
10+
using v8::Object;
11+
using v8::String;
12+
using v8::Value;
13+
14+
AsyncResource::AsyncResource(Isolate* isolate,
15+
Local<Object> resource,
16+
const char* name,
17+
async_id trigger_async_id)
18+
: isolate_(isolate),
19+
resource_(isolate, resource) {
20+
async_context_ = EmitAsyncInit(isolate, resource, name,
21+
trigger_async_id);
22+
}
23+
24+
AsyncResource::~AsyncResource() {
25+
EmitAsyncDestroy(isolate_, async_context_);
26+
resource_.Reset();
27+
}
28+
29+
MaybeLocal<Value> AsyncResource::MakeCallback(Local<Function> callback,
30+
int argc,
31+
Local<Value>* argv) {
32+
return node::MakeCallback(isolate_, get_resource(),
33+
callback, argc, argv,
34+
async_context_);
35+
}
36+
37+
MaybeLocal<Value> AsyncResource::MakeCallback(const char* method,
38+
int argc,
39+
Local<Value>* argv) {
40+
return node::MakeCallback(isolate_, get_resource(),
41+
method, argc, argv,
42+
async_context_);
43+
}
44+
45+
MaybeLocal<Value> AsyncResource::MakeCallback(Local<String> symbol,
46+
int argc,
47+
Local<Value>* argv) {
48+
return node::MakeCallback(isolate_, get_resource(),
49+
symbol, argc, argv,
50+
async_context_);
51+
}
52+
53+
Local<Object> AsyncResource::get_resource() {
54+
return resource_.Get(isolate_);
55+
}
56+
57+
async_id AsyncResource::get_async_id() const {
58+
return async_context_.async_id;
59+
}
60+
61+
async_id AsyncResource::get_trigger_async_id() const {
62+
return async_context_.trigger_async_id;
63+
}
64+
65+
AsyncResource::CallbackScope::CallbackScope(AsyncResource* res)
66+
: node::CallbackScope(res->isolate_,
67+
res->resource_.Get(res->isolate_),
68+
res->async_context_) {}
69+
70+
} // namespace node

src/node.h

+14-42
Original file line numberDiff line numberDiff line change
@@ -758,69 +758,41 @@ v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
758758
/* Helper class users can optionally inherit from. If
759759
* `AsyncResource::MakeCallback()` is used, then all four callbacks will be
760760
* called automatically. */
761-
class AsyncResource {
761+
class NODE_EXTERN AsyncResource {
762762
public:
763763
AsyncResource(v8::Isolate* isolate,
764764
v8::Local<v8::Object> resource,
765765
const char* name,
766-
async_id trigger_async_id = -1)
767-
: isolate_(isolate),
768-
resource_(isolate, resource) {
769-
async_context_ = EmitAsyncInit(isolate, resource, name,
770-
trigger_async_id);
771-
}
766+
async_id trigger_async_id = -1);
772767

773-
virtual ~AsyncResource() {
774-
EmitAsyncDestroy(isolate_, async_context_);
775-
resource_.Reset();
776-
}
768+
virtual ~AsyncResource();
769+
770+
AsyncResource(const AsyncResource&) = delete;
771+
void operator=(const AsyncResource&) = delete;
777772

778773
v8::MaybeLocal<v8::Value> MakeCallback(
779774
v8::Local<v8::Function> callback,
780775
int argc,
781-
v8::Local<v8::Value>* argv) {
782-
return node::MakeCallback(isolate_, get_resource(),
783-
callback, argc, argv,
784-
async_context_);
785-
}
776+
v8::Local<v8::Value>* argv);
786777

787778
v8::MaybeLocal<v8::Value> MakeCallback(
788779
const char* method,
789780
int argc,
790-
v8::Local<v8::Value>* argv) {
791-
return node::MakeCallback(isolate_, get_resource(),
792-
method, argc, argv,
793-
async_context_);
794-
}
781+
v8::Local<v8::Value>* argv);
795782

796783
v8::MaybeLocal<v8::Value> MakeCallback(
797784
v8::Local<v8::String> symbol,
798785
int argc,
799-
v8::Local<v8::Value>* argv) {
800-
return node::MakeCallback(isolate_, get_resource(),
801-
symbol, argc, argv,
802-
async_context_);
803-
}
786+
v8::Local<v8::Value>* argv);
804787

805-
v8::Local<v8::Object> get_resource() {
806-
return resource_.Get(isolate_);
807-
}
808-
809-
async_id get_async_id() const {
810-
return async_context_.async_id;
811-
}
812-
813-
async_id get_trigger_async_id() const {
814-
return async_context_.trigger_async_id;
815-
}
788+
v8::Local<v8::Object> get_resource();
789+
async_id get_async_id() const;
790+
async_id get_trigger_async_id() const;
816791

817792
protected:
818-
class CallbackScope : public node::CallbackScope {
793+
class NODE_EXTERN CallbackScope : public node::CallbackScope {
819794
public:
820-
explicit CallbackScope(AsyncResource* res)
821-
: node::CallbackScope(res->isolate_,
822-
res->resource_.Get(res->isolate_),
823-
res->async_context_) {}
795+
explicit CallbackScope(AsyncResource* res);
824796
};
825797

826798
private:

0 commit comments

Comments
 (0)