From ea0c7c9383db25f4e8f00442a15d601608ade56f Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 9 Oct 2020 12:58:57 +0200 Subject: [PATCH] Fix PromiseRejectMessage::get_value() (#493) Change its return type to `Option>`. The C++ API returns `Local` but that can be an empty handle. Fixes #491 --- src/promise.rs | 3 +-- tests/test_api.rs | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/promise.rs b/src/promise.rs index 8ea85c9a..10517cd4 100644 --- a/src/promise.rs +++ b/src/promise.rs @@ -214,8 +214,7 @@ impl<'msg> PromiseRejectMessage<'msg> { unsafe { v8__PromiseRejectMessage__GetEvent(self) } } - pub fn get_value(&self) -> Local<'msg, Value> { + pub fn get_value(&self) -> Option> { unsafe { Local::from_raw(v8__PromiseRejectMessage__GetValue(self)) } - .unwrap() } } diff --git a/tests/test_api.rs b/tests/test_api.rs index d17a797a..547e03e3 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -1674,7 +1674,7 @@ extern "C" fn promise_reject_callback(msg: v8::PromiseRejectMessage) { assert_eq!(event, v8::PromiseRejectEvent::PromiseRejectWithNoHandler); let promise = msg.get_promise(); assert_eq!(promise.state(), v8::PromiseState::Rejected); - let value = msg.get_value(); + let value = msg.get_value().unwrap(); { let scope = &mut v8::HandleScope::new(scope); let value_str = value.to_string(scope).unwrap(); @@ -1698,6 +1698,34 @@ fn set_promise_reject_callback() { } } +#[test] +fn promise_reject_callback_no_value() { + extern "C" fn promise_reject_callback(m: v8::PromiseRejectMessage) { + use v8::PromiseRejectEvent::*; + let value = m.get_value(); + match m.get_event() { + PromiseHandlerAddedAfterReject => assert!(value.is_none()), + PromiseRejectWithNoHandler => assert!(value.is_some()), + _ => unreachable!(), + }; + } + let _setup_guard = setup(); + let isolate = &mut v8::Isolate::new(Default::default()); + isolate.set_promise_reject_callback(promise_reject_callback); + { + let scope = &mut v8::HandleScope::new(isolate); + let context = v8::Context::new(scope); + let scope = &mut v8::ContextScope::new(scope, context); + let source = r#" + function kaboom(resolve, reject) { + throw new Error("kaboom"); + } + new Promise(kaboom).then(_ => {}); + "#; + eval(scope, source).unwrap(); + } +} + fn mock_script_origin<'s>( scope: &mut v8::HandleScope<'s>, resource_name_: &str,