diff --git a/src/node_api.cc b/src/node_api.cc index 64d26a7a502e..cc6ec220b599 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -786,6 +786,10 @@ void napi_module_register_by_symbol(v8::Local exports, _exports = init(env, v8impl::JsValueFromV8LocalValue(exports)); }); + // Setting module.exports below would clear an exception thrown by the + // module's init function, so return and let it reach the caller. + if (env->isolate->HasPendingException()) return; + // If register function returned a non-null exports object different from // the exports object we passed it, set that as the "exports" property of // the module. diff --git a/src/node_binding.cc b/src/node_binding.cc index b8a1a6f64c47..b4560045eba1 100644 --- a/src/node_binding.cc +++ b/src/node_binding.cc @@ -1090,6 +1090,10 @@ void GetLinkedBinding(const FunctionCallbackInfo& args) { env, "Linked binding has no declared entry point."); } + // Reading module.exports below would clear an exception thrown by the + // binding's initializer, so return and let it reach the caller. + if (env->isolate()->HasPendingException()) return; + Local effective_exports; if (module->Get(env->context(), env->exports_string()) .ToLocal(&effective_exports)) { diff --git a/test/cctest/test_linked_binding.cc b/test/cctest/test_linked_binding.cc index d36b5874e741..32f9e4d8d7cf 100644 --- a/test/cctest/test_linked_binding.cc +++ b/test/cctest/test_linked_binding.cc @@ -419,3 +419,111 @@ TEST_F(LinkedBindingTest, ManyBindingsTest) { CHECK_EQ(strcmp(*utf8val, "world"), 0); CHECK_EQ(calls, 5); } + +void InitializeThrowingBinding(v8::Local exports, + v8::Local module, + v8::Local context, + void* priv) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + isolate->ThrowException(v8::Exception::Error( + v8::String::NewFromUtf8Literal(isolate, "init failed"))); +} + +TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingThrowsTest) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + + AddLinkedBinding( + *test_env, "local_linked_throws", InitializeThrowingBinding, nullptr); + + const char* run_script = + "try { process._linkedBinding('local_linked_throws'); 'not thrown'; }" + " catch (err) { err.message; }"; + CHECK_EQ(RunScript(isolate_, run_script), "init failed"); +} + +void InitializeExportsThenThrowingBinding(v8::Local exports, + v8::Local module, + v8::Local context, + void* priv) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::Local new_exports = v8::Object::New(isolate); + CHECK(new_exports + ->Set(context, + v8::String::NewFromUtf8Literal(isolate, "key"), + v8::String::NewFromUtf8Literal(isolate, "value")) + .FromJust()); + CHECK(module.As() + ->Set(context, + v8::String::NewFromUtf8Literal(isolate, "exports"), + new_exports) + .FromJust()); + isolate->ThrowException(v8::Exception::Error( + v8::String::NewFromUtf8Literal(isolate, "init failed"))); +} + +TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingExportsThenThrowsTest) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + + AddLinkedBinding(*test_env, + "local_linked_exports_throws", + InitializeExportsThenThrowingBinding, + nullptr); + + const char* run_script = + "try { process._linkedBinding('local_linked_exports_throws').key; }" + " catch (err) { err.message; }"; + CHECK_EQ(RunScript(isolate_, run_script), "init failed"); +} + +napi_value InitializeThrowingNapiBinding(napi_env env, napi_value exports) { + CHECK_EQ(napi_throw_error(env, nullptr, "init failed"), napi_ok); + return nullptr; +} + +TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiThrowsTest) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + + AddLinkedBinding(*test_env, + "local_linked_napi_throws", + InitializeThrowingNapiBinding, + NAPI_VERSION); + + const char* run_script = + "try { process._linkedBinding('local_linked_napi_throws');" + " 'not thrown'; } catch (err) { err.message; }"; + CHECK_EQ(RunScript(isolate_, run_script), "init failed"); +} + +napi_value InitializeExportsThenThrowingNapiBinding(napi_env env, + napi_value exports) { + napi_value new_exports; + napi_value value; + CHECK_EQ(napi_create_object(env, &new_exports), napi_ok); + CHECK_EQ(napi_create_string_utf8(env, "value", NAPI_AUTO_LENGTH, &value), + napi_ok); + CHECK_EQ(napi_set_named_property(env, new_exports, "key", value), napi_ok); + CHECK_EQ(napi_throw_error(env, nullptr, "init failed"), napi_ok); + return new_exports; +} + +TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiExportsThrowsTest) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + + AddLinkedBinding(*test_env, + "local_linked_napi_exports_throws", + InitializeExportsThenThrowingNapiBinding, + NAPI_VERSION); + + const char* run_script = + "try { process._linkedBinding('local_linked_napi_exports_throws').key; }" + " catch (err) { err.message; }"; + CHECK_EQ(RunScript(isolate_, run_script), "init failed"); +} diff --git a/test/node-api/test_init_exception/binding.gyp b/test/node-api/test_init_exception/binding.gyp new file mode 100644 index 000000000000..bfd03ff5d9b2 --- /dev/null +++ b/test/node-api/test_init_exception/binding.gyp @@ -0,0 +1,8 @@ +{ + "targets": [ + { + "target_name": "test_init_exception", + "sources": [ "test_init_exception.c" ] + } + ] +} diff --git a/test/node-api/test_init_exception/test.js b/test/node-api/test_init_exception/test.js new file mode 100644 index 000000000000..41524301840d --- /dev/null +++ b/test/node-api/test_init_exception/test.js @@ -0,0 +1,10 @@ +'use strict'; +const common = require('../../common'); +const assert = require('assert'); + +// An exception thrown by the init function must reach the caller of +// require(), even when the init function also returns new exports. +assert.throws( + () => require(`./build/${common.buildType}/test_init_exception`), + { message: 'Error during Init' }, +); diff --git a/test/node-api/test_init_exception/test_init_exception.c b/test/node-api/test_init_exception/test_init_exception.c new file mode 100644 index 000000000000..6c32521f5f5b --- /dev/null +++ b/test/node-api/test_init_exception/test_init_exception.c @@ -0,0 +1,17 @@ +#include +#include "../../js-native-api/common.h" + +// Throws after creating new exports. require() must throw the error instead +// of returning the new exports. +static napi_value Init(napi_env env, napi_value exports) { + napi_value new_exports; + napi_value value; + NODE_API_CALL(env, napi_create_object(env, &new_exports)); + NODE_API_CALL( + env, napi_create_string_utf8(env, "value", NAPI_AUTO_LENGTH, &value)); + NODE_API_CALL(env, napi_set_named_property(env, new_exports, "key", value)); + NODE_API_CALL(env, napi_throw_error(env, NULL, "Error during Init")); + return new_exports; +} + +NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)