1pub struct JSValue {
3 pub(crate) inner: cxx::UniquePtr<crate::sys::ffi::JSIValue>,
4}
5
6impl JSValue {
7 pub fn undefined() -> Self {
9 let ptr = crate::sys::ffi::create_value_undefined();
10 Self { inner: ptr }
11 }
12
13 pub fn null() -> Self {
15 let ptr = crate::sys::ffi::create_value_null();
16 Self { inner: ptr }
17 }
18
19 pub fn bool(value: bool) -> Self {
21 let ptr = crate::sys::ffi::create_value_bool(value);
22 Self { inner: ptr }
23 }
24
25 pub fn number(value: f64) -> Self {
27 let ptr = crate::sys::ffi::create_value_number(value);
28 Self { inner: ptr }
29 }
30
31 pub unsafe fn from_raw(ptr: *mut crate::sys::ffi::JSIValue) -> Self {
32 Self {
33 inner: cxx::UniquePtr::from_raw(ptr),
34 }
35 }
36
37 fn as_ref(&self) -> &crate::sys::ffi::JSIValue {
38 self.inner.as_ref().expect("JSValue inner pointer is null")
39 }
40
41 pub(crate) fn inner(&self) -> &cxx::UniquePtr<crate::sys::ffi::JSIValue> {
42 &self.inner
43 }
44
45 #[cfg(feature = "unsafe")]
47 pub fn inner_unsafe(&self) -> &cxx::UniquePtr<crate::sys::ffi::JSIValue> {
48 &self.inner
49 }
50
51 pub fn is_undefined(&self) -> bool {
52 self.as_ref().isUndefined()
53 }
54
55 pub fn is_null(&self) -> bool {
56 self.as_ref().isNull()
57 }
58
59 pub fn is_bool(&self) -> bool {
60 self.as_ref().isBool()
61 }
62
63 pub fn is_number(&self) -> bool {
64 self.as_ref().isNumber()
65 }
66
67 pub fn is_string(&self) -> bool {
68 self.as_ref().isString()
69 }
70
71 pub fn is_object(&self) -> bool {
72 self.as_ref().isObject()
73 }
74
75 pub fn is_bigint(&self) -> bool {
76 self.as_ref().isBigInt()
77 }
78
79 pub fn as_function(&self, runtime: &mut crate::JSRuntime) -> Option<crate::JSFunction> {
80 if !self.is_object() {
81 return None;
82 }
83
84 let obj = crate::sys::ffi::value_as_object(runtime.pin_mut(), self.inner());
85 let func = crate::sys::ffi::object_as_function(runtime.pin_mut(), &obj);
86
87 Some(crate::JSFunction { inner: func })
88 }
89
90 pub fn as_bool(&self) -> bool {
91 crate::sys::ffi::value_as_bool(self.inner())
92 }
93
94 pub fn as_number(&self) -> f64 {
95 crate::sys::ffi::value_as_number(self.inner())
96 }
97
98 pub fn as_string(&self, runtime: &mut crate::JSRuntime) -> Option<crate::JSString> {
99 if !self.is_string() {
100 return None;
101 }
102
103 let str = crate::sys::ffi::value_as_string(runtime.pin_mut(), self.inner());
104
105 Some(crate::JSString { inner: str })
106 }
107
108 pub fn as_bigint(&self, runtime: &mut crate::JSRuntime) -> Option<crate::JSBigInt> {
109 if !self.is_bigint() {
110 return None;
111 }
112
113 let bigint = crate::sys::ffi::value_as_bigint(runtime.pin_mut(), self.inner());
114
115 Some(crate::JSBigInt { inner: bigint })
116 }
117}