From 72cac515fbdb9cc3aeffcef98aca2ee1c203e693 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 5 Aug 2024 14:03:27 +0200 Subject: [PATCH 1/3] fix: Make CompressionResource garbage collectable --- ext/web/compression.rs | 52 +++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/ext/web/compression.rs b/ext/web/compression.rs index 4f4d90cbfc2983..1f51d1826101ff 100644 --- a/ext/web/compression.rs +++ b/ext/web/compression.rs @@ -3,10 +3,6 @@ use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::op2; -use deno_core::OpState; -use deno_core::Resource; -use deno_core::ResourceId; -use deno_core::ToJsBuffer; use flate2::write::DeflateDecoder; use flate2::write::DeflateEncoder; use flate2::write::GzDecoder; @@ -14,13 +10,13 @@ use flate2::write::GzEncoder; use flate2::write::ZlibDecoder; use flate2::write::ZlibEncoder; use flate2::Compression; -use std::borrow::Cow; use std::cell::RefCell; use std::io::Write; -use std::rc::Rc; #[derive(Debug)] -struct CompressionResource(RefCell); +struct CompressionResource(RefCell>); + +impl deno_core::GarbageCollected for CompressionResource {} /// https://wicg.github.io/compression/#supported-formats #[derive(Debug)] @@ -33,19 +29,12 @@ enum Inner { GzEncoder(GzEncoder>), } -impl Resource for CompressionResource { - fn name(&self) -> Cow { - "compression".into() - } -} - -#[op2(fast)] -#[smi] +#[op2] +#[cppgc] pub fn op_compression_new( - state: &mut OpState, #[string] format: &str, is_decoder: bool, -) -> ResourceId { +) -> Result { let w = Vec::new(); let inner = match (format, is_decoder) { ("deflate", true) => Inner::DeflateDecoder(ZlibDecoder::new(w)), @@ -60,21 +49,19 @@ pub fn op_compression_new( ("gzip", false) => { Inner::GzEncoder(GzEncoder::new(w, Compression::default())) } - _ => unreachable!(), + _ => return Err(type_error("Unsupported format")), }; - let resource = CompressionResource(RefCell::new(inner)); - state.resource_table.add(resource) + Ok(CompressionResource(RefCell::new(Some(inner)))) } #[op2] -#[serde] +#[buffer] pub fn op_compression_write( - state: &mut OpState, - #[smi] rid: ResourceId, + #[cppgc] resource: &CompressionResource, #[anybuffer] input: &[u8], -) -> Result { - let resource = state.resource_table.get::(rid)?; +) -> Result, AnyError> { let mut inner = resource.0.borrow_mut(); + let inner = inner.as_mut().ok_or_else(|| type_error("resource is closed"))?; let out: Vec = match &mut *inner { Inner::DeflateDecoder(d) => { d.write_all(input).map_err(|e| type_error(e.to_string()))?; @@ -108,19 +95,18 @@ pub fn op_compression_write( } } .collect(); - Ok(out.into()) + Ok(out) } #[op2] -#[serde] +#[buffer] pub fn op_compression_finish( - state: &mut OpState, - #[smi] rid: ResourceId, + #[cppgc] resource: &CompressionResource, report_errors: bool, -) -> Result { - let resource = state.resource_table.take::(rid)?; - let resource = Rc::try_unwrap(resource).unwrap(); - let inner = resource.0.into_inner(); +) -> Result, AnyError> { + let inner = resource.0.borrow_mut().take().ok_or_else(|| { + type_error("resource is closed") + })?; let out = match inner { Inner::DeflateDecoder(d) => { d.finish().map_err(|e| type_error(e.to_string())) From 1d46d7f896330eabd446afb218142fa87c7f9400 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 5 Aug 2024 14:06:53 +0200 Subject: [PATCH 2/3] x --- ext/web/compression.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ext/web/compression.rs b/ext/web/compression.rs index 1f51d1826101ff..dad4b3050922bb 100644 --- a/ext/web/compression.rs +++ b/ext/web/compression.rs @@ -61,7 +61,9 @@ pub fn op_compression_write( #[anybuffer] input: &[u8], ) -> Result, AnyError> { let mut inner = resource.0.borrow_mut(); - let inner = inner.as_mut().ok_or_else(|| type_error("resource is closed"))?; + let inner = inner + .as_mut() + .ok_or_else(|| type_error("resource is closed"))?; let out: Vec = match &mut *inner { Inner::DeflateDecoder(d) => { d.write_all(input).map_err(|e| type_error(e.to_string()))?; @@ -104,9 +106,11 @@ pub fn op_compression_finish( #[cppgc] resource: &CompressionResource, report_errors: bool, ) -> Result, AnyError> { - let inner = resource.0.borrow_mut().take().ok_or_else(|| { - type_error("resource is closed") - })?; + let inner = resource + .0 + .borrow_mut() + .take() + .ok_or_else(|| type_error("resource is closed"))?; let out = match inner { Inner::DeflateDecoder(d) => { d.finish().map_err(|e| type_error(e.to_string())) From 4aa4a4a79cb53707405665e8c416ab10e5572961 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 5 Aug 2024 14:37:19 +0200 Subject: [PATCH 3/3] lint --- ext/web/compression.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/web/compression.rs b/ext/web/compression.rs index dad4b3050922bb..b9ae12ef17325c 100644 --- a/ext/web/compression.rs +++ b/ext/web/compression.rs @@ -132,9 +132,9 @@ pub fn op_compression_finish( if report_errors { Err(err) } else { - Ok(Vec::with_capacity(0).into()) + Ok(Vec::with_capacity(0)) } } - Ok(out) => Ok(out.into()), + Ok(out) => Ok(out), } }