From f4bb81da94479cf48ebb029cf8829be410163eff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=88=B1=E7=89=B9=E6=AF=8F=E7=BE=8A=E9=98=9F?= Date: Sat, 6 Jun 2026 22:44:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=BD=AC=20object=20?= =?UTF-8?q?=E4=BA=A7=E7=94=9F=E9=87=8E=E6=8C=87=E9=92=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/oxide_types/src/value.rs | 12 +++++++--- crates/oxide_vm/tests/value_tests.rs | 33 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/crates/oxide_types/src/value.rs b/crates/oxide_types/src/value.rs index 8e71858..2565f91 100644 --- a/crates/oxide_types/src/value.rs +++ b/crates/oxide_types/src/value.rs @@ -171,17 +171,23 @@ impl JsValue { } 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 } 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 } 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 } diff --git a/crates/oxide_vm/tests/value_tests.rs b/crates/oxide_vm/tests/value_tests.rs index 91ade95..fbb6877 100644 --- a/crates/oxide_vm/tests/value_tests.rs +++ b/crates/oxide_vm/tests/value_tests.rs @@ -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");