fix: 修复转 object 产生野指针问题
This commit is contained in:
@@ -171,17 +171,23 @@ impl JsValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_ptr(&self) -> *const u8 {
|
pub fn as_ptr(&self) -> *const u8 {
|
||||||
debug_assert!(self.is_object(), "JsValue is not an object");
|
if !self.is_object() {
|
||||||
|
return std::ptr::null();
|
||||||
|
}
|
||||||
(self.0 & PTR_MASK) as *const u8
|
(self.0 & PTR_MASK) as *const u8
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_object_ptr(&self) -> *mut u8 {
|
pub fn as_object_ptr(&self) -> *mut u8 {
|
||||||
debug_assert!(self.is_object(), "JsValue is not an object");
|
if !self.is_object() {
|
||||||
|
return std::ptr::null_mut();
|
||||||
|
}
|
||||||
(self.0 & PTR_MASK) as *mut u8
|
(self.0 & PTR_MASK) as *mut u8
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_js_object_ptr(&self) -> *mut JsObject {
|
pub fn as_js_object_ptr(&self) -> *mut JsObject {
|
||||||
debug_assert!(self.is_object(), "JsValue is not an object");
|
if !self.is_object() {
|
||||||
|
return std::ptr::null_mut();
|
||||||
|
}
|
||||||
(self.0 & PTR_MASK) as *mut JsObject
|
(self.0 & PTR_MASK) as *mut JsObject
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -91,6 +91,39 @@ fn object_pointer_roundtrip() {
|
|||||||
assert_eq!(ptr, &x as *const u8);
|
assert_eq!(ptr, &x as *const u8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn as_ptr_non_object_returns_null() {
|
||||||
|
assert!(JsValue::int(0).as_ptr().is_null());
|
||||||
|
assert!(JsValue::float(0.0).as_ptr().is_null());
|
||||||
|
assert!(JsValue::bool(true).as_ptr().is_null());
|
||||||
|
assert!(JsValue::null().as_ptr().is_null());
|
||||||
|
assert!(JsValue::undefined().as_ptr().is_null());
|
||||||
|
let x = 42u8;
|
||||||
|
assert!(!JsValue::object(&x as *const u8).as_ptr().is_null());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn as_object_ptr_non_object_returns_null() {
|
||||||
|
assert!(JsValue::int(0).as_object_ptr().is_null());
|
||||||
|
assert!(JsValue::float(0.0).as_object_ptr().is_null());
|
||||||
|
assert!(JsValue::bool(true).as_object_ptr().is_null());
|
||||||
|
assert!(JsValue::null().as_object_ptr().is_null());
|
||||||
|
assert!(JsValue::undefined().as_object_ptr().is_null());
|
||||||
|
let x = 42u8;
|
||||||
|
assert!(!JsValue::object(&x as *const u8).as_object_ptr().is_null());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn as_js_object_ptr_non_object_returns_null() {
|
||||||
|
assert!(JsValue::int(0).as_js_object_ptr().is_null());
|
||||||
|
assert!(JsValue::float(0.0).as_js_object_ptr().is_null());
|
||||||
|
assert!(JsValue::bool(true).as_js_object_ptr().is_null());
|
||||||
|
assert!(JsValue::null().as_js_object_ptr().is_null());
|
||||||
|
assert!(JsValue::undefined().as_js_object_ptr().is_null());
|
||||||
|
let x = 42u8;
|
||||||
|
assert!(!JsValue::object(&x as *const u8).as_js_object_ptr().is_null());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn display_int() {
|
fn display_int() {
|
||||||
assert_eq!(format!("{}", JsValue::int(42)), "42");
|
assert_eq!(format!("{}", JsValue::int(42)), "42");
|
||||||
|
|||||||
Reference in New Issue
Block a user