Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,10 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> 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.
Expand Down
4 changes: 4 additions & 0 deletions src/node_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,10 @@ void GetLinkedBinding(const FunctionCallbackInfo<Value>& 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<Value> effective_exports;
if (module->Get(env->context(), env->exports_string())
.ToLocal(&effective_exports)) {
Expand Down
108 changes: 108 additions & 0 deletions test/cctest/test_linked_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,111 @@ TEST_F(LinkedBindingTest, ManyBindingsTest) {
CHECK_EQ(strcmp(*utf8val, "world"), 0);
CHECK_EQ(calls, 5);
}

void InitializeThrowingBinding(v8::Local<v8::Object> exports,
v8::Local<v8::Value> module,
v8::Local<v8::Context> 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<v8::Object> exports,
v8::Local<v8::Value> module,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Local<v8::Object> 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<v8::Object>()
->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");
}
8 changes: 8 additions & 0 deletions test/node-api/test_init_exception/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "test_init_exception",
"sources": [ "test_init_exception.c" ]
}
]
}
10 changes: 10 additions & 0 deletions test/node-api/test_init_exception/test.js
Original file line number Diff line number Diff line change
@@ -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' },
);
17 changes: 17 additions & 0 deletions test/node-api/test_init_exception/test_init_exception.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <node_api.h>
#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)
Loading