@@ -419,3 +419,111 @@ TEST_F(LinkedBindingTest, ManyBindingsTest) { |
419 | 419 | CHECK_EQ(strcmp(*utf8val, "world"), 0); |
420 | 420 | CHECK_EQ(calls, 5); |
421 | 421 | } |
| 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 | +} |