Skip to content

Commit e54d11e

Browse files
joyeecheungdanbev
authored andcommitted
src: move InternalMakeCallback and MakeCallback
This commit moves InternalMakeCallback and MakeCallback into callback_scope.cc. Since these are just wrappers on top of `InternalCallbackScope`, this makes the implementations easier to find. #25299 PR-URL: #25299 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b406c9c commit e54d11e

File tree

2 files changed

+131
-133
lines changed

2 files changed

+131
-133
lines changed

src/callback_scope.cc

+131
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

-133
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;
@@ -556,138 +555,6 @@ void RemoveEnvironmentCleanupHook(Isolate* isolate,
556555
env->RemoveCleanupHook(fun, arg);
557556
}
558557

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

0 commit comments

Comments
 (0)