1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

Revert #[cppgc] &mut T usage (#24304)

This commit is contained in:
Divy Srivastava 2024-06-21 16:12:44 +05:30 committed by GitHub
parent e6756c3e66
commit fc197a7ac0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 28 deletions

View file

@ -1,5 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use std::cell::RefCell;
use std::collections::HashSet; use std::collections::HashSet;
use std::net::IpAddr; use std::net::IpAddr;
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
@ -17,6 +18,12 @@ use ipnetwork::Ipv4Network;
use ipnetwork::Ipv6Network; use ipnetwork::Ipv6Network;
use serde::Serialize; use serde::Serialize;
pub struct BlockListResource {
blocklist: RefCell<BlockList>,
}
impl deno_core::GcResource for BlockListResource {}
#[derive(Serialize)] #[derive(Serialize)]
struct SocketAddressSerialization(String, String); struct SocketAddressSerialization(String, String);
@ -59,51 +66,52 @@ pub fn op_socket_address_get_serialization(
#[op2] #[op2]
#[cppgc] #[cppgc]
pub fn op_blocklist_new() -> BlockList { pub fn op_blocklist_new() -> BlockListResource {
BlockList::new() let blocklist = BlockList::new();
BlockListResource {
blocklist: RefCell::new(blocklist),
}
} }
#[op2(fast)] #[op2(fast)]
pub fn op_blocklist_add_address( pub fn op_blocklist_add_address(
#[cppgc] blocklist: &mut BlockList, #[cppgc] wrap: &BlockListResource,
#[string] addr: &str, #[string] addr: &str,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
blocklist.add_address(addr) wrap.blocklist.borrow_mut().add_address(addr)
} }
#[op2(fast)] #[op2(fast)]
pub fn op_blocklist_add_range( pub fn op_blocklist_add_range(
#[cppgc] blocklist: &mut BlockList, #[cppgc] wrap: &BlockListResource,
#[string] start: &str, #[string] start: &str,
#[string] end: &str, #[string] end: &str,
) -> Result<bool, AnyError> { ) -> Result<bool, AnyError> {
blocklist.add_range(start, end) wrap.blocklist.borrow_mut().add_range(start, end)
} }
#[op2(fast)] #[op2(fast)]
pub fn op_blocklist_add_subnet( pub fn op_blocklist_add_subnet(
#[cppgc] blocklist: &mut BlockList, #[cppgc] wrap: &BlockListResource,
#[string] addr: &str, #[string] addr: &str,
#[smi] prefix: u8, #[smi] prefix: u8,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
blocklist.add_subnet(addr, prefix) wrap.blocklist.borrow_mut().add_subnet(addr, prefix)
} }
#[op2(fast)] #[op2(fast)]
pub fn op_blocklist_check( pub fn op_blocklist_check(
#[cppgc] blocklist: &BlockList, #[cppgc] wrap: &BlockListResource,
#[string] addr: &str, #[string] addr: &str,
#[string] type_: &str, #[string] r#type: &str,
) -> Result<bool, AnyError> { ) -> Result<bool, AnyError> {
blocklist.check(addr, type_) wrap.blocklist.borrow().check(addr, r#type)
} }
struct BlockList { struct BlockList {
rules: HashSet<IpNetwork>, rules: HashSet<IpNetwork>,
} }
impl deno_core::GcResource for BlockList {}
impl BlockList { impl BlockList {
pub fn new() -> Self { pub fn new() -> Self {
BlockList { BlockList {

View file

@ -2,6 +2,8 @@
use deno_core::error::type_error; use deno_core::error::type_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op2; use deno_core::op2;
use std::borrow::Cow;
use std::cell::RefCell;
use zlib::*; use zlib::*;
mod alloc; mod alloc;
@ -228,17 +230,17 @@ impl ZlibInner {
} }
struct Zlib { struct Zlib {
inner: Option<ZlibInner>, inner: RefCell<Option<ZlibInner>>,
}
impl Zlib {
fn as_mut(&mut self) -> Option<&mut ZlibInner> {
self.inner.as_mut()
}
} }
impl deno_core::GcResource for Zlib {} impl deno_core::GcResource for Zlib {}
impl deno_core::Resource for Zlib {
fn name(&self) -> Cow<str> {
"zlib".into()
}
}
#[op2] #[op2]
#[cppgc] #[cppgc]
pub fn op_zlib_new(#[smi] mode: i32) -> Result<Zlib, AnyError> { pub fn op_zlib_new(#[smi] mode: i32) -> Result<Zlib, AnyError> {
@ -249,12 +251,15 @@ pub fn op_zlib_new(#[smi] mode: i32) -> Result<Zlib, AnyError> {
..Default::default() ..Default::default()
}; };
Ok(Zlib { inner: Some(inner) }) Ok(Zlib {
inner: RefCell::new(Some(inner)),
})
} }
#[op2(fast)] #[op2(fast)]
pub fn op_zlib_close(#[cppgc] zlib: &mut Zlib) -> Result<(), AnyError> { pub fn op_zlib_close(#[cppgc] resource: &Zlib) -> Result<(), AnyError> {
let zlib = zlib let mut resource = resource.inner.borrow_mut();
let zlib = resource
.as_mut() .as_mut()
.ok_or_else(|| type_error("zlib not initialized"))?; .ok_or_else(|| type_error("zlib not initialized"))?;
@ -268,7 +273,7 @@ pub fn op_zlib_close(#[cppgc] zlib: &mut Zlib) -> Result<(), AnyError> {
#[op2(fast)] #[op2(fast)]
#[smi] #[smi]
pub fn op_zlib_write( pub fn op_zlib_write(
#[cppgc] zlib: &mut Zlib, #[cppgc] resource: &Zlib,
#[smi] flush: i32, #[smi] flush: i32,
#[buffer] input: &[u8], #[buffer] input: &[u8],
#[smi] in_off: u32, #[smi] in_off: u32,
@ -278,6 +283,7 @@ pub fn op_zlib_write(
#[smi] out_len: u32, #[smi] out_len: u32,
#[buffer] result: &mut [u32], #[buffer] result: &mut [u32],
) -> Result<i32, AnyError> { ) -> Result<i32, AnyError> {
let mut zlib = resource.inner.borrow_mut();
let zlib = zlib let zlib = zlib
.as_mut() .as_mut()
.ok_or_else(|| type_error("zlib not initialized"))?; .ok_or_else(|| type_error("zlib not initialized"))?;
@ -295,13 +301,14 @@ pub fn op_zlib_write(
#[op2(fast)] #[op2(fast)]
#[smi] #[smi]
pub fn op_zlib_init( pub fn op_zlib_init(
#[cppgc] zlib: &mut Zlib, #[cppgc] resource: &Zlib,
#[smi] level: i32, #[smi] level: i32,
#[smi] window_bits: i32, #[smi] window_bits: i32,
#[smi] mem_level: i32, #[smi] mem_level: i32,
#[smi] strategy: i32, #[smi] strategy: i32,
#[buffer] dictionary: &[u8], #[buffer] dictionary: &[u8],
) -> Result<i32, AnyError> { ) -> Result<i32, AnyError> {
let mut zlib = resource.inner.borrow_mut();
let zlib = zlib let zlib = zlib
.as_mut() .as_mut()
.ok_or_else(|| type_error("zlib not initialized"))?; .ok_or_else(|| type_error("zlib not initialized"))?;
@ -341,7 +348,8 @@ pub fn op_zlib_init(
#[op2(fast)] #[op2(fast)]
#[smi] #[smi]
pub fn op_zlib_reset(#[cppgc] zlib: &mut Zlib) -> Result<i32, AnyError> { pub fn op_zlib_reset(#[cppgc] resource: &Zlib) -> Result<i32, AnyError> {
let mut zlib = resource.inner.borrow_mut();
let zlib = zlib let zlib = zlib
.as_mut() .as_mut()
.ok_or_else(|| type_error("zlib not initialized"))?; .ok_or_else(|| type_error("zlib not initialized"))?;
@ -353,9 +361,10 @@ pub fn op_zlib_reset(#[cppgc] zlib: &mut Zlib) -> Result<i32, AnyError> {
#[op2(fast)] #[op2(fast)]
pub fn op_zlib_close_if_pending( pub fn op_zlib_close_if_pending(
#[cppgc] zlib: &mut Zlib, #[cppgc] resource: &Zlib,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let pending_close = { let pending_close = {
let mut zlib = resource.inner.borrow_mut();
let zlib = zlib let zlib = zlib
.as_mut() .as_mut()
.ok_or_else(|| type_error("zlib not initialized"))?; .ok_or_else(|| type_error("zlib not initialized"))?;
@ -364,7 +373,7 @@ pub fn op_zlib_close_if_pending(
zlib.pending_close zlib.pending_close
}; };
if pending_close { if pending_close {
if let Some(mut res) = zlib.inner.take() { if let Some(mut res) = resource.inner.borrow_mut().take() {
let _ = res.close(); let _ = res.close();
} }
} }