0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-21 15:04:33 -05:00

Fix PromiseRejectMessage::get_value() (#493)

Change its return type to `Option<Local<Value>>`. The C++ API
returns `Local<Value>` but that can be an empty handle.

Fixes #491
This commit is contained in:
Ben Noordhuis 2020-10-09 12:58:57 +02:00 committed by GitHub
parent 667583fe7f
commit ea0c7c9383
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View file

@ -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<Local<'msg, Value>> {
unsafe { Local::from_raw(v8__PromiseRejectMessage__GetValue(self)) }
.unwrap()
}
}

View file

@ -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,