Skip to content

Commit

Permalink
Fix Drop::drop and clippies
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl committed Dec 20, 2024
1 parent e2496c3 commit 683444e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 36 deletions.
43 changes: 16 additions & 27 deletions core/engine/src/value/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl NanBitTag {
// Either it is a constant float value,
if value == f64::INFINITY.to_bits()
|| value == f64::NEG_INFINITY.to_bits()
// or it is exactly a NaN value, which is the same as the BigInt tag.
// or it is exactly a NaN value, which is the same as the `BigInt` tag.
// Reminder that pointers cannot be null, so this is safe.
|| value == NanBitTag::Pointer as u64
{
Expand Down Expand Up @@ -137,7 +137,7 @@ impl NanBitTag {
value & NanBitTag::Integer32 as u64 == NanBitTag::Integer32 as u64
}

/// Checks that a value is a valid BigInt.
/// Checks that a value is a valid `BigInt`.
#[inline]
const fn is_bigint(value: u64) -> bool {
(value & NanBitTag::TaggedMask as u64 == NanBitTag::Pointer as u64)
Expand Down Expand Up @@ -296,27 +296,6 @@ impl NanBitTag {
// Simply cast for bits.
Self::Pointer as u64 | Self::String as u64 | value
}

/// Drops a value if it is a pointer, otherwise do nothing.
#[inline]
unsafe fn drop_pointer(value: u64) {
let value_ptr = value & Self::PointerMask as u64;

if value_ptr == 0
|| value & NanBitTag::Pointer as u64 != 0
|| value == NanBitTag::Pointer as u64
{
return;
}

match value & 0x3 {
0 => drop(unsafe { Box::from_raw(value_ptr as *mut JsBigInt) }),
1 => drop(unsafe { Box::from_raw(value_ptr as *mut JsObject) }),
2 => drop(unsafe { Box::from_raw(value_ptr as *mut JsSymbol) }),
3 => drop(unsafe { Box::from_raw(value_ptr as *mut JsString) }),
_ => unreachable!(),
}
}
}

/// A NaN-boxed `[super::JsValue]`'s inner.
Expand All @@ -341,14 +320,16 @@ impl fmt::Debug for InnerValue {

impl Finalize for InnerValue {
fn finalize(&self) {
eprintln!("Finalizing InnerValue: {:?}", self.as_variant());
if let Some(o) = self.as_object() {
o.finalize();
}
}
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe impl Trace for InnerValue {
custom_trace! {this, mark, {
if let JsVariant::Object(o) = this.as_variant() {
if let Some(o) = this.as_object() {
mark(o);
}
}}
Expand Down Expand Up @@ -615,9 +596,17 @@ impl InnerValue {

impl Drop for InnerValue {
fn drop(&mut self) {
let maybe_ptr = self.0 & NanBitTag::PointerMask as u64;

// Drop the pointer if it is a pointer.
unsafe {
NanBitTag::drop_pointer(self.0);
if self.is_object() {
drop(unsafe { Box::from_raw(maybe_ptr as *mut JsObject) });
} else if self.is_bigint() {
drop(unsafe { Box::from_raw(maybe_ptr as *mut JsBigInt) });
} else if self.is_symbol() {
drop(unsafe { Box::from_raw(maybe_ptr as *mut JsSymbol) });
} else if self.is_string() {
drop(unsafe { Box::from_raw(maybe_ptr as *mut JsString) });
}
}
}
Expand Down
14 changes: 5 additions & 9 deletions core/engine/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl JsValue {
#[inline]
#[must_use]
pub fn is_callable(&self) -> bool {
self.as_object().map_or(false, |obj| obj.is_callable())
self.as_object().map_or(false, JsObject::is_callable)
}

/// Returns the callable value if the value is callable, otherwise `None`.
Expand All @@ -197,7 +197,7 @@ impl JsValue {
#[inline]
#[must_use]
pub fn is_constructor(&self) -> bool {
self.as_object().map_or(false, |obj| obj.is_constructor())
self.as_object().map_or(false, JsObject::is_constructor)
}

/// Returns the constructor if the value is a constructor, otherwise `None`.
Expand Down Expand Up @@ -258,11 +258,7 @@ impl JsValue {
#[inline]
#[must_use]
pub fn as_symbol(&self) -> Option<JsSymbol> {
if let Some(symbol) = self.0.as_symbol() {
Some(symbol.clone())
} else {
None
}
self.0.as_symbol().cloned()
}

/// Returns true if the value is undefined.
Expand Down Expand Up @@ -322,7 +318,7 @@ impl JsValue {
#[must_use]
pub fn as_number(&self) -> Option<f64> {
if let Some(i) = self.as_i32() {
Some(i as f64)
Some(f64::from(i))
} else {
self.0.as_float64()
}
Expand Down Expand Up @@ -459,7 +455,7 @@ impl JsValue {
JsVariant::Undefined => Err(JsNativeError::typ()
.with_message("cannot convert undefined to a BigInt")
.into()),
JsVariant::String(ref string) => JsBigInt::from_js_string(string).map_or_else(
JsVariant::String(string) => JsBigInt::from_js_string(string).map_or_else(
|| {
Err(JsNativeError::syntax()
.with_message(format!(
Expand Down

0 comments on commit 683444e

Please sign in to comment.