From d70257af6ce64a0725a0d21d9913ea8710f62078 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 24 Oct 2018 23:33:01 -0700 Subject: [PATCH] Tweak some logic in `JsValue::drop` While technically correct the current implementation sort of made it only roundaboutedly so. Tweak the logic a bit and the associated comment to ensure that stack values are never dropped and the global constants are all skipped. --- src/lib.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3b862469..1c5ce6ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -534,10 +534,14 @@ impl Drop for JsValue { #[inline] fn drop(&mut self) { unsafe { - // if the first bit is set then this is a stack value, so we for - // sure need to drop it. Otherwise if this is one of the special - // reserved values there's no need to drop it. - if (self.idx & 1) == 1 || self.idx >= JSIDX_RESERVED { + // The first bit indicates whether this is a stack value or not. + // Stack values should never be dropped (they're always in + // `ManuallyDrop`) + debug_assert!(self.idx & 1 == 0); + + // We don't want to drop the first few elements as they're all + // reserved, but everything else is safe to drop. + if self.idx >= JSIDX_RESERVED { __wbindgen_object_drop_ref(self.idx); } }