fix: 修复转 object 产生野指针问题

This commit is contained in:
爱特每羊队
2026-06-06 22:44:51 +08:00
parent 26685dc31a
commit f4bb81da94
2 changed files with 42 additions and 3 deletions
+33
View File
@@ -91,6 +91,39 @@ fn object_pointer_roundtrip() {
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]
fn display_int() {
assert_eq!(format!("{}", JsValue::int(42)), "42");