From 150c27e4c869096f9e1f2e4ff837fb64156b578b Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Wed, 18 Dec 2019 15:05:33 +0100 Subject: [PATCH] Represent v8::Maybe in rust as Option (#73) --- src/binding.cc | 16 ++++++++-------- src/promise.rs | 11 +++++++---- src/support.h | 18 +++++++++++++----- src/support.rs | 18 ++++++++++++++++++ tests/test_api.rs | 2 +- 5 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index 51276dfc..4d284455 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -306,16 +306,16 @@ v8::Promise* v8__Promise__Resolver__GetPromise(v8::Promise::Resolver* self) { return local_to_ptr(self->GetPromise()); } -bool v8__Promise__Resolver__Resolve(v8::Promise::Resolver* self, - v8::Local context, - v8::Local value) { - return maybe_to_value(self->Resolve(context, value), false); +MaybeBool v8__Promise__Resolver__Resolve(v8::Promise::Resolver* self, + v8::Local context, + v8::Local value) { + return maybe_to_maybe_bool(self->Resolve(context, value)); } -bool v8__Promise__Resolver__Reject(v8::Promise::Resolver* self, - v8::Local context, - v8::Local value) { - return maybe_to_value(self->Reject(context, value), false); +MaybeBool v8__Promise__Resolver__Reject(v8::Promise::Resolver* self, + v8::Local context, + v8::Local value) { + return maybe_to_maybe_bool(self->Reject(context, value)); } v8::Promise::PromiseState v8__Promise__State(v8::Promise* self) { diff --git a/src/promise.rs b/src/promise.rs index 433e39cd..ed05a116 100644 --- a/src/promise.rs +++ b/src/promise.rs @@ -1,3 +1,4 @@ +use crate::support::MaybeBool; use crate::support::Opaque; use crate::Context; use crate::Function; @@ -14,12 +15,12 @@ extern "C" { resolver: *mut PromiseResolver, context: *mut Context, value: *mut Value, - ) -> bool; + ) -> MaybeBool; fn v8__Promise__Resolver__Reject( resolver: *mut PromiseResolver, context: *mut Context, value: *mut Value, - ) -> bool; + ) -> MaybeBool; fn v8__Promise__State(promise: *mut Promise) -> PromiseState; fn v8__Promise__HasHandler(promise: *mut Promise) -> bool; fn v8__Promise__Result(promise: *mut Promise) -> *mut Value; @@ -157,9 +158,10 @@ impl PromiseResolver { &mut self, mut context: Local<'sc, Context>, mut value: Local<'sc, Value>, - ) -> bool { + ) -> Option { unsafe { v8__Promise__Resolver__Resolve(&mut *self, &mut *context, &mut *value) + .into() } } @@ -170,9 +172,10 @@ impl PromiseResolver { &mut self, mut context: Local<'sc, Context>, mut value: Local<'sc, Value>, - ) -> bool { + ) -> Option { unsafe { v8__Promise__Resolver__Reject(&mut *self, &mut *context, &mut *value) + .into() } } } diff --git a/src/support.h b/src/support.h index e3085db8..e315d896 100644 --- a/src/support.h +++ b/src/support.h @@ -33,6 +33,19 @@ void construct_in_place(uninit_t& buf, Args... args) { new (&buf) construct_in_place_helper(buf, std::forward(args)...); } + +// The C-ABI compatible equivalent of V8's Maybe. +enum class MaybeBool { JustFalse = 0, JustTrue = 1, Nothing = 2 }; + +inline static MaybeBool maybe_to_maybe_bool(v8::Maybe maybe) { + if (maybe.IsNothing()) { + return MaybeBool::Nothing; + } else if (maybe.FromJust()) { + return MaybeBool::JustTrue; + } else { + return MaybeBool::JustFalse; + } +} } // namespace support template @@ -59,9 +72,4 @@ inline static v8::MaybeLocal ptr_to_maybe_local(T* ptr) { return *reinterpret_cast*>(&ptr); } -template -inline static T maybe_to_value(v8::Maybe maybe, T default_value) { - return maybe.FromMaybe(default_value); -} - #endif // SUPPORT_H_ diff --git a/src/support.rs b/src/support.rs index 8f188350..90f1884e 100644 --- a/src/support.rs +++ b/src/support.rs @@ -137,6 +137,24 @@ where } } +#[repr(C)] +#[derive(Debug)] +pub enum MaybeBool { + JustFalse = 0, + JustTrue = 1, + Nothing = 2, +} + +impl Into> for MaybeBool { + fn into(self) -> Option { + match self { + MaybeBool::JustFalse => Some(false), + MaybeBool::JustTrue => Some(true), + MaybeBool::Nothing => None, + } + } +} + #[derive(Copy, Clone, Debug)] #[repr(transparent)] pub struct CxxVTable(pub *const Opaque); diff --git a/tests/test_api.rs b/tests/test_api.rs index 1c1bf386..db2074be 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -432,7 +432,7 @@ fn promise_rejected() { v8::String::new(scope, "test", v8::NewStringType::Normal).unwrap(); let value: Local = cast(str); let rejected = resolver.reject(context, value); - assert!(rejected); + assert!(rejected.unwrap()); assert_eq!(promise.state(), v8::PromiseState::Rejected); let result = promise.result(scope); let result_str: v8::Local = cast(result);