diff --git a/Cargo.toml b/Cargo.toml index d6c2eff2..1ff7b9ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,8 @@ base64-simd = "0.8" cssparser = "0.29" infer = "0.16" libavif = { version = "0.14", default-features = false, features = ["codec-aom"] } -napi = { version = "3.0.0-alpha.19", default-features = false, features = ["napi3", "serde-json"] } -napi-derive = { version = "3.0.0-alpha.17", default-features = false } +napi = { version = "3.0.0-alpha.20", default-features = false, features = ["napi3", "serde-json"] } +napi-derive = { version = "3.0.0-alpha.18", default-features = false } nom = "7" num_cpus = "1" regex = "1" diff --git a/build.rs b/build.rs index 60fed6f8..be98ef63 100644 --- a/build.rs +++ b/build.rs @@ -22,8 +22,12 @@ fn main() { env::set_var("CXX", "clang-cl"); } _ => { - env::set_var("CC", "clang"); - env::set_var("CXX", "clang++"); + if env::var("CC").is_err() { + env::set_var("CC", "clang"); + } + if env::var("CXX").is_err() { + env::set_var("CXX", "clang++"); + } } } diff --git a/src/image.rs b/src/image.rs index b7a487e7..56a3c33e 100644 --- a/src/image.rs +++ b/src/image.rs @@ -114,7 +114,7 @@ impl ImageData { } } -#[napi] +#[napi(custom_finalize)] pub struct Image { pub(crate) bitmap: Option, pub(crate) complete: bool, @@ -127,6 +127,15 @@ pub struct Image { pub(crate) src: Option, } +impl ObjectFinalize for Image { + fn finalize(self, env: Env) -> Result<()> { + if let Some(bitmap) = self.bitmap { + env.adjust_external_memory(-(bitmap.0.width as i64) * (bitmap.0.height as i64) * 4)?; + } + Ok(()) + } +} + #[napi] impl Image { #[napi(constructor)] @@ -239,7 +248,7 @@ impl Image { height: self.height, color_space: self.color_space, data: Some(data), - this_ref: env.create_reference(&*this)?, + this_ref: Ref::new(&env, &*this)?, }; let task_output = env.spawn(decoder)?; @@ -442,6 +451,7 @@ impl Task for BitmapDecoder { self_mut.src = self.data.take(); self_mut.is_svg = bitmap.is_svg; self_mut.bitmap = Some(bitmap.data); + env.adjust_external_memory((output.width as i64) * (output.height as i64) * 4)?; } DecodeStatus::Empty => {} DecodeStatus::InvalidSvg => {