feat: 补充 JsObject 类型转换辅助方法

This commit is contained in:
爱特每羊队
2026-06-05 18:06:06 +08:00
parent 047730405f
commit 4de72d9816
+13
View File
@@ -1,5 +1,7 @@
use std::fmt;
use crate::object::JsObject;
/// Quiet NaN prefix — bits 63-51 = sign(1) + exponent(0x7FF) + quiet_bit(1)
const QNAN_PREFIX: u64 = 0xFFF8_0000_0000_0000;
@@ -178,6 +180,17 @@ impl JsValue {
(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");
(self.0 & PTR_MASK) as *mut JsObject
}
pub fn from_js_object(ptr: *mut JsObject) -> Self {
let addr = ptr as u64;
debug_assert!(addr <= PTR_MASK, "object pointer must fit in 48 bits");
Self(make_tag(TAG_OBJECT) | addr)
}
pub fn as_string_index(&self) -> u32 {
debug_assert!(self.is_string(), "JsValue is not a string");
(self.0 & STRING_INDEX_MASK) as u32