From bb0be74b0b476dd0dcb3972f64d6410aa2ea3b0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sabiniarz?= <31597105+mhvsa@users.noreply.github.com> Date: Wed, 1 Apr 2020 23:47:19 +0200 Subject: [PATCH] add v8::Proxy (#330) --- src/binding.cc | 23 ++++++++++++++++++++ src/lib.rs | 2 ++ src/proxy.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++ tests/test_api.rs | 24 +++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 src/proxy.rs diff --git a/src/binding.cc b/src/binding.cc index c492b465..52cf95cd 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -1303,6 +1303,29 @@ v8::Object* v8__PropertyCallbackInfo__This( return local_to_ptr(self.This()); } +v8::Proxy* v8__Proxy__New(v8::Local context, + v8::Local target, + v8::Local handler) { + return maybe_local_to_ptr(v8::Proxy::New(context, target, handler)); +} + +v8::Value* v8__Proxy__GetHandler(v8::Proxy* self){ + return local_to_ptr(self->GetHandler()); +} + +v8::Value* v8__Proxy__GetTarget(v8::Proxy* self){ + return local_to_ptr(self->GetTarget()); +} + +bool v8__Proxy__IsRevoked(v8::Proxy* self) { + return self->IsRevoked(); +} + +void v8__Proxy__Revoke(v8::Proxy* self) { + self->Revoke(); +} + + void v8__SnapshotCreator__CONSTRUCT(uninit_t& buf, const intptr_t* external_references) { construct_in_place(buf, external_references); diff --git a/src/lib.rs b/src/lib.rs index 77a25eb9..fc43675d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,6 +94,7 @@ mod primitive_array; mod primitives; mod promise; mod property_attribute; +mod proxy; mod scope_traits; mod script; mod script_or_module; @@ -144,6 +145,7 @@ pub use platform::TaskImpl; pub use primitives::*; pub use promise::{PromiseRejectEvent, PromiseRejectMessage, PromiseState}; pub use property_attribute::*; +pub use proxy::*; pub use scope::CallbackScope; pub use scope::ContextScope; pub use scope::FunctionCallbackScope; diff --git a/src/proxy.rs b/src/proxy.rs new file mode 100644 index 00000000..3ad60ec3 --- /dev/null +++ b/src/proxy.rs @@ -0,0 +1,54 @@ +use crate::Context; +use crate::Local; +use crate::Object; +use crate::Proxy; +use crate::ToLocal; +use crate::Value; + +extern "C" { + fn v8__Proxy__New( + context: *mut Context, + target: *mut Object, + handler: *mut Object, + ) -> *mut Proxy; + fn v8__Proxy__GetHandler(proxy: *mut Proxy) -> *mut Value; + fn v8__Proxy__GetTarget(proxy: *mut Proxy) -> *mut Value; + fn v8__Proxy__IsRevoked(proxy: *mut Proxy) -> bool; + fn v8__Proxy__Revoke(proxy: *mut Proxy); +} + +impl Proxy { + pub fn new<'sc>( + scope: &mut impl ToLocal<'sc>, + mut context: Local, + mut target: Local, + mut handler: Local, + ) -> Option> { + unsafe { + let ptr = v8__Proxy__New(&mut *context, &mut *target, &mut *handler); + scope.to_local(ptr) + } + } + + pub fn get_handler<'sc>( + &mut self, + scope: &mut impl ToLocal<'sc>, + ) -> Local<'sc, Value> { + unsafe { scope.to_local(v8__Proxy__GetHandler(&mut *self)) }.unwrap() + } + + pub fn get_target<'sc>( + &mut self, + scope: &mut impl ToLocal<'sc>, + ) -> Local<'sc, Value> { + unsafe { scope.to_local(v8__Proxy__GetTarget(&mut *self)) }.unwrap() + } + + pub fn is_revoked(&mut self) -> bool { + unsafe { v8__Proxy__IsRevoked(self) } + } + + pub fn revoke(&mut self) { + unsafe { v8__Proxy__Revoke(self) }; + } +} diff --git a/tests/test_api.rs b/tests/test_api.rs index b6e4f802..acbbefa9 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -1295,6 +1295,30 @@ fn promise_rejected() { assert_eq!(result_str.to_rust_string_lossy(scope), "test".to_string()); } } +#[test] +fn proxy() { + let _setup_guard = setup(); + let mut params = v8::Isolate::create_params(); + params.set_array_buffer_allocator(v8::new_default_allocator()); + let mut isolate = v8::Isolate::new(params); + { + let mut hs = v8::HandleScope::new(&mut isolate); + let scope = hs.enter(); + let context = v8::Context::new(scope); + let mut cs = v8::ContextScope::new(scope, context); + let scope = cs.enter(); + let target = v8::Object::new(scope); + let handler = v8::Object::new(scope); + let maybe_proxy = v8::Proxy::new(scope, context, target, handler); + assert!(maybe_proxy.is_some()); + let mut proxy = maybe_proxy.unwrap(); + assert!(target == proxy.get_target(scope)); + assert!(handler == proxy.get_handler(scope)); + assert!(!proxy.is_revoked()); + proxy.revoke(); + assert!(proxy.is_revoked()); + } +} fn fn_callback( scope: v8::FunctionCallbackScope,