From 2310f679a1c200b8c4a35749703ce1e3429f65f5 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 2 Oct 2020 14:10:55 -0700 Subject: src: move node_binding to modern THROW_ERR* Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/35469 Reviewed-By: Joyee Cheung --- src/node_binding.cc | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'src/node_binding.cc') diff --git a/src/node_binding.cc b/src/node_binding.cc index 0d577a8c8e8..0db930adff1 100644 --- a/src/node_binding.cc +++ b/src/node_binding.cc @@ -6,6 +6,8 @@ #include "node_native_module_env.h" #include "util.h" +#include + #if HAVE_OPENSSL #define NODE_BUILTIN_OPENSSL_MODULES(V) V(crypto) V(tls_wrap) #else @@ -424,13 +426,13 @@ void DLOpen(const FunctionCallbackInfo& args) { CHECK_NULL(thread_local_modpending); if (args.Length() < 2) { - env->ThrowError("process.dlopen needs at least 2 arguments."); - return; + return THROW_ERR_MISSING_ARGS( + env, "process.dlopen needs at least 2 arguments"); } int32_t flags = DLib::kDefaultFlags; if (args.Length() > 2 && !args[2]->Int32Value(context).To(&flags)) { - return env->ThrowTypeError("flag argument must be an integer."); + return THROW_ERR_INVALID_ARG_TYPE(env, "flag argument must be an integer."); } Local module; @@ -456,15 +458,13 @@ void DLOpen(const FunctionCallbackInfo& args) { thread_local_modpending = nullptr; if (!is_opened) { - Local errmsg = - OneByteString(env->isolate(), dlib->errmsg_.c_str()); + std::string errmsg = dlib->errmsg_.c_str(); dlib->Close(); #ifdef _WIN32 // Windows needs to add the filename into the error message - errmsg = String::Concat( - env->isolate(), errmsg, args[1]->ToString(context).ToLocalChecked()); + errmsg += *filename; #endif // _WIN32 - env->isolate()->ThrowException(Exception::Error(errmsg)); + THROW_ERR_DLOPEN_FAILED(env, errmsg.c_str()); return false; } @@ -494,7 +494,7 @@ void DLOpen(const FunctionCallbackInfo& args) { sizeof(errmsg), "Module did not self-register: '%s'.", *filename); - env->ThrowError(errmsg); + THROW_ERR_DLOPEN_FAILED(env, errmsg); return false; } } @@ -525,7 +525,7 @@ void DLOpen(const FunctionCallbackInfo& args) { // NOTE: `mp` is allocated inside of the shared library's memory, calling // `dlclose` will deallocate it dlib->Close(); - env->ThrowError(errmsg); + THROW_ERR_DLOPEN_FAILED(env, errmsg); return false; } CHECK_EQ(mp->nm_flags & NM_F_BUILTIN, 0); @@ -538,7 +538,7 @@ void DLOpen(const FunctionCallbackInfo& args) { mp->nm_register_func(exports, module, mp->nm_priv); } else { dlib->Close(); - env->ThrowError("Module has no declared entry point."); + THROW_ERR_DLOPEN_FAILED(env, "Module has no declared entry point."); return false; } @@ -577,12 +577,6 @@ static Local InitModule(Environment* env, return exports; } -static void ThrowIfNoSuchModule(Environment* env, const char* module_v) { - char errmsg[1024]; - snprintf(errmsg, sizeof(errmsg), "No such module: %s", module_v); - env->ThrowError(errmsg); -} - void GetInternalBinding(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); @@ -611,7 +605,9 @@ void GetInternalBinding(const FunctionCallbackInfo& args) { env->isolate())) .FromJust()); } else { - return ThrowIfNoSuchModule(env, *module_v); + char errmsg[1024]; + snprintf(errmsg, sizeof(errmsg), "No such module: %s", *module_v); + return THROW_ERR_INVALID_MODULE(env, errmsg); } args.GetReturnValue().Set(exports); @@ -646,7 +642,7 @@ void GetLinkedBinding(const FunctionCallbackInfo& args) { sizeof(errmsg), "No such module was linked: %s", *module_name_v); - return env->ThrowError(errmsg); + return THROW_ERR_INVALID_MODULE(env, errmsg); } Local module = Object::New(env->isolate()); @@ -661,7 +657,9 @@ void GetLinkedBinding(const FunctionCallbackInfo& args) { } else if (mod->nm_register_func != nullptr) { mod->nm_register_func(exports, module, mod->nm_priv); } else { - return env->ThrowError("Linked module has no declared entry point."); + return THROW_ERR_INVALID_MODULE( + env, + "Linked moduled has no declared entry point."); } auto effective_exports = -- cgit v1.2.3