Skip to content

Commit 7dde560

Browse files
gahaasdanbev
authored andcommitted
src: replace deprecated uses of FunctionTemplate::GetFunction
PR-URL: nodejs#22993 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ujjwal Sharma <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Eugene Ostroukhov <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent a0c1326 commit 7dde560

36 files changed

+134
-71
lines changed

doc/api/addons.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ functions and returning those back to JavaScript:
635635

636636
namespace demo {
637637

638+
using v8::Context;
638639
using v8::Function;
639640
using v8::FunctionCallbackInfo;
640641
using v8::FunctionTemplate;
@@ -652,8 +653,9 @@ void MyFunction(const FunctionCallbackInfo<Value>& args) {
652653
void CreateFunction(const FunctionCallbackInfo<Value>& args) {
653654
Isolate* isolate = args.GetIsolate();
654655

656+
Local<Context> context = isolate->GetCurrentContext();
655657
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, MyFunction);
656-
Local<Function> fn = tpl->GetFunction();
658+
Local<Function> fn = tpl->GetFunction(context).ToLocalChecked();
657659

658660
// omit this to make it anonymous
659661
fn->SetName(String::NewFromUtf8(isolate, "theFunction"));
@@ -777,9 +779,10 @@ void MyObject::Init(Local<Object> exports) {
777779
// Prototype
778780
NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
779781

780-
constructor.Reset(isolate, tpl->GetFunction());
782+
Local<Context> context = isolate->GetCurrentContext();
783+
constructor.Reset(isolate, tpl->GetFunction(context).ToLocalChecked());
781784
exports->Set(String::NewFromUtf8(isolate, "MyObject"),
782-
tpl->GetFunction());
785+
tpl->GetFunction(context).ToLocalChecked());
783786
}
784787

785788
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
@@ -969,7 +972,8 @@ void MyObject::Init(Isolate* isolate) {
969972
// Prototype
970973
NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
971974

972-
constructor.Reset(isolate, tpl->GetFunction());
975+
Local<Context> context = isolate->GetCurrentContext();
976+
constructor.Reset(isolate, tpl->GetFunction(context).ToLocalChecked());
973977
}
974978

975979
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
@@ -1177,7 +1181,8 @@ void MyObject::Init(Isolate* isolate) {
11771181
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
11781182
tpl->InstanceTemplate()->SetInternalFieldCount(1);
11791183

1180-
constructor.Reset(isolate, tpl->GetFunction());
1184+
Local<Context> context = isolate->GetCurrentContext();
1185+
constructor.Reset(isolate, tpl->GetFunction(context).ToLocalChecked());
11811186
}
11821187

11831188
void MyObject::New(const FunctionCallbackInfo<Value>& args) {

src/cares_wrap.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -2224,23 +2224,23 @@ void Initialize(Local<Object> target,
22242224
Local<String> addrInfoWrapString =
22252225
FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap");
22262226
aiw->SetClassName(addrInfoWrapString);
2227-
target->Set(addrInfoWrapString, aiw->GetFunction());
2227+
target->Set(addrInfoWrapString, aiw->GetFunction(context).ToLocalChecked());
22282228

22292229
Local<FunctionTemplate> niw =
22302230
BaseObject::MakeLazilyInitializedJSTemplate(env);
22312231
AsyncWrap::AddWrapMethods(env, niw);
22322232
Local<String> nameInfoWrapString =
22332233
FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap");
22342234
niw->SetClassName(nameInfoWrapString);
2235-
target->Set(nameInfoWrapString, niw->GetFunction());
2235+
target->Set(nameInfoWrapString, niw->GetFunction(context).ToLocalChecked());
22362236

22372237
Local<FunctionTemplate> qrw =
22382238
BaseObject::MakeLazilyInitializedJSTemplate(env);
22392239
AsyncWrap::AddWrapMethods(env, qrw);
22402240
Local<String> queryWrapString =
22412241
FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap");
22422242
qrw->SetClassName(queryWrapString);
2243-
target->Set(queryWrapString, qrw->GetFunction());
2243+
target->Set(queryWrapString, qrw->GetFunction(context).ToLocalChecked());
22442244

22452245
Local<FunctionTemplate> channel_wrap =
22462246
env->NewFunctionTemplate(ChannelWrap::New);
@@ -2267,7 +2267,8 @@ void Initialize(Local<Object> target,
22672267
Local<String> channelWrapString =
22682268
FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap");
22692269
channel_wrap->SetClassName(channelWrapString);
2270-
target->Set(channelWrapString, channel_wrap->GetFunction());
2270+
target->Set(channelWrapString,
2271+
channel_wrap->GetFunction(context).ToLocalChecked());
22712272
}
22722273

22732274
} // anonymous namespace

src/env-inl.h

+10-6
Original file line numberDiff line numberDiff line change
@@ -715,13 +715,15 @@ inline v8::Local<v8::FunctionTemplate>
715715
inline void Environment::SetMethod(v8::Local<v8::Object> that,
716716
const char* name,
717717
v8::FunctionCallback callback) {
718+
v8::Local<v8::Context> context = isolate()->GetCurrentContext();
718719
v8::Local<v8::Function> function =
719-
NewFunctionTemplate(callback,
720-
v8::Local<v8::Signature>(),
720+
NewFunctionTemplate(callback, v8::Local<v8::Signature>(),
721721
// TODO(TimothyGu): Investigate if SetMethod is ever
722722
// used for constructors.
723723
v8::ConstructorBehavior::kAllow,
724-
v8::SideEffectType::kHasSideEffect)->GetFunction();
724+
v8::SideEffectType::kHasSideEffect)
725+
->GetFunction(context)
726+
.ToLocalChecked();
725727
// kInternalized strings are created in the old space.
726728
const v8::NewStringType type = v8::NewStringType::kInternalized;
727729
v8::Local<v8::String> name_string =
@@ -733,13 +735,15 @@ inline void Environment::SetMethod(v8::Local<v8::Object> that,
733735
inline void Environment::SetMethodNoSideEffect(v8::Local<v8::Object> that,
734736
const char* name,
735737
v8::FunctionCallback callback) {
738+
v8::Local<v8::Context> context = isolate()->GetCurrentContext();
736739
v8::Local<v8::Function> function =
737-
NewFunctionTemplate(callback,
738-
v8::Local<v8::Signature>(),
740+
NewFunctionTemplate(callback, v8::Local<v8::Signature>(),
739741
// TODO(TimothyGu): Investigate if SetMethod is ever
740742
// used for constructors.
741743
v8::ConstructorBehavior::kAllow,
742-
v8::SideEffectType::kHasNoSideEffect)->GetFunction();
744+
v8::SideEffectType::kHasNoSideEffect)
745+
->GetFunction(context)
746+
.ToLocalChecked();
743747
// kInternalized strings are created in the old space.
744748
const v8::NewStringType type = v8::NewStringType::kInternalized;
745749
v8::Local<v8::String> name_string =

src/env.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,10 @@ void Environment::Start(const std::vector<std::string>& args,
268268
auto process_template = FunctionTemplate::New(isolate());
269269
process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate(), "process"));
270270

271-
auto process_object =
272-
process_template->GetFunction()->NewInstance(context()).ToLocalChecked();
271+
auto process_object = process_template->GetFunction(context())
272+
.ToLocalChecked()
273+
->NewInstance(context())
274+
.ToLocalChecked();
273275
set_process_object(process_object);
274276

275277
SetupProcessObject(this, args, exec_args);

src/fs_event_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void FSEventWrap::Initialize(Local<Object> target,
121121
Local<FunctionTemplate>(),
122122
static_cast<PropertyAttribute>(ReadOnly | DontDelete | v8::DontEnum));
123123

124-
target->Set(fsevent_string, t->GetFunction());
124+
target->Set(fsevent_string, t->GetFunction(context).ToLocalChecked());
125125
}
126126

127127

src/inspector_js_api.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,10 @@ void Initialize(Local<Object> target, Local<Value> unused,
310310
AsyncWrap::AddWrapMethods(env, tmpl);
311311
env->SetProtoMethod(tmpl, "dispatch", JSBindingsConnection::Dispatch);
312312
env->SetProtoMethod(tmpl, "disconnect", JSBindingsConnection::Disconnect);
313-
target->Set(env->context(), conn_str, tmpl->GetFunction()).ToChecked();
313+
target
314+
->Set(env->context(), conn_str,
315+
tmpl->GetFunction(env->context()).ToLocalChecked())
316+
.ToChecked();
314317
}
315318

316319
} // namespace

src/js_stream.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ void JSStream::Initialize(Local<Object> target,
211211
env->SetProtoMethod(t, "emitEOF", EmitEOF);
212212

213213
StreamBase::AddMethods<JSStream>(env, t);
214-
target->Set(jsStreamString, t->GetFunction());
214+
target->Set(jsStreamString, t->GetFunction(context).ToLocalChecked());
215215
}
216216

217217
} // namespace node

src/module_wrap.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,8 @@ void ModuleWrap::Initialize(Local<Object> target,
801801
env->SetProtoMethodNoSideEffect(tpl, "getStaticDependencySpecifiers",
802802
GetStaticDependencySpecifiers);
803803

804-
target->Set(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"), tpl->GetFunction());
804+
target->Set(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"),
805+
tpl->GetFunction(context).ToLocalChecked());
805806
env->SetMethod(target, "resolve", Resolve);
806807
env->SetMethod(target,
807808
"setImportModuleDynamicallyCallback",

src/node.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,10 @@ inline void NODE_SET_METHOD(v8::Local<v8::Object> recv,
366366
v8::FunctionCallback callback) {
367367
v8::Isolate* isolate = v8::Isolate::GetCurrent();
368368
v8::HandleScope handle_scope(isolate);
369+
v8::Local<v8::Context> context = isolate->GetCurrentContext();
369370
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
370371
callback);
371-
v8::Local<v8::Function> fn = t->GetFunction();
372+
v8::Local<v8::Function> fn = t->GetFunction(context).ToLocalChecked();
372373
v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
373374
v8::NewStringType::kInternalized).ToLocalChecked();
374375
fn->SetName(fn_name);

src/node_api.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,9 @@ napi_status napi_define_class(napi_env env,
15181518
}
15191519
}
15201520

1521-
*result = v8impl::JsValueFromV8LocalValue(scope.Escape(tpl->GetFunction()));
1521+
v8::Local<v8::Context> context = isolate->GetCurrentContext();
1522+
*result = v8impl::JsValueFromV8LocalValue(
1523+
scope.Escape(tpl->GetFunction(context).ToLocalChecked()));
15221524

15231525
if (static_property_count > 0) {
15241526
std::vector<napi_property_descriptor> static_descriptors;

src/node_contextify.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ void ContextifyContext::Init(Environment* env, Local<Object> target) {
203203
Local<FunctionTemplate> function_template =
204204
FunctionTemplate::New(env->isolate());
205205
function_template->InstanceTemplate()->SetInternalFieldCount(1);
206-
env->set_script_data_constructor_function(function_template->GetFunction());
206+
env->set_script_data_constructor_function(
207+
function_template->GetFunction(env->context()).ToLocalChecked());
207208

208209
env->SetMethod(target, "makeContext", MakeContext);
209210
env->SetMethod(target, "isContext", IsContext);
@@ -608,7 +609,8 @@ class ContextifyScript : public BaseObject {
608609
env->SetProtoMethod(script_tmpl, "runInContext", RunInContext);
609610
env->SetProtoMethod(script_tmpl, "runInThisContext", RunInThisContext);
610611

611-
target->Set(class_name, script_tmpl->GetFunction());
612+
target->Set(class_name,
613+
script_tmpl->GetFunction(env->context()).ToLocalChecked());
612614
env->set_script_context_constructor_template(script_tmpl);
613615
}
614616

src/node_crypto.cc

+13-9
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,8 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
366366
t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kTicketKeyIVIndex"),
367367
Integer::NewFromUnsigned(env->isolate(), kTicketKeyIVIndex));
368368

369-
target->Set(secureContextString, t->GetFunction());
369+
target->Set(secureContextString,
370+
t->GetFunction(env->context()).ToLocalChecked());
370371
env->set_secure_context_constructor_template(t);
371372
}
372373

@@ -2561,7 +2562,7 @@ void CipherBase::Initialize(Environment* env, Local<Object> target) {
25612562
env->SetProtoMethod(t, "setAAD", SetAAD);
25622563

25632564
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "CipherBase"),
2564-
t->GetFunction());
2565+
t->GetFunction(env->context()).ToLocalChecked());
25652566
}
25662567

