Skip to content

Commit dca6741

Browse files
joyeecheungBridgeAR
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 <me@gus.host> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 81924ff commit dca6741

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;
@@ -560,138 +559,6 @@ void RemoveEnvironmentCleanupHook(Isolate* isolate,
560559
env->RemoveCleanupHook(fun, arg);
561560
}
562561

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

0 commit comments

Comments
 (0)