Skip to content

Commit e301ca8

Browse files
committed
src: move InternalMakeCallback and MakeCallback into callback_scope.cc
Since these are just wrappers on top of `InternalCallbackScope`, this makes the implementations easier to find.
1 parent 9987f1a commit e301ca8

File tree

2 files changed

+131
-133
lines changed

2 files changed

+131
-133
lines changed

src/callback_scope.cc

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55

66
namespace node {
77

8+
using v8::Context;
9+
using v8::EscapableHandleScope;
810
using v8::Function;
911
using v8::HandleScope;
1012
using v8::Isolate;
1113
using v8::Local;
14+
using v8::MaybeLocal;
15+
using v8::NewStringType;
1216
using v8::Object;
17+
using v8::String;
18+
using v8::Value;
1319

1420
using AsyncHooks = Environment::AsyncHooks;
1521

@@ -126,4 +132,129 @@ void InternalCallbackScope::Close() {
126132
}
127133
}
128134

135+
MaybeLocal<Value> InternalMakeCallback(Environment* env,
136+
Local<Object> recv,
137+
const Local<Function> callback,
138+
int argc,
139+
Local<Value> argv[],
140+
async_context asyncContext) {
141+
CHECK(!recv.IsEmpty());
142+
InternalCallbackScope scope(env, recv, asyncContext);
143+
if (scope.Failed()) {
144+
return MaybeLocal<Value>();
145+
}
146+
147+
Local<Function> domain_cb = env->domain_callback();
148+
MaybeLocal<Value> ret;
149+
if (asyncContext.async_id != 0 || domain_cb.IsEmpty() || recv.IsEmpty()) {
150+
ret = callback->Call(env->context(), recv, argc, argv);
151+
} else {
152+
std::vector<Local<Value>> args(1 + argc);
153+
args[0] = callback;
154+
std::copy(&argv[0], &argv[argc], args.begin() + 1);
155+
ret = domain_cb->Call(env->context(), recv, args.size(), &args[0]);
156+
}
157+
158+
if (ret.IsEmpty()) {
159+
scope.MarkAsFailed();
160+
return MaybeLocal<Value>();
161+
}
162+
163+
scope.Close();
164+
if (scope.Failed()) {
165+
return MaybeLocal<Value>();
166+
}
167+
168+
return ret;
169+
}
170+
171+
// Public MakeCallback()s
172+
173+
MaybeLocal<Value> MakeCallback(Isolate* isolate,
174+
Local<Object> recv,
175+
const char* method,
176+
int argc,
177+
Local<Value> argv[],
178+
async_context asyncContext) {
179+
Local<String> method_string =
180+
String::NewFromUtf8(isolate, method, NewStringType::kNormal)
181+
.ToLocalChecked();
182+
return MakeCallback(isolate, recv, method_string, argc, argv, asyncContext);
183+
}
184+
185+
MaybeLocal<Value> MakeCallback(Isolate* isolate,
186+
Local<Object> recv,
187+
Local<String> symbol,
188+
int argc,
189+
Local<Value> argv[],
190+
async_context asyncContext) {
191+
Local<Value> callback_v =
192+
recv->Get(isolate->GetCurrentContext(), symbol).ToLocalChecked();
193+
if (callback_v.IsEmpty()) return Local<Value>();
194+
if (!callback_v->IsFunction()) return Local<Value>();
195+
Local<Function> callback = callback_v.As<Function>();
196+
return MakeCallback(isolate, recv, callback, argc, argv, asyncContext);
197+
}
198+
199+
MaybeLocal<Value> MakeCallback(Isolate* isolate,
200+
Local<Object> recv,
201+
Local<Function> callback,
202+
int argc,
203+
Local<Value> argv[],
204+
async_context asyncContext) {
205+
// Observe the following two subtleties:
206+
//
207+
// 1. The environment is retrieved from the callback function's context.
208+
// 2. The context to enter is retrieved from the environment.
209+
//
210+
// Because of the AssignToContext() call in src/node_contextify.cc,
211+
// the two contexts need not be the same.
212+
Environment* env = Environment::GetCurrent(callback->CreationContext());
213+
CHECK_NOT_NULL(env);
214+
Context::Scope context_scope(env->context());
215+
MaybeLocal<Value> ret =
216+
InternalMakeCallback(env, recv, callback, argc, argv, asyncContext);
217+
if (ret.IsEmpty() && env->makecallback_depth() == 0) {
218+
// This is only for legacy compatiblity and we may want to look into
219+
// removing/adjusting it.
220+
return Undefined(env->isolate());
221+
}
222+
return ret;
223+
}
224+
225+
// Legacy MakeCallback()s
226+
227+
Local<Value> MakeCallback(Isolate* isolate,
228+
Local<Object> recv,
229+
const char* method,
230+
int argc,
231+
Local<Value>* argv) {
232+
EscapableHandleScope handle_scope(isolate);
233+
return handle_scope.Escape(
234+
MakeCallback(isolate, recv, method, argc, argv, {0, 0})
235+
.FromMaybe(Local<Value>()));
236+
}
237+
238+
Local<Value> MakeCallback(Isolate* isolate,
239+
Local<Object> recv,
240+
Local<String> symbol,
241+
int argc,
242+
Local<Value>* argv) {
243+
EscapableHandleScope handle_scope(isolate);
244+
return handle_scope.Escape(
245+
MakeCallback(isolate, recv, symbol, argc, argv, {0, 0})
246+
.FromMaybe(Local<Value>()));
247+
}
248+
249+
Local<Value> MakeCallback(Isolate* isolate,
250+
Local<Object> recv,
251+
Local<Function> callback,
252+
int argc,
253+
Local<Value>* argv) {
254+
EscapableHandleScope handle_scope(isolate);
255+
return handle_scope.Escape(
256+
MakeCallback(isolate, recv, callback, argc, argv, {0, 0})
257+
.FromMaybe(Local<Value>()));
258+
}
259+
129260
} // namespace node

