From e6b443a6e80d5b95bc1fb458a8922df4b537427d Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 4 Jul 2022 07:02:13 +0530 Subject: [PATCH] Add `ReturnValue::set_bool` (#1020) --- src/binding.cc | 8 ++++---- src/function.rs | 5 +++++ tests/test_api.rs | 23 +++++++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index 843fd080..b8934d02 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -1846,6 +1846,10 @@ void v8__ReturnValue__Set(v8::ReturnValue* self, self->Set(ptr_to_local(&value)); } +void v8__ReturnValue__Set__Bool(v8::ReturnValue* self, bool i) { + self->Set(i); +} + void v8__ReturnValue__Set__Int32(v8::ReturnValue* self, int32_t i) { self->Set(i); } @@ -1859,10 +1863,6 @@ void v8__ReturnValue__Set__Double(v8::ReturnValue* self, double i) { self->Set(i); } -void v8__ReturnValue__Set__Bool(v8::ReturnValue* self, bool value) { - self->Set(value); -} - void v8__ReturnValue__SetNull(v8::ReturnValue* self) { self->SetNull(); } diff --git a/src/function.rs b/src/function.rs index 9f50b71e..e642cd17 100644 --- a/src/function.rs +++ b/src/function.rs @@ -78,6 +78,7 @@ extern "C" { ) -> *const Object; fn v8__ReturnValue__Set(this: *mut ReturnValue, value: *const Value); + fn v8__ReturnValue__Set__Bool(this: *mut ReturnValue, value: bool); fn v8__ReturnValue__Set__Int32(this: *mut ReturnValue, value: i32); fn v8__ReturnValue__Set__Uint32(this: *mut ReturnValue, value: u32); fn v8__ReturnValue__Set__Double(this: *mut ReturnValue, value: f64); @@ -148,6 +149,10 @@ impl<'cb> ReturnValue<'cb> { unsafe { v8__ReturnValue__Set(&mut *self, &*value) } } + pub fn set_bool(&mut self, value: bool) { + unsafe { v8__ReturnValue__Set__Bool(&mut *self, value) } + } + pub fn set_int32(&mut self, value: i32) { unsafe { v8__ReturnValue__Set__Int32(&mut *self, value) } } diff --git a/tests/test_api.rs b/tests/test_api.rs index 573571d1..1e739d78 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -2324,6 +2324,29 @@ fn return_value() { let global = context.global(scope); let recv: v8::Local = global.into(); + // set_bool + { + let template = v8::FunctionTemplate::new( + scope, + |scope: &mut v8::HandleScope, + args: v8::FunctionCallbackArguments, + mut rv: v8::ReturnValue| { + assert_eq!(args.length(), 0); + assert!(rv.get(scope).is_undefined()); + rv.set_bool(false); + }, + ); + + let function = template + .get_function(scope) + .expect("Unable to create function"); + let value = function + .call(scope, recv, &[]) + .expect("Function call failed"); + assert!(value.is_boolean()); + assert!(!value.is_true()); + } + // set_int32 { let template = v8::FunctionTemplate::new(