0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-21 15:04:33 -05:00

add v8::Proxy (#330)

This commit is contained in:
Michał Sabiniarz 2020-04-01 23:47:19 +02:00 committed by GitHub
parent 7b5e4e99d0
commit bb0be74b0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 103 additions and 0 deletions

View file

@ -1303,6 +1303,29 @@ v8::Object* v8__PropertyCallbackInfo__This(
return local_to_ptr(self.This());
}
v8::Proxy* v8__Proxy__New(v8::Local<v8::Context> context,
v8::Local<v8::Object> target,
v8::Local<v8::Object> 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<v8::SnapshotCreator>& buf,
const intptr_t* external_references) {
construct_in_place<v8::SnapshotCreator>(buf, external_references);

View file

@ -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;

54
src/proxy.rs Normal file
View file

@ -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<Context>,
mut target: Local<Object>,
mut handler: Local<Object>,
) -> Option<Local<'sc, Proxy>> {
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) };
}
}

View file

@ -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,