forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_resource.cc
90 lines (75 loc) · 2.99 KB
/
async_resource.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "async_context_frame.h"
#include "env-inl.h"
#include "node.h"
namespace node {
using v8::Function;
using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::String;
using v8::Value;
AsyncResource::AsyncResource(Isolate* isolate,
Local<Object> resource,
const char* name,
async_id trigger_async_id)
: env_(Environment::GetCurrent(isolate)),
resource_(isolate, resource) {
CHECK_NOT_NULL(env_);
env_->SetAsyncResourceContextFrame(
reinterpret_cast<std::uintptr_t>(this),
{isolate, async_context_frame::current(isolate)});
async_context_ = EmitAsyncInit(isolate, resource, name, trigger_async_id);
}
AsyncResource::~AsyncResource() {
CHECK_NOT_NULL(env_);
EmitAsyncDestroy(env_, async_context_);
env_->RemoveAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this));
}
MaybeLocal<Value> AsyncResource::MakeCallback(Local<Function> callback,
int argc,
Local<Value>* argv) {
auto isolate = env_->isolate();
auto context_frame =
env_->GetAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this))
.Get(isolate);
async_context_frame::Scope async_context_frame_scope(isolate, context_frame);
return node::MakeCallback(
isolate, get_resource(), callback, argc, argv, async_context_);
}
MaybeLocal<Value> AsyncResource::MakeCallback(const char* method,
int argc,
Local<Value>* argv) {
auto isolate = env_->isolate();
auto context_frame =
env_->GetAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this))
.Get(isolate);
async_context_frame::Scope async_context_frame_scope(isolate, context_frame);
return node::MakeCallback(
isolate, get_resource(), method, argc, argv, async_context_);
}
MaybeLocal<Value> AsyncResource::MakeCallback(Local<String> symbol,
int argc,
Local<Value>* argv) {
auto isolate = env_->isolate();
auto context_frame =
env_->GetAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this))
.Get(isolate);
async_context_frame::Scope async_context_frame_scope(isolate, context_frame);
return node::MakeCallback(
isolate, get_resource(), symbol, argc, argv, async_context_);
}
Local<Object> AsyncResource::get_resource() {
return resource_.Get(env_->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->env_,
res->resource_.Get(res->env_->isolate()),
res->async_context_) {}
} // namespace node