25672568

@@ -3195,7 +3196,8 @@ void Hmac::Initialize(Environment* env, v8::Local<Object> target) {
31953196
env->SetProtoMethod(t, "update", HmacUpdate);
31963197
env->SetProtoMethod(t, "digest", HmacDigest);
31973198

3198-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Hmac"), t->GetFunction());
3199+
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Hmac"),
3200+
t->GetFunction(env->context()).ToLocalChecked());
31993201
}
32003202

32013203

@@ -3314,7 +3316,8 @@ void Hash::Initialize(Environment* env, v8::Local<Object> target) {
33143316
env->SetProtoMethod(t, "update", HashUpdate);
33153317
env->SetProtoMethod(t, "digest", HashDigest);
33163318

3317-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Hash"), t->GetFunction());
3319+
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Hash"),
3320+
t->GetFunction(env->context()).ToLocalChecked());
33183321
}
33193322

33203323

@@ -3508,7 +3511,8 @@ void Sign::Initialize(Environment* env, v8::Local<Object> target) {
35083511
env->SetProtoMethod(t, "update", SignUpdate);
35093512
env->SetProtoMethod(t, "sign", SignFinal);
35103513

3511-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Sign"), t->GetFunction());
3514+
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Sign"),
3515+
t->GetFunction(env->context()).ToLocalChecked());
35123516
}
35133517

