Skip to content

Commit

Permalink
Fixed Double Values on 32-bit Platforms
Browse files Browse the repository at this point in the history
Added Value::is_private
  • Loading branch information
Redfire75369 committed Dec 8, 2023
1 parent 9188c2a commit ea25afc
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions mozjs-sys/src/jsval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ fn AssertGCPointerAlignment(bits: u64) {
#[inline(always)]
fn AssertGCPointerAlignment(bits: u64) {}

#[cfg(target_pointer_width = "64")]
#[inline(always)]
fn IsValidUserModePointer(bits: u64) -> bool {
bits & 0xFFFF_0000_0000_0000 == 0
}

#[cfg(target_pointer_width = "32")]
#[inline(always)]
fn IsValidUserModePointer(_: u64) -> bool {
true
}

#[inline(always)]
pub fn Int32Value(i: i32) -> JSVal {
BuildJSVal(ValueTag::INT32, i as u32 as u64)
Expand All @@ -115,9 +127,9 @@ pub fn NullValue() -> JSVal {

#[inline(always)]
pub fn DoubleValue(f: f64) -> JSVal {
let bits: u64 = f.to_bits();
assert!(bits <= ValueShiftedTag::MAX_DOUBLE as u64);
JSVal { asBits_: bits }
let val = JSVal { asBits_: f.to_bits() };
assert!(val.is_double());
val
}

#[inline(always)]
Expand Down Expand Up @@ -174,8 +186,7 @@ pub fn ObjectOrNullValue(o: *mut JSObject) -> JSVal {
#[inline(always)]
pub fn PrivateValue(o: *const c_void) -> JSVal {
let ptrBits = o as usize as u64;
#[cfg(target_pointer_width = "64")]
assert_eq!(ptrBits & 0xFFFF000000000000, 0);
assert!(IsValidUserModePointer(ptrBits));
JSVal { asBits_: ptrBits }
}

Expand Down Expand Up @@ -222,11 +233,18 @@ impl JSVal {
self.toTag() == ValueTag::INT32 as u64
}

#[cfg(target_pointer_width = "64")]
#[inline(always)]
pub fn is_double(&self) -> bool {
self.asBits() <= ValueShiftedTag::MAX_DOUBLE as u64
}

#[cfg(target_pointer_width = "32")]
#[inline(always)]
pub fn is_double(&self) -> bool {
(self.asBits() >> JSVAL_TAG_SHIFT) as u32 <= JSVAL_TAG_CLEAR
}

#[cfg(target_pointer_width = "64")]
#[inline(always)]
pub fn is_number(&self) -> bool {
Expand Down Expand Up @@ -401,11 +419,14 @@ impl JSVal {
self.payload() != 0
}

#[inline(always)]
pub fn is_private(&self) -> bool {
self.is_double() && IsValidUserModePointer(self.asBits())
}

#[inline(always)]
pub fn to_private(&self) -> *const c_void {
assert!(self.is_double());
#[cfg(target_pointer_width = "64")]
assert_eq!(self.asBits() & 0xFFFF000000000000, 0);
assert!(self.is_private());
self.asBits() as usize as *const c_void
}

Expand Down

0 comments on commit ea25afc

Please sign in to comment.