src/node.cc

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ using v8::Array;
109109
using v8::Boolean;
110110
using v8::Context;
111111
using v8::DEFAULT;
112-
using v8::EscapableHandleScope;
113112
using v8::Exception;
114113
using v8::Function;
115114
using v8::FunctionCallbackInfo;
@@ -553,138 +552,6 @@ void RemoveEnvironmentCleanupHook(Isolate* isolate,
553552
env->RemoveCleanupHook(fun, arg);
554553
}
555554

556-
MaybeLocal<Value> InternalMakeCallback(Environment* env,
557-
Local<Object> recv,
558-
const Local<Function> callback,
559-
int argc,
560-
Local<Value> argv[],
561-
async_context asyncContext) {
562-
CHECK(!recv.IsEmpty());
563-
InternalCallbackScope scope(env, recv, asyncContext);
564-
if (scope.Failed()) {
565-
return MaybeLocal<Value>();
566-
}
567-
568-
Local<Function> domain_cb = env->domain_callback();
569-
MaybeLocal<Value> ret;
570-
if (asyncContext.async_id != 0 || domain_cb.IsEmpty() || recv.IsEmpty()) {
571-
ret = callback->Call(env->context(), recv, argc, argv);
572-
} else {
573-
std::vector<Local<Value>> args(1 + argc);
574-
args[0] = callback;
575-
std::copy(&argv[0], &argv[argc], args.begin() + 1);
576-
ret = domain_cb->Call(env->context(), recv, args.size(), &args[0]);
577-
}
578-
579-
if (ret.IsEmpty()) {
580-
scope.MarkAsFailed();
581-
return MaybeLocal<Value>();
582-
}
583-
584-
scope.Close();
585-
if (scope.Failed()) {
586-
return MaybeLocal<Value>();
587-
}
588-
589-
return ret;
590-
}
591-
592-
593-
// Public MakeCallback()s
594-
595-
596-
MaybeLocal<Value> MakeCallback(Isolate* isolate,
597-
Local<Object> recv,
598-
const char* method,
599-
int argc,
600-
Local<Value> argv[],
601-
async_context asyncContext) {
602-
Local<String> method_string =
603-
String::NewFromUtf8(isolate, method, NewStringType::kNormal)
604-
.ToLocalChecked();
605-
return MakeCallback(isolate, recv, method_string, argc, argv, asyncContext);
606-
}
607-
608-
609-
MaybeLocal<Value> MakeCallback(Isolate* isolate,
610-
Local<Object> recv,
611-
Local<String> symbol,
612-
int argc,
613-
Local<Value> argv[],
614-
async_context asyncContext) {
615-
Local<Value> callback_v = recv->Get(isolate->GetCurrentContext(),
616-
symbol).ToLocalChecked();
617-
if (callback_v.IsEmpty()) return Local<Value>();
618-
if (!callback_v->IsFunction()) return Local<Value>();
619-
Local<Function> callback = callback_v.As<Function>();
620-
return MakeCallback(isolate, recv, callback, argc, argv, asyncContext);
621-
}
622-
623-
624-
MaybeLocal<Value> MakeCallback(Isolate* isolate,
625-
Local<Object> recv,
626-
Local<Function> callback,
627-
int argc,
628-
Local<Value> argv[],
629-
async_context asyncContext) {
630-
// Observe the following two subtleties:
631-
//
632-
// 1. The environment is retrieved from the callback function's context.
633-
// 2. The context to enter is retrieved from the environment.
634-
//
635-
// Because of the AssignToContext() call in src/node_contextify.cc,
636-
// the two contexts need not be the same.
637-
Environment* env = Environment::GetCurrent(callback->CreationContext());
638-
CHECK_NOT_NULL(env);
639-
Context::Scope context_scope(env->context());
640-
MaybeLocal<Value> ret = InternalMakeCallback(env, recv, callback,
641-
argc, argv, asyncContext);
642-
if (ret.IsEmpty() && env->makecallback_depth() == 0) {
643-
// This is only for legacy compatiblity and we may want to look into
644-
// removing/adjusting it.
645-
return Undefined(env->isolate());
646-
}
647-
return ret;
648-
}
649-
650-
651-
// Legacy MakeCallback()s
652-
653-
Local<Value> MakeCallback(Isolate* isolate,
654-
Local<Object> recv,
655-
const char* method,
656-
int argc,
657-
Local<Value>* argv) {
658-
EscapableHandleScope handle_scope(isolate);
659-
return handle_scope.Escape(
660-
MakeCallback(isolate, recv, method, argc, argv, {0, 0})
661-
.FromMaybe(Local<Value>()));
662-
}
663-
664-
665-
Local<Value> MakeCallback(Isolate* isolate,
666-
Local<Object> recv,
667-
Local<String> symbol,
668-
int argc,
669-
Local<Value>* argv) {
670-
EscapableHandleScope handle_scope(isolate);
671-
return handle_scope.Escape(
672-
MakeCallback(isolate, recv, symbol, argc, argv, {0, 0})
673-
.FromMaybe(Local<Value>()));
674-
}
675-
676-
677-
Local<Value> MakeCallback(Isolate* isolate,
678-
Local<Object> recv,
679-
Local<Function> callback,
680-
int argc,
681-
Local<Value>* argv) {
682-
EscapableHandleScope handle_scope(isolate);
683-
return handle_scope.Escape(
684-
MakeCallback(isolate, recv, callback, argc, argv, {0, 0})
685-
.FromMaybe(Local<Value>()));
686-
}
687-
688555
static void WaitForInspectorDisconnect(Environment* env) {
689556
#if HAVE_INSPECTOR
690557
if (env->inspector_agent()->IsActive()) {

0 commit comments

Comments
 (0)