@@ -188,7 +188,7 @@ void DynamicLibrary::CleanupFunctionInfo(
188188 delete info;
189189}
190190
191- Local <Function> DynamicLibrary::CreateFunction (
191+ MaybeLocal <Function> DynamicLibrary::CreateFunction (
192192 Environment* env,
193193 const std::string& name,
194194 const std::shared_ptr<FFIFunction>& fn) {
@@ -197,21 +197,32 @@ Local<Function> DynamicLibrary::CreateFunction(
197197 FFIFunctionInfo* info = new FFIFunctionInfo ();
198198 info->fn = fn;
199199 Local<External> data = External::New (isolate, info);
200- Local<Function> ret =
201- Function::New (env->context (), DynamicLibrary::InvokeFunction, data)
202- .ToLocalChecked ();
200+ MaybeLocal<Function> maybe_ret =
201+ Function::New (env->context (), DynamicLibrary::InvokeFunction, data);
202+ Local<Function> ret;
203+ if (!maybe_ret.ToLocal (&ret)) {
204+ return MaybeLocal<Function>();
205+ }
206+
207+ Local<String> name_str;
208+ if (!String::NewFromUtf8 (isolate, name.c_str (), NewStringType::kNormal )
209+ .ToLocal (&name_str)) {
210+ return MaybeLocal<Function>();
211+ }
212+
203213 info->self .Reset (isolate, ret);
204214 info->self .SetWeak (
205215 info, DynamicLibrary::CleanupFunctionInfo, WeakCallbackType::kParameter );
206- ret->SetName (
207- String::NewFromUtf8 (isolate, name.c_str (), NewStringType::kNormal )
208- .ToLocalChecked ());
209- ret->Set (env->context (),
210- FIXED_ONE_BYTE_STRING (env->isolate (), " pointer" ),
211- BigInt::NewFromUnsigned (
212- isolate,
213- static_cast <uint64_t >(reinterpret_cast <uintptr_t >(fn->ptr ))))
214- .Check ();
216+ ret->SetName (name_str);
217+ if (!ret->Set (
218+ env->context (),
219+ env->pointer_string (),
220+ BigInt::NewFromUnsigned (
221+ isolate,
222+ static_cast <uint64_t >(reinterpret_cast <uintptr_t >(fn->ptr ))))
223+ .FromMaybe (false )) {
224+ return MaybeLocal<Function>();
225+ }
215226
216227 return ret;
217228}
@@ -386,7 +397,14 @@ void DynamicLibrary::InvokeCallback(ffi_cif* cif,
386397 ABORT ();
387398 }
388399
389- Local<Value> result_val = result.ToLocalChecked ();
400+ Local<Value> result_val;
401+ if (!result.ToLocal (&result_val)) {
402+ if (try_catch.HasCaught ()) {
403+ FPrintF (stderr, " Callbacks cannot return an exception\n " );
404+ ABORT ();
405+ }
406+ return ;
407+ }
390408
391409 if (result_val->IsPromise ()) {
392410 FPrintF (stderr, " Callbacks cannot return promises\n " );
@@ -402,10 +420,14 @@ void DynamicLibrary::InvokeCallback(ffi_cif* cif,
402420void DynamicLibrary::GetPath (const FunctionCallbackInfo<Value>& args) {
403421 DynamicLibrary* lib = Unwrap<DynamicLibrary>(args.This ());
404422
405- args.GetReturnValue ().Set (String::NewFromUtf8 (args.GetIsolate (),
406- lib->path_ .c_str (),
407- NewStringType::kNormal )
408- .ToLocalChecked ());
423+ Local<String> path;
424+ if (!String::NewFromUtf8 (
425+ args.GetIsolate (), lib->path_ .c_str (), NewStringType::kNormal )
426+ .ToLocal (&path)) {
427+ return ;
428+ }
429+
430+ args.GetReturnValue ().Set (path);
409431}
410432
411433void DynamicLibrary::GetFunction (const FunctionCallbackInfo<Value>& args) {
@@ -448,7 +470,11 @@ void DynamicLibrary::GetFunction(const FunctionCallbackInfo<Value>& args) {
448470 lib->functions_ .emplace (*name, fn);
449471 }
450472
451- Local<Function> ret = lib->CreateFunction (env, *name, fn);
473+ MaybeLocal<Function> maybe_ret = lib->CreateFunction (env, *name, fn);
474+ Local<Function> ret;
475+ if (!maybe_ret.ToLocal (&ret)) {
476+ return ;
477+ }
452478 args.GetReturnValue ().Set (ret);
453479}
454480
@@ -539,27 +565,43 @@ void DynamicLibrary::GetFunctions(const FunctionCallbackInfo<Value>& args) {
539565 }
540566
541567 for (const auto & item : pending) {
542- Local<Function> ret = lib->CreateFunction (env, item.name , item.fn );
543-
544- functions
545- ->Set (context,
546- String::NewFromUtf8 (
547- isolate, item.name .c_str (), NewStringType::kNormal )
548- .ToLocalChecked (),
549- ret)
550- .Check ();
568+ MaybeLocal<Function> maybe_ret =
569+ lib->CreateFunction (env, item.name , item.fn );
570+ Local<Function> ret;
571+ if (!maybe_ret.ToLocal (&ret)) {
572+ return ;
573+ }
574+
575+ Local<String> name_string;
576+ if (!String::NewFromUtf8 (
577+ isolate, item.name .c_str (), NewStringType::kNormal )
578+ .ToLocal (&name_string)) {
579+ return ;
580+ }
581+
582+ if (!functions->Set (context, name_string, ret).FromMaybe (false )) {
583+ return ;
584+ }
551585 }
552586 } else {
553587 for (const auto & entry : lib->functions_ ) {
554- Local<Function> fn = lib->CreateFunction (env, entry.first , entry.second );
555-
556- functions
557- ->Set (context,
558- String::NewFromUtf8 (
559- isolate, entry.first .c_str (), NewStringType::kNormal )
560- .ToLocalChecked (),
561- fn)
562- .Check ();
588+ MaybeLocal<Function> maybe_fn =
589+ lib->CreateFunction (env, entry.first , entry.second );
590+ Local<Function> fn;
591+ if (!maybe_fn.ToLocal (&fn)) {
592+ return ;
593+ }
594+
595+ Local<String> name_string;
596+ if (!String::NewFromUtf8 (
597+ isolate, entry.first .c_str (), NewStringType::kNormal )
598+ .ToLocal (&name_string)) {
599+ return ;
600+ }
601+
602+ if (!functions->Set (context, name_string, fn).FromMaybe (false )) {
603+ return ;
604+ }
563605 }
564606 }
565607
@@ -608,16 +650,23 @@ void DynamicLibrary::GetSymbols(const FunctionCallbackInfo<Value>& args) {
608650 return ;
609651 }
610652 for (const auto & entry : lib->symbols_ ) {
611- symbols
612- ->Set (context,
613- String::NewFromUtf8 (
614- isolate, entry.first .c_str (), NewStringType::kNormal )
615- .ToLocalChecked (),
616- BigInt::NewFromUnsigned (
617- isolate,
618- static_cast <uint64_t >(
619- reinterpret_cast <uintptr_t >(entry.second ))))
620- .Check ();
653+ Local<String> symbol_key;
654+ if (!String::NewFromUtf8 (
655+ isolate, entry.first .c_str (), NewStringType::kNormal )
656+ .ToLocal (&symbol_key)) {
657+ return ;
658+ }
659+
660+ if (!symbols
661+ ->Set (context,
662+ symbol_key,
663+ BigInt::NewFromUnsigned (
664+ isolate,
665+ static_cast <uint64_t >(
666+ reinterpret_cast <uintptr_t >(entry.second ))))
667+ .FromMaybe (false )) {
668+ return ;
669+ }
621670 }
622671
623672 args.GetReturnValue ().Set (symbols);
0 commit comments