From 7b094328eafcabb685bd308302a6694d0cb87bf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 14 Dec 2019 23:14:09 +0100 Subject: [PATCH] add v8::ReturnValue (#66) --- src/function.cc | 7 +++++++ src/function.rs | 31 +++++++++++++++++++++++++++++++ src/local.rs | 4 ++-- src/number.rs | 2 +- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/function.cc b/src/function.cc index 2b70eebb..5583ddcd 100644 --- a/src/function.cc +++ b/src/function.cc @@ -39,4 +39,11 @@ void v8__FunctionCallbackInfo__SetReturnValue(v8::FunctionCallbackInfoGetReturnValue(); rv.Set(value); } + +v8::ReturnValue *v8__FunctionCallbackInfo__GetReturnValue(v8::FunctionCallbackInfo *self) +{ + v8::ReturnValue* return_value = nullptr; + *return_value = self->GetReturnValue(); + return return_value; +} } diff --git a/src/function.rs b/src/function.rs index f2b5e693..1bfa157f 100644 --- a/src/function.rs +++ b/src/function.rs @@ -1,6 +1,7 @@ use crate::isolate::{CxxIsolate, LockedIsolate}; use crate::support::{int, Opaque}; use crate::Context; +use crate::HandleScope; use crate::Local; use crate::Value; @@ -34,6 +35,32 @@ extern "C" { info: &FunctionCallbackInfo, value: *mut Value, ); + fn v8__FunctionCallbackInfo__GetReturnValue( + info: &FunctionCallbackInfo, + ) -> ReturnValue; +} + +#[repr(C)] +pub struct ReturnValue(T, Opaque); + +impl ReturnValue { + // NOTE: simplest setter, possibly we'll need to add + // more setters specialized per type + pub fn set(&self, _value: Local<'_, U>) { + unimplemented!(); + } + + /// Convenience getter for Isolate + pub fn get_isolate(&self) -> &mut CxxIsolate { + unimplemented!(); + } + + /// Getter. Creates a new Local<> so it comes with a certain performance + /// hit. If the ReturnValue was not yet set, this will return the undefined + /// value. + pub fn get<'sc>(&self, _scope: &mut HandleScope<'sc>) -> Local<'sc, Value> { + unimplemented!(); + } } #[repr(C)] @@ -44,6 +71,10 @@ impl FunctionCallbackInfo { unsafe { v8__FunctionCallbackInfo__SetReturnValue(&*self, &mut *value) }; } + pub fn get_return_value(&self) -> ReturnValue { + unsafe { v8__FunctionCallbackInfo__GetReturnValue(&*self) } + } + pub fn get_isolate(&self) -> &mut CxxIsolate { unsafe { v8__FunctionCallbackInfo__GetIsolate(self) } } diff --git a/src/local.rs b/src/local.rs index 252f4ef5..ed32c237 100644 --- a/src/local.rs +++ b/src/local.rs @@ -1,8 +1,8 @@ +use crate::value::Value; use std::marker::PhantomData; use std::ops::Deref; use std::ops::DerefMut; use std::ptr::NonNull; -use crate::value::Value; pub struct Local<'sc, T>(NonNull, PhantomData<&'sc ()>); @@ -33,7 +33,7 @@ impl<'sc, T> DerefMut for Local<'sc, T> { } } -// TODO make it possible for targets other than Local. For example +// TODO make it possible for targets other than Local. For example // Local should be able to be down cast to Local. impl<'sc, T> From> for Local<'sc, Value> where diff --git a/src/number.rs b/src/number.rs index 08f120e7..b12cbc06 100644 --- a/src/number.rs +++ b/src/number.rs @@ -3,9 +3,9 @@ use std::ops::Deref; use crate::isolate::CxxIsolate; use crate::isolate::LockedIsolate; use crate::support::Opaque; +use crate::value::Value; use crate::HandleScope; use crate::Local; -use crate::value::Value; extern "C" { fn v8__Number__New(isolate: &mut CxxIsolate, value: f64) -> *mut Number;