Skip to content

Commit 76453f1

Browse files
gahaastargos
authored andcommitted
src: replace deprecated uses of FunctionTemplate::GetFunction
PR-URL: #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 8bf004b commit 76453f1

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
@@ -710,13 +710,15 @@ inline v8::Local<v8::FunctionTemplate>
710710
inline void Environment::SetMethod(v8::Local<v8::Object> that,
711711
const char* name,
712712
v8::FunctionCallback callback) {
713+
v8::Local<v8::Context> context = isolate()->GetCurrentContext();
713714
v8::Local<v8::Function> function =
714-
NewFunctionTemplate(callback,
715-
v8::Local<v8::Signature>(),
715+
NewFunctionTemplate(callback, v8::Local<v8::Signature>(),
716716
// TODO(TimothyGu): Investigate if SetMethod is ever
717717
// used for constructors.
718718
v8::ConstructorBehavior::kAllow,
719-
v8::SideEffectType::kHasSideEffect)->GetFunction();
719+
v8::SideEffectType::kHasSideEffect)
720+
->GetFunction(context)
721+
.ToLocalChecked();
720722
// kInternalized strings are created in the old space.
721723
const v8::NewStringType type = v8::NewStringType::kInternalized;
722724
v8::Local<v8::String> name_string =
@@ -728,13 +730,15 @@ inline void Environment::SetMethod(v8::Local<v8::Object> that,
728730
inline void Environment::SetMethodNoSideEffect(v8::Local<v8::Object> that,
729731
const char* name,
730732
v8::FunctionCallback callback) {
733+
v8::Local<v8::Context> context = isolate()->GetCurrentContext();
731734
v8::Local<v8::Function> function =
732-
NewFunctionTemplate(callback,
733-
v8::Local<v8::Signature>(),
735+
NewFunctionTemplate(callback, v8::Local<v8::Signature>(),
734736
// TODO(TimothyGu): Investigate if SetMethod is ever
735737
// used for constructors.
736738
v8::ConstructorBehavior::kAllow,
737-
v8::SideEffectType::kHasNoSideEffect)->GetFunction();
739+
v8::SideEffectType::kHasNoSideEffect)
740+
->GetFunction(context)
741+
.ToLocalChecked();
738742
// kInternalized strings are created in the old space.
739743
const v8::NewStringType type = v8::NewStringType::kInternalized;
740744
v8::Local<v8::String> name_string =

src/env.cc

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

233-
auto process_object =
234-
process_template->GetFunction()->NewInstance(context()).ToLocalChecked();
233+
auto process_object = process_template->GetFunction(context())
234+
.ToLocalChecked()
235+
->NewInstance(context())
236+
.ToLocalChecked();
235237
set_process_object(process_object);
236238

237239
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
@@ -372,9 +372,10 @@ inline void NODE_SET_METHOD(v8::Local<v8::Object> recv,
372372
v8::FunctionCallback callback) {
373373
v8::Isolate* isolate = v8::Isolate::GetCurrent();
374374
v8::HandleScope handle_scope(isolate);
375+
v8::Local<v8::Context> context = isolate->GetCurrentContext();
375376
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
376377
callback);
377-
v8::Local<v8::Function> fn = t->GetFunction();
378+
v8::Local<v8::Function> fn = t->GetFunction(context).ToLocalChecked();
378379
v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
379380
v8::NewStringType::kInternalized).ToLocalChecked();
380381
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
@@ -202,7 +202,8 @@ void ContextifyContext::Init(Environment* env, Local<Object> target) {
202202
Local<FunctionTemplate> function_template =
203203
FunctionTemplate::New(env->isolate());
204204
function_template->InstanceTemplate()->SetInternalFieldCount(1);
205-
env->set_script_data_constructor_function(function_template->GetFunction());
205+
env->set_script_data_constructor_function(
206+
function_template->GetFunction(env->context()).ToLocalChecked());
206207

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

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

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

@@ -2555,7 +2556,7 @@ void CipherBase::Initialize(Environment* env, Local<Object> target) {
25552556
env->SetProtoMethod(t, "setAAD", SetAAD);
25562557

25572558
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "CipherBase"),
2558-
t->GetFunction());
2559+
t->GetFunction(env->context()).ToLocalChecked());
25592560
}
25602561

25612562

