0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-26 09:13:46 -05:00

Represent v8::Maybe<bool> in rust as Option<bool> (#73)

This commit is contained in:
Bert Belder 2019-12-18 15:05:33 +01:00
parent df691b6f38
commit 150c27e4c8
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
5 changed files with 47 additions and 18 deletions

View file

@ -306,16 +306,16 @@ v8::Promise* v8__Promise__Resolver__GetPromise(v8::Promise::Resolver* self) {
return local_to_ptr(self->GetPromise()); return local_to_ptr(self->GetPromise());
} }
bool v8__Promise__Resolver__Resolve(v8::Promise::Resolver* self, MaybeBool v8__Promise__Resolver__Resolve(v8::Promise::Resolver* self,
v8::Local<v8::Context> context, v8::Local<v8::Context> context,
v8::Local<v8::Value> value) { v8::Local<v8::Value> value) {
return maybe_to_value(self->Resolve(context, value), false); return maybe_to_maybe_bool(self->Resolve(context, value));
} }
bool v8__Promise__Resolver__Reject(v8::Promise::Resolver* self, MaybeBool v8__Promise__Resolver__Reject(v8::Promise::Resolver* self,
v8::Local<v8::Context> context, v8::Local<v8::Context> context,
v8::Local<v8::Value> value) { v8::Local<v8::Value> value) {
return maybe_to_value(self->Reject(context, value), false); return maybe_to_maybe_bool(self->Reject(context, value));
} }
v8::Promise::PromiseState v8__Promise__State(v8::Promise* self) { v8::Promise::PromiseState v8__Promise__State(v8::Promise* self) {

View file

@ -1,3 +1,4 @@
use crate::support::MaybeBool;
use crate::support::Opaque; use crate::support::Opaque;
use crate::Context; use crate::Context;
use crate::Function; use crate::Function;
@ -14,12 +15,12 @@ extern "C" {
resolver: *mut PromiseResolver, resolver: *mut PromiseResolver,
context: *mut Context, context: *mut Context,
value: *mut Value, value: *mut Value,
) -> bool; ) -> MaybeBool;
fn v8__Promise__Resolver__Reject( fn v8__Promise__Resolver__Reject(
resolver: *mut PromiseResolver, resolver: *mut PromiseResolver,
context: *mut Context, context: *mut Context,
value: *mut Value, value: *mut Value,
) -> bool; ) -> MaybeBool;
fn v8__Promise__State(promise: *mut Promise) -> PromiseState; fn v8__Promise__State(promise: *mut Promise) -> PromiseState;
fn v8__Promise__HasHandler(promise: *mut Promise) -> bool; fn v8__Promise__HasHandler(promise: *mut Promise) -> bool;
fn v8__Promise__Result(promise: *mut Promise) -> *mut Value; fn v8__Promise__Result(promise: *mut Promise) -> *mut Value;
@ -157,9 +158,10 @@ impl PromiseResolver {
&mut self, &mut self,
mut context: Local<'sc, Context>, mut context: Local<'sc, Context>,
mut value: Local<'sc, Value>, mut value: Local<'sc, Value>,
) -> bool { ) -> Option<bool> {
unsafe { unsafe {
v8__Promise__Resolver__Resolve(&mut *self, &mut *context, &mut *value) v8__Promise__Resolver__Resolve(&mut *self, &mut *context, &mut *value)
.into()
} }
} }
@ -170,9 +172,10 @@ impl PromiseResolver {
&mut self, &mut self,
mut context: Local<'sc, Context>, mut context: Local<'sc, Context>,
mut value: Local<'sc, Value>, mut value: Local<'sc, Value>,
) -> bool { ) -> Option<bool> {
unsafe { unsafe {
v8__Promise__Resolver__Reject(&mut *self, &mut *context, &mut *value) v8__Promise__Resolver__Reject(&mut *self, &mut *context, &mut *value)
.into()
} }
} }
} }

View file

@ -33,6 +33,19 @@ void construct_in_place(uninit_t<T>& buf, Args... args) {
new (&buf) new (&buf)
construct_in_place_helper<T, Args...>(buf, std::forward<Args>(args)...); construct_in_place_helper<T, Args...>(buf, std::forward<Args>(args)...);
} }
// The C-ABI compatible equivalent of V8's Maybe<bool>.
enum class MaybeBool { JustFalse = 0, JustTrue = 1, Nothing = 2 };
inline static MaybeBool maybe_to_maybe_bool(v8::Maybe<bool> maybe) {
if (maybe.IsNothing()) {
return MaybeBool::Nothing;
} else if (maybe.FromJust()) {
return MaybeBool::JustTrue;
} else {
return MaybeBool::JustFalse;
}
}
} // namespace support } // namespace support
template <class T> template <class T>
@ -59,9 +72,4 @@ inline static v8::MaybeLocal<T> ptr_to_maybe_local(T* ptr) {
return *reinterpret_cast<v8::MaybeLocal<T>*>(&ptr); return *reinterpret_cast<v8::MaybeLocal<T>*>(&ptr);
} }
template <class T>
inline static T maybe_to_value(v8::Maybe<T> maybe, T default_value) {
return maybe.FromMaybe(default_value);
}
#endif // SUPPORT_H_ #endif // SUPPORT_H_

View file

@ -137,6 +137,24 @@ where
} }
} }
#[repr(C)]
#[derive(Debug)]
pub enum MaybeBool {
JustFalse = 0,
JustTrue = 1,
Nothing = 2,
}
impl Into<Option<bool>> for MaybeBool {
fn into(self) -> Option<bool> {
match self {
MaybeBool::JustFalse => Some(false),
MaybeBool::JustTrue => Some(true),
MaybeBool::Nothing => None,
}
}
}
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
#[repr(transparent)] #[repr(transparent)]
pub struct CxxVTable(pub *const Opaque); pub struct CxxVTable(pub *const Opaque);

View file

@ -432,7 +432,7 @@ fn promise_rejected() {
v8::String::new(scope, "test", v8::NewStringType::Normal).unwrap(); v8::String::new(scope, "test", v8::NewStringType::Normal).unwrap();
let value: Local<v8::Value> = cast(str); let value: Local<v8::Value> = cast(str);
let rejected = resolver.reject(context, value); let rejected = resolver.reject(context, value);
assert!(rejected); assert!(rejected.unwrap());
assert_eq!(promise.state(), v8::PromiseState::Rejected); assert_eq!(promise.state(), v8::PromiseState::Rejected);
let result = promise.result(scope); let result = promise.result(scope);
let result_str: v8::Local<v8::String> = cast(result); let result_str: v8::Local<v8::String> = cast(result);