GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — src: rethrow linked binding initializer errors · nodejs/node@274c961

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • node_api.cc
    • node_binding.cc
  • test
    • cctest
      • test_linked_binding.cc
    • node-api/test_init_exception
      • binding.gyp
      • test.js
      • test_init_exception.c
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/node_api.cc‎

Copy file name to clipboardExpand all lines: src/node_api.cc
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,10 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
786786
_exports = init(env, v8impl::JsValueFromV8LocalValue(exports));
787787
});
788788

789+
// Setting module.exports below would clear an exception thrown by the
790+
// module's init function, so return and let it reach the caller.
791+
if (env->isolate->HasPendingException()) return;
792+
789793
// If register function returned a non-null exports object different from
790794
// the exports object we passed it, set that as the "exports" property of
791795
// the module.
Collapse file

‎src/node_binding.cc‎

Copy file name to clipboardExpand all lines: src/node_binding.cc
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,10 @@ void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
10901090
env, "Linked binding has no declared entry point.");
10911091
}
10921092

1093+
// Reading module.exports below would clear an exception thrown by the
1094+
// binding's initializer, so return and let it reach the caller.
1095+
if (env->isolate()->HasPendingException()) return;
1096+
10931097
Local<Value> effective_exports;
10941098
if (module->Get(env->context(), env->exports_string())
10951099
.ToLocal(&effective_exports)) {
Collapse file

‎test/cctest/test_linked_binding.cc‎

Copy file name to clipboardExpand all lines: test/cctest/test_linked_binding.cc
+108Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,111 @@ TEST_F(LinkedBindingTest, ManyBindingsTest) {
419419
CHECK_EQ(strcmp(*utf8val, "world"), 0);
420420
CHECK_EQ(calls, 5);
421421
}
422+
423+
void InitializeThrowingBinding(v8::Local<v8::Object> exports,
424+
v8::Local<v8::Value> module,
425+
v8::Local<v8::Context> context,
426+
void* priv) {
427+
v8::Isolate* isolate = v8::Isolate::GetCurrent();
428+
isolate->ThrowException(v8::Exception::Error(
429+
v8::String::NewFromUtf8Literal(isolate, "init failed")));
430+
}
431+
432+
TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingThrowsTest) {
433+
const v8::HandleScope handle_scope(isolate_);
434+
const Argv argv;
435+
Env test_env{handle_scope, argv};
436+
437+
AddLinkedBinding(
438+
*test_env, "local_linked_throws", InitializeThrowingBinding, nullptr);
439+
440+
const char* run_script =
441+
"try { process._linkedBinding('local_linked_throws'); 'not thrown'; }"
442+
" catch (err) { err.message; }";
443+
CHECK_EQ(RunScript(isolate_, run_script), "init failed");
444+
}
445+
446+
void InitializeExportsThenThrowingBinding(v8::Local<v8::Object> exports,
447+
v8::Local<v8::Value> module,
448+
v8::Local<v8::Context> context,
449+
void* priv) {
450+
v8::Isolate* isolate = v8::Isolate::GetCurrent();
451+
v8::Local<v8::Object> new_exports = v8::Object::New(isolate);
452+
CHECK(new_exports
453+
->Set(context,
454+
v8::String::NewFromUtf8Literal(isolate, "key"),
455+
v8::String::NewFromUtf8Literal(isolate, "value"))
456+
.FromJust());
457+
CHECK(module.As<v8::Object>()
458+
->Set(context,
459+
v8::String::NewFromUtf8Literal(isolate, "exports"),
460+
new_exports)
461+
.FromJust());
462+
isolate->ThrowException(v8::Exception::Error(
463+
v8::String::NewFromUtf8Literal(isolate, "init failed")));
464+
}
465+
466+
TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingExportsThenThrowsTest) {
467+
const v8::HandleScope handle_scope(isolate_);
468+
const Argv argv;
469+
Env test_env{handle_scope, argv};
470+
471+
AddLinkedBinding(*test_env,
472+
"local_linked_exports_throws",
473+
InitializeExportsThenThrowingBinding,
474+
nullptr);
475+
476+
const char* run_script =
477+
"try { process._linkedBinding('local_linked_exports_throws').key; }"
478+
" catch (err) { err.message; }";
479+
CHECK_EQ(RunScript(isolate_, run_script), "init failed");
480+
}
481+
482+
napi_value InitializeThrowingNapiBinding(napi_env env, napi_value exports) {
483+
CHECK_EQ(napi_throw_error(env, nullptr, "init failed"), napi_ok);
484+
return nullptr;
485+
}
486+
487+
TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiThrowsTest) {
488+
const v8::HandleScope handle_scope(isolate_);
489+
const Argv argv;
490+
Env test_env{handle_scope, argv};
491+
492+
AddLinkedBinding(*test_env,
493+
"local_linked_napi_throws",
494+
InitializeThrowingNapiBinding,
495+
NAPI_VERSION);
496+
497+
const char* run_script =
498+
"try { process._linkedBinding('local_linked_napi_throws');"
499+
" 'not thrown'; } catch (err) { err.message; }";
500+
CHECK_EQ(RunScript(isolate_, run_script), "init failed");
501+
}
502+
503+
napi_value InitializeExportsThenThrowingNapiBinding(napi_env env,
504+
napi_value exports) {
505+
napi_value new_exports;
506+
napi_value value;
507+
CHECK_EQ(napi_create_object(env, &new_exports), napi_ok);
508+
CHECK_EQ(napi_create_string_utf8(env, "value", NAPI_AUTO_LENGTH, &value),
509+
napi_ok);
510+
CHECK_EQ(napi_set_named_property(env, new_exports, "key", value), napi_ok);
511+
CHECK_EQ(napi_throw_error(env, nullptr, "init failed"), napi_ok);
512+
return new_exports;
513+
}
514+
515+
TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiExportsThrowsTest) {
516+
const v8::HandleScope handle_scope(isolate_);
517+
const Argv argv;
518+
Env test_env{handle_scope, argv};
519+
520+
AddLinkedBinding(*test_env,
521+
"local_linked_napi_exports_throws",
522+
InitializeExportsThenThrowingNapiBinding,
523+
NAPI_VERSION);
524+
525+
const char* run_script =
526+
"try { process._linkedBinding('local_linked_napi_exports_throws').key; }"
527+
" catch (err) { err.message; }";
528+
CHECK_EQ(RunScript(isolate_, run_script), "init failed");
529+
}
Collapse file