@@ -3201,7 +3202,8 @@ void Hmac::Initialize(Environment* env, v8::Local<Object> target) {
32013202
env->SetProtoMethod(t, "update", HmacUpdate);
32023203
env->SetProtoMethod(t, "digest", HmacDigest);
32033204

3204-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Hmac"), t->GetFunction());
3205+
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Hmac"),
3206+
t->GetFunction(env->context()).ToLocalChecked());
32053207
}
32063208

32073209

@@ -3320,7 +3322,8 @@ void Hash::Initialize(Environment* env, v8::Local<Object> target) {
33203322
env->SetProtoMethod(t, "update", HashUpdate);
33213323
env->SetProtoMethod(t, "digest", HashDigest);
33223324

3323-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Hash"), t->GetFunction());
3325+
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Hash"),
3326+
t->GetFunction(env->context()).ToLocalChecked());
33243327
}
33253328

33263329

@@ -3514,7 +3517,8 @@ void Sign::Initialize(Environment* env, v8::Local<Object> target) {
35143517
env->SetProtoMethod(t, "update", SignUpdate);
35153518
env->SetProtoMethod(t, "sign", SignFinal);
35163519

3517-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Sign"), t->GetFunction());
3520+
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Sign"),
3521+
t->GetFunction(env->context()).ToLocalChecked());
35183522
}
35193523

35203524

@@ -3716,7 +3720,7 @@ void Verify::Initialize(Environment* env, v8::Local<Object> target) {
37163720
env->SetProtoMethod(t, "verify", VerifyFinal);
37173721

37183722
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Verify"),
3719-
t->GetFunction());
3723+
t->GetFunction(env->context()).ToLocalChecked());
37203724
}
37213725

37223726

@@ -3954,7 +3958,7 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
39543958
attributes);
39553959

39563960
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellman"),
3957-
t->GetFunction());
3961+
t->GetFunction(env->context()).ToLocalChecked());
39583962

39593963
Local<FunctionTemplate> t2 = env->NewFunctionTemplate(DiffieHellmanGroup);
39603964
t2->InstanceTemplate()->SetInternalFieldCount(1);
@@ -3983,7 +3987,7 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
39833987
attributes);
39843988

39853989
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellmanGroup"),
3986-
t2->GetFunction());
3990+
t2->GetFunction(env->context()).ToLocalChecked());
39873991
}
39883992

39893993

@@ -4332,7 +4336,7 @@ void ECDH::Initialize(Environment* env, Local<Object> target) {
43324336
env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey);
43334337

43344338
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ECDH"),
4335-
t->GetFunction());
4339+
t->GetFunction(env->context()).ToLocalChecked());
43364340
}
43374341

43384342

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
@@ -2096,7 +2096,10 @@ void Initialize(Local<Object> target,
20962096
Local<String> wrapString =
20972097
FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap");
20982098
fst->SetClassName(wrapString);
2099-
target->Set(context, wrapString, fst->GetFunction()).FromJust();
2099+
target
2100+
->Set(context, wrapString,
2101+
fst->GetFunction(env->context()).ToLocalChecked())
2102+
.FromJust();
21002103

21012104
// Create FunctionTemplate for FileHandleReadWrap. There’s no need
21022105
// to do anything in the constructor, so we only store the instance template.
@@ -2130,7 +2133,10 @@ void Initialize(Local<Object> target,
21302133
FIXED_ONE_BYTE_STRING(env->isolate(), "FileHandle");
21312134
fd->SetClassName(handleString);
21322135
StreamBase::AddMethods<FileHandle>(env, fd);
2133-
target->Set(context, handleString, fd->GetFunction()).FromJust();
2136+
target
2137+
->Set(context, handleString,
2138+
fd->GetFunction(env->context()).ToLocalChecked())
2139+
.FromJust();
21342140
env->set_fd_constructor_template(fdt);
21352141

21362142
// 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
@@ -239,7 +239,7 @@ void Initialize(Local<Object> target,
239239
env->SetProtoMethod(category_set, "disable", NodeCategorySet::Disable);
240240

241241
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "CategorySet"),
242-
category_set->GetFunction());
242+
category_set->GetFunction(env->context()).ToLocalChecked());
243243

244244
Local<String> isTraceCategoryEnabled =
245245
FIXED_ONE_BYTE_STRING(env->isolate(), "isTraceCategoryEnabled");

0 commit comments

Comments
 (0)