35143518

@@ -3710,7 +3714,7 @@ void Verify::Initialize(Environment* env, v8::Local<Object> target) {
37103714
env->SetProtoMethod(t, "verify", VerifyFinal);
37113715

37123716
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Verify"),
3713-
t->GetFunction());
3717+
t->GetFunction(env->context()).ToLocalChecked());
37143718
}
37153719

37163720

@@ -3948,7 +3952,7 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
39483952
attributes);
39493953

39503954
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellman"),
3951-
t->GetFunction());
3955+
t->GetFunction(env->context()).ToLocalChecked());
39523956

39533957
Local<FunctionTemplate> t2 = env->NewFunctionTemplate(DiffieHellmanGroup);
39543958
t2->InstanceTemplate()->SetInternalFieldCount(1);
@@ -3977,7 +3981,7 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
39773981
attributes);
39783982

39793983
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellmanGroup"),
3980-
t2->GetFunction());
3984+
t2->GetFunction(env->context()).ToLocalChecked());
39813985
}
39823986

39833987

@@ -4326,7 +4330,7 @@ void ECDH::Initialize(Environment* env, Local<Object> target) {
43264330
env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey);
43274331

43284332
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ECDH"),
4329-
t->GetFunction());
4333+
t->GetFunction(env->context()).ToLocalChecked());
43304334
}
43314335

43324336

src/node_dtrace.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ void InitDTrace(Environment* env, Local<Object> target) {
276276

277277
for (size_t i = 0; i < arraysize(tab); i++) {
278278
Local<String> key = OneByteString(env->isolate(), tab[i].name);
279-
Local<Value> val = env->NewFunctionTemplate(tab[i].func)->GetFunction();
279+
Local<Value> val = env->NewFunctionTemplate(tab[i].func)
280+
->GetFunction(env->context())
281+
.ToLocalChecked();
280282
target->Set(key, val);
281283
}
282284

src/node_file.cc

+8-2
Original file line numberDiff line numberDiff line change
@@ -2240,7 +2240,10 @@ void Initialize(Local<Object> target,
22402240
Local<String> wrapString =
22412241
FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqCallback");
22422242
fst->SetClassName(wrapString);
2243-
target->Set(context, wrapString, fst->GetFunction()).FromJust();
2243+
target
2244+
->Set(context, wrapString,
2245+
fst->GetFunction(env->context()).ToLocalChecked())
2246+
.FromJust();
22442247

22452248
// Create FunctionTemplate for FileHandleReadWrap. There’s no need
22462249
// to do anything in the constructor, so we only store the instance template.
@@ -2274,7 +2277,10 @@ void Initialize(Local<Object> target,
22742277
FIXED_ONE_BYTE_STRING(env->isolate(), "FileHandle");
22752278
fd->SetClassName(handleString);
22762279
StreamBase::AddMethods<FileHandle>(env, fd);
2277-
target->Set(context, handleString, fd->GetFunction()).FromJust();
2280+
target
2281+
->Set(context, handleString,
2282+
fd->GetFunction(env->context()).ToLocalChecked())
2283+
.FromJust();
22782284
env->set_fd_constructor_template(fdt);
22792285

22802286
// Create FunctionTemplate for FileHandle::CloseReq

src/node_http2.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -2987,7 +2987,7 @@ void Initialize(Local<Object> target,
29872987
env->set_http2stream_constructor_template(streamt);
29882988
target->Set(context,
29892989
FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Stream"),
2990-
stream->GetFunction()).FromJust();
2990+
stream->GetFunction(env->context()).ToLocalChecked()).FromJust();
29912991

29922992
Local<FunctionTemplate> session =
29932993
env->NewFunctionTemplate(Http2Session::New);
@@ -3015,7 +3015,7 @@ void Initialize(Local<Object> target,
30153015
Http2Session::RefreshSettings<nghttp2_session_get_remote_settings>);
30163016
target->Set(context,
30173017
http2SessionClassName,
3018-
session->GetFunction()).FromJust();
3018+
session->GetFunction(env->context()).ToLocalChecked()).FromJust();
30193019

30203020
Local<Object> constants = Object::New(isolate);
30213021
Local<Array> name_for_error_code = Array::New(isolate);

src/node_http_parser.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ void Initialize(Local<Object> target,
776776
env->SetProtoMethod(t, "getCurrentBuffer", Parser::GetCurrentBuffer);
777777

778778
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "HTTPParser"),
779-
t->GetFunction());
779+
t->GetFunction(env->context()).ToLocalChecked());
780780
}
781781

782782
} // anonymous namespace

src/node_perf.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ void Initialize(Local<Object> target,
405405

406406
Local<FunctionTemplate> pe = FunctionTemplate::New(isolate);
407407
pe->SetClassName(performanceEntryString);
408-
Local<Function> fn = pe->GetFunction();
408+
Local<Function> fn = pe->GetFunction(context).ToLocalChecked();
409409
target->Set(context, performanceEntryString, fn).FromJust();
410410
env->set_performance_entry_template(fn);
411411

src/node_stat_watcher.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ void StatWatcher::Initialize(Environment* env, Local<Object> target) {
5656

5757
env->SetProtoMethod(t, "start", StatWatcher::Start);
5858

59-
target->Set(statWatcherString, t->GetFunction());
59+
target->Set(statWatcherString,
60+
t->GetFunction(env->context()).ToLocalChecked());
6061
}
6162

6263

src/node_trace_events.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void Initialize(Local<Object> target,
113113
env->SetProtoMethod(category_set, "disable", NodeCategorySet::Disable);
114114

115115
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "CategorySet"),
116-
category_set->GetFunction());
116+
category_set->GetFunction(env->context()).ToLocalChecked());
117117

118118
Local<String> isTraceCategoryEnabled =
119119
FIXED_ONE_BYTE_STRING(env->isolate(), "isTraceCategoryEnabled");

0 commit comments

Comments
 (0)