Skip to content

Commit f934c22

Browse files
committed
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.
1 parent 76387c1 commit f934c22

File tree

3 files changed

+85
-42
lines changed

3 files changed

+85
-42
lines changed

node.gyp

Lines changed: 1 addition & 0 deletions
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

Lines changed: 70 additions & 0 deletions
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

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -756,69 +756,41 @@ v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
756756
/* Helper class users can optionally inherit from. If
757757
* `AsyncResource::MakeCallback()` is used, then all four callbacks will be
758758
* called automatically. */
759-
class AsyncResource {
759+
class NODE_EXTERN AsyncResource {
760760
public:
761761
AsyncResource(v8::Isolate* isolate,
762762
v8::Local<v8::Object> resource,
763763
const char* name,
764-
async_id trigger_async_id = -1)
765-
: isolate_(isolate),
766-
resource_(isolate, resource) {
767-
async_context_ = EmitAsyncInit(isolate, resource, name,
768-
trigger_async_id);
769-
}
764+
async_id trigger_async_id = -1);
770765

771-
virtual ~AsyncResource() {
772-
EmitAsyncDestroy(isolate_, async_context_);
773-
resource_.Reset();
774-
}
766+
virtual ~AsyncResource();
767+
768+
AsyncResource(const AsyncResource&) = delete;
769+
void operator=(const AsyncResource&) = delete;
775770

776771
v8::MaybeLocal<v8::Value> MakeCallback(
777772
v8::Local<v8::Function> callback,
778773
int argc,
779-
v8::Local<v8::Value>* argv) {
780-
return node::MakeCallback(isolate_, get_resource(),
781-
callback, argc, argv,
782-
async_context_);
783-
}
774+
v8::Local<v8::Value>* argv);
784775

785776
v8::MaybeLocal<v8::Value> MakeCallback(
786777
const char* method,
787778
int argc,
788-
v8::Local<v8::Value>* argv) {
789-
return node::MakeCallback(isolate_, get_resource(),
790-
method, argc, argv,
791-
async_context_);
792-
}
779+
v8::Local<v8::Value>* argv);
793780

794781
v8::MaybeLocal<v8::Value> MakeCallback(
795782
v8::Local<v8::String> symbol,
796783
int argc,
797-
v8::Local<v8::Value>* argv) {
798-
return node::MakeCallback(isolate_, get_resource(),
799-
symbol, argc, argv,
800-
async_context_);
801-
}
784+
v8::Local<v8::Value>* argv);
802785

803-
v8::Local<v8::Object> get_resource() {
804-
return resource_.Get(isolate_);
805-
}
806-
807-
async_id get_async_id() const {
808-
return async_context_.async_id;
809-
}
810-
811-
async_id get_trigger_async_id() const {
812-
return async_context_.trigger_async_id;
813-
}
786+
v8::Local<v8::Object> get_resource();
787+
async_id get_async_id() const;
788+
async_id get_trigger_async_id() const;
814789

815790
protected:
816-
class CallbackScope : public node::CallbackScope {
791+
class NODE_EXTERN CallbackScope : public node::CallbackScope {
817792
public:
818-
explicit CallbackScope(AsyncResource* res)
819-
: node::CallbackScope(res->isolate_,
820-
res->resource_.Get(res->isolate_),
821-
res->async_context_) {}
793+
explicit CallbackScope(AsyncResource* res);
822794
};
823795

824796
private:

0 commit comments

Comments
 (0)