‎test/node-api/test_init_exception/binding.gyp‎

Copy file name to clipboard
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "test_init_exception",
5+
"sources": [ "test_init_exception.c" ]
6+
}
7+
]
8+
}
Collapse file

‎test/node-api/test_init_exception/test.js‎

Copy file name to clipboard
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
const common = require('../../common');
3+
const assert = require('assert');
4+
5+
// An exception thrown by the init function must reach the caller of
6+
// require(), even when the init function also returns new exports.
7+
assert.throws(
8+
() => require(`./build/${common.buildType}/test_init_exception`),
9+
{ message: 'Error during Init' },
10+
);
Collapse file

‎test/node-api/test_init_exception/test_init_exception.c‎

Copy file name to clipboard
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <node_api.h>
2+
#include "../../js-native-api/common.h"
3+
4+
// Throws after creating new exports. require() must throw the error instead
5+
// of returning the new exports.
6+
static napi_value Init(napi_env env, napi_value exports) {
7+
napi_value new_exports;
8+
napi_value value;
9+
NODE_API_CALL(env, napi_create_object(env, &new_exports));
10+
NODE_API_CALL(
11+
env, napi_create_string_utf8(env, "value", NAPI_AUTO_LENGTH, &value));
12+
NODE_API_CALL(env, napi_set_named_property(env, new_exports, "key", value));
13+
NODE_API_CALL(env, napi_throw_error(env, NULL, "Error during Init"));
14+
return new_exports;
15+
}
16+
17